import Event from '../typings/event'; /* eslint-disable @typescript-eslint/no-empty-function */ type Instance = WechatMiniprogram.Component.Instance, WechatMiniprogram.Component.PropertyOption, Record>; interface Relation { /** 目标组件的相对关系 */ type: 'parent' | 'child' | 'ancestor' | 'descendant'; /** 关系生命周期函数,当关系被建立在页面节点树中时触发,触发时机在组件attached生命周期之后 */ linked?: (target: Event.Target) => void; /** 关系生命周期函数,当关系在页面节点树中发生改变时触发,触发时机在组件moved生命周期之后 */ linkChanged?: (target: Event.Target) => void; /** 关系生命周期函数,当关系脱离页面节点树时触发,触发时机在组件detached生命周期之后 */ unlinked?: (target: Event.Target) => void; /** 如果这一项被设置,则它表示关联的目标节点所应具有的behavior,所有拥有这一behavior的组件节点都会被关联 */ target?: Component; } interface EventOptions { /** 事件是否冒泡 */ bubbles?: boolean; /** 事件是否可以穿越组件边界,为false时,事件将只能在引用组件的节点树上触发,不进入其他任何组件内部 */ composed?: boolean; /** 事件是否拥有捕获阶段 */ capturePhase?: bigint; } /** * 是否忽略空函数 * @return 为true则不是空函数,为false则是空函数 */ // eslint-disable-next-line @typescript-eslint/ban-types export function ingoreEmptyFunction(fun: any) { if (typeof fun === 'function') { // eslint-disable-next-line @typescript-eslint/ban-types const funString = (fun as any as Function).toString().replace(/\s+|\n/g, ''); const funBodyStatIndex = funString.indexOf('{'); const funBodyEndIndex = funString.lastIndexOf('}'); const funBody = funString.substring(funBodyStatIndex + 1, funBodyEndIndex); if (funBody === '' || funBody.indexOf('__此处是函数定义__') >= 0) { return false; } } return true; } abstract class Component> { constructor(data: State, properties: Record) { // 因为微信的奇葩机制,这里做一个强制的类型转换 this.properties = properties as any as Props; this.data = data; } /** 组件的内部数据,和 properties 一同用于组件的模板渲染 */ public readonly data: Readonly; /** 组件的对外属性,是属性名到属性设置的映射表 */ public readonly properties!: Readonly; /** 组件的文件路径 */ protected readonly is!: Readonly; /** 节点id */ protected readonly id!:Readonly; /** 节点dataset */ protected readonly dataset!: Readonly>; /** * 组件数据字段监听器,用于监听 properties 和 data 的变化, * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/observer.html | 数据监听器} * @version 2.6.1 */ public readonly watch!: Record void> /** * 类似于mixins和traits的组件间代码复用机制 * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/behaviors.html | behaviors} */ protected readonly behaviors!: (string | {new (): Component})[]; /** * 组件间关系定义,参见 组件间关系 * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/relations.html | 组件间关系} */ protected readonly relations!: Record; /** * 组件接受的外部样式类 * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html | 外部样式类} */ protected readonly externalClasses!: Readonly; /** 一些选项(文档中介绍相关特性时会涉及具体的选项设置,这里暂不列举) */ protected readonly options!: Readonly>; /** * 定义段过滤器,用于自定义组件扩展, * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/extend.html | 自定义组件扩展} * @version 2.2.3 */ protected definitionFilter(behavior: Component) {} /** 组件生命周期函数-在组件实例刚刚被创建时执行,注意此时不能调用 setData */ protected created() {} /** 组件生命周期函数-在组件实例进入页面节点树时执行 */ protected attached() {} /** 组件生命周期函数-在组件布局完成后执行 */ protected ready() {} /** 组件生命周期函数-在组件实例被移动到节点树另一个位置时执行 */ protected moved() {} /** 组件生命周期函数-在组件实例被从页面节点树移除时执行 */ protected detache() {} /** 每当组件方法抛出错误时执行 */ protected error(error: Error) {} /** 页面被展示 */ protected show() {} /** 页面被隐藏 */ protected hide() {} /** 页面尺寸变化 */ protected resize() {} /** * 函数用于将数据从逻辑层发送到视图层(异步),同时改变对应的 this.data 的值(同步) * @description 其中 key 可以以数据路径的形式给出,支持改变数组中的某一项或对象的某个属性,如 array[2].message,a.b.c.d,并且不需要在 this.data 中预先定义。 * @description 直接修改 this.data 而不调用 this.setData 是无法改变页面的状态的,还会造成数据不一致。 * @description 仅支持设置可 JSON 化的数据。 * @description 单次设置的数据不能超过1024kB,请尽量避免一次设置过多的数据。 * @description 请不要把 data 中任何一项的 value 设为 undefined ,否则这一项将不被设置并可能遗留一些潜在问题。 */ public setData!: (newData: State | Record, callback?: () => void) => void; /** 检查组件是否具有 behavior (检查时会递归检查被直接或间接引入的所有behavior) */ protected hasBehavior!: (behavior: Component) => void; /** * 触发事件 * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/events.html | 组件间通信与事件} */ protected triggerEvent!: (name: string, detail: Record, options?: EventOptions) => void; /** 创建一个 SelectorQuery 对象,选择器选取范围为这个组件实例内 */ protected createSelectorQuery!: () => WechatMiniprogram.SelectorQuery; /** 创建一个 IntersectionObserver 对象,选择器选取范围为这个组件实例内 */ protected createIntersectionObserver!: () => WechatMiniprogram.IntersectionObserver; /** * 创建一个 MediaQueryObserver 对象 * @version 2.11.1 */ protected createMediaQueryObserver!: () => WechatMiniprogram.IntersectionObserver; /** 使用选择器选择组件实例节点,返回匹配到的第一个组件实例对象(会被 wx://component-export 影响) */ public selectComponent!: (selector: string) => Component | T; /** 使用选择器选择组件实例节点,返回匹配到的全部组件实例对象组成的数组(会被 wx://component-export 影响) */ protected selectAllComponents!: (selector: string) => Component[]; /** * 选取当前组件节点所在的组件实例(即组件的引用者),返回它的组件实例对象(会被 wx://component-export 影响) * @version 2.8.2 */ protected selectOwnerComponent!: (selector: string) => Component; /** * 获取这个关系所对应的所有关联节点 * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/relations.html | 组件间关系} */ protected getRelationNodes!: (relationKey: string) => Relation[]; /** * 立刻执行 callback ,其中的多个 setData 之间不会触发界面绘制(只有某些特殊场景中需要,如用于在不同组件同时 setData 时进行界面绘制同步) * @version 2.4.0 */ protected groupSetData!: (callback: () => void) => void; /** * 返回当前页面的 custom-tab-bar 的组件实例 * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html | tabBar} * @version 2.6.2 */ protected getTabBar!: () => any; /** * 返回页面标识符(一个字符串),可以用来判断几个自定义组件实例是不是在同一个页面内 * @version 2.7.1 */ protected getPageId!: () => string; /** * 执行关键帧动画, * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/view/animation.html | 详见动画} * @version 2.9.0 */ protected animate!: (animate: string, selector: string[], keyframes: any[], duration: number, callback: () => void) => void; /** * 清除关键帧动画 * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/view/animation.html | 详见动画} * @version 2.9.0 */ protected clearAnimation!: (selector: string, options: any, callback: () => void) => void; /** * 清除关键帧动画 * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/view/animation.html | 详见动画} * @version 2.12.0 */ protected setUpdatePerformanceListener!: (options: any, callback: () => void) => void; public init(): Instance { const instance: Partial = { methods: {}, lifetimes: {}, pageLifetimes: {} }; const rootKeys = ['is', 'id', 'dataset', 'data', 'properties', 'watch']; const lifetimesKeys = ['created', 'attached', 'ready', 'moved', 'detached', 'error']; const pageLifetimesKeys = ['show', 'hide', 'resize']; const ingore = ['init', 'constructor']; for (const key in this) { if (rootKeys.includes(key)) { instance[key] = this[key]; } else if (typeof this[key] === 'function') { if (ingoreEmptyFunction(this[key])) { if (lifetimesKeys.includes(key)) { instance.lifetimes[key] = this[key]; } else if (pageLifetimesKeys.includes(key)) { instance.pageLifetimes[key] = this[key]; } else if (!ingore.includes(key)) { instance.methods[key] = this[key]; } } } else if (!ingore.includes(key)) { instance[key] = this[key]; } } return instance as Required; } } export default Component;