小程序主包方法迁移到分包-调用策略

TypeScript 复制代码
/*
 * @Date: 2024-12-10 15:59:32
 * @Description: 加载异步代码
 */

import { type Type, type ReversedType, type ReversedTypeRecord } from '@/lazy/type'

type Key = keyof Type


/** 只支持调用函数 */
export const lazierInit = new Proxy<ReversedType>({} as any, {
  get<T extends Key>(_, key: T): Promise<ReversedTypeRecord[T]['value']> {
    return <Promise<ReversedTypeRecord[T]['value']>><unknown>function (...args) {
      return new Promise((resolve) =>{
        /** @ts-ignore */
        resolve(require.async("../../../lazy/type.js"))
      }).then((res) => {
        const {type : reversedType} = res as { type: Type }
        if (typeof reversedType[key] !== 'function') {
          return ''
        }
        return  (reversedType[key] as (...arg: any[]) => any).call(this,...args)
      }).catch(err => {
        wx.$.collectEvent.event("lazyFalse", {
          key: key,
          data: JSON.stringify(err)
        })
        return Promise.reject(err)
      })
    }
  }
})



/** 支持直接访问常量(如数组)和调用函数  
 * 
 * // 1. 调用函数(带参数)
 *await wx.$.l.operationMidCallV3(arg1, arg2);
 *
 * // 2. 访问常量数组
 *const shouldShow = (await wx.$.l.newResumeMidPops1()).includes('call_B');
 * 
 * // 3. 更简洁的常量访问方式(无参数调用)
 * const arr = await wx.$.l.newResumeMidPops1();
 * 
 * // 4.需要将组件的实例this等 一起带给方法
 * wx.$.l.callPhoneBtnOfList.call(this, e.detail, { ... })
 * 
*/
const moduleCache: { [key: string]: any } = {};

export const lazier = new Proxy({} as any, {
  get<T extends string>(_: any, key: T) {
    return function (this: any, ...args: any[]) {
      return (async () => {
        if (!moduleCache[key]) {
          try {
            const res = await new Promise<any>((resolve) => {
              /** @ts-ignore */
              resolve(require.async("../../../lazy/type.js"));
            });
            const { type: reversedType } = res as { type: any };
            moduleCache[key] = reversedType[key];
          } catch (err) {
            wx.$.collectEvent.event("lazyFalse", { key, data: JSON.stringify(err) });
            throw err;
          }
        }

        const cachedValue = moduleCache[key];
        if (typeof cachedValue === 'function') {
          return cachedValue.apply(this, args);
        }
        return cachedValue;
      })();
    };
  }
});

---------------避免用户使用程序的过程中,缓存无限增大,内存泄露。

  • 结合缓存过期时间和LRU(最近最少使用)策略,确保缓存的有效性和内存使用效率。

继续优化:

TypeScript 复制代码
interface CacheItem {
  value: any;
  expires: number;
}

const MAX_CACHE_SIZE = 1000;
const moduleCache: { [key: string]: CacheItem } = {};
const cacheOrder: string[] = [];

function addToCache(key: string, value: any) {
  if (cacheOrder.length >= MAX_CACHE_SIZE) {
    const oldestKey = cacheOrder.shift();
    if (oldestKey) {
      delete moduleCache[oldestKey];
    }
  }
  moduleCache[key] = { value, expires: Date.now() + 60000 }; // 设置1分钟过期时间
  cacheOrder.push(key);
}

export const lazier = new Proxy({} as any, {
  get<T extends string>(_: any, key: T) {
    return function (this: any, ...args: any[]) {
      return (async () => {
        const now = Date.now();
        if (!moduleCache[key] || moduleCache[key].expires < now) {
          try {
            const res = await new Promise<any>((resolve) => {
              /** @ts-ignore */
              resolve(require.async("../../../lazy/type.js"));
            });
            const { type: reversedType } = res as { type: any };
            addToCache(key, reversedType[key]);
          } catch (err) {
            wx.$.collectEvent.event("lazyFalse", { key, data: JSON.stringify(err) });
            throw err;
          }
        }

        const cachedValue = moduleCache[key].value;
        if (typeof cachedValue === 'function') {
          return cachedValue.apply(this, args);
        }
        return cachedValue;
      })();
    };
  }
});
相关推荐
tryCbest8 小时前
Python基础之爬虫技术(一)
开发语言·爬虫·python
hixiong1238 小时前
C# OpenCVSharp实现Hand Pose Estimation Mediapipe
开发语言·opencv·ai·c#·手势识别
集成显卡8 小时前
AI取名大师 | PM2 部署 Bun.js 应用及配置 Let‘s Encrypt 免费 HTTPS 证书
开发语言·javascript·人工智能
AI小云8 小时前
【Numpy数据运算】数组间运算
开发语言·python·numpy
是苏浙9 小时前
零基础入门C语言之C语言实现数据结构之单链表经典算法
c语言·开发语言·数据结构·算法
纵有疾風起9 小时前
C++—vector:vecor使用及模拟实现
开发语言·c++·经验分享·开源·stl·vector
任子菲阳10 小时前
学Java第四十三天——Map双列集合
java·开发语言
wearegogog12310 小时前
基于MATLAB的谷物颗粒计数方法
开发语言·matlab
脸大是真的好~10 小时前
黑马JAVAWeb -Vue工程化-API风格 - 组合式API
前端·javascript·vue.js
Jackson@ML10 小时前
360度看C#编程语言
开发语言·c#