1.0.2 • Published 2 years ago

@manycore/saas-miniapp-message v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@manycore/saas-miniapp-message

酷家乐商家后台小程序Manycore接口调用、消息通讯工具包

使用方式

# npm
npm install @manycore/saas-miniapp-message
# yarn
yarn add @manycore/saas-miniapp-message
/**
 * 广播事件
 *
 * @param type 事件 type
 * @param payload 事件传递的数据
 * @param {(FrameHost | Window)} [postMessager=window.parent]
 *
 * 当传入的对象 `payload` 不是 plain 对象的时候(如 Error、function、DOMElement、JSX 等),这里会报错
 * 「Uncaught DOMException: The object could not be cloned.」或 「DataCloneError: The object could not be cloned.」
 */
function broadcast<P = void>(type: string, payload?: P, postMessager?: FrameHost | Window): void;

/**
 * 广播事件,返回 Promise , subscribe 中必须传入`postMessager`参数来承接该事件时,否则此 Promise 将永远 Pending
 *
 * @param type 事件 type
 * @param payload 事件传递的数据
 * @param {(FrameHost | Window)} [postMessager=window.parent]
 *
 * 当传入的对象 `payload` 不是 plain 对象的时候(如 Error、function、DOMElement、JSX 等),这里会报错
 * 「Uncaught DOMException: The object could not be cloned.」或 「DataCloneError: The object could not be cloned.」
 */
function broadcastPromise<T = void, P = void>(
    type: string,
    payload: P,
    postMessager?: FrameHost | Window
): Promise<T>;

/**
 * 注册广播的事件回调,返回可用于反注册的方法
 *
 * @param type 事件 type
 * @param fn 事件的处理函数
 */
function subscribe<T = void, P = void>(type: string, fn: (payload: P) => Promise<T>): () => void;
function subscribe<P = void>(type: string, fn: (payload: P) => void): () => void;

/**
 * 单次注册广播的事件回调,运行一次后将自动移除,返回可用于反注册的方法
 *
 * @param type 事件 type
 * @param fn 事件的处理函数
 */
function subscribeOnce<T = void, P = void>(
    type: string,
    fn: (payload: P) => Promise<T>
): () => void;
function subscribeOnce<P = void>(type: string, fn: (payload: P) => void): () => void;