@ysfe/cross-domain-caller
TypeScript icon, indicating that this package has built-in type declarations

1.2.2 • Public • Published

@ysfe/cross-domain-caller

支持多客户端消息消费的跨域通信工具

usage

install

  • exec: yarn add @ysfe/cross-domain-caller

invoke

import CrossDomainCaller from ' @ysfe/cross-domain-caller'

/* 调用方 */
const caller = new CrossDomainCaller()
// 关键点 1: 设置接收方window
caller.setTarget(iframe.contentWindow)
caller.invoke(method, data) // 发起调用

/* 处理方 */
const handler = new CrossDomainCaller()

// 注册事件处理方法
handler.on(method, (data: any, lastRes?: any) => {
    // event handle function
})

// 注册自定义事件处理函数
handler.setMessageHandler(async (method: string, data?: any) => {
    // ? 判断是否归属 sdk app 事件调用
    if (this.handlers[method]) {
        this.handlers[method](data)
    } else {
        // ! 注意: 缺少处理方法需要抛出异常 `throw new Error(`event '${method}' not defined.`)`
        throw new Error(`event '${method}' not defined.`)
    }
})

实现说明

暂时先看源码.

TODO

Readme

Keywords

none

Package Sidebar

Install

npm i @ysfe/cross-domain-caller

Weekly Downloads

0

Version

1.2.2

License

MIT

Unpacked Size

31 kB

Total Files

10

Last publish

Collaborators

  • wellfee
  • huanyansss