action: string;
[x: string]: unknown
}
interface MiniAppMessageSubscriber<T extends unknown = {}> {
(params: MiniAppMessage<T>): void
unknown怎么处理}
interface MiniAppEventBus {
/**
* @description 回调函数订阅单个、或多个type
* @template T
* @param {(string | string[])} type
* @param {MiniAppMessageSubscriber<T>} callback
* @memberof MiniAppEventBus
*/
subscribe<T extends unknown = {}>(type: string | string[], callback: MiniAppMessageSubscriber<T>): void;
/**
* @description Promise 订阅单个、或多个type
* @template T
* @param {(string | string[])} type
* @returns {Promise<MiniAppMessage<T>>}
* @memberof MiniAppEventBus
*/
subscribeAsync<T extends {} = MiniAppMessageBase>(type: string | string[]): Promise<MiniAppMessage<T>>; /**
* @description 取消订阅单个、或多个type
* @param {(string | string[])} type
* @returns {Promise<void>}
* @memberof MiniAppEventBus
*/
unSubscribe(type: string | string[]): Promise<void>;
/**
* @description postMessage替代,⽆需关注环境变量
* @param {MessageToMiniApp} msg
* @returns {Promise<unknown>}
* @memberof MiniAppEventBus
*/
postMessage(msg: MessageToMiniApp): Promise<unknown>;
}
class MiniAppEventBus implements MiniAppEventBus{
/**
* @description: 监听函数
* @type {Map<string, MiniAppMessageSubscriber[]>}
* @memberof MiniAppEventBus
*/
listeners: Map<string, MiniAppMessageSubscriber[]>;
constructor() {
this.listeners = new Map<string, Array<MiniAppMessageSubscriber<unknown>>>();
this.init();
}
/**
* @description 初始化
* @private
* @memberof MiniAppEventBus
*/
private init() {
if (!) {
/
/ 引⼊脚本
injectMiniAppScript();
}
this.startListen();
}
/**
* @description 保证my变量存在的时候执⾏函数func
* @private
* @param {Function} func
* @returns
* @memberof MiniAppEventBus
*/
private async ensureEnv(func: Function) {
return new Promise((resolve) => {
const promiseResolve = () => {
resolve(func.call(this));
};
// 全局变量
if () {
promiseResolve();
}
document.addEventListener('myLoad', promiseResolve);
});
}
/**
* @description 监听⼩程序消息
* @private
* @memberof MiniAppEventBus
*/
private listen() {
this.dispatch<unknown>(pe, msg);
};
}
private async startListen() {
sureEnv(this.listen);
}
/**
* @description 发送消息,必须包含action
* @param {MessageToMiniApp} msg
* @returns
* @memberof MiniAppEventBus
*/
public postMessage(msg: MessageToMiniApp) {
return new Promise((resolve) => {
const realPost = () => {
postMessage(msg));
};
sureEnv(realPost));
});
}
/**
* @description 订阅消息,⽀持单个或多个
* @template T
* @param {(string|string[])} type
* @param {MiniAppMessageSubscriber<T>} callback
* @returns
* @memberof MiniAppEventBus
*/
public subscribe<T extends unknown = {}>(type: string | string[], callback: MiniAppMessageSubscriber<T>) { const subscribeSingleAction = (type: string, cb: MiniAppMessageSubscriber<T>) => {
let listeners = (type) || [];
listeners.push(cb);
this.listeners.set(type, listeners);
};
this.forEach(type,(type:string)=>subscribeSingleAction(type,callback));
}
private forEach(type:string | string[],cb:(type:string)=>void){
if (typeof type === 'string') {
return cb(type);
}
for (const key in type) {
if (Object.prototype.hasOwnProperty.call(type, key)) {
const element = type[key];
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论