Skip to content

Documentation (v1.1.0)

Lars Andersson edited this page Sep 27, 2020 · 2 revisions

NotificationSystem()

Type: struct

The notification bus. Handles all the message forwarding and keeps track of all the subscribers.

subscribe([id])

Type: function

Enables an instance to receive and read messages on the notification bus. The argument tells which instance to subscribe. When omitting argument, the calling instance subscribes itself.

Argument Type Description
[id] Real Instance ID of the instance to subscribe
Default: Self

Returns: N/A

unsubscribe([id])

Type: function

Removes an instance from the notification bus. The argument tells which instance to unsubscribe. When omitting argument, the calling instance unsubscribes itself.

Argument Type Description
[id] Real Instance ID of the instance to unsubscribe
Default: Self

Returns: N/A

broadcast(message, [callback, data])

Type: function

Broadcasts a message on the notification bus. Instances subscribed to the notification bus will receive the message, and trigger their callbacks bound to the specific message. The notification bus is global, so any instance can broadcast a message.

Argument Type Description
message Any Message to broadcast to the receivers
[callback] Function Function to run as a callback. This callback will trigger after the callback bound on the receiver end
Default: Undefined
[data] Any Argument passed to the callback on the receiver end
Default: -1

Returns: N/A

Examples:

// Broadcasts the string "Hello" on the notification bus, without any callbacks
broadcast("Hello");

enum MESSAGE {
    hello,
    world
}

// Broadcasts the enum value 'hello' on the notification bus, without any callbacks
broadcast(MESSAGE.hello);

// Broadcasts the enum value 'world' on the notification bus, with a callback to trigger
// after any callback bound on the receiver side
broadcast(MESSAGE.world, function() {
    show_debug_message("Hello, world!");
});

// Broadcasts the string "Foo" on the notification bus, with the data value 10 sent as argument to
// the callback bound on the receiver side
broadcast("Foo", 10);

// Broadcasts the string "Bar" on the notification bus, with a callback to trigger
// after any callback bound on the receiver side and with the data value "foobar" sent as argument
// to the callback bound on the receiver side
broadcast("Bar", function() {
    show_debug_message("Hello, hello?");
}, "foobar");

Receiver()

Type: struct

Enables an instance to store callbacks, and respond to messages sent over the notification bus.

Returns: Receiver instance

add(message, [callback])

Type: function

Add a trigger message to the receiver, and possibly bind a callback to this trigger message. The receiver will react when seeing the trigger message on the notification bus.

Argument Type Description
message Any Message to listen for
[callback] Function Callback function to run when message received. The callback can accept one argument
Default: Undefined

Returns: N/A

remove(message)

Type: function

Removes the trigger message from the receiver.

Argument Type Description
message  Any Message to stop listening for

Returns: N/A

Examples:

receiver = new Receiver();

// Prints the string "a, b, c" when seeing "Hello" on the notification bus
// The callbacks are stored within the Receiver struct
receiver.add("Hello", function() {
   show_debug_message("a, b, c");
});

// Allow the instance to trigger callbacks sent with the message enum 'world'
receiver.add(MESSAGE.world);

// Callback with parameter
receiver.add("Bar", function(data) {
   show_debug_message(data);
});

// Stops listening for the message string "Hello", and removes the callback
receiver.remove("Hello");