janpaul123 / paperprograms

Run Javascript on pieces of paper!

Home Page:https://paperprograms.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proposal: Event API

biowaffeln opened this issue · comments

idea

I would like to propose an API for papers to communicate with each other by sending events.
The idea came from a usecase like in this tweet - basically a setup in which one has a keyboard-paper that emits events (e.g. keypress) and sends them to another paper, which can then handle those events.

the API

The API would look similar to that of the Node Event API. A paper could emit events like so:

/* paper.emit(eventname, paperNumber, data) */
paper.emit('keypress', '123', {key: 'a'})

with the second parameter specifying the paper to which you want to send the event. Alternatively one could also have a broadcast method if one wanted to send an event to all papers:

paper.broadcast('keypress', {key: 'a'})

and handling of events would look like so:

paper.on('keypress', function(data) {
  // do something
})  

Any opinions on that?

You can already send and receive events using BroadcastChannel, which is why I didn't add it to the API originally. Might be nice to add some examples in the documentation though?