event bus based on post message
npm i postmessage-event-bus
import { CreatePostMessageEventBus } from 'postmessage-event-bus'
require('postmessage-event-bus')
<script src="https://unpkg.com/[email protected]/eventBus.min.js"></script>
let pmeb = CreatePostMessageEventBus({
success: function () {
console.log('registry successed')
},
receive: function (data) {
console.log('receive post message data,data.name is message type', data, data.name)
},
request: function (cmd) {
console.log('other window need u send some event. cmd is the message type', cmd)
this.send({
name: 'change', // message type
sum: 100 // value
... // other values
})
},
// events that u wanna listen
on: ['increase', 'decrease'],
// events that u will dispatch
// this is for auto dispatch events for some pages that listening thoes events
// otherwise they would notify current page manually
// eg:
// after initialized or success callback in other window
// pmeb.send({name: 'change'})
emit: ['change']
})
pmeb.send({
name: 'otherEvent', // u can dispatch event that not specified in emit params
data: { ... }
})
https://zeyongtsai.github.io/postmessage-event-bus/root.html