Skip to main content

API

stream

The stream object holds the array of all registered events. Users can browse this object for all available events.

eventStream.stream;

registerEvent

eventStream.registerEvent(event, callback, (once = false), (duration = false));
NameTypeDefaultDescription
eventstringRequiredName of the event. example: GMCP.Item.Add
callbackfunctionRequiredCallback function to fire when event is raised.
oncebooleanOptionalListener will fire once and be removed (single fire).
durationmilisecondsOptionalListener will be live and then removed after elapsed time.

Users can register an anonymous function to the event. This can be useful for quick snippets or temporary event listeners.

eventStream.registerEvent("testEvent", () => {
console.log("arrow function");
});

Named functions can be more helpful for accessing the listener at a later time. Typically when a user wants to remove a listener from an event.

const testFunction = () => {
console.log("named arrow function");
};
eventStream.registerEvent("testEvent", testFunction);

Named functions can be more helpful for accessing the listener at a later time. Typically when a user wants to remove a listener from an event.

const singleFire = () => {
console.log("single fire event");
};
eventStream.registerEvent("testEvent", testFunction, true);
// Listener will only fire once after testEvent is raised.
// This listener will not be present for subsequent testEvent events.

raiseEvent

eventStream.raiseEvent(event, args);
ParameterTypeDescription
eventstringName of the event. example: GMCP.Item.Add
argsobjectData object to be passed as a parameter to all callback functions.

Events are string ids used to flag all associated listener functions to fire. By default all GMCP received from the server are raised as events. Users can add any number of additional events.

eventStream.raiseEvent("testEvent");
/*
Expected console output based on previous examples:
> "arrow function"
> "named arrow function"
> "single fire event"
*/
eventStream.raiseEvent("testEvent");
/*
Expected console output based on previous examples:
> "arrow function"
> "named arrow function"

** Note the "single fire event" output is not present in this second run
*/

removeListener

eventStream.removeListener(event, callbackId);
ParameterTypeDescription
eventstringName of the event. example: GMCP.Item.Add
callbackIdstringCallback id. Function name

Removes a listener from an event. Typical usage is by function name. Will also accept an integer representing the array position of the listener.

eventStream.removeListener("testEvent", "testFunction");
eventStream.raiseEvent("testEvent");
/*
Expected console output based on previous examples:
> "arrow function"
*/

purge

eventStream.purge(event);

Removes all listeners from an event.

eventStream.purge("testEvent");