SigSlot : Signal-Slot communications

更新时间:
2024-03-04
下载文档

SigSlot : Signal-Slot communications

SigSlot is an event driven asynchronous communication component that supports multi task and multi process. If you need multi process support, you must start JSRE (Global Signal Slot) and use the -g option to enable current process GSS support when starting the process. In EdgerOS, apps from the same vendor can use the GSS feature to subscribe and publish messages to each other.

SigSlot is a typical subscribe and publish communication mechanism. Based on SigSlot, you can easily perform multi-tasking decoupling. Each task is designed independently, which greatly reduces the difficulty of application development.

User can use the following code to import the SigSlot module.

var SigSlot = require('sigslot');

Example

  • main.js
#!/bin/javascript

var SigSlot = require('sigslot');

var sigslot = new SigSlot('Test');
var task = new Task('./task.js');
var msg = { a: 3, b: 5 };

// Subscribe
sigslot.slot('event1', (msg) => {
  console.log('main event1 arrived:', msg);
});

while (true) {
  // Publish
  sigslot.emit('event1', msg);
  sys.sleep(1000);
}
  • task.js
var SigSlot = require('sigslot');

var sigslot = new SigSlot('Test');

// Subscribe
sigslot.slot('event1', (msg) => {
  console.log('task event1 arrived:', msg);
});

while (true) {
  // Nothing to do, relax CPU.
  sys.sleep(1000);
}

Can be run as follows:

$ ./main.js
[JSRE-USR]main event1 arrived: {a:3,b:5}
[JSRE-USR]task event1 arrived: {a:3,b:5}
[JSRE-USR]main event1 arrived: {a:3,b:5}
[JSRE-USR]task event1 arrived: {a:3,b:5}
...

Support

The following shows SigSlot module APIs available for each permissions.

 User ModePrivilege Mode
SigSlot
SigSlot.gssMode
SigSlot.gssIsConnect
SigSlot.gssWaitConnect
SigSlot.gssQueueCount
SigSlot.gssHasUnconfirmed
sigslot.connect
sigslot.disconnect
sigslot.slot
sigslot.on
sigslot.off
sigslot.has
sigslot.emit
sigslot.ref
sigslot.unref
SigSlot.Multiple
msigslot.connect
msigslot.disconnect
msigslot.slot
msigslot.on
msigslot.off
msigslot.has
msigslot.emit
msigslot.delete

SigSlot Class

new SigSlot([name[, global]])

  • name {String} SigSlot name. default: undefined.
  • global {Boolean} Whether the new sigslot object is valid for all processes in the entire system. default: false.
  • Returns: {Object} A new sigslot object.

Create a sigslot object. If no name is specified, create an anonymous sigslot object. If name is specified, try to open the sigslot object with same name. If not found, create a new object. The name parameter is valid for all tasks of the entire process. Different tasks can create sigslot objects with same name for inter-task communication.

Example

var anonymous = new SigSlot();
var mtask = new SigSlot('SS-1');

SigSlot.gssMode()

  • Returns: {String} Operating mode.

Get the current process GSS working mode:

  • 'off' GSS is not enabled.
  • 'listener' GSS listen only mode.
  • 'publisher' GSS can subscribe and publish.

SigSlot.gssIsConnect()

  • Returns: {Boolean} Is connected.

Get whether the current process is connected to the Global Signal Slot forwarding server (GSSD). When the current process starts, if GSS is enabled, the JSRE environment will automatically try to connect to GSSD.

SigSlot.gssWaitConnect([timeout])

  • timeout {Integer} Wait timeout milliseconds. default: wait forever.
  • Returns: {Boolean} Is connected.

This function is a synchronous wait function, waiting for the current process to successfully connect to GSSD. If the timeout is specified, no connection is successful within the specified time, false will returned.

Example

SigSlot.gssWaitConnect(); // wait for gss connect

SigSlot.gssQueueCount()

  • Returns: {Integer} Current gss publish queue length.

Before connecting with GSSD, all information that needs to be delivered to other processes will be saved in the queue to be sent. This function gets the number of messages to be sent currently.

SigSlot.gssHasUnconfirmed()

  • Returns: {Boolean} Whether there are unconfirmed subscription messages for the current process.

When a sigslot object with a global property calls the sigslot.on() method, an event subscription message will be generated and sent to the GSS server. When current process does not successfully connect with the GSS server, the subscription request will be stored in an unconfirmed queue, this function gets whether there are unconfirmed subscription messages for the current process.

SigSlot Object

sigslot.connect(event, func[, arg])

  • event {String} Subscribe event name.
  • func {Function} Event callback function.
    • arg {Any} Optional callback argument.
    • msg {String} | {Object} Message.
  • arg {Any} Event callback argument.

Set the func callback function of the given event.

Example

sigslot.connect('onSomeEvent1', (msg) => {
  console.log('Event1!', msg);
});

sigslot.connect('onSomeEvent2', (arg, msg) => {
  console.log('Event2!', arg, msg);
}, arg);

sigslot.disconnect([event[, func]])

  • event {String} Unsubscribe event name.
  • func {Function} Event callback function.

Unsubscribe event and remove event callback. If no event is specified, then unsubscribe all events.

Example

sigslot.disconnect('onSomeEvent1', func); // Delete the specified listener of the specified event
sigslot.disconnect('onSomeEvent1'); // Delete all of the specified event listener
sigslot.disconnect(); // // Delete all listener, this sigslot can be recycled by the garbage collector.

sigslot.slot(event, func[, arg])

  • event {String} Subscribe event name.
  • func {Function} Event callback function. default: remove event callback.
    • arg {Any} Optional callback argument.
    • msg {String} | {Object} Message.
  • arg {Any} Event callback argument.

Alias for sigslot.connect().

sigslot.on(event, func[, arg])

  • event {String} Subscribe event name.
  • func {Function} Event callback function.
    • arg {Any} Optional callback argument.
    • msg {String} | {Object} Message.
  • arg {Any} Event callback argument.

Alias for sigslot.connect().

sigslot.off([event[, func]])

  • event {String} Unsubscribe event name.
  • func {Function} Event callback function.

Alias for sigslot.disconnect().

sigslot.has(event[, func])

  • event {String} Unsubscribe event name.
  • func {Function} Event callback function.
  • Returns: {Boolean} Whether the specified event has been subscribed.

Whether the specified event has been subscribed, if the func parameter is specified, it is judged whether the specified event is subscribed by the specified function.

sigslot.emit(event[, message])

  • event {String} Publish event name.
  • message {String} | {Object} Event message. default: ''.

Publish a specified event, The message can be a string or an object. If it is an object, the system discards all methods and passes the object to slot.

Unlike EventEmitter, EventEmitter call callback function immediately when it generates an event, but SigSlot not an immediate callback, it sends the event to all tasks that subscribe this event, and then generates an interrupt request. Only when the task responds interrupt request, the event slot will be executed.

Example

sigslot.emit('onSomeEvent1', { a: 1, b: 2 });
sigslot.emit('onSomeEvent2', { a: 3, b: 4, c: 'event' });

sigslot.ref()

This function adds a reference to this SigSlot object to prevent the object from being recycled.

sigslot.unref()

This function reduces the reference of this SigSlot object and must be used in pairs with sigslot.ref().

SigSlot.Multiple Class

Multiple SigSlot, the document will be open in the future.

文档内容是否对您有所帮助?
有帮助
没帮助