diff --git a/README.md b/README.md index a26e34a..acd523a 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,21 @@ ComponentWithComputed({ (类似地,也有 `BehaviorWithComputed` 构造器对应于 `Bahavior` 。) +**注意:当使用 `ComponentWithComputed` 或者 `BehaviorWithComputed` 的时候,要留意 `computedBehavior` 的加入时机** + +`computedBehavior` 会通过 `unshift` 方法加入到 `behaviors` 的开始位置,如果需要在加入 `computedBehavior` 之前加入其他的 behavior,需要通过 `behaviorsBeforeComputed` 属性加入,如: + +```ts +import { ComponentWithComputed } from 'miniprogram-computed' + +ComponentWithComputed({ + behaviors: ['b'], + behaviorsBeforeComputed: ['a'], +}) +``` + +最终会将behaviors处理成:`['a', 'computedBehavior', 'c']` + **关于 TS 兼容问题** 若在小程序中用 `TypeScript` 进行开发并使用到了 `Component` 构造器。这时定义 `computed` 或 `watch` 字段会出现类型报错。 diff --git a/dist/index.js b/dist/index.js index 8dcd4dc..d643548 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -var E=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var H=E(M=>{"use strict";Object.defineProperty(M,"__esModule",{value:!0}),function(t,e){for(var r in e)Object.defineProperty(t,r,{enumerable:!0,get:e[r]})}(M,{parseMultiDataPaths:function(){return it},getDataOnPath:function(){return ut}});var N=/^\s/,C=function(t,e){throw Error('Parsing data path "'+t+'" failed at char "'+t[e]+'" (index '+e+")")},rt=function(t,e){for(var r=e.index;e.index{U.exports=function(){let e=null,r;function i(n){return n?typeof n=="object"||typeof n=="function":!1}function f(n){if(n!==null&&!i(n))throw new TypeError("Object prototype may only be an Object or null: "+n)}let a=Object,u=Boolean(a.create)||!({__proto__:null}instanceof a),d=a.create||(u?function(o){return f(o),{__proto__:o}}:function(o){if(f(o),o===null)throw new SyntaxError("Native Object.create is required to create objects with null prototype");var h=function(){};return h.prototype=o,new h}),m=function(){return null},p=a.getPrototypeOf||([].__proto__===Array.prototype?function(o){let h=o.__proto__;return i(h)?h:null}:m);return r=function(n,o){if((this&&this instanceof r?this.constructor:void 0)===void 0)throw new TypeError("Constructor Proxy requires 'new'");if(!i(n)||!i(o))throw new TypeError("Cannot create proxy with a non-object as target or handler");let y=function(){};e=function(){n=null,y=function(c){throw new TypeError(`Cannot perform '${c}' on a proxy that has been revoked`)}},setTimeout(function(){e=null},0);let v=o;o={get:null,set:null,apply:null,construct:null};for(let c in v){if(!(c in o))throw new TypeError(`Proxy polyfill does not support trap '${c}'`);o[c]=v[c]}typeof v=="function"&&(o.apply=v.apply.bind(v));let P=p(n),s,l=!1,b=!1;typeof n=="function"?(s=function(){let _=this&&this.constructor===s,x=Array.prototype.slice.call(arguments);if(y(_?"construct":"apply"),_&&o.construct)return o.construct.call(this,n,x);if(!_&&o.apply)return o.apply(n,this,x);if(_){x.unshift(n);let j=n.bind.apply(n,x);return new j}return n.apply(this,x)},l=!0):n instanceof Array?(s=[],b=!0):s=u||P!==null?d(P):{};let g=o.get?function(c){return y("get"),o.get(this,c,s)}:function(c){return y("get"),this[c]},w=o.set?function(c,_){y("set");let x=o.set(this,c,_,s)}:function(c,_){y("set"),this[c]=_},A=a.getOwnPropertyNames(n),D={};A.forEach(function(c){if((l||b)&&c in s)return;let _=a.getOwnPropertyDescriptor(n,c),x={enumerable:Boolean(_.enumerable),get:g.bind(n,c),set:w.bind(n,c)};a.defineProperty(s,c,x),D[c]=!0});let k=!0;if(l||b){let c=a.setPrototypeOf||([].__proto__===Array.prototype?function(x,j){return f(j),x.__proto__=j,x}:m);P&&c(s,P)||(k=!1)}if(o.get||!k)for(let c in n)D[c]||a.defineProperty(s,c,{get:g.bind(n,c)});return a.seal(n),a.seal(s),s},r.revocable=function(n,o){return{proxy:new r(n,o),revoke:e}},r}});var S=E(I=>{"use strict";Object.defineProperty(I,"__esModule",{value:!0}),function(t,e){for(var r in e)Object.defineProperty(t,r,{enumerable:!0,get:e[r]})}(I,{create:function(){return at},unwrap:function(){return W}});var ct=(0,function(t){return t&&t.__esModule?t:{default:t}}(z()).default)(),Z=function(t,e,r){if(typeof t!="object"||t===null)return t;var i={get:function(f,a){if(a==="__rawObject__")return f;var u=null,d=r.concat(a),m=f[a];return e.push({path:d,value:m}),Z(m,e,d)}};try{return new Proxy(t,i)}catch(f){return new ct(t,i)}};function at(t,e){return Z(t,e,[])}function W(t){if(t!==null&&typeof t=="object"&&typeof t.__rawObject__!="object"){if(Array.isArray(t))return t.map(function(r){return W(r)});var e={};return Object.keys(t).forEach(function(r){e[r]=W(t[r])}),e}return typeof t!="object"||t===null||typeof t.__rawObject__!="object"?t:t.__rawObject__}});var Y=E(F=>{"use strict";Object.defineProperty(F,"__esModule",{value:!0}),Object.defineProperty(F,"behavior",{enumerable:!0,get:function(){return lt}});var G,ft=J(require("rfdc")),st=J(require("fast-deep-equal")),O=L(H()),T=L(S());function J(t){return t&&t.__esModule?t:{default:t}}function K(t){if(typeof WeakMap!="function")return null;var e=new WeakMap,r=new WeakMap;return(K=function(i){return i?r:e})(t)}function L(t,e){if(!e&&t&&t.__esModule)return t;if(t===null||typeof t!="object"&&typeof t!="function")return{default:t};var r=K(e);if(r&&r.has(t))return r.get(t);var i={},f=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var a in t)if(a!=="default"&&Object.prototype.hasOwnProperty.call(t,a)){var u=f?Object.getOwnPropertyDescriptor(t,a):null;u&&(u.get||u.set)?Object.defineProperty(i,a,u):i[a]=t[a]}return i.default=t,r&&r.set(t,i),i}var Q=(0,ft.default)({proto:!0});(function(t){t[t.CREATED=0]="CREATED",t[t.ATTACHED=1]="ATTACHED"})(G||(G={}));var pt=0;function X(t,e){return t===e||t!=t&&e!=e}var lt=Behavior({lifetimes:{attached:function(){this.setData({_computedWatchInit:1})},created:function(){this.setData({_computedWatchInit:0})}},definitionFilter:function(t){var e,r=t.computed,i=t.watch,f=[],a=pt++;f.push({fields:"_computedWatchInit",observer:function(){var u=this,d=this.data._computedWatchInit;if(d===0){var m={computedUpdaters:[],computedRelatedPathValues:{},watchCurVal:{},_triggerFromComputedAttached:{}};this._computedWatchInfo||(this._computedWatchInfo={}),this._computedWatchInfo[a]=m,i&&Object.keys(i).forEach(function(n){var o=O.parseMultiDataPaths(n).map(function(h){var y=h.path,v=h.options,P=O.getDataOnPath(u.data,y);return v.deepCmp?Q(P):P});m.watchCurVal[n]=o})}else if(d===1){var p=this._computedWatchInfo[a];r&&Object.keys(r).forEach(function(n){var o,h=r[n],y=[],v=h(T.create(u.data,y)),P=y.map(function(l){var b=l.path;return{path:b,value:O.getDataOnPath(u.data,b)}});u.setData(((o={})[n]=T.unwrap(v),o)),p._triggerFromComputedAttached[n]=!0,p.computedRelatedPathValues[n]=P;var s=function(){for(var l,b=p.computedRelatedPathValues[n],g=!1,w=0;w()=>(t||e((t={exports:{}}).exports,t),t.exports);var H=E(M=>{"use strict";Object.defineProperty(M,"__esModule",{value:!0}),function(e,t){for(var r in t)Object.defineProperty(e,r,{enumerable:!0,get:t[r]})}(M,{parseMultiDataPaths:function(){return ie},getDataOnPath:function(){return ue}});var N=/^\s/,C=function(e,t){throw Error('Parsing data path "'+e+'" failed at char "'+e[t]+'" (index '+t+")")},re=function(e,t){for(var r=t.index;t.index{U.exports=function(){let t=null,r;function i(n){return n?typeof n=="object"||typeof n=="function":!1}function s(n){if(n!==null&&!i(n))throw new TypeError("Object prototype may only be an Object or null: "+n)}let c=Object,u=Boolean(c.create)||!({__proto__:null}instanceof c),d=c.create||(u?function(o){return s(o),{__proto__:o}}:function(o){if(s(o),o===null)throw new SyntaxError("Native Object.create is required to create objects with null prototype");var h=function(){};return h.prototype=o,new h}),m=function(){return null},p=c.getPrototypeOf||([].__proto__===Array.prototype?function(o){let h=o.__proto__;return i(h)?h:null}:m);return r=function(n,o){if((this&&this instanceof r?this.constructor:void 0)===void 0)throw new TypeError("Constructor Proxy requires 'new'");if(!i(n)||!i(o))throw new TypeError("Cannot create proxy with a non-object as target or handler");let v=function(){};t=function(){n=null,v=function(a){throw new TypeError(`Cannot perform '${a}' on a proxy that has been revoked`)}},setTimeout(function(){t=null},0);let y=o;o={get:null,set:null,apply:null,construct:null};for(let a in y){if(!(a in o))throw new TypeError(`Proxy polyfill does not support trap '${a}'`);o[a]=y[a]}typeof y=="function"&&(o.apply=y.apply.bind(y));let P=p(n),f,l=!1,b=!1;typeof n=="function"?(f=function(){let _=this&&this.constructor===f,x=Array.prototype.slice.call(arguments);if(v(_?"construct":"apply"),_&&o.construct)return o.construct.call(this,n,x);if(!_&&o.apply)return o.apply(n,this,x);if(_){x.unshift(n);let j=n.bind.apply(n,x);return new j}return n.apply(this,x)},l=!0):n instanceof Array?(f=[],b=!0):f=u||P!==null?d(P):{};let g=o.get?function(a){return v("get"),o.get(this,a,f)}:function(a){return v("get"),this[a]},w=o.set?function(a,_){v("set");let x=o.set(this,a,_,f)}:function(a,_){v("set"),this[a]=_},A=c.getOwnPropertyNames(n),D={};A.forEach(function(a){if((l||b)&&a in f)return;let _=c.getOwnPropertyDescriptor(n,a),x={enumerable:Boolean(_.enumerable),get:g.bind(n,a),set:w.bind(n,a)};c.defineProperty(f,a,x),D[a]=!0});let k=!0;if(l||b){let a=c.setPrototypeOf||([].__proto__===Array.prototype?function(x,j){return s(j),x.__proto__=j,x}:m);P&&a(f,P)||(k=!1)}if(o.get||!k)for(let a in n)D[a]||c.defineProperty(f,a,{get:g.bind(n,a)});return c.seal(n),c.seal(f),f},r.revocable=function(n,o){return{proxy:new r(n,o),revoke:t}},r}});var S=E(I=>{"use strict";Object.defineProperty(I,"__esModule",{value:!0}),function(e,t){for(var r in t)Object.defineProperty(e,r,{enumerable:!0,get:t[r]})}(I,{create:function(){return ce},unwrap:function(){return W}});var ae=(0,function(e){return e&&e.__esModule?e:{default:e}}(z()).default)(),Z=function(e,t,r){if(typeof e!="object"||e===null)return e;var i={get:function(s,c){if(c==="__rawObject__")return s;var u=null,d=r.concat(c),m=s[c];return t.push({path:d,value:m}),Z(m,t,d)}};try{return new Proxy(e,i)}catch(s){return new ae(e,i)}};function ce(e,t){return Z(e,t,[])}function W(e){if(e!==null&&typeof e=="object"&&typeof e.__rawObject__!="object"){if(Array.isArray(e))return e.map(function(r){return W(r)});var t={};return Object.keys(e).forEach(function(r){t[r]=W(e[r])}),t}return typeof e!="object"||e===null||typeof e.__rawObject__!="object"?e:e.__rawObject__}});var Y=E(B=>{"use strict";Object.defineProperty(B,"__esModule",{value:!0}),Object.defineProperty(B,"behavior",{enumerable:!0,get:function(){return le}});var G,se=J(require("rfdc")),fe=J(require("fast-deep-equal")),O=L(H()),T=L(S());function J(e){return e&&e.__esModule?e:{default:e}}function K(e){if(typeof WeakMap!="function")return null;var t=new WeakMap,r=new WeakMap;return(K=function(i){return i?r:t})(e)}function L(e,t){if(!t&&e&&e.__esModule)return e;if(e===null||typeof e!="object"&&typeof e!="function")return{default:e};var r=K(t);if(r&&r.has(e))return r.get(e);var i={},s=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var c in e)if(c!=="default"&&Object.prototype.hasOwnProperty.call(e,c)){var u=s?Object.getOwnPropertyDescriptor(e,c):null;u&&(u.get||u.set)?Object.defineProperty(i,c,u):i[c]=e[c]}return i.default=e,r&&r.set(e,i),i}var Q=(0,se.default)({proto:!0});(function(e){e[e.CREATED=0]="CREATED",e[e.ATTACHED=1]="ATTACHED"})(G||(G={}));var pe=0;function X(e,t){return e===t||e!=e&&t!=t}var le=Behavior({lifetimes:{attached:function(){this.setData({_computedWatchInit:1})},created:function(){this.setData({_computedWatchInit:0})}},definitionFilter:function(e){var t,r=e.computed,i=e.watch,s=[],c=pe++;s.push({fields:"_computedWatchInit",observer:function(){var u=this,d=this.data._computedWatchInit;if(d===0){var m={computedUpdaters:[],computedRelatedPathValues:{},watchCurVal:{},_triggerFromComputedAttached:{}};this._computedWatchInfo||(this._computedWatchInfo={}),this._computedWatchInfo[c]=m,i&&Object.keys(i).forEach(function(n){var o=O.parseMultiDataPaths(n).map(function(h){var v=h.path,y=h.options,P=O.getDataOnPath(u.data,v);return y.deepCmp?Q(P):P});m.watchCurVal[n]=o})}else if(d===1){var p=this._computedWatchInfo[c];r&&Object.keys(r).forEach(function(n){var o,h=r[n],v=[],y=h(T.create(u.data,v)),P=v.map(function(l){var b=l.path;return{path:b,value:O.getDataOnPath(u.data,b)}});u.setData(((o={})[n]=T.unwrap(y),o)),p._triggerFromComputedAttached[n]=!0,p.computedRelatedPathValues[n]=P;var f=function(){for(var l,b=p.computedRelatedPathValues[n],g=!1,w=0;w & ThisType< ComputedInstance< TData, @@ -65,6 +68,9 @@ export function ComponentWithComputed< options.behaviors = [] } options.behaviors.unshift(behavior) + if (Array.isArray(options.behaviorsBeforeComputed)) { + options.behaviors = options.behaviorsBeforeComputed.concat(options.behaviors) + } return Component(options) } @@ -92,6 +98,9 @@ export function BehaviorWithComputed< options.behaviors = [] } options.behaviors.unshift(behavior) + if (Array.isArray(options.behaviorsBeforeComputed)) { + options.behaviors = options.behaviorsBeforeComputed.concat(options.behaviors) + } return Behavior(options) } diff --git a/types/index.d.ts b/types/index.d.ts index b104eef..5f863b3 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -15,7 +15,9 @@ declare type ComputedOptions>; +}) & Partial<{ + behaviorsBeforeComputed?: string[]; +}> & ThisType>; export declare function ComponentWithComputed void>, TComputed extends Record) => any>, TCustomInstanceProperty extends WechatMiniprogram.IAnyObject = {}>(options: ComputedOptions): string; export declare function BehaviorWithComputed void>, TComputed extends Record) => any>, TCustomInstanceProperty extends WechatMiniprogram.IAnyObject = {}>(options: ComputedOptions): string; export declare enum DataTracerMode {