import system from 'bsl-build/system'; import { ListenerCallback } from 'bsl-core/app/Scheduler'; import Component from 'bsl-mp/app/Component'; import Page from 'bsl-mp/app/Page'; import { clearInterval, setInterval } from 'bsl-mp/app/Scheduler'; import { getGlobalData } from 'bsl-mp/utils/globalData'; import { onRouteChange, RouteAction } from 'bsl-mp/utils/link'; type Fun = (...arg: any) => any; export interface RecordConfig { __record__: { /** * 最长空闲时间 * @description 单位毫秒 */ maxIdleTime: number; } } interface Context { instance: T extends 'component' ? Component : Page /** * 已运行时间 * @description 只在onUnload生命周期中生效 */ timeFrame?: number; } interface Listen { [key: string]: (context: Context, ...args: any[]) => void } /** record记录的方法名称 */ const recordData: Record = {}; /** recordTrigger记录的回调函数 */ const recordListen: Record = {}; const RecordKeys = { onUnloadKey: '__recordOnUnLoadRecordKey__', onLoadKey: '__recordOnLoadRecordKey__' }; const timerWeek: WeakMap, ListenerCallback> = new WeakMap(); /** 计时 */ let timeFrame = 0; /** 在一个页面周期中是否有触发过一次record */ let isTriggerInPage = false; /** recordData是否已新增 */ let isPush = false; // 执行顺序,用来构成Page或Component的实例索引 let index = 0; function createKey(index: number, key: string) { return index + '_' + key; } export function recordTrigger(listen: Listen) { return () => { if (isPush) { for (const key in listen) { recordListen[createKey(index, key)] = listen[key] as any as Fun; } index++; isPush = false; } if (system.env === 'development') { setTimeout(() => { for (const lk in recordListen) { if (!recordData[lk]) { console.error(lk, '此recordTrigger key在记录器中不存在!'); console.error('recordData', recordData); console.error('recordListen', recordListen); } } }, 2000); } }; } export function record(recordName: string) { const newRecordName = createKey(index, recordName); return function(target: any, key: string, descriptor: PropertyDescriptor) { recordData[newRecordName] = descriptor.value; // 对onUnload和onLoad方法使用装饰器时,由内部来调用 if (target instanceof Page && (key === 'onUnload' || key === 'onLoad')) { const recordKey = RecordKeys[(key + 'Key') as keyof typeof RecordKeys]; (target as any)[recordKey] = newRecordName; } (target as any)[key] = wrapper(newRecordName, descriptor.value, key, target); isPush = true; return target; }; } function wrapper(recordName: string, cb: Fun, funName: string, sourceInstance: Component | Page) { return function(this: Component | Page, ...arg: any) { // 只在页面示例中有这个值 const __route__ = (this as any).__route__; // const recordListen = getGlobalData().__recordListen__; const config = getGlobalData().__record__; isTriggerInPage = true; wx.nextTick(() => { if (recordListen[recordName]) { const ctx: Context = { instance: this as any }; if (funName !== 'onUnload' && funName !== 'onLoad') { recordListen[recordName](ctx, ...arg); } } else if (recordName.split('_')[1] !== '') { console.error(`"${recordName}" 不在recordListen中`, recordListen); } }); // 页面实例并且onUnload方法存在,且触发onload方法时 if (__route__ && (sourceInstance as any).onUnload) { const page = this as Page; const maxIdleTime = config?.maxIdleTime || 5 * 60 * 1000; const ctx: Context = { instance: page, timeFrame: 0 }; let offRouteChange: () => void; const onRouteChangeCallback = (action: RouteAction) => { // const recordListen = getGlobalData().__recordListen__; if (action === 'leave') { if (timeFrame > 1000) { ctx.timeFrame = timeFrame; if (recordListen[(sourceInstance as any)[RecordKeys.onUnloadKey]]) { recordListen[(sourceInstance as any)[RecordKeys.onUnloadKey]](ctx); } } clearInterval(timerWeek.get(sourceInstance as Page)); timerWeek.delete(sourceInstance as Page); offRouteChange!(); } else { isTriggerInPage = false; timeFrame = 0; const timer = setInterval(() => { // 页面无操作一段时间停止记录 if (isTriggerInPage === false && timeFrame > maxIdleTime) { clearInterval(timer); return true; } timeFrame += 1000; return true; }, 1000); timerWeek.set(sourceInstance as Page, timer); if (recordListen[(sourceInstance as any)[RecordKeys.onLoadKey]]) { recordListen[(sourceInstance as any)[RecordKeys.onLoadKey]](ctx, ...arg); } } }; if (funName === 'onLoad') { offRouteChange = onRouteChange(page, onRouteChangeCallback); } } return cb.call(this, ...arg); }; }