[Script] Mediator 0.2.1 (Update)
-
Mediator (v0.2)
Author : mumy255
This is a JavaScript script designed for managing and transmitting data between different modules or components. Its core functionality is to store data in the form of key-value pairs and use a listener mechanism to respond to data changes.
Download:
Mediator 0.2.1 and Document.zip (for nextgen 0.27)
Mediator 0.2 and Document.zip (for nextgen 0.26)
Mediator 0.1 and Document.zip (for nextgen 0.26)
Getting the
MediatorModule InstanceCode Example
var mediator; script.on("enable", () => { mediator = Client.getModuleManager().get("Mediator"); });Step-by-Step Explanation
-
script.on("enable", callback):script.on("enable", ...)is an event listener that triggers when the script is enabled. The callback function is executed once the script is enabled.- Inside this callback, the operation to fetch the
Mediatormodule instance is performed.
-
Client.getModuleManager():Client.getModuleManager()is an API that retrieves the client module manager. It is responsible for managing all loaded and enabled modules.
-
.get("Mediator"):get("Mediator")is used to fetch the instance of the module namedMediatorfrom the module manager."Mediator"is the unique identifier for this module.- If the
Mediatormodule is loaded and enabled, theget()method will return the module's instance. If the module is not enabled or loaded, it may returnnull.
-
Saving the Module Instance:
- The retrieved module instance is saved to the
mediatorvariable, which can be used for subsequent operations.
- The retrieved module instance is saved to the
Notes
- The
mediatorwill only be correctly assigned after the script is enabled, so the fetching operation should be inside thescript.on("enable", ...)callback. - If the module doesn't exist or wasn't loaded correctly,
mediatorwill benull.
Extended Example
var mediator; script.on("enable", () => { mediator = Client.getModuleManager().get("Mediator"); if (mediator) { Client.displayChatMessage("Mediator module is now available."); } else { Client.displayChatMessage("Failed to get Mediator module."); } });In this example, the code checks if
mediatorwas successfully fetched, and displays a message based on the result.
Methods
putData(key: string, value: any | null): booleanAdds data to the data store and notifies relevant listeners when the data changes.
Parameters:
Property Description Type key The key of the data item stringvalue The value of the data item, can be nullto clear the dataany/nullReturn Value:
Returns a boolean indicating whether the operation was successful.Example:
const success = dataStore.putData("user", { name: "John", age: 30 }); Client.displayChatMessage(success); // true
getData(key: string): any | nullRetrieves the data for the specified key.
Parameters:
Property Description Type key The key of the data item stringReturn Value:
Returns the data for the specified key, ornullif the data does not exist.Example:
const user = dataStore.getData("user"); Client.displayChatMessage(user); // { name: "John", age: 30 }
clearData(key: string): booleanClears the data for the specified key.
Parameters:
Property Description Type key The key of the data item stringReturn Value:
Returns a boolean indicating whether the operation was successful.Example:
const success = dataStore.clearData("user"); Client.displayChatMessage(success); // true
hasData(key: string): booleanChecks if data exists for the specified key.
Parameters:
Property Description Type key The key of the data item stringReturn Value:
Returns a boolean indicating whether data exists for the specified key.Example:
const hasData = dataStore.hasData("user"); Client.displayChatMessage(hasData); // true
registerListener(jsObject: { key: string; name: string }, callback: Listener): booleanRegisters a listener to monitor changes in data for the specified key.
Parameters:
Property Description Type jsObject An object containing key(data item's key) andname(listener name){ key: string; name: string }callback The callback function for the listener, which accepts valueandstate(PREorPOST) as parameters(value: any/null, state: string) => voidReturn Value:
Returns a boolean indicating whether the listener was successfully registered.Example:
const listener: Listener = (value, state) => { Client.displayChatMessage(`Data changed: ${value}, State: ${state}`); }; mediator.registerListener({ key: "user", name: "userListener" }, listener);
unregisterListener(key: string, name: string): booleanUnregisters a listener to stop monitoring data changes for the specified key.
Parameters:
Property Description Type key The key of the data item stringname The name of the listener stringReturn Value:
Returns a boolean indicating whether the listener was successfully unregistered.Example:
const success = mediator.unregisterListener("user", "userListener"); Client.displayChatMessage(success); // true
Example Usage
// Add data to the data store mediator.putData("user", { name: "John", age: 30 }); // Retrieve the data const user = mediator.getData("user"); console.log(user); // Output: { name: "John", age: 30 } // Check if the data exists const hasData = mediator.hasData("user"); console.log(hasData); // Output: true // Register a listener const listener: Listener = (value, state) => { Client.displayChatMessage(`Data changed: ${value}, State: ${state}`); }; mediator.registerListener({ key: "user", name: "userListener" }, listener); // Update the data and trigger the listener mediator.putData("user", { name: "Jane", age: 25 }); // The state will be "POST" // Unregister the listener mediator.unregisterListener("user", "userListener"); // Clear the data mediator.clearData("user"); -
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login