as-model 0.1.8 → 0.1.10
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.js +9 -0
- package/esm/index.js +34 -19
- package/esm/key/index.js +46 -38
- package/esm/model/index.js +33 -17
- package/esm/store/enhance/selector.js +43 -50
- package/esm/store/enhance/signal.js +20 -26
- package/esm/store/index.js +58 -39
- package/esm/store/instance.js +41 -55
- package/esm/tools/proxy.js +34 -14
- package/esm/tools/shallowEqual.js +11 -12
- package/esm/updater/index.js +61 -47
- package/esm/updater/notifier.js +59 -71
- package/esm/updater/tunnel.js +61 -63
- package/esm/validation/index.js +7 -7
- package/index.d.ts +3 -0
- package/package.json +1 -1
- package/esm/model/type.js +0 -0
- package/esm/tools.js +0 -78
package/esm/store/index.js
CHANGED
|
@@ -1,20 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
4
20
|
import { createUpdater } from "../updater";
|
|
5
21
|
import { isModelKey, isModelUsage } from "../validation";
|
|
6
22
|
import { modelKeyIdentifier, modelStoreIdentifier } from "../identifiers";
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var
|
|
14
|
-
|
|
23
|
+
import {
|
|
24
|
+
extractInstance,
|
|
25
|
+
createField as createInstanceField,
|
|
26
|
+
createMethod as createInstanceMethod
|
|
27
|
+
} from "./instance";
|
|
28
|
+
function createPrimaryKey(modelFn, config = {}) {
|
|
29
|
+
var _a;
|
|
30
|
+
const ifModelKey = isModelKey(modelFn);
|
|
31
|
+
const ifModelUsage = isModelUsage(modelFn);
|
|
32
|
+
const model = ifModelKey ? modelFn.source : modelFn;
|
|
33
|
+
const selector = (_a = config.selector) != null ? _a : ifModelKey || ifModelUsage ? modelFn.selector : function defaultSelector(i) {
|
|
15
34
|
return i();
|
|
16
35
|
};
|
|
17
|
-
|
|
36
|
+
const wrapModel = function wrapModel2(state) {
|
|
18
37
|
return model(state);
|
|
19
38
|
};
|
|
20
39
|
wrapModel.source = model;
|
|
@@ -25,70 +44,70 @@ function createPrimaryKey(modelFn) {
|
|
|
25
44
|
}
|
|
26
45
|
return wrapModel;
|
|
27
46
|
}
|
|
28
|
-
function createStore(modelLike) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
var hasKeyState = !!modelKey && "defaultState" in modelKey;
|
|
47
|
+
function createStore(modelLike, config = {}) {
|
|
48
|
+
const ifModelKey = isModelKey(modelLike);
|
|
49
|
+
const model = ifModelKey ? modelLike.source : modelLike;
|
|
50
|
+
const modelKey = ifModelKey ? modelLike : void 0;
|
|
51
|
+
const conf = function computeConfig() {
|
|
52
|
+
const hasConfigState = "state" in config;
|
|
53
|
+
const hasKeyState = !!modelKey && "defaultState" in modelKey;
|
|
36
54
|
if (hasConfigState) {
|
|
37
55
|
return config;
|
|
38
56
|
}
|
|
39
57
|
if (hasKeyState) {
|
|
40
|
-
return
|
|
41
|
-
state: modelKey === null || modelKey === void 0 ? void 0 : modelKey.defaultState
|
|
42
|
-
});
|
|
58
|
+
return __spreadProps(__spreadValues({}, config), { state: modelKey == null ? void 0 : modelKey.defaultState });
|
|
43
59
|
}
|
|
44
60
|
return config;
|
|
45
61
|
}();
|
|
46
|
-
|
|
62
|
+
const combinedMiddleWare = function combinedMiddleWare2(s) {
|
|
47
63
|
return function updaterMiddleWare(next) {
|
|
48
|
-
|
|
64
|
+
const { middleWares } = conf;
|
|
49
65
|
if (middleWares == null) {
|
|
50
66
|
return next;
|
|
51
67
|
}
|
|
52
|
-
|
|
68
|
+
const updateMiddleWares = [...middleWares].reverse().map((middleWare) => {
|
|
53
69
|
return middleWare(s);
|
|
54
70
|
});
|
|
55
|
-
return updateMiddleWares.reduce(
|
|
71
|
+
return updateMiddleWares.reduce((finalDispatcher, um) => {
|
|
56
72
|
return um(finalDispatcher);
|
|
57
73
|
}, next);
|
|
58
74
|
};
|
|
59
75
|
};
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
76
|
+
const updater = createUpdater(model, combinedMiddleWare, conf);
|
|
77
|
+
const key = modelKey != null ? modelKey : createPrimaryKey(model, config);
|
|
78
|
+
const getInstance = function getInstance2() {
|
|
63
79
|
return extractInstance(updater);
|
|
64
80
|
};
|
|
65
|
-
|
|
81
|
+
const store = {
|
|
66
82
|
key,
|
|
67
|
-
subscribe
|
|
68
|
-
|
|
83
|
+
subscribe(dispatcher) {
|
|
84
|
+
const { connect, disconnect } = updater.createTunnel(dispatcher);
|
|
69
85
|
connect();
|
|
70
86
|
return disconnect;
|
|
71
87
|
},
|
|
72
88
|
getInstance,
|
|
73
|
-
update
|
|
89
|
+
update(args) {
|
|
74
90
|
updater.update(args);
|
|
75
91
|
},
|
|
76
|
-
destroy
|
|
92
|
+
destroy() {
|
|
77
93
|
updater.destroy();
|
|
78
94
|
},
|
|
79
|
-
payload
|
|
95
|
+
payload(callback) {
|
|
80
96
|
return updater.payload(callback);
|
|
81
97
|
},
|
|
82
|
-
isDestroyed
|
|
98
|
+
isDestroyed() {
|
|
83
99
|
return updater.isDestroyed;
|
|
84
100
|
},
|
|
101
|
+
extends(e) {
|
|
102
|
+
return Object.assign(store, e);
|
|
103
|
+
},
|
|
85
104
|
updater,
|
|
86
105
|
modelStoreIdentifier
|
|
87
106
|
};
|
|
88
107
|
return store;
|
|
89
108
|
}
|
|
90
|
-
|
|
91
|
-
|
|
109
|
+
const createField = createInstanceField;
|
|
110
|
+
const createMethod = createInstanceMethod;
|
|
92
111
|
import { createSignal, createSelector } from "./enhance";
|
|
93
112
|
export {
|
|
94
113
|
createField,
|
package/esm/store/instance.js
CHANGED
|
@@ -1,81 +1,71 @@
|
|
|
1
|
-
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
2
|
-
import { _ as _type_of } from "@swc/helpers/_/_type_of";
|
|
3
1
|
import { createProxy, shallowEqual } from "../tools";
|
|
4
|
-
|
|
5
|
-
field
|
|
2
|
+
const cacheIdentify = {
|
|
3
|
+
field(d) {
|
|
6
4
|
if (!d) {
|
|
7
5
|
return false;
|
|
8
6
|
}
|
|
9
|
-
|
|
7
|
+
const f = d;
|
|
10
8
|
return f.identifier === cacheIdentify.field;
|
|
11
9
|
},
|
|
12
|
-
method
|
|
10
|
+
method(d) {
|
|
13
11
|
if (typeof d !== "function") {
|
|
14
12
|
return false;
|
|
15
13
|
}
|
|
16
|
-
|
|
14
|
+
const m = d;
|
|
17
15
|
return m.identifier === cacheIdentify.method;
|
|
18
16
|
}
|
|
19
17
|
};
|
|
20
18
|
function createField(callback, deps) {
|
|
21
|
-
|
|
19
|
+
const currentDeps = function computeDeps() {
|
|
22
20
|
if (deps == null) {
|
|
23
21
|
return deps;
|
|
24
22
|
}
|
|
25
|
-
if (deps.some(
|
|
26
|
-
return cacheIdentify.field(d) && d.deps == null;
|
|
27
|
-
})) {
|
|
23
|
+
if (deps.some((d) => cacheIdentify.field(d) && d.deps == null)) {
|
|
28
24
|
return void 0;
|
|
29
25
|
}
|
|
30
|
-
return deps.flatMap(
|
|
26
|
+
return deps.flatMap((d) => {
|
|
31
27
|
if (cacheIdentify.field(d)) {
|
|
32
28
|
return d.deps;
|
|
33
29
|
}
|
|
34
30
|
return d;
|
|
35
31
|
});
|
|
36
32
|
}();
|
|
37
|
-
|
|
33
|
+
const value = callback();
|
|
38
34
|
return {
|
|
39
35
|
callback,
|
|
40
36
|
deps: currentDeps,
|
|
41
37
|
identifier: cacheIdentify.field,
|
|
42
38
|
value,
|
|
43
|
-
get
|
|
39
|
+
get() {
|
|
44
40
|
return value;
|
|
45
41
|
}
|
|
46
42
|
};
|
|
47
43
|
}
|
|
48
|
-
function createMethod(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
args[_key] = arguments[_key];
|
|
52
|
-
}
|
|
53
|
-
return method2.apply(void 0, _to_consumable_array(args));
|
|
44
|
+
function createMethod(method) {
|
|
45
|
+
const replace = function replace2(...args) {
|
|
46
|
+
return method(...args);
|
|
54
47
|
};
|
|
55
|
-
Object.assign(replace,
|
|
48
|
+
Object.assign(replace, method());
|
|
56
49
|
replace.identifier = cacheIdentify.method;
|
|
57
50
|
return replace;
|
|
58
51
|
}
|
|
59
52
|
function wrapToActionMethod(updater, methodName) {
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
const { cacheMethods } = updater;
|
|
54
|
+
const cachedMethod = cacheMethods[methodName];
|
|
62
55
|
if (typeof cachedMethod === "function") {
|
|
63
56
|
return cachedMethod;
|
|
64
57
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
var instance = updater.instance;
|
|
70
|
-
var method2 = instance[methodName];
|
|
71
|
-
if (typeof method2 !== "function") {
|
|
58
|
+
const actionMethod = function actionMethod2(...args) {
|
|
59
|
+
const { instance } = updater;
|
|
60
|
+
const method = instance[methodName];
|
|
61
|
+
if (typeof method !== "function") {
|
|
72
62
|
throw new Error("Can not change methods in runtime.");
|
|
73
63
|
}
|
|
74
|
-
if (cacheIdentify.method(
|
|
75
|
-
return
|
|
64
|
+
if (cacheIdentify.method(method)) {
|
|
65
|
+
return method(...args);
|
|
76
66
|
}
|
|
77
|
-
|
|
78
|
-
|
|
67
|
+
const state = method(...args);
|
|
68
|
+
const action = {
|
|
79
69
|
type: methodName,
|
|
80
70
|
state,
|
|
81
71
|
prevState: updater.state,
|
|
@@ -90,26 +80,26 @@ function wrapToActionMethod(updater, methodName) {
|
|
|
90
80
|
return actionMethod;
|
|
91
81
|
}
|
|
92
82
|
function wrapToField(updater, propertyName, value, onGot) {
|
|
93
|
-
|
|
83
|
+
const { cacheFields } = updater;
|
|
94
84
|
if (!cacheIdentify.field(value)) {
|
|
95
85
|
if (onGot) {
|
|
96
86
|
onGot(propertyName, value);
|
|
97
87
|
}
|
|
98
88
|
return value;
|
|
99
89
|
}
|
|
100
|
-
|
|
101
|
-
|
|
90
|
+
const field = value;
|
|
91
|
+
const cachedField = cacheFields[propertyName];
|
|
102
92
|
if (cachedField) {
|
|
103
93
|
return cachedField.getter;
|
|
104
94
|
}
|
|
105
|
-
|
|
106
|
-
get
|
|
107
|
-
|
|
95
|
+
const getter = {
|
|
96
|
+
get() {
|
|
97
|
+
const currentField = updater.instance[propertyName];
|
|
108
98
|
if (!cacheIdentify.field(currentField)) {
|
|
109
99
|
throw new Error("Field should always be field.");
|
|
110
100
|
}
|
|
111
|
-
|
|
112
|
-
|
|
101
|
+
const current = currentField.get();
|
|
102
|
+
const fieldInCache = updater.cacheFields[propertyName];
|
|
113
103
|
if (!currentField.deps || fieldInCache == null) {
|
|
114
104
|
cacheFields[propertyName] = {
|
|
115
105
|
getter,
|
|
@@ -129,37 +119,33 @@ function wrapToField(updater, propertyName, value, onGot) {
|
|
|
129
119
|
return current;
|
|
130
120
|
}
|
|
131
121
|
};
|
|
132
|
-
cacheFields[propertyName] = {
|
|
133
|
-
getter,
|
|
134
|
-
value: field2.value,
|
|
135
|
-
deps: field2.deps
|
|
136
|
-
};
|
|
122
|
+
cacheFields[propertyName] = { getter, value: field.value, deps: field.deps };
|
|
137
123
|
return getter;
|
|
138
124
|
}
|
|
139
125
|
function extractInstance(updater, onGet) {
|
|
140
|
-
|
|
141
|
-
if (
|
|
126
|
+
const { instance } = updater;
|
|
127
|
+
if (typeof instance !== "object" || !instance) {
|
|
142
128
|
throw new Error("The instance should be an object or array.");
|
|
143
129
|
}
|
|
144
|
-
|
|
145
|
-
|
|
130
|
+
const properties = Object.getOwnPropertyNames(instance);
|
|
131
|
+
const handleGetter = function handleGetter2(key, value) {
|
|
146
132
|
if (!onGet) {
|
|
147
133
|
return;
|
|
148
134
|
}
|
|
149
135
|
onGet(key, value);
|
|
150
136
|
};
|
|
151
137
|
return createProxy(instance, {
|
|
152
|
-
get
|
|
153
|
-
|
|
138
|
+
get(target, p) {
|
|
139
|
+
const value = target[p];
|
|
154
140
|
if (typeof value === "function" && properties.indexOf(p) >= 0) {
|
|
155
|
-
|
|
141
|
+
const actionMethod = wrapToActionMethod(updater, p);
|
|
156
142
|
Object.assign(actionMethod, value);
|
|
157
143
|
handleGetter(p, actionMethod);
|
|
158
144
|
return actionMethod;
|
|
159
145
|
}
|
|
160
146
|
return wrapToField(updater, p, value, handleGetter);
|
|
161
147
|
},
|
|
162
|
-
set
|
|
148
|
+
set() {
|
|
163
149
|
return false;
|
|
164
150
|
}
|
|
165
151
|
});
|
package/esm/tools/proxy.js
CHANGED
|
@@ -1,37 +1,57 @@
|
|
|
1
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
2
17
|
function getDescriptors(target, receiver, ownOrPrototype, handler) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
it.forEach(
|
|
18
|
+
const it = Object.keys(ownOrPrototype);
|
|
19
|
+
const result = {};
|
|
20
|
+
it.forEach((key) => {
|
|
6
21
|
result[key] = {
|
|
7
|
-
get:
|
|
22
|
+
get: () => {
|
|
8
23
|
if (!handler.get) {
|
|
9
24
|
return target[key];
|
|
10
25
|
}
|
|
11
26
|
return handler.get(target, key, receiver);
|
|
12
27
|
},
|
|
13
|
-
set:
|
|
28
|
+
set: (v) => {
|
|
14
29
|
if (!handler.set) {
|
|
15
30
|
target[key] = v;
|
|
16
31
|
return;
|
|
17
32
|
}
|
|
18
|
-
|
|
33
|
+
const valid = handler.set(target, key, v, receiver);
|
|
19
34
|
if (!valid) {
|
|
20
|
-
throw new Error(
|
|
35
|
+
throw new Error(`${key} in proxy target is not mutable`);
|
|
21
36
|
}
|
|
22
37
|
}
|
|
23
38
|
};
|
|
24
39
|
});
|
|
25
40
|
return result;
|
|
26
41
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
42
|
+
const createSimpleProxy = (target, handler) => {
|
|
43
|
+
const proxy = {};
|
|
44
|
+
const own = getDescriptors(target, proxy, target, handler);
|
|
45
|
+
const prototype = getDescriptors(
|
|
46
|
+
target,
|
|
47
|
+
proxy,
|
|
48
|
+
Object.getPrototypeOf(target),
|
|
49
|
+
handler
|
|
50
|
+
);
|
|
51
|
+
Object.defineProperties(proxy, __spreadValues(__spreadValues({}, prototype), own));
|
|
32
52
|
return proxy;
|
|
33
53
|
};
|
|
34
|
-
|
|
54
|
+
const createProxy = (target, handler) => {
|
|
35
55
|
if (typeof Proxy !== "function") {
|
|
36
56
|
return createSimpleProxy(target, handler);
|
|
37
57
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { _ as _type_of } from "@swc/helpers/_/_type_of";
|
|
2
1
|
function isObject(data) {
|
|
3
|
-
return data &&
|
|
2
|
+
return data && typeof data === "object";
|
|
4
3
|
}
|
|
5
4
|
function shallowEqual(prev, current) {
|
|
6
5
|
if (Object.is(prev, current)) {
|
|
@@ -9,22 +8,22 @@ function shallowEqual(prev, current) {
|
|
|
9
8
|
if (!isObject(prev) || !isObject(current)) {
|
|
10
9
|
return false;
|
|
11
10
|
}
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
const prevKeys = Object.keys(prev);
|
|
12
|
+
const currentKeys = Object.keys(current);
|
|
14
13
|
if (prevKeys.length !== currentKeys.length) {
|
|
15
14
|
return false;
|
|
16
15
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
const pre = prev;
|
|
17
|
+
const curr = current;
|
|
18
|
+
const hasDiffKey = prevKeys.some(
|
|
19
|
+
(key) => !Object.prototype.hasOwnProperty.call(curr, key)
|
|
20
|
+
);
|
|
22
21
|
if (hasDiffKey) {
|
|
23
22
|
return false;
|
|
24
23
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
const hasDiffValue = currentKeys.some((key) => {
|
|
25
|
+
const currentValue = curr[key];
|
|
26
|
+
const prevValue = pre[key];
|
|
28
27
|
return !Object.is(currentValue, prevValue);
|
|
29
28
|
});
|
|
30
29
|
return !hasDiffValue;
|
package/esm/updater/index.js
CHANGED
|
@@ -1,30 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
3
20
|
import { createNoStateModel } from "../validation";
|
|
4
21
|
import { generateNotifier } from "./notifier";
|
|
5
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
generateTunnelCreator,
|
|
24
|
+
createUnInitializedUpdater,
|
|
25
|
+
destroy
|
|
26
|
+
} from "./tunnel";
|
|
6
27
|
function createInitializedUpdater(updater, middleWare) {
|
|
7
|
-
|
|
28
|
+
const createTunnel = generateTunnelCreator(updater);
|
|
8
29
|
return {
|
|
9
30
|
notify: generateNotifier(updater, middleWare),
|
|
10
31
|
createTunnel
|
|
11
32
|
};
|
|
12
33
|
}
|
|
13
34
|
function createUpdateFn(updater, middleWare) {
|
|
14
|
-
return function update() {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
var state = "state" in args ? args.state : u.state;
|
|
35
|
+
return function update(args = {}) {
|
|
36
|
+
updater.mutate((u, effect) => {
|
|
37
|
+
var _a;
|
|
38
|
+
const model = (_a = args.model) != null ? _a : u.model;
|
|
39
|
+
const initialState = "initialState" in args ? args.initialState : u.state;
|
|
40
|
+
const state = "state" in args ? args.state : u.state;
|
|
21
41
|
if (u.controlled) {
|
|
22
|
-
|
|
23
|
-
return
|
|
24
|
-
state,
|
|
25
|
-
instance,
|
|
26
|
-
model
|
|
27
|
-
});
|
|
42
|
+
const instance2 = model(state);
|
|
43
|
+
return __spreadProps(__spreadValues({}, u), { state, instance: instance2, model });
|
|
28
44
|
}
|
|
29
45
|
if (u.isDestroyed) {
|
|
30
46
|
return u;
|
|
@@ -33,46 +49,45 @@ function createUpdateFn(updater, middleWare) {
|
|
|
33
49
|
throw new Error("Should update initialState first.");
|
|
34
50
|
}
|
|
35
51
|
if (!u.initialized) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return
|
|
52
|
+
const instance2 = model(initialState);
|
|
53
|
+
const initializedUpdater = createInitializedUpdater(u, middleWare);
|
|
54
|
+
return __spreadValues(__spreadProps(__spreadValues({}, u), {
|
|
39
55
|
model,
|
|
40
56
|
state: initialState,
|
|
41
|
-
instance:
|
|
57
|
+
instance: instance2,
|
|
42
58
|
initialized: true
|
|
43
59
|
}), initializedUpdater);
|
|
44
60
|
}
|
|
45
61
|
if (Object.is(u.model, model) && Object.is(u.state, state)) {
|
|
46
62
|
return u;
|
|
47
63
|
}
|
|
48
|
-
|
|
49
|
-
effect(
|
|
64
|
+
const instance = model(state);
|
|
65
|
+
effect((up) => {
|
|
50
66
|
up.notify({
|
|
51
67
|
type: null,
|
|
52
68
|
method: null,
|
|
53
69
|
prevInstance: u.instance,
|
|
54
|
-
instance
|
|
70
|
+
instance,
|
|
55
71
|
prevState: u.state,
|
|
56
72
|
state
|
|
57
73
|
});
|
|
58
74
|
});
|
|
59
|
-
return
|
|
75
|
+
return __spreadProps(__spreadValues({}, u), {
|
|
60
76
|
state,
|
|
61
77
|
model,
|
|
62
|
-
instance
|
|
78
|
+
instance,
|
|
63
79
|
initialized: true
|
|
64
80
|
});
|
|
65
81
|
});
|
|
66
82
|
};
|
|
67
83
|
}
|
|
68
|
-
|
|
69
|
-
function createUpdater(model, middleWare) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
var updater = _object_spread({
|
|
84
|
+
const lazyModel = createNoStateModel();
|
|
85
|
+
function createUpdater(model, middleWare, config = {}) {
|
|
86
|
+
const hasDefaultState = "state" in config;
|
|
87
|
+
const { controlled, state: defaultState } = config;
|
|
88
|
+
const defaultInstance = hasDefaultState ? model(defaultState) : lazyModel(void 0);
|
|
89
|
+
const unInitializedUpdater = createUnInitializedUpdater();
|
|
90
|
+
const updater = __spreadValues({
|
|
76
91
|
sidePayload: void 0,
|
|
77
92
|
version: 0,
|
|
78
93
|
isDestroyed: false,
|
|
@@ -88,22 +103,21 @@ function createUpdater(model, middleWare) {
|
|
|
88
103
|
controlled: !!controlled,
|
|
89
104
|
isSubscribing: false,
|
|
90
105
|
config,
|
|
91
|
-
payload
|
|
106
|
+
payload(setter) {
|
|
92
107
|
if (typeof setter === "function") {
|
|
93
108
|
updater.sidePayload = setter(updater.sidePayload);
|
|
94
109
|
}
|
|
95
110
|
return updater.sidePayload;
|
|
96
111
|
},
|
|
97
|
-
mutate
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
effects.forEach(
|
|
101
|
-
return e(u);
|
|
102
|
-
});
|
|
112
|
+
mutate(callback) {
|
|
113
|
+
const effects = [];
|
|
114
|
+
const runEffects = function runEffects2(u) {
|
|
115
|
+
effects.forEach((e) => e(u));
|
|
103
116
|
};
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
117
|
+
const result = callback(
|
|
118
|
+
updater,
|
|
119
|
+
(effectFn) => effects.push(effectFn)
|
|
120
|
+
);
|
|
107
121
|
if (updater === result) {
|
|
108
122
|
runEffects(result);
|
|
109
123
|
return updater;
|
|
@@ -112,13 +126,13 @@ function createUpdater(model, middleWare) {
|
|
|
112
126
|
runEffects(updater);
|
|
113
127
|
return updater;
|
|
114
128
|
},
|
|
115
|
-
update:
|
|
129
|
+
update: (args) => {
|
|
116
130
|
},
|
|
117
|
-
destroy
|
|
131
|
+
destroy() {
|
|
118
132
|
destroy(updater);
|
|
119
133
|
}
|
|
120
134
|
}, unInitializedUpdater);
|
|
121
|
-
|
|
135
|
+
const initialized = createInitializedUpdater(updater, middleWare);
|
|
122
136
|
Object.assign(updater, initialized, {
|
|
123
137
|
update: createUpdateFn(updater, middleWare)
|
|
124
138
|
});
|