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

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 L=Object.defineProperty;var $=(r,t,e)=>t in r?L(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var i=(r,t,e)=>$(r,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=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 u extends Error{constructor(t,e){super(t,e),this.message=t,this.options=e,this.name=this.constructor.name}}class b extends u{constructor(t,e,o){super(`Trying to update state of ${t} from ${e} to ${o}`),this.name=this.constructor.name}}class K extends u{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,h){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=h;const n=e.name;if(!(n in this._model&&typeof this._model[n]=="function"))throw new u(`Model does not contain method ${n}`);if(typeof e[s.actionFlag]!="function")throw new u(`Method ${n} is not action`);this.name=n}static create(t,e,o,h){return l.shallowReactive(new s(t,e,o,h))}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 d=n.then(()=>{this.ready()}).catch(a=>{if(a instanceof u||a instanceof RangeError||a instanceof ReferenceError||a instanceof SyntaxError||a instanceof TypeError||a instanceof URIError||a instanceof EvalError)throw a;const y=B(a);if(y&&!this.is(s.possibleState.pending,s.possibleState.lock))throw new K(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:a}))});return this._value={promise:d,abortController:o},d}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 c=s;const T=Proxy;function D(r){if(!(r instanceof g))throw new Error("ProtoModel instance is required");return new T(l.shallowReactive(r),{get(e,o,h){const n=Reflect.get(e,o,h),d=typeof n=="function";return d&&c.actionFlag in n&&typeof n[c.actionFlag]=="function"?e.action(n):d?n.bind(e):n}})}const p=Symbol("scope"),m=Symbol("model"),A=Symbol("actions"),v=Symbol("actionIds"),w=Symbol("actionStates"),E=Symbol("actionsSize"),f=Symbol("watchStopHandlers");var k,M,P,C,R,x,F;F=p,x=m,R=A,C=v,P=w,M=E,k=f;const S=class S{constructor(){i(this,F,l.effectScope(!0));i(this,x,null);i(this,R,new WeakMap);i(this,C,new WeakMap);i(this,P,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),o=S.createModel(e);return e[m]=o,o}get hasPendingActions(){return!!this.getActionStatesRef(c.possibleState.pending).value}get hasActionWithError(){return!!this.getActionStatesRef(c.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(()=>l.watchEffect(t[0])):this[p].run(()=>l.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(()=>l.computed(t,e))}updateBit(t,e,o){const h=o?1:0,n=~(1<<e);return t&n|h<<e}createAction(t){const e=()=>{if(!this[m])throw new Error("Model not set");return this[m]},o=c.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)||l.ref(0);return this[w].get(t)===void 0&&this[w].set(t,e),e}action(t){if(!(c.actionFlag in t&&typeof t[c.actionFlag]=="function"))throw new u("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");const e=t.name.toString(),o={[e]:function(...h){return this.action(o[e]).exec(...h)}};return o[e][c.actionFlag]=r,o[e]}exports.Action=c;exports.ActionError=I;exports.ActionInternalError=u;exports.ActionStatusConflictError=b;exports.ActionUnexpectedAbortError=K;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 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;
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  var I = Object.defineProperty;
2
- var K = (r, t, e) => t in r ? I(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
- var i = (r, t, e) => K(r, typeof t != "symbol" ? t + "" : t, e);
4
- import { shallowReactive as F, effectScope as L, watchEffect as $, watch as B, computed as D, ref as T } from "vue";
2
+ var $ = (r, t, e) => t in r ? I(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
+ var i = (r, t, e) => $(r, typeof t != "symbol" ? t + "" : t, e);
4
+ import { shallowReactive as F, effectScope as K, watchEffect as L, watch as B, computed as D, ref as T } from "vue";
5
5
  class H extends Error {
6
6
  constructor(t, e) {
7
7
  super(e.cause.message), this.actionName = t, this.options = e, this.name = this.constructor.name;
@@ -32,12 +32,12 @@ class O extends u {
32
32
  }
33
33
  }
34
34
  const j = (r) => r instanceof DOMException && r.name === "AbortError" || typeof r == "object" && r !== null && "message" in r && r.message === "canceled", s = class s {
35
- constructor(t, e, o, c) {
35
+ constructor(t, e, o, a) {
36
36
  i(this, "name");
37
37
  i(this, "_state", s.possibleState.ready);
38
38
  i(this, "_value", null);
39
39
  i(this, "_args", null);
40
- this._model = t, this.actionFunction = e, this.ownerGetter = o, this.setStateCb = c;
40
+ this._model = t, this.actionFunction = e, this.ownerGetter = o, this.setStateCb = a;
41
41
  const n = e.name;
42
42
  if (!(n in this._model && typeof this._model[n] == "function"))
43
43
  throw new u(`Model does not contain method ${n}`);
@@ -45,8 +45,8 @@ const j = (r) => r instanceof DOMException && r.name === "AbortError" || typeof
45
45
  throw new u(`Method ${n} is not action`);
46
46
  this.name = n;
47
47
  }
48
- static create(t, e, o, c) {
49
- return F(new s(t, e, o, c));
48
+ static create(t, e, o, a) {
49
+ return F(new s(t, e, o, a));
50
50
  }
51
51
  toString() {
52
52
  return this.name;
@@ -116,10 +116,10 @@ const j = (r) => r instanceof DOMException && r.name === "AbortError" || typeof
116
116
  return this.state = s.possibleState.ready, Promise.resolve();
117
117
  const l = n.then(() => {
118
118
  this.ready();
119
- }).catch((a) => {
120
- if (a instanceof u || a instanceof RangeError || a instanceof ReferenceError || a instanceof SyntaxError || a instanceof TypeError || a instanceof URIError || a instanceof EvalError)
121
- throw a;
122
- const w = j(a);
119
+ }).catch((c) => {
120
+ if (c instanceof u || c instanceof RangeError || c instanceof ReferenceError || c instanceof SyntaxError || c instanceof TypeError || c instanceof URIError || c instanceof EvalError)
121
+ throw c;
122
+ const w = j(c);
123
123
  if (w && !this.is(s.possibleState.pending, s.possibleState.lock))
124
124
  throw new O(this.name, this.state);
125
125
  if (w && this._value.abortController instanceof AbortController && this._value.abortController.signal.reason === s.abortedByLock) {
@@ -130,7 +130,7 @@ const j = (r) => r instanceof DOMException && r.name === "AbortError" || typeof
130
130
  this.state = s.possibleState.abort;
131
131
  return;
132
132
  }
133
- this.setError(new H(this.name, { cause: a }));
133
+ this.setError(new H(this.name, { cause: c }));
134
134
  });
135
135
  return this._value = {
136
136
  promise: l,
@@ -192,8 +192,8 @@ function G(r) {
192
192
  return new z(
193
193
  F(r),
194
194
  {
195
- get(e, o, c) {
196
- const n = Reflect.get(e, o, c), l = typeof n == "function";
195
+ get(e, o, a) {
196
+ const n = Reflect.get(e, o, a), l = typeof n == "function";
197
197
  return l && h.actionFlag in n && typeof n[h.actionFlag] == "function" ? e.action(n) : l ? n.bind(e) : n;
198
198
  }
199
199
  }
@@ -205,7 +205,7 @@ x = d, R = m, P = g, C = y, M = S, 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
208
- i(this, x, L(!0));
208
+ i(this, x, K(!0));
209
209
  i(this, R, null);
210
210
  // we use WeakMap to store actions as keys to avoid memory leaks
211
211
  i(this, P, /* @__PURE__ */ new WeakMap());
@@ -294,7 +294,7 @@ const f = class f {
294
294
  watch(...t) {
295
295
  if (t.length === 0)
296
296
  throw new Error("watch requires at least one argument");
297
- const e = t.length === 1 ? this[d].run(() => $(t[0])) : this[d].run(() => B(...t));
297
+ const e = t.length === 1 ? this[d].run(() => L(t[0])) : this[d].run(() => B(...t));
298
298
  if (!e)
299
299
  throw new Error("watchStopHandler is undefined");
300
300
  return this[p].add(e), () => {
@@ -306,8 +306,8 @@ const f = class f {
306
306
  }
307
307
  // @see https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/math/bits/updateBit.js
308
308
  updateBit(t, e, o) {
309
- const c = o ? 1 : 0, n = ~(1 << e);
310
- return t & n | c << e;
309
+ const a = o ? 1 : 0, n = ~(1 << e);
310
+ return t & n | a << e;
311
311
  }
312
312
  createAction(t) {
313
313
  const e = () => {
@@ -404,12 +404,19 @@ function V(r, t) {
404
404
  throw new Error("Action decorator is not supported for static methods");
405
405
  if (t.private)
406
406
  throw new Error("Action decorator is not supported for private methods");
407
- const e = t.name.toString(), o = {
407
+ let e;
408
+ try {
409
+ e = t.name.toString();
410
+ } catch (a) {
411
+ const l = `Invalid context. Can\`t get name of the method: ${a instanceof Error ? a.message : "unknown error"}`;
412
+ throw new Error(l);
413
+ }
414
+ const o = {
408
415
  // Action constructor checks that model has method with the same name.
409
416
  // We can`t define anonymous function and change name because Function.name is readonly property.
410
417
  // So we need to create stub object to save original method name
411
- [e]: function(...c) {
412
- return this.action(o[e]).exec(...c);
418
+ [e]: function(...a) {
419
+ return this.action(o[e]).exec(...a);
413
420
  }
414
421
  };
415
422
  return o[e][h.actionFlag] = r, o[e];
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.4",
4
+ "version": "2.2.0-beta.5",
5
5
  "description": "A state management library based on models for Vue.js",
6
6
  "author": "abratko",
7
7
  "license": "MIT",