1.2.2 • Published 3 years ago

iframe-cross-domain-call v1.2.2

Weekly downloads
91
License
MIT
Repository
-
Last release
3 years ago

@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

1.2.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.2

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.9

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago