@geodaoyu/accessor 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +1 -0
- package/dist/index.es.js +900 -0
- package/dist/index.umd.js +1 -0
- package/package.json +16 -6
- package/src/Accessor.js +10 -0
- package/src/index.js +2 -0
- package/src/reactiveUtils.js +5 -0
- package/src/accessor.ts +0 -119
- package/src/index.ts +0 -3
- package/test/construct.test.js +0 -31
- package/test/get.test.js +0 -34
- package/test/set.test.js +0 -56
- package/test/watch.test.js +0 -168
- package/tsconfig.json +0 -19
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(x,W){typeof exports=="object"&&typeof module<"u"?W(exports):typeof define=="function"&&define.amd?define(["exports"],W):(x=typeof globalThis<"u"?globalThis:x||self,W(x.Accessor={}))})(this,(function(x){"use strict";function W(e){const t=Object.create(null);for(const n of e.split(","))t[n]=1;return n=>n in t}const He=process.env.NODE_ENV!=="production"?Object.freeze({}):{};process.env.NODE_ENV!=="production"&&Object.freeze([]);const $e=()=>{},se=Object.assign,Ce=Object.prototype.hasOwnProperty,ie=(e,t)=>Ce.call(e,t),T=Array.isArray,A=e=>J(e)==="[object Map]",ze=e=>J(e)==="[object Set]",Se=e=>typeof e=="function",Ye=e=>typeof e=="string",H=e=>typeof e=="symbol",P=e=>e!==null&&typeof e=="object",Fe=Object.prototype.toString,J=e=>Fe.call(e),Oe=e=>J(e).slice(8,-1),Be=e=>J(e)==="[object Object]",oe=e=>Ye(e)&&e!=="NaN"&&e[0]!=="-"&&""+parseInt(e,10)===e,Ue=(e=>{const t=Object.create(null);return(n=>t[n]||(t[n]=e(n)))})(e=>e.charAt(0).toUpperCase()+e.slice(1)),m=(e,t)=>!Object.is(e,t);function E(e,...t){console.warn(`[Vue warn] ${e}`,...t)}let u;const ce=new WeakSet;class Ge{constructor(t){this.fn=t,this.deps=void 0,this.depsTail=void 0,this.flags=5,this.next=void 0,this.cleanup=void 0,this.scheduler=void 0}pause(){this.flags|=64}resume(){this.flags&64&&(this.flags&=-65,ce.has(this)&&(ce.delete(this),this.trigger()))}notify(){this.flags&2&&!(this.flags&32)||this.flags&8||Je(this)}run(){if(!(this.flags&1))return this.fn();this.flags|=2,Re(this),De(this);const t=u,n=g;u=this,g=!0;try{return this.fn()}finally{process.env.NODE_ENV!=="production"&&u!==this&&E("Active effect was not restored correctly - this is likely a Vue internal bug."),Ne(this),u=t,g=n,this.flags&=-3}}stop(){if(this.flags&1){for(let t=this.deps;t;t=t.nextDep)ue(t);this.deps=this.depsTail=void 0,Re(this),this.onStop&&this.onStop(),this.flags&=-2}}trigger(){this.flags&64?ce.add(this):this.scheduler?this.scheduler():this.runIfDirty()}runIfDirty(){le(this)&&this.run()}get dirty(){return le(this)}}let ye=0,$,C;function Je(e,t=!1){if(e.flags|=8,t){e.next=C,C=e;return}e.next=$,$=e}function ae(){ye++}function fe(){if(--ye>0)return;if(C){let t=C;for(C=void 0;t;){const n=t.next;t.next=void 0,t.flags&=-9,t=n}}let e;for(;$;){let t=$;for($=void 0;t;){const n=t.next;if(t.next=void 0,t.flags&=-9,t.flags&1)try{t.trigger()}catch(s){e||(e=s)}t=n}}if(e)throw e}function De(e){for(let t=e.deps;t;t=t.nextDep)t.version=-1,t.prevActiveLink=t.dep.activeLink,t.dep.activeLink=t}function Ne(e){let t,n=e.depsTail,s=n;for(;s;){const r=s.prevDep;s.version===-1?(s===n&&(n=r),ue(s),qe(s)):t=s,s.dep.activeLink=s.prevActiveLink,s.prevActiveLink=void 0,s=r}e.deps=t,e.depsTail=n}function le(e){for(let t=e.deps;t;t=t.nextDep)if(t.dep.version!==t.version||t.dep.computed&&(Qe(t.dep.computed)||t.dep.version!==t.version))return!0;return!!e._dirty}function Qe(e){if(e.flags&4&&!(e.flags&16)||(e.flags&=-17,e.globalVersion===Q)||(e.globalVersion=Q,!e.isSSR&&e.flags&128&&(!e.deps&&!e._dirty||!le(e))))return;e.flags|=2;const t=e.dep,n=u,s=g;u=e,g=!0;try{De(e);const r=e.fn(e._value);(t.version===0||m(r,e._value))&&(e.flags|=128,e._value=r,t.version++)}catch(r){throw t.version++,r}finally{u=n,g=s,Ne(e),e.flags&=-3}}function ue(e,t=!1){const{dep:n,prevSub:s,nextSub:r}=e;if(s&&(s.nextSub=r,e.prevSub=void 0),r&&(r.prevSub=s,e.nextSub=void 0),process.env.NODE_ENV!=="production"&&n.subsHead===e&&(n.subsHead=r),n.subs===e&&(n.subs=s,!s&&n.computed)){n.computed.flags&=-5;for(let i=n.computed.deps;i;i=i.nextDep)ue(i,!0)}!t&&!--n.sc&&n.map&&n.map.delete(n.key)}function qe(e){const{prevDep:t,nextDep:n}=e;t&&(t.nextDep=n,e.prevDep=void 0),n&&(n.prevDep=t,e.nextDep=void 0)}let g=!0;const xe=[];function Te(){xe.push(g),g=!1}function me(){const e=xe.pop();g=e===void 0?!0:e}function Re(e){const{cleanup:t}=e;if(e.cleanup=void 0,t){const n=u;u=void 0;try{t()}finally{u=n}}}let Q=0;class Xe{constructor(t,n){this.sub=t,this.dep=n,this.version=n.version,this.nextDep=this.prevDep=this.nextSub=this.prevSub=this.prevActiveLink=void 0}}class Ze{constructor(t){this.computed=t,this.version=0,this.activeLink=void 0,this.subs=void 0,this.map=void 0,this.key=void 0,this.sc=0,this.__v_skip=!0,process.env.NODE_ENV!=="production"&&(this.subsHead=void 0)}track(t){if(!u||!g||u===this.computed)return;let n=this.activeLink;if(n===void 0||n.sub!==u)n=this.activeLink=new Xe(u,this),u.deps?(n.prevDep=u.depsTail,u.depsTail.nextDep=n,u.depsTail=n):u.deps=u.depsTail=n,Ve(n);else if(n.version===-1&&(n.version=this.version,n.nextDep)){const s=n.nextDep;s.prevDep=n.prevDep,n.prevDep&&(n.prevDep.nextDep=s),n.prevDep=u.depsTail,n.nextDep=void 0,u.depsTail.nextDep=n,u.depsTail=n,u.deps===n&&(u.deps=s)}return process.env.NODE_ENV!=="production"&&u.onTrack&&u.onTrack(se({effect:u},t)),n}trigger(t){this.version++,Q++,this.notify(t)}notify(t){ae();try{if(process.env.NODE_ENV!=="production")for(let n=this.subsHead;n;n=n.nextSub)n.sub.onTrigger&&!(n.sub.flags&8)&&n.sub.onTrigger(se({effect:n.sub},t));for(let n=this.subs;n;n=n.prevSub)n.sub.notify()&&n.sub.dep.notify()}finally{fe()}}}function Ve(e){if(e.dep.sc++,e.sub.flags&4){const t=e.dep.computed;if(t&&!e.dep.subs){t.flags|=20;for(let s=t.deps;s;s=s.nextDep)Ve(s)}const n=e.dep.subs;n!==e&&(e.prevSub=n,n&&(n.nextSub=e)),process.env.NODE_ENV!=="production"&&e.dep.subsHead===void 0&&(e.dep.subsHead=e),e.dep.subs=e}}const pe=new WeakMap,R=Symbol(process.env.NODE_ENV!=="production"?"Object iterate":""),de=Symbol(process.env.NODE_ENV!=="production"?"Map keys iterate":""),z=Symbol(process.env.NODE_ENV!=="production"?"Array iterate":"");function _(e,t,n){if(g&&u){let s=pe.get(e);s||pe.set(e,s=new Map);let r=s.get(n);r||(s.set(n,r=new Ze),r.map=s,r.key=n),process.env.NODE_ENV!=="production"?r.track({target:e,type:t,key:n}):r.track()}}function O(e,t,n,s,r,i){const o=pe.get(e);if(!o){Q++;return}const c=a=>{a&&(process.env.NODE_ENV!=="production"?a.trigger({target:e,type:t,key:n,newValue:s,oldValue:r,oldTarget:i}):a.trigger())};if(ae(),t==="clear")o.forEach(c);else{const a=T(e),d=a&&oe(n);if(a&&n==="length"){const v=Number(s);o.forEach((f,h)=>{(h==="length"||h===z||!H(h)&&h>=v)&&c(f)})}else switch((n!==void 0||o.has(void 0))&&c(o.get(n)),d&&c(o.get(z)),t){case"add":a?d&&c(o.get("length")):(c(o.get(R)),A(e)&&c(o.get(de)));break;case"delete":a||(c(o.get(R)),A(e)&&c(o.get(de)));break;case"set":A(e)&&c(o.get(R));break}}fe()}function K(e){const t=p(e);return t===e?t:(_(t,"iterate",z),b(e)?t:t.map(j))}function he(e){return _(e=p(e),"iterate",z),e}function y(e,t){return V(e)?F(e)?B(j(t)):B(t):j(t)}const ke={__proto__:null,[Symbol.iterator](){return ve(this,Symbol.iterator,e=>y(this,e))},concat(...e){return K(this).concat(...e.map(t=>T(t)?K(t):t))},entries(){return ve(this,"entries",e=>(e[1]=y(this,e[1]),e))},every(e,t){return S(this,"every",e,t,void 0,arguments)},filter(e,t){return S(this,"filter",e,t,n=>n.map(s=>y(this,s)),arguments)},find(e,t){return S(this,"find",e,t,n=>y(this,n),arguments)},findIndex(e,t){return S(this,"findIndex",e,t,void 0,arguments)},findLast(e,t){return S(this,"findLast",e,t,n=>y(this,n),arguments)},findLastIndex(e,t){return S(this,"findLastIndex",e,t,void 0,arguments)},forEach(e,t){return S(this,"forEach",e,t,void 0,arguments)},includes(...e){return _e(this,"includes",e)},indexOf(...e){return _e(this,"indexOf",e)},join(e){return K(this).join(e)},lastIndexOf(...e){return _e(this,"lastIndexOf",e)},map(e,t){return S(this,"map",e,t,void 0,arguments)},pop(){return Y(this,"pop")},push(...e){return Y(this,"push",e)},reduce(e,...t){return je(this,"reduce",e,t)},reduceRight(e,...t){return je(this,"reduceRight",e,t)},shift(){return Y(this,"shift")},some(e,t){return S(this,"some",e,t,void 0,arguments)},splice(...e){return Y(this,"splice",e)},toReversed(){return K(this).toReversed()},toSorted(e){return K(this).toSorted(e)},toSpliced(...e){return K(this).toSpliced(...e)},unshift(...e){return Y(this,"unshift",e)},values(){return ve(this,"values",e=>y(this,e))}};function ve(e,t,n){const s=he(e),r=s[t]();return s!==e&&!b(e)&&(r._next=r.next,r.next=()=>{const i=r._next();return i.done||(i.value=n(i.value)),i}),r}const et=Array.prototype;function S(e,t,n,s,r,i){const o=he(e),c=o!==e&&!b(e),a=o[t];if(a!==et[t]){const f=a.apply(e,i);return c?j(f):f}let d=n;o!==e&&(c?d=function(f,h){return n.call(this,y(e,f),h,e)}:n.length>2&&(d=function(f,h){return n.call(this,f,h,e)}));const v=a.call(o,d,s);return c&&r?r(v):v}function je(e,t,n,s){const r=he(e);let i=n;return r!==e&&(b(e)?n.length>3&&(i=function(o,c,a){return n.call(this,o,c,a,e)}):i=function(o,c,a){return n.call(this,o,y(e,c),a,e)}),r[t](i,...s)}function _e(e,t,n){const s=p(e);_(s,"iterate",z);const r=s[t](...n);return(r===-1||r===!1)&&vt(n[0])?(n[0]=p(n[0]),s[t](...n)):r}function Y(e,t,n=[]){Te(),ae();const s=p(e)[t].apply(e,n);return fe(),me(),s}const tt=W("__proto__,__v_isRef,__isVue"),Ie=new Set(Object.getOwnPropertyNames(Symbol).filter(e=>e!=="arguments"&&e!=="caller").map(e=>Symbol[e]).filter(H));function nt(e){H(e)||(e=String(e));const t=p(this);return _(t,"has",e),t.hasOwnProperty(e)}class Me{constructor(t=!1,n=!1){this._isReadonly=t,this._isShallow=n}get(t,n,s){if(n==="__v_skip")return t.__v_skip;const r=this._isReadonly,i=this._isShallow;if(n==="__v_isReactive")return!r;if(n==="__v_isReadonly")return r;if(n==="__v_isShallow")return i;if(n==="__v_raw")return s===(r?i?pt:Le:i?ut:Ke).get(t)||Object.getPrototypeOf(t)===Object.getPrototypeOf(s)?t:void 0;const o=T(t);if(!r){let a;if(o&&(a=ke[n]))return a;if(n==="hasOwnProperty")return nt}const c=Reflect.get(t,n,D(t)?t:s);if((H(n)?Ie.has(n):tt(n))||(r||_(t,"get",n),i))return c;if(D(c)){const a=o&&oe(n)?c:c.value;return r&&P(a)?we(a):a}return P(c)?r?we(c):be(c):c}}class rt extends Me{constructor(t=!1){super(!1,t)}set(t,n,s,r){let i=t[n];const o=T(t)&&oe(n);if(!this._isShallow){const d=V(i);if(!b(s)&&!V(s)&&(i=p(i),s=p(s)),!o&&D(i)&&!D(s))return d?(process.env.NODE_ENV!=="production"&&E(`Set operation on key "${String(n)}" failed: target is readonly.`,t[n]),!0):(i.value=s,!0)}const c=o?Number(n)<t.length:ie(t,n),a=Reflect.set(t,n,s,D(t)?t:r);return t===p(r)&&(c?m(s,i)&&O(t,"set",n,s,i):O(t,"add",n,s)),a}deleteProperty(t,n){const s=ie(t,n),r=t[n],i=Reflect.deleteProperty(t,n);return i&&s&&O(t,"delete",n,void 0,r),i}has(t,n){const s=Reflect.has(t,n);return(!H(n)||!Ie.has(n))&&_(t,"has",n),s}ownKeys(t){return _(t,"iterate",T(t)?"length":R),Reflect.ownKeys(t)}}class st extends Me{constructor(t=!1){super(!0,t)}set(t,n){return process.env.NODE_ENV!=="production"&&E(`Set operation on key "${String(n)}" failed: target is readonly.`,t),!0}deleteProperty(t,n){return process.env.NODE_ENV!=="production"&&E(`Delete operation on key "${String(n)}" failed: target is readonly.`,t),!0}}const it=new rt,ot=new st,ge=e=>e,q=e=>Reflect.getPrototypeOf(e);function ct(e,t,n){return function(...s){const r=this.__v_raw,i=p(r),o=A(i),c=e==="entries"||e===Symbol.iterator&&o,a=e==="keys"&&o,d=r[e](...s),v=n?ge:t?B:j;return!t&&_(i,"iterate",a?de:R),{next(){const{value:f,done:h}=d.next();return h?{value:f,done:h}:{value:c?[v(f[0]),v(f[1])]:v(f),done:h}},[Symbol.iterator](){return this}}}}function X(e){return function(...t){if(process.env.NODE_ENV!=="production"){const n=t[0]?`on key "${t[0]}" `:"";E(`${Ue(e)} operation ${n}failed: target is readonly.`,p(this))}return e==="delete"?!1:e==="clear"?void 0:this}}function at(e,t){const n={get(r){const i=this.__v_raw,o=p(i),c=p(r);e||(m(r,c)&&_(o,"get",r),_(o,"get",c));const{has:a}=q(o),d=t?ge:e?B:j;if(a.call(o,r))return d(i.get(r));if(a.call(o,c))return d(i.get(c));i!==o&&i.get(r)},get size(){const r=this.__v_raw;return!e&&_(p(r),"iterate",R),r.size},has(r){const i=this.__v_raw,o=p(i),c=p(r);return e||(m(r,c)&&_(o,"has",r),_(o,"has",c)),r===c?i.has(r):i.has(r)||i.has(c)},forEach(r,i){const o=this,c=o.__v_raw,a=p(c),d=t?ge:e?B:j;return!e&&_(a,"iterate",R),c.forEach((v,f)=>r.call(i,d(v),d(f),o))}};return se(n,e?{add:X("add"),set:X("set"),delete:X("delete"),clear:X("clear")}:{add(r){!t&&!b(r)&&!V(r)&&(r=p(r));const i=p(this);return q(i).has.call(i,r)||(i.add(r),O(i,"add",r,r)),this},set(r,i){!t&&!b(i)&&!V(i)&&(i=p(i));const o=p(this),{has:c,get:a}=q(o);let d=c.call(o,r);d?process.env.NODE_ENV!=="production"&&Pe(o,c,r):(r=p(r),d=c.call(o,r));const v=a.call(o,r);return o.set(r,i),d?m(i,v)&&O(o,"set",r,i,v):O(o,"add",r,i),this},delete(r){const i=p(this),{has:o,get:c}=q(i);let a=o.call(i,r);a?process.env.NODE_ENV!=="production"&&Pe(i,o,r):(r=p(r),a=o.call(i,r));const d=c?c.call(i,r):void 0,v=i.delete(r);return a&&O(i,"delete",r,void 0,d),v},clear(){const r=p(this),i=r.size!==0,o=process.env.NODE_ENV!=="production"?A(r)?new Map(r):new Set(r):void 0,c=r.clear();return i&&O(r,"clear",void 0,void 0,o),c}}),["keys","values","entries",Symbol.iterator].forEach(r=>{n[r]=ct(r,e,t)}),n}function Ae(e,t){const n=at(e,t);return(s,r,i)=>r==="__v_isReactive"?!e:r==="__v_isReadonly"?e:r==="__v_raw"?s:Reflect.get(ie(n,r)&&r in s?n:s,r,i)}const ft={get:Ae(!1,!1)},lt={get:Ae(!0,!1)};function Pe(e,t,n){const s=p(n);if(s!==n&&t.call(e,s)){const r=Oe(e);E(`Reactive ${r} contains both the raw and reactive versions of the same object${r==="Map"?" as keys":""}, which can lead to inconsistencies. Avoid differentiating between the raw and reactive versions of an object and only use the reactive version if possible.`)}}const Ke=new WeakMap,ut=new WeakMap,Le=new WeakMap,pt=new WeakMap;function dt(e){switch(e){case"Object":case"Array":return 1;case"Map":case"Set":case"WeakMap":case"WeakSet":return 2;default:return 0}}function ht(e){return e.__v_skip||!Object.isExtensible(e)?0:dt(Oe(e))}function be(e){return V(e)?e:We(e,!1,it,ft,Ke)}function we(e){return We(e,!0,ot,lt,Le)}function We(e,t,n,s,r){if(!P(e))return process.env.NODE_ENV!=="production"&&E(`value cannot be made ${t?"readonly":"reactive"}: ${String(e)}`),e;if(e.__v_raw&&!(t&&e.__v_isReactive))return e;const i=ht(e);if(i===0)return e;const o=r.get(e);if(o)return o;const c=new Proxy(e,i===2?s:n);return r.set(e,c),c}function F(e){return V(e)?F(e.__v_raw):!!(e&&e.__v_isReactive)}function V(e){return!!(e&&e.__v_isReadonly)}function b(e){return!!(e&&e.__v_isShallow)}function vt(e){return e?!!e.__v_raw:!1}function p(e){const t=e&&e.__v_raw;return t?p(t):e}const j=e=>P(e)?be(e):e,B=e=>P(e)?we(e):e;function D(e){return e?e.__v_isRef===!0:!1}const Z={},k=new WeakMap;let I;function _t(e,t=!1,n=I){if(n){let s=k.get(n);s||k.set(n,s=[]),s.push(e)}else process.env.NODE_ENV!=="production"&&!t&&E("onWatcherCleanup() was called when there was no active watcher to associate with.")}function gt(e,t,n=He){const{immediate:s,deep:r,once:i,scheduler:o,augmentJob:c,call:a}=n,d=l=>{(n.onWarn||E)("Invalid watch source: ",l,"A watch source can only be a getter/effect function, a ref, a reactive object, or an array of these types.")},v=l=>r?l:b(l)||r===!1||r===0?N(l,1):N(l);let f,h,U,ee,te=!1,ne=!1;if(D(e)?(h=()=>e.value,te=b(e)):F(e)?(h=()=>v(e),te=!0):T(e)?(ne=!0,te=e.some(l=>F(l)||b(l)),h=()=>e.map(l=>{if(D(l))return l.value;if(F(l))return v(l);if(Se(l))return a?a(l,2):l();process.env.NODE_ENV!=="production"&&d(l)})):Se(e)?t?h=a?()=>a(e,2):e:h=()=>{if(U){Te();try{U()}finally{me()}}const l=I;I=f;try{return a?a(e,3,[ee]):e(ee)}finally{I=l}}:(h=$e,process.env.NODE_ENV!=="production"&&d(e)),t&&r){const l=h,w=r===!0?1/0:r;h=()=>N(l(),w)}const L=()=>{f.stop()};if(i&&t){const l=t;t=(...w)=>{l(...w),L()}}let M=ne?new Array(e.length).fill(Z):Z;const G=l=>{if(!(!(f.flags&1)||!f.dirty&&!l))if(t){const w=f.run();if(r||te||(ne?w.some((Ee,re)=>m(Ee,M[re])):m(w,M))){U&&U();const Ee=I;I=f;try{const re=[w,M===Z?void 0:ne&&M[0]===Z?[]:M,ee];M=w,a?a(t,3,re):t(...re)}finally{I=Ee}}}else f.run()};return c&&c(G),f=new Ge(h),f.scheduler=o?()=>o(G,!1):G,ee=l=>_t(l,!1,f),U=f.onStop=()=>{const l=k.get(f);if(l){if(a)a(l,4);else for(const w of l)w();k.delete(f)}},process.env.NODE_ENV!=="production"&&(f.onTrack=n.onTrack,f.onTrigger=n.onTrigger),t?s?G(!0):M=f.run():o?o(G.bind(null,!0),!0):f.run(),L.pause=f.pause.bind(f),L.resume=f.resume.bind(f),L.stop=L,L}function N(e,t=1/0,n){if(t<=0||!P(e)||e.__v_skip||(n=n||new Map,(n.get(e)||0)>=t))return e;if(n.set(e,t),t--,D(e))N(e.value,t,n);else if(T(e))for(let s=0;s<e.length;s++)N(e[s],t,n);else if(ze(e)||A(e))e.forEach(s=>{N(s,t,n)});else if(Be(e)){for(const s in e)N(e[s],t,n);for(const s of Object.getOwnPropertySymbols(e))Object.prototype.propertyIsEnumerable.call(e,s)&&N(e[s],t,n)}return e}class bt{constructor(t){return t&&Object.assign(this,t),be(this)}}const wt={watch:gt};x.Accessor=bt,x.reactiveUtils=wt,Object.defineProperty(x,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geodaoyu/accessor",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "mini @arcgis/core/core/Accessor & @arcgis/core/core/reactiveUtils",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"
|
|
9
|
-
"
|
|
8
|
+
"start": "vite",
|
|
9
|
+
"build": "vite build",
|
|
10
|
+
"test": "vitest --run"
|
|
10
11
|
},
|
|
11
12
|
"keywords": [
|
|
12
13
|
"Accessor",
|
|
@@ -17,12 +18,21 @@
|
|
|
17
18
|
"author": "GeoDaoyu",
|
|
18
19
|
"license": "MIT",
|
|
19
20
|
"type": "module",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist/**/*",
|
|
23
|
+
"src/**/*",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
20
27
|
"repository": {
|
|
21
28
|
"type": "git",
|
|
22
29
|
"url": "https://github.com/GeoDaoyu/Accessor.git"
|
|
23
30
|
},
|
|
24
31
|
"devDependencies": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
32
|
+
"vite": "^7.3.0",
|
|
33
|
+
"vitest": "^1.0.0"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@vue/reactivity": "^3.5.26"
|
|
27
37
|
}
|
|
28
38
|
}
|
package/src/Accessor.js
ADDED
package/src/index.js
ADDED
package/src/accessor.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
type WatchCallback = (
|
|
2
|
-
newValue: any,
|
|
3
|
-
oldValue: any,
|
|
4
|
-
propertyName: string,
|
|
5
|
-
target: Accessor
|
|
6
|
-
) => void;
|
|
7
|
-
|
|
8
|
-
interface WatchHandle extends Object {
|
|
9
|
-
/**
|
|
10
|
-
* Removes the watch handle.
|
|
11
|
-
*/
|
|
12
|
-
remove(): void;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface Handle {
|
|
16
|
-
path: string;
|
|
17
|
-
callback: Function;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
class Accessor {
|
|
21
|
-
declaredClass: string;
|
|
22
|
-
private _handles: Set<Handle>;
|
|
23
|
-
|
|
24
|
-
constructor(props: object = {}) {
|
|
25
|
-
// 为当前实例创建一个代理
|
|
26
|
-
const proxy = new Proxy(this, {
|
|
27
|
-
set: (target, key, value, receiver) => {
|
|
28
|
-
if (key !== '_handles' && target._handles) {
|
|
29
|
-
const oldValue = target[key];
|
|
30
|
-
target._handles.forEach((handle) => {
|
|
31
|
-
if (handle.path === key) {
|
|
32
|
-
handle.callback(value, oldValue, key, target);
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
return Reflect.set(target, key, value, receiver);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
// 初始化属性
|
|
41
|
-
for (let prop in props) {
|
|
42
|
-
proxy[prop] = props[prop];
|
|
43
|
-
}
|
|
44
|
-
proxy.declaredClass = "Accessor";
|
|
45
|
-
proxy._handles = new Set();
|
|
46
|
-
|
|
47
|
-
return proxy;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
get(path: string): any {
|
|
51
|
-
const dotIndex = path.indexOf(".");
|
|
52
|
-
if (dotIndex !== -1) {
|
|
53
|
-
const key = path.slice(0, dotIndex);
|
|
54
|
-
const value = path.slice(dotIndex + 1);
|
|
55
|
-
return this[key] && this[key].get(value);
|
|
56
|
-
}
|
|
57
|
-
return this[path];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
set(path: string | object, value: any): this {
|
|
61
|
-
if (typeof path === "string") {
|
|
62
|
-
const dotIndex = path.indexOf(".");
|
|
63
|
-
if (dotIndex !== -1) {
|
|
64
|
-
const key = path.slice(0, dotIndex);
|
|
65
|
-
const childPath = path.slice(dotIndex + 1);
|
|
66
|
-
if (this[key]) {
|
|
67
|
-
this[key].set(childPath, value);
|
|
68
|
-
}
|
|
69
|
-
} else {
|
|
70
|
-
this[path] = value;
|
|
71
|
-
}
|
|
72
|
-
} else {
|
|
73
|
-
for (const key in path) {
|
|
74
|
-
this.set(key, path[key]);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return this;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
watch(path: string | string[], callback: WatchCallback): WatchHandle {
|
|
82
|
-
const handles = [];
|
|
83
|
-
const pathArray = [];
|
|
84
|
-
if (typeof path === "object") {
|
|
85
|
-
pathArray.push(...path);
|
|
86
|
-
}
|
|
87
|
-
if (typeof path === "string") {
|
|
88
|
-
if (path.includes(",")) {
|
|
89
|
-
pathArray.push(...path.replace(" ", "").split(","));
|
|
90
|
-
} else {
|
|
91
|
-
pathArray.push(path);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
pathArray.forEach((item) => {
|
|
95
|
-
const dotIndex = item.indexOf(".");
|
|
96
|
-
const handle =
|
|
97
|
-
dotIndex !== -1
|
|
98
|
-
? this[item.slice(0, dotIndex)].watch(
|
|
99
|
-
item.slice(dotIndex + 1),
|
|
100
|
-
callback
|
|
101
|
-
)
|
|
102
|
-
: { path: item, callback };
|
|
103
|
-
|
|
104
|
-
handles.push(handle);
|
|
105
|
-
this._handles.add(handle);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
const watchHandle = {
|
|
109
|
-
remove: () => {
|
|
110
|
-
handles.forEach((handle) => {
|
|
111
|
-
this._handles.delete(handle);
|
|
112
|
-
});
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
return watchHandle;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export default Accessor;
|
package/src/index.ts
DELETED
package/test/construct.test.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import Accessor from '../dist/index.js';
|
|
2
|
-
import assert from 'assert';
|
|
3
|
-
|
|
4
|
-
describe('#constructor()', function() {
|
|
5
|
-
it('typeof accessor should be "object"', function() {
|
|
6
|
-
const accessor = new Accessor();
|
|
7
|
-
assert.strictEqual(typeof accessor, 'object');
|
|
8
|
-
});
|
|
9
|
-
/**
|
|
10
|
-
* 可以通过 对象的形式给类 赋初值
|
|
11
|
-
*/
|
|
12
|
-
it('accessor\'s props can be an object', function() {
|
|
13
|
-
const view = new Accessor({
|
|
14
|
-
zoom: 4,
|
|
15
|
-
});
|
|
16
|
-
assert.strictEqual(view.zoom, 4);
|
|
17
|
-
});
|
|
18
|
-
/**
|
|
19
|
-
* 子类可以继承Accessor
|
|
20
|
-
*/
|
|
21
|
-
it('subclass can extend from Accessor', function() {
|
|
22
|
-
class View extends Accessor {
|
|
23
|
-
constructor() {
|
|
24
|
-
super();
|
|
25
|
-
this.declaredClass = "View";
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
const view = new View();
|
|
29
|
-
assert.strictEqual(view instanceof Accessor, true);
|
|
30
|
-
});
|
|
31
|
-
});
|
package/test/get.test.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import Accessor from '../dist/index.js';
|
|
2
|
-
import assert from 'assert';
|
|
3
|
-
|
|
4
|
-
describe('#get()', function() {
|
|
5
|
-
/**
|
|
6
|
-
* 属性不存在,返回undefined
|
|
7
|
-
*/
|
|
8
|
-
it('should return undefined when the property in the path does not exist', function() {
|
|
9
|
-
const accessor = new Accessor();
|
|
10
|
-
assert.strictEqual(accessor.get('map'), undefined);
|
|
11
|
-
});
|
|
12
|
-
/**
|
|
13
|
-
* 设值取值
|
|
14
|
-
*/
|
|
15
|
-
it('should return 4 when the property is set to 4', function() {
|
|
16
|
-
const view = new Accessor();
|
|
17
|
-
view.set('zoom', 4);
|
|
18
|
-
assert.strictEqual(view.get('zoom'), 4);
|
|
19
|
-
});
|
|
20
|
-
it('deep path:should return undefined when the property in the path does not exist', function() {
|
|
21
|
-
const view = new Accessor();
|
|
22
|
-
assert.strictEqual(view.get('map.basemap'), undefined);
|
|
23
|
-
});
|
|
24
|
-
/**
|
|
25
|
-
* 获取属性的属性
|
|
26
|
-
*/
|
|
27
|
-
it('should return the property in the deep path', function() {
|
|
28
|
-
const map = new Accessor();
|
|
29
|
-
const basemap = new Accessor();
|
|
30
|
-
map.set('basemap', basemap);
|
|
31
|
-
basemap.set('title', 'World Topographic Map');
|
|
32
|
-
assert.strictEqual(map.get('basemap.title'), 'World Topographic Map');
|
|
33
|
-
});
|
|
34
|
-
});
|
package/test/set.test.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import Accessor from "../dist/index.js";
|
|
2
|
-
import assert from "assert";
|
|
3
|
-
|
|
4
|
-
describe("#set()", function () {
|
|
5
|
-
it("should return 4 when the property is set to 4", function () {
|
|
6
|
-
const view = new Accessor();
|
|
7
|
-
view.set("zoom", 4);
|
|
8
|
-
assert.strictEqual(view.get("zoom"), 4);
|
|
9
|
-
});
|
|
10
|
-
it("should return 4 when the property is 4", function () {
|
|
11
|
-
const view = new Accessor();
|
|
12
|
-
view.zoom = 4;
|
|
13
|
-
assert.strictEqual(view.zoom, 4);
|
|
14
|
-
});
|
|
15
|
-
/**
|
|
16
|
-
* 设置属性的属性
|
|
17
|
-
*/
|
|
18
|
-
it("deep path set property", function () {
|
|
19
|
-
const map = new Accessor();
|
|
20
|
-
const basemap = new Accessor();
|
|
21
|
-
map.set("basemap", basemap);
|
|
22
|
-
map.set("basemap.title", "World Topographic Map");
|
|
23
|
-
assert.strictEqual(map.get("basemap.title"), "World Topographic Map");
|
|
24
|
-
});
|
|
25
|
-
/**
|
|
26
|
-
* 设置属性的属性,当属性不存在的时候,不设置
|
|
27
|
-
*/
|
|
28
|
-
it("deep path set property which does not exist", function () {
|
|
29
|
-
const map = new Accessor();
|
|
30
|
-
map.set("basemap.title", "World Topographic Map");
|
|
31
|
-
assert.strictEqual(map.get("basemap.title"), undefined);
|
|
32
|
-
});
|
|
33
|
-
/**
|
|
34
|
-
* 可以通过对象的形式批量设置属性
|
|
35
|
-
*/
|
|
36
|
-
it("An object with key-value pairs can be passed into", function () {
|
|
37
|
-
const view = new Accessor();
|
|
38
|
-
view.set({
|
|
39
|
-
center: [-4.4861, 48.3904],
|
|
40
|
-
scale: 5000,
|
|
41
|
-
});
|
|
42
|
-
assert.strictEqual(view.get("scale"), 5000);
|
|
43
|
-
});
|
|
44
|
-
/**
|
|
45
|
-
* 柯里化
|
|
46
|
-
*/
|
|
47
|
-
it("An object with key-value pairs can be passed into", function () {
|
|
48
|
-
const view = new Accessor();
|
|
49
|
-
const updateView = view.set.bind(view);
|
|
50
|
-
updateView({
|
|
51
|
-
center: [-4.4861, 48.3904],
|
|
52
|
-
scale: 5000,
|
|
53
|
-
});
|
|
54
|
-
assert.strictEqual(view.get("scale"), 5000);
|
|
55
|
-
});
|
|
56
|
-
});
|
package/test/watch.test.js
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import Accessor from "../dist/index.js";
|
|
2
|
-
import assert from "assert";
|
|
3
|
-
class Counter extends Accessor {
|
|
4
|
-
constructor() {
|
|
5
|
-
super();
|
|
6
|
-
this.number = 0;
|
|
7
|
-
}
|
|
8
|
-
setNumber = (value) => {
|
|
9
|
-
this.number = value;
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
describe("#watch()", function () {
|
|
14
|
-
it("watch property change", function () {
|
|
15
|
-
const result = [];
|
|
16
|
-
const callback = (newValue, oldValue, propertyName, target) => {
|
|
17
|
-
result.push(newValue, oldValue, propertyName, target);
|
|
18
|
-
};
|
|
19
|
-
const view = new Accessor();
|
|
20
|
-
view.zoom = 4;
|
|
21
|
-
view.watch("zoom", callback);
|
|
22
|
-
view.zoom = 5;
|
|
23
|
-
assert.deepStrictEqual(result, [5, 4, "zoom", view]);
|
|
24
|
-
});
|
|
25
|
-
/**
|
|
26
|
-
* 深层属性的变更的监听
|
|
27
|
-
* 注意,此时callback的target是指向 拥有这个属性的类,而不是最外层的类
|
|
28
|
-
*/
|
|
29
|
-
it("watch deep path property change", function () {
|
|
30
|
-
const result = [];
|
|
31
|
-
const callback = (newValue, oldValue, propertyName, target) => {
|
|
32
|
-
result.push(newValue, oldValue, propertyName, target);
|
|
33
|
-
};
|
|
34
|
-
const view = new Accessor({
|
|
35
|
-
map: new Accessor({
|
|
36
|
-
basemap: new Accessor({
|
|
37
|
-
title: "streets-vector",
|
|
38
|
-
}),
|
|
39
|
-
}),
|
|
40
|
-
});
|
|
41
|
-
view.watch("map.basemap.title", callback);
|
|
42
|
-
view.map.basemap.title = "topo-vector";
|
|
43
|
-
assert.deepStrictEqual(result, [
|
|
44
|
-
"topo-vector",
|
|
45
|
-
"streets-vector",
|
|
46
|
-
"title",
|
|
47
|
-
view.map.basemap,
|
|
48
|
-
]);
|
|
49
|
-
});
|
|
50
|
-
/**
|
|
51
|
-
* 一个callback监听多个属性, 字符串形式
|
|
52
|
-
*/
|
|
53
|
-
it("watch multiple propertys change in string", function () {
|
|
54
|
-
const result = [];
|
|
55
|
-
const callback = (newValue, oldValue, propertyName, target) => {
|
|
56
|
-
result.push(newValue, oldValue, propertyName, target);
|
|
57
|
-
};
|
|
58
|
-
const view = new Accessor({
|
|
59
|
-
zoom: 12,
|
|
60
|
-
scale: 144447.638572,
|
|
61
|
-
});
|
|
62
|
-
view.watch("zoom, scale", callback);
|
|
63
|
-
view.zoom = 11;
|
|
64
|
-
view.scale = 288895.277144;
|
|
65
|
-
assert.deepStrictEqual(result, [
|
|
66
|
-
11,
|
|
67
|
-
12,
|
|
68
|
-
"zoom",
|
|
69
|
-
view,
|
|
70
|
-
288895.277144,
|
|
71
|
-
144447.638572,
|
|
72
|
-
"scale",
|
|
73
|
-
view,
|
|
74
|
-
]);
|
|
75
|
-
});
|
|
76
|
-
/**
|
|
77
|
-
* 一个callback监听多个属性, 数组形式
|
|
78
|
-
*/
|
|
79
|
-
it("watch multiple propertys change in string array", function () {
|
|
80
|
-
const result = [];
|
|
81
|
-
const callback = (newValue, oldValue, propertyName, target) => {
|
|
82
|
-
result.push(newValue, oldValue, propertyName, target);
|
|
83
|
-
};
|
|
84
|
-
const view = new Accessor({
|
|
85
|
-
zoom: 12,
|
|
86
|
-
scale: 144447.638572,
|
|
87
|
-
});
|
|
88
|
-
view.watch(["zoom", "scale"], callback);
|
|
89
|
-
view.zoom = 11;
|
|
90
|
-
view.scale = 288895.277144;
|
|
91
|
-
assert.deepStrictEqual(result, [
|
|
92
|
-
11,
|
|
93
|
-
12,
|
|
94
|
-
"zoom",
|
|
95
|
-
view,
|
|
96
|
-
288895.277144,
|
|
97
|
-
144447.638572,
|
|
98
|
-
"scale",
|
|
99
|
-
view,
|
|
100
|
-
]);
|
|
101
|
-
});
|
|
102
|
-
/**
|
|
103
|
-
* 调用remove方法移除handle,callback将不执行
|
|
104
|
-
*/
|
|
105
|
-
it("remove watch handle", function () {
|
|
106
|
-
const result = [];
|
|
107
|
-
const callback = (newValue, oldValue, propertyName, target) => {
|
|
108
|
-
result.push(newValue, oldValue, propertyName, target);
|
|
109
|
-
};
|
|
110
|
-
const view = new Accessor();
|
|
111
|
-
view.zoom = 4;
|
|
112
|
-
const handle = view.watch("zoom", callback);
|
|
113
|
-
handle.remove();
|
|
114
|
-
view.zoom = 5;
|
|
115
|
-
assert.deepStrictEqual(result, []);
|
|
116
|
-
});
|
|
117
|
-
/**
|
|
118
|
-
* 只在注册的属性变更后执行callback
|
|
119
|
-
*/
|
|
120
|
-
it("only watch registered property", function () {
|
|
121
|
-
const result = [];
|
|
122
|
-
const callback = (newValue, oldValue, propertyName, target) => {
|
|
123
|
-
result.push(newValue, oldValue, propertyName, target);
|
|
124
|
-
};
|
|
125
|
-
const view = new Accessor();
|
|
126
|
-
view.zoom = 4;
|
|
127
|
-
view.watch("scale", callback);
|
|
128
|
-
view.zoom = 5;
|
|
129
|
-
assert.deepStrictEqual(result, []);
|
|
130
|
-
});
|
|
131
|
-
/**
|
|
132
|
-
* 子类上的方法也可以被监听
|
|
133
|
-
*/
|
|
134
|
-
it("watch subclass member", function () {
|
|
135
|
-
const counter = new Counter();
|
|
136
|
-
const result = [];
|
|
137
|
-
const callback = (newValue, oldValue, propertyName, target) => {
|
|
138
|
-
result.push(newValue, oldValue, propertyName, target);
|
|
139
|
-
};
|
|
140
|
-
counter.number = 4;
|
|
141
|
-
counter.watch("number", callback);
|
|
142
|
-
counter.setNumber(5);
|
|
143
|
-
|
|
144
|
-
assert.deepStrictEqual(result, [5, 4, "number", counter]);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* 子类上的方法 监听变更次数
|
|
149
|
-
*/
|
|
150
|
-
it("watch subclass member changed times", function () {
|
|
151
|
-
const counter = new Counter();
|
|
152
|
-
let times = 0;
|
|
153
|
-
const callback = () => {
|
|
154
|
-
times++;
|
|
155
|
-
};
|
|
156
|
-
counter.watch("number", callback);
|
|
157
|
-
counter.number = 1; // +1;
|
|
158
|
-
counter.number = 1; // +1;
|
|
159
|
-
counter.set("number", 2); // +1;
|
|
160
|
-
counter.set("number", 2); // +1;
|
|
161
|
-
counter.set({ number: 3 }); // +1;
|
|
162
|
-
counter.set({ number: 3 }); // +1;
|
|
163
|
-
counter.setNumber(4); // +1;
|
|
164
|
-
counter.setNumber(4); // +1;
|
|
165
|
-
|
|
166
|
-
assert.equal(times, 8);
|
|
167
|
-
});
|
|
168
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"experimentalDecorators": true,
|
|
4
|
-
"module": "Node16",
|
|
5
|
-
"target": "ESNext",
|
|
6
|
-
"sourceMap": false,
|
|
7
|
-
"rootDir": "./src",
|
|
8
|
-
"outDir": "./dist",
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"declaration": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"moduleResolution": "node16",
|
|
13
|
-
"verbatimModuleSyntax": true
|
|
14
|
-
},
|
|
15
|
-
"include": [
|
|
16
|
-
"src/**/*.ts",
|
|
17
|
-
],
|
|
18
|
-
"exclude": []
|
|
19
|
-
}
|