podium-lib / issues

All podium issues, bugs, questions etc goes here. Documentation is to be found at https://podium-lib.io
1 stars 0 forks source link

Create cross Podlet communication in browser #28

Closed trygve-lie closed 4 years ago

trygve-lie commented 4 years ago

We need a way to communicate between Podlets in the browser. The use case is that something happening in one Podlet should trigger a change in another Podlet on the page.

Such communication should:

Initially this is an EventEmitter which is attached to a global scope in a document where Podlets can subscribe to events and emit events.

API should be something down this road;

// In podlet A - Broadcast an event
import PodiumMsgBus from '@podium/browser';

const msgBus = new PodiumMsgBus();

msgBus.publish({ channel: 'search', topic: 'query', payload: 42 });

// In podlet B - Subscribe for events
import PodiumMsgBus from '@podium/browser';

const msgBus = new PodiumMsgBus();

msgBus.subscribe({ channel: 'search', topic: 'query' }, event => {
    console.log(event) // emitted object should be instance of a defined Event class
});

It should also be possible to retrieve a given set of previous messages from a given channel and topic combo.

import PodiumMsgBus from '@podium/browser';

const msgBus = new PodiumMsgBus();

const latest = await msgBus.log({ channel: 'search', topic: 'query' });

msgBus.subscribe({ channel: 'search', topic: 'query' }, event => {
    console.log(event) // emitted object should be instance of a defined Event class
});

NOTES:

alexanbj commented 4 years ago

Repository is found here: https://github.com/podium-lib/browser

alexanbj commented 4 years ago

I've now published a beta version: https://www.npmjs.com/package/@podium/browser