@vue-modeler/model 2.2.0-beta.5 → 2.2.0-beta.6

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 CHANGED
@@ -1 +1 @@
1
- "use strict";var K=Object.defineProperty;var L=(r,t,e)=>t in r?K(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var i=(r,t,e)=>L(r,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("vue");class I extends Error{constructor(t,e){super(e.cause.message),this.actionName=t,this.options=e,this.name=this.constructor.name}get cause(){return this.options.cause}throwCause(){throw this.cause}toString(){return this.options.cause.message}}class d extends Error{constructor(t,e){super(t,e),this.message=t,this.options=e,this.name=this.constructor.name}}class b extends d{constructor(t,e,o){super(`Trying to update state of ${t} from ${e} to ${o}`),this.name=this.constructor.name}}class $ extends d{constructor(t,e){super(`Unexpected AbortError for the action ${t} in state ${e}`),this.name=this.constructor.name}}const B=r=>r instanceof DOMException&&r.name==="AbortError"||typeof r=="object"&&r!==null&&"message"in r&&r.message==="canceled",s=class s{constructor(t,e,o,a){i(this,"name");i(this,"_state",s.possibleState.ready);i(this,"_value",null);i(this,"_args",null);this._model=t,this.actionFunction=e,this.ownerGetter=o,this.setStateCb=a;const n=e.name;if(!(n in this._model&&typeof this._model[n]=="function"))throw new d(`Model does not contain method ${n}`);if(typeof e[s.actionFlag]!="function")throw new d(`Method ${n} is not action`);this.name=n}static create(t,e,o,a){return u.shallowReactive(new s(t,e,o,a))}toString(){return this.name}get owner(){return this.ownerGetter()}get possibleStates(){return Object.values(s.possibleState)}get state(){return this._state}set state(t){const e=this._state;this._state=t,this.setStateCb&&this.setStateCb(this,e,t)}get abortController(){return this.isPending?this._value.abortController:null}get args(){return this._args||[]}get promise(){return this.isPending?this._value.promise:null}get error(){return this.isError?this._value:null}get abortReason(){return this.isAbort?this._value.abortController.signal.reason:null}get isPending(){return this.state===s.possibleState.pending}get isError(){return this.state===s.possibleState.error}get isReady(){return this.state===s.possibleState.ready}get isLock(){return this.state===s.possibleState.lock}get isAbort(){return this.state===s.possibleState.abort}is(...t){return!!t.find(e=>this.state===e)}exec(...t){if(this.is(s.possibleState.lock,s.possibleState.pending))throw new b(this.name,this.state,s.possibleState.pending);const e=[...t];let o=t.length&&t[t.length-1];o instanceof AbortController||(o=new AbortController,e.push(o)),this.state=s.possibleState.pending,this._args=t;const n=this.actionFunction[s.actionFlag].apply(this._model,e);if(!(n instanceof Promise))return this.state=s.possibleState.ready,Promise.resolve();const l=n.then(()=>{this.ready()}).catch(c=>{if(c instanceof d||c instanceof RangeError||c instanceof ReferenceError||c instanceof SyntaxError||c instanceof TypeError||c instanceof URIError||c instanceof EvalError)throw c;const y=B(c);if(y&&!this.is(s.possibleState.pending,s.possibleState.lock))throw new $(this.name,this.state);if(y&&this._value.abortController instanceof AbortController&&this._value.abortController.signal.reason===s.abortedByLock){this.state=s.possibleState.lock,this._value=null;return}if(y){this.state=s.possibleState.abort;return}this.setError(new I(this.name,{cause:c}))});return this._value={promise:l,abortController:o},l}abort(t){return this.isPending?(this._value.abortController.abort(t),this._value.promise):Promise.resolve()}lock(){return this.isPending?this.abort(s.abortedByLock):(this.state=s.possibleState.lock,this._value=null,Promise.resolve())}unlock(){if(!this.isLock)throw new b(this.name,this.state,s.possibleState.ready);return this.ready()}setError(t){if(!this.isPending)throw new b(this.name,this.state,s.possibleState.error);return this.state=s.possibleState.error,this._value=t,this}ready(){return this.state=s.possibleState.ready,this}resetError(){if(!this.error)throw new b(this.name,this.state,s.possibleState.ready);return this.ready()}};i(s,"actionFlag",Symbol("__action_original_method__")),i(s,"possibleState",{pending:"pending",error:"error",lock:"lock",ready:"ready",abort:"abort"}),i(s,"abortedByLock",Symbol("lock"));let h=s;const T=Proxy;function D(r){if(!(r instanceof g))throw new Error("ProtoModel instance is required");return new T(u.shallowReactive(r),{get(e,o,a){const n=Reflect.get(e,o,a),l=typeof n=="function";return l&&h.actionFlag in n&&typeof n[h.actionFlag]=="function"?e.action(n):l?n.bind(e):n}})}const p=Symbol("scope"),S=Symbol("model"),A=Symbol("actions"),v=Symbol("actionIds"),w=Symbol("actionStates"),E=Symbol("actionsSize"),f=Symbol("watchStopHandlers");var k,M,C,P,R,x,F;F=p,x=S,R=A,P=v,C=w,M=E,k=f;const m=class m{constructor(){i(this,F,u.effectScope(!0));i(this,x,null);i(this,R,new WeakMap);i(this,P,new WeakMap);i(this,C,new Map);i(this,M,0);i(this,k,new Set)}static model(...t){if(this.prototype===m.prototype)throw new Error("ProtoModel is abstract class and can not be instantiated");const e=new this(...t),o=m.createModel(e);return e[S]=o,o}get hasPendingActions(){return!!this.getActionStatesRef(h.possibleState.pending).value}get hasActionWithError(){return!!this.getActionStatesRef(h.possibleState.error).value}watch(...t){if(t.length===0)throw new Error("watch requires at least one argument");const e=t.length===1?this[p].run(()=>u.watchEffect(t[0])):this[p].run(()=>u.watch(...t));if(!e)throw new Error("watchStopHandler is undefined");return this[f].add(e),()=>{e(),this[f].delete(e)}}computed(t,e){return this[p].run(()=>u.computed(t,e))}updateBit(t,e,o){const a=o?1:0,n=~(1<<e);return t&n|a<<e}createAction(t){const e=()=>{if(!this[S])throw new Error("Model not set");return this[S]},o=h.create(this,t,e,this.setActionState.bind(this));return this[A].set(t,o),this[v].set(o,++this[E]),this.setActionState(o),o}getActionStatesRef(t){const e=this[w].get(t)||u.ref(0);return this[w].get(t)===void 0&&this[w].set(t,e),e}action(t){if(!(h.actionFlag in t&&typeof t[h.actionFlag]=="function"))throw new d("Action decorator is not applied to the method");return this[A].get(t)||this.createAction(t)}setActionState(t){const e=this[v].get(t);if(!e)throw new Error("Action not found");for(const o of t.possibleStates)o!==t.state&&(this.getActionStatesRef(o).value=this.updateBit(this.getActionStatesRef(o).value,e,!1));this.getActionStatesRef(t.state).value=this.updateBit(this.getActionStatesRef(t.state).value,e,!0)}isModelOf(t){return this instanceof t}destructor(){this[f].forEach(t=>{t()}),this[f]=new Set,this[p].stop()}};i(m,"createModel",D);let g=m;function O(r,t){if(t.static)throw new Error("Action decorator is not supported for static methods");if(t.private)throw new Error("Action decorator is not supported for private methods");let e;try{e=t.name.toString()}catch(a){const l=`Invalid context. Can\`t get name of the method: ${a instanceof Error?a.message:"unknown error"}`;throw new Error(l)}const o={[e]:function(...a){return this.action(o[e]).exec(...a)}};return o[e][h.actionFlag]=r,o[e]}exports.Action=h;exports.ActionError=I;exports.ActionInternalError=d;exports.ActionStatusConflictError=b;exports.ActionUnexpectedAbortError=$;exports.ProtoModel=g;exports.action=O;
1
+ "use strict";var K=Object.defineProperty;var L=(r,t,e)=>t in r?K(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var i=(r,t,e)=>L(r,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("vue");class I extends Error{constructor(t,e){super(e.cause.message),this.actionName=t,this.options=e,this.name=this.constructor.name}get cause(){return this.options.cause}throwCause(){throw this.cause}toString(){return this.options.cause.message}}class d extends Error{constructor(t,e){super(t,e),this.message=t,this.options=e,this.name=this.constructor.name}}class b extends d{constructor(t,e,o){super(`Trying to update state of ${t} from ${e} to ${o}`),this.name=this.constructor.name}}class $ extends d{constructor(t,e){super(`Unexpected AbortError for the action ${t} in state ${e}`),this.name=this.constructor.name}}const B=r=>r instanceof DOMException&&r.name==="AbortError"||typeof r=="object"&&r!==null&&"message"in r&&r.message==="canceled",s=class s{constructor(t,e,o,a){i(this,"name");i(this,"_state",s.possibleState.ready);i(this,"_value",null);i(this,"_args",null);this._model=t,this.actionFunction=e,this.ownerGetter=o,this.setStateCb=a;const n=e.name;if(!(n in this._model&&typeof this._model[n]=="function"))throw new d(`Model does not contain method ${n}`);if(typeof e[s.actionFlag]!="function")throw new d(`Method ${n} is not action`);this.name=n}static create(t,e,o,a){return u.shallowReactive(new s(t,e,o,a))}toString(){return this.name}get owner(){return this.ownerGetter()}get possibleStates(){return Object.values(s.possibleState)}get state(){return this._state}set state(t){const e=this._state;this._state=t,this.setStateCb&&this.setStateCb(this,e,t)}get abortController(){return this.isPending?this._value.abortController:null}get args(){return this._args||[]}get promise(){return this.isPending?this._value.promise:null}get error(){return this.isError?this._value:null}get abortReason(){return this.isAbort?this._value.abortController.signal.reason:null}get isPending(){return this.state===s.possibleState.pending}get isError(){return this.state===s.possibleState.error}get isReady(){return this.state===s.possibleState.ready}get isLock(){return this.state===s.possibleState.lock}get isAbort(){return this.state===s.possibleState.abort}is(...t){return!!t.find(e=>this.state===e)}exec(...t){if(this.is(s.possibleState.lock,s.possibleState.pending))throw new b(this.name,this.state,s.possibleState.pending);const e=[...t];let o=t.length&&t[t.length-1];o instanceof AbortController||(o=new AbortController,e.push(o)),this.state=s.possibleState.pending,this._args=t;const n=this.actionFunction[s.actionFlag].apply(this._model,e);if(!(n instanceof Promise))return this.state=s.possibleState.ready,Promise.resolve();const l=n.then(()=>{this.ready()}).catch(c=>{if(c instanceof d||c instanceof RangeError||c instanceof ReferenceError||c instanceof SyntaxError||c instanceof TypeError||c instanceof URIError||c instanceof EvalError)throw c;const y=B(c);if(y&&!this.is(s.possibleState.pending,s.possibleState.lock))throw new $(this.name,this.state);if(y&&this._value.abortController instanceof AbortController&&this._value.abortController.signal.reason===s.abortedByLock){this.state=s.possibleState.lock,this._value=null;return}if(y){this.state=s.possibleState.abort;return}this.setError(new I(this.name,{cause:c}))});return this._value={promise:l,abortController:o},l}abort(t){return this.isPending?(this._value.abortController.abort(t),this._value.promise):Promise.resolve()}lock(){return this.isPending?this.abort(s.abortedByLock):(this.state=s.possibleState.lock,this._value=null,Promise.resolve())}unlock(){if(!this.isLock)throw new b(this.name,this.state,s.possibleState.ready);return this.ready()}setError(t){if(!this.isPending)throw new b(this.name,this.state,s.possibleState.error);return this.state=s.possibleState.error,this._value=t,this}ready(){return this.state=s.possibleState.ready,this}resetError(){if(!this.error)throw new b(this.name,this.state,s.possibleState.ready);return this.ready()}};i(s,"actionFlag",Symbol("__action_original_method__")),i(s,"possibleState",{pending:"pending",error:"error",lock:"lock",ready:"ready",abort:"abort"}),i(s,"abortedByLock",Symbol("lock"));let h=s;const p=Symbol("scope"),w=Symbol("model"),A=Symbol("actions"),v=Symbol("actionIds"),m=Symbol("actionStates"),E=Symbol("actionsSize"),f=Symbol("watchStopHandlers"),T=Proxy;function D(r){if(!(r instanceof g))throw new Error("ProtoModel instance is required");const t=new T(u.shallowReactive(r),{get(e,o,a){const n=Reflect.get(e,o,a),l=typeof n=="function";return l&&h.actionFlag in n&&typeof n[h.actionFlag]=="function"?e.action(n):l?n.bind(e):n}});return r[w]=t,t}var k,M,C,P,R,x,F;F=p,x=w,R=A,P=v,C=m,M=E,k=f;const S=class S{constructor(){i(this,F,u.effectScope(!0));i(this,x,null);i(this,R,new WeakMap);i(this,P,new WeakMap);i(this,C,new Map);i(this,M,0);i(this,k,new Set)}static model(...t){if(this.prototype===S.prototype)throw new Error("ProtoModel is abstract class and can not be instantiated");const e=new this(...t);return S.createModel(e)}get hasPendingActions(){return!!this.getActionStatesRef(h.possibleState.pending).value}get hasActionWithError(){return!!this.getActionStatesRef(h.possibleState.error).value}watch(...t){if(t.length===0)throw new Error("watch requires at least one argument");const e=t.length===1?this[p].run(()=>u.watchEffect(t[0])):this[p].run(()=>u.watch(...t));if(!e)throw new Error("watchStopHandler is undefined");return this[f].add(e),()=>{e(),this[f].delete(e)}}computed(t,e){return this[p].run(()=>u.computed(t,e))}updateBit(t,e,o){const a=o?1:0,n=~(1<<e);return t&n|a<<e}createAction(t){const e=()=>{if(!this[w])throw new Error("Model not set");return this[w]},o=h.create(this,t,e,this.setActionState.bind(this));return this[A].set(t,o),this[v].set(o,++this[E]),this.setActionState(o),o}getActionStatesRef(t){const e=this[m].get(t)||u.ref(0);return this[m].get(t)===void 0&&this[m].set(t,e),e}action(t){if(!(h.actionFlag in t&&typeof t[h.actionFlag]=="function"))throw new d("Action decorator is not applied to the method");return this[A].get(t)||this.createAction(t)}setActionState(t){const e=this[v].get(t);if(!e)throw new Error("Action not found");for(const o of t.possibleStates)o!==t.state&&(this.getActionStatesRef(o).value=this.updateBit(this.getActionStatesRef(o).value,e,!1));this.getActionStatesRef(t.state).value=this.updateBit(this.getActionStatesRef(t.state).value,e,!0)}isModelOf(t){return this instanceof t}destructor(){this[f].forEach(t=>{t()}),this[f]=new Set,this[p].stop()}};i(S,"createModel",D);let g=S;function O(r,t){if(t.static)throw new Error("Action decorator is not supported for static methods");if(t.private)throw new Error("Action decorator is not supported for private methods");let e;try{e=t.name.toString()}catch(a){const l=`Invalid context. Can\`t get name of the method: ${a instanceof Error?a.message:"unknown error"}`;throw new Error(l)}const o={[e]:function(...a){return this.action(o[e]).exec(...a)}};return o[e][h.actionFlag]=r,o[e]}exports.Action=h;exports.ActionError=I;exports.ActionInternalError=d;exports.ActionStatusConflictError=b;exports.ActionUnexpectedAbortError=$;exports.ProtoModel=g;exports.action=O;
package/dist/index.js CHANGED
@@ -185,11 +185,11 @@ i(s, "actionFlag", Symbol("__action_original_method__")), i(s, "possibleState",
185
185
  abort: "abort"
186
186
  }), i(s, "abortedByLock", Symbol("lock"));
187
187
  let h = s;
188
- const z = Proxy;
188
+ const d = Symbol("scope"), S = Symbol("model"), g = Symbol("actions"), y = Symbol("actionIds"), m = Symbol("actionStates"), v = Symbol("actionsSize"), p = Symbol("watchStopHandlers"), z = Proxy;
189
189
  function G(r) {
190
190
  if (!(r instanceof A))
191
191
  throw new Error("ProtoModel instance is required");
192
- return new z(
192
+ const t = new z(
193
193
  F(r),
194
194
  {
195
195
  get(e, o, a) {
@@ -198,10 +198,10 @@ function G(r) {
198
198
  }
199
199
  }
200
200
  );
201
+ return r[S] = t, t;
201
202
  }
202
- const d = Symbol("scope"), m = Symbol("model"), g = Symbol("actions"), y = Symbol("actionIds"), S = Symbol("actionStates"), v = Symbol("actionsSize"), p = Symbol("watchStopHandlers");
203
203
  var k, E, M, C, P, R, x;
204
- x = d, R = m, P = g, C = y, M = S, E = v, k = p;
204
+ x = d, R = S, P = g, C = y, M = m, E = v, k = p;
205
205
  const f = class f {
206
206
  constructor() {
207
207
  // each model has its own effect scope to avoid memory leaks
@@ -226,8 +226,8 @@ const f = class f {
226
226
  static model(...t) {
227
227
  if (this.prototype === f.prototype)
228
228
  throw new Error("ProtoModel is abstract class and can not be instantiated");
229
- const e = new this(...t), o = f.createModel(e);
230
- return e[m] = o, o;
229
+ const e = new this(...t);
230
+ return f.createModel(e);
231
231
  }
232
232
  get hasPendingActions() {
233
233
  return !!this.getActionStatesRef(h.possibleState.pending).value;
@@ -311,9 +311,9 @@ const f = class f {
311
311
  }
312
312
  createAction(t) {
313
313
  const e = () => {
314
- if (!this[m])
314
+ if (!this[S])
315
315
  throw new Error("Model not set");
316
- return this[m];
316
+ return this[S];
317
317
  }, o = h.create(
318
318
  this,
319
319
  t,
@@ -323,8 +323,8 @@ const f = class f {
323
323
  return this[g].set(t, o), this[y].set(o, ++this[v]), this.setActionState(o), o;
324
324
  }
325
325
  getActionStatesRef(t) {
326
- const e = this[S].get(t) || T(0);
327
- return this[S].get(t) === void 0 && this[S].set(t, e), e;
326
+ const e = this[m].get(t) || T(0);
327
+ return this[m].get(t) === void 0 && this[m].set(t, e), e;
328
328
  }
329
329
  /**
330
330
  * Gets Action instance by wrapped original method or create Action instance.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@vue-modeler/model",
4
- "version": "2.2.0-beta.5",
4
+ "version": "2.2.0-beta.6",
5
5
  "description": "A state management library based on models for Vue.js",
6
6
  "author": "abratko",
7
7
  "license": "MIT",