@vue-modeler/model 2.2.0-beta.4 → 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 +1 -1
- package/dist/index.js +38 -31
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var I = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var i = (r, t, e) =>
|
|
4
|
-
import { shallowReactive as F, effectScope as
|
|
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,
|
|
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 =
|
|
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,
|
|
49
|
-
return F(new s(t, e, o,
|
|
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((
|
|
120
|
-
if (
|
|
121
|
-
throw
|
|
122
|
-
const w = j(
|
|
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:
|
|
133
|
+
this.setError(new H(this.name, { cause: c }));
|
|
134
134
|
});
|
|
135
135
|
return this._value = {
|
|
136
136
|
promise: l,
|
|
@@ -185,27 +185,27 @@ 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
|
-
|
|
192
|
+
const t = new z(
|
|
193
193
|
F(r),
|
|
194
194
|
{
|
|
195
|
-
get(e, o,
|
|
196
|
-
const n = Reflect.get(e, o,
|
|
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
|
}
|
|
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 =
|
|
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
|
|
208
|
-
i(this, x,
|
|
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());
|
|
@@ -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)
|
|
230
|
-
return e
|
|
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;
|
|
@@ -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(() =>
|
|
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,14 +306,14 @@ 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
|
|
310
|
-
return t & n |
|
|
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 = () => {
|
|
314
|
-
if (!this[
|
|
314
|
+
if (!this[S])
|
|
315
315
|
throw new Error("Model not set");
|
|
316
|
-
return this[
|
|
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[
|
|
327
|
-
return this[
|
|
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.
|
|
@@ -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
|
-
|
|
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(...
|
|
412
|
-
return this.action(o[e]).exec(...
|
|
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];
|