QUICK TIPS: Application Events\Messages


#1

Hi All,

This is a follow article on QUICK TIPS: Window Events giving you the same instructions on setting up Application events and messaging.

Most of these events are triggered by the internal messaging system via API methods, like the VFS.

Ref: https://os.js.org/doc/client/OSjs.Core.Process.html

These event hooks was introduced in the latest commits, make sure to update!

Events

This is the list of available internal events (name, description and what arguments you get back):

  message       All events                               => (msg, object, options)
  attention     When application gets attention signal   => (args)
  hashchange    When URL hash has changed                => (args)
  api           API event                                => (method)
  destroy       Destruction event                        => (killed)
  destroyWindow Attached window destruction event        => (win)
  vfs           For all VFS events                       => (msg, object, options)
  vfs:mount     VFS mount event                          => (module, options, msg)
  vfs:unmount   VFS unmount event                        => (module, options, msg)
  vfs:write     VFS write event                          => (dest, options, msg)
  vfs:mkdir     VFS mkdir event                          => (dest, options, msg)
  vfs:move      VFS move event                           => ({src,dest}, options, msg)
  vfs:delete    VFS delete event                         => (dest, options, msg)
  vfs:upload    VFS upload event                         => (file, options, msg)
  vfs:update    VFS update event                         => (dir, options, msg)

Note: this is the Window instance.

Binding Events

// 'app' would be 'this' in most cases

app._on('vfs', function(msg, obj) {
  console.log('A VFS event occured', msg, obj)
});
app._on('destroyWindow', function(win) {
  console.log('A window was removed from my app', win)
});

Custom Events

// 'app' would be 'this' in most cases

app._on('something', function(foo) { 
  console.log(foo); // => "bar"
});

app._emit('something', ['bar']);

Sending a message

You can broadcast messages to all applications (that will emit an event in your application) by simply doing:

API.message('foo', {bar: 'baz'})

// If you send your application ID as a source, it will not be triggered in that application.
API.message('foo', {bar: 'baz'}, {source: app.__pid})

Ref: https://os.js.org/doc/client/OSjs.Core.Process.html#_onMessage

Using prototype chain

You can also use the prototype chain and override the internal methods to do this. See the API documentation for Process for more information on this (linked in the top).


UPDATE: Version bump - alpha77