Skip to content

Latest commit

 

History

History
65 lines (59 loc) · 1.54 KB

README.md

File metadata and controls

65 lines (59 loc) · 1.54 KB

postmessage-event-bus

event bus based on post message

Usage

Install

npm

npm i postmessage-event-bus
es6 modules
import { CreatePostMessageEventBus } from 'postmessage-event-bus'
cmd
require('postmessage-event-bus')

umd

<script src="https://unpkg.com/[email protected]/eventBus.min.js"></script>

API

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: { ... }
})

Demo

https://zeyongtsai.github.io/postmessage-event-bus/root.html