Skip to content

Implement support for callback style programming

Deucе requested to merge Deuce/sbbs:echicken-less-grumpy into master

If js.do_callbacks is set to true, a script will not terminate when it finishes. Instead, it will enter an event loop running installed callbacks when configured events occurs. Currently, callbacks can be added with the following new methods:

  • js.setInterval(callback_function, period_in_ms[, thisObj]);
  • js.setTimeout(callback_function, timeout_in_ms[, thisObj]);
  • SocketInstance.on('read' | 'write', callback_function);
  • SocketInstance.once('read' | 'write', callback_function);
  • SocketInstance.connect(host, port, callback_function);
  • console.on('read', callback_function);
  • console.once('read', callback_function);

Aside from Socket.connect(), these functions return a value that can be passed to a matching clear function:

  • js.clearInterval(id);
  • js.clearTimeout(id);
  • SocketInstance.clearOn('read' | 'write', id);
  • SocketInstance.clearOnce('read' | 'write', id);
  • console.clearOn('read' | 'write', id);
  • console.clearOnce('read' | 'write', id);

Additionally, user-defined events are now supported:

  • js.addEventListener(eventName, callback)
  • js.removeEventListener(id)
  • js.dispatchEvent(eventName[, thisObj])

We also get a cool event-based DNS library.

Edited by Deucе

Merge request reports