Skip to content

zeyongTsai/postmessage-event-bus

Repository files navigation

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

About

event bus based on post message

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published