janpaul123 / paperprograms

Run Javascript on pieces of paper!
https://paperprograms.org
MIT License
484 stars 55 forks source link

Proposal: Event API #64

Open biowaffeln opened 6 years ago

biowaffeln commented 6 years ago

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?

janpaul123 commented 6 years ago

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?