as-model 0.1.28 → 0.2.1
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/README.md +558 -558
- package/dist/index.js +148 -139
- package/esm/model/index.js +6 -6
- package/esm/store/cache.js +108 -0
- package/esm/store/enhance/selector.js +1 -107
- package/esm/store/enhance/signal.js +15 -3
- package/esm/store/index.js +11 -6
- package/esm/store/instance.js +36 -25
- package/index.d.ts +324 -322
- package/package.json +68 -68
|
@@ -1,92 +1,3 @@
|
|
|
1
|
-
import { cacheIdentify } from "../instance";
|
|
2
|
-
import { createProxy, shallowEqual } from "../../tools";
|
|
3
|
-
function wrapToField(cache, propertyName, value, onGot) {
|
|
4
|
-
const { cacheFields } = cache;
|
|
5
|
-
if (!cacheIdentify.field(value)) {
|
|
6
|
-
if (onGot) {
|
|
7
|
-
onGot(propertyName, value);
|
|
8
|
-
}
|
|
9
|
-
return value;
|
|
10
|
-
}
|
|
11
|
-
const field = value;
|
|
12
|
-
const cachedField = cacheFields[propertyName];
|
|
13
|
-
if (cachedField) {
|
|
14
|
-
return cachedField.getter;
|
|
15
|
-
}
|
|
16
|
-
const getter = {
|
|
17
|
-
get() {
|
|
18
|
-
const currentField = cache.target[propertyName];
|
|
19
|
-
if (!cacheIdentify.field(currentField)) {
|
|
20
|
-
throw new Error("Field should always be field.");
|
|
21
|
-
}
|
|
22
|
-
const current = currentField.get();
|
|
23
|
-
const fieldInCache = cache.cacheFields[propertyName];
|
|
24
|
-
if (!currentField.deps || fieldInCache == null) {
|
|
25
|
-
cacheFields[propertyName] = {
|
|
26
|
-
getter,
|
|
27
|
-
value: current,
|
|
28
|
-
deps: currentField.deps
|
|
29
|
-
};
|
|
30
|
-
return current;
|
|
31
|
-
}
|
|
32
|
-
if (shallowEqual(currentField.deps, fieldInCache.deps)) {
|
|
33
|
-
return fieldInCache.value;
|
|
34
|
-
}
|
|
35
|
-
cacheFields[propertyName] = {
|
|
36
|
-
getter,
|
|
37
|
-
value: current,
|
|
38
|
-
deps: currentField.deps
|
|
39
|
-
};
|
|
40
|
-
return current;
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
cacheFields[propertyName] = { getter, value: field.value, deps: field.deps };
|
|
44
|
-
return getter;
|
|
45
|
-
}
|
|
46
|
-
function wrapToActionMethod(cache, methodName) {
|
|
47
|
-
const { cacheMethods } = cache;
|
|
48
|
-
const cachedMethod = cacheMethods[methodName];
|
|
49
|
-
if (typeof cachedMethod === "function") {
|
|
50
|
-
return cachedMethod;
|
|
51
|
-
}
|
|
52
|
-
const actionMethod = function actionMethod2(...args) {
|
|
53
|
-
const { target } = cache;
|
|
54
|
-
const method = target[methodName];
|
|
55
|
-
if (typeof method !== "function") {
|
|
56
|
-
throw new Error("Can not change methods in runtime.");
|
|
57
|
-
}
|
|
58
|
-
return method(...args);
|
|
59
|
-
};
|
|
60
|
-
cacheMethods[methodName] = actionMethod;
|
|
61
|
-
return actionMethod;
|
|
62
|
-
}
|
|
63
|
-
const cacheProperties = (cache, onGet) => {
|
|
64
|
-
return function createCachePropertiesProxy() {
|
|
65
|
-
const instance = cache.target;
|
|
66
|
-
const properties = Object.getOwnPropertyNames(instance);
|
|
67
|
-
const handleGetter = function handleGetter2(key, value) {
|
|
68
|
-
if (!onGet) {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
onGet(key, value);
|
|
72
|
-
};
|
|
73
|
-
return createProxy(instance, {
|
|
74
|
-
get(target, p) {
|
|
75
|
-
const value = target[p];
|
|
76
|
-
if (typeof value === "function" && properties.indexOf(p) >= 0) {
|
|
77
|
-
const actionMethod = wrapToActionMethod(cache, p);
|
|
78
|
-
Object.assign(actionMethod, value);
|
|
79
|
-
handleGetter(p, actionMethod);
|
|
80
|
-
return actionMethod;
|
|
81
|
-
}
|
|
82
|
-
return wrapToField(cache, p, value, handleGetter);
|
|
83
|
-
},
|
|
84
|
-
set() {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
};
|
|
89
|
-
};
|
|
90
1
|
function createSelector(store, opts) {
|
|
91
2
|
const { equality } = opts != null ? opts : {};
|
|
92
3
|
const selectStore = {
|
|
@@ -107,27 +18,11 @@ function createSelector(store, opts) {
|
|
|
107
18
|
selectStore.selectedInstance = nextSelectedInstance;
|
|
108
19
|
}
|
|
109
20
|
};
|
|
110
|
-
const propertiesCache = {
|
|
111
|
-
target: store.getInstance(),
|
|
112
|
-
cacheFields: {},
|
|
113
|
-
cacheMethods: {}
|
|
114
|
-
};
|
|
115
21
|
const generateSelectedInstance = function generateSelectedInstance2(getInstance) {
|
|
116
|
-
const { key: storeKey } = store;
|
|
117
22
|
if (cache.selector) {
|
|
118
23
|
return cache.selector(getInstance);
|
|
119
24
|
}
|
|
120
|
-
|
|
121
|
-
return getInstance();
|
|
122
|
-
}
|
|
123
|
-
const result = storeKey == null ? void 0 : storeKey.selector(getInstance);
|
|
124
|
-
if (result == null || typeof result !== "object") {
|
|
125
|
-
throw new Error(
|
|
126
|
-
"The default selector result should be an object or array"
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
propertiesCache.target = result;
|
|
130
|
-
return cacheProperties(propertiesCache)();
|
|
25
|
+
return getInstance();
|
|
131
26
|
};
|
|
132
27
|
selectStore.selectedInstance = generateSelectedInstance(store.getInstance);
|
|
133
28
|
const enhance = (dispatcher) => {
|
|
@@ -160,6 +55,5 @@ function createSelector(store, opts) {
|
|
|
160
55
|
};
|
|
161
56
|
}
|
|
162
57
|
export {
|
|
163
|
-
cacheProperties,
|
|
164
58
|
createSelector
|
|
165
59
|
};
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { extractInstance } from "../instance";
|
|
2
|
+
import { cacheIdentify } from "../cache";
|
|
2
3
|
function createSignal(store) {
|
|
3
4
|
const signalStore = {
|
|
4
5
|
collection: {},
|
|
5
6
|
started: false,
|
|
6
7
|
enabled: false
|
|
7
8
|
};
|
|
9
|
+
const propertiesCache = {
|
|
10
|
+
target: store.getInstance(),
|
|
11
|
+
cacheFields: {},
|
|
12
|
+
cacheMethods: {}
|
|
13
|
+
};
|
|
8
14
|
const enhance = (dispatcher) => {
|
|
9
15
|
return (action) => {
|
|
10
16
|
if (!signalStore.enabled) {
|
|
@@ -15,7 +21,11 @@ function createSignal(store) {
|
|
|
15
21
|
if (collection == null) {
|
|
16
22
|
return;
|
|
17
23
|
}
|
|
18
|
-
const storeInstance = extractInstance(
|
|
24
|
+
const storeInstance = extractInstance(
|
|
25
|
+
store.updater,
|
|
26
|
+
store.key.wrapper,
|
|
27
|
+
propertiesCache
|
|
28
|
+
);
|
|
19
29
|
const keys = Object.keys(collection);
|
|
20
30
|
if (!keys.length) {
|
|
21
31
|
return;
|
|
@@ -53,7 +63,9 @@ function createSignal(store) {
|
|
|
53
63
|
const { cutOff } = options != null ? options : {};
|
|
54
64
|
return extractInstance(
|
|
55
65
|
store.updater,
|
|
56
|
-
|
|
66
|
+
store.key.wrapper,
|
|
67
|
+
propertiesCache,
|
|
68
|
+
{ onGet: cutOff ? void 0 : collectUsedFields }
|
|
57
69
|
);
|
|
58
70
|
};
|
|
59
71
|
const signal = function signal2(options) {
|
package/esm/store/index.js
CHANGED
|
@@ -32,6 +32,7 @@ var __objRest = (source, exclude) => {
|
|
|
32
32
|
import { createUpdater } from "../updater";
|
|
33
33
|
import { isModelKey, isModelUsage } from "../validation";
|
|
34
34
|
import { modelKeyIdentifier, modelStoreIdentifier } from "../identifiers";
|
|
35
|
+
import { noop } from "../tools";
|
|
35
36
|
import {
|
|
36
37
|
extractInstance,
|
|
37
38
|
createField as createInstanceField,
|
|
@@ -42,14 +43,14 @@ function createPrimaryKey(modelFn, config = {}) {
|
|
|
42
43
|
const ifModelKey = isModelKey(modelFn);
|
|
43
44
|
const ifModelUsage = isModelUsage(modelFn);
|
|
44
45
|
const model = ifModelKey ? modelFn.source : modelFn;
|
|
45
|
-
const
|
|
46
|
+
const wrapper = (_a = config.wrapper) != null ? _a : ifModelKey || ifModelUsage ? modelFn.wrapper : function defaultSelector(i) {
|
|
46
47
|
return i();
|
|
47
48
|
};
|
|
48
49
|
const wrapModel = function wrapModel2(state) {
|
|
49
50
|
return model(state);
|
|
50
51
|
};
|
|
51
52
|
wrapModel.source = model;
|
|
52
|
-
wrapModel.
|
|
53
|
+
wrapModel.wrapper = wrapper;
|
|
53
54
|
wrapModel.modelKeyIdentifier = modelKeyIdentifier;
|
|
54
55
|
if ("state" in config) {
|
|
55
56
|
wrapModel.defaultState = config.state;
|
|
@@ -87,8 +88,10 @@ function createStore(modelLike, config = {}) {
|
|
|
87
88
|
};
|
|
88
89
|
const updater = createUpdater(model, combinedMiddleWare, conf);
|
|
89
90
|
const key = modelKey != null ? modelKey : createPrimaryKey(model, config);
|
|
90
|
-
const
|
|
91
|
-
|
|
91
|
+
const propertiesCache = {
|
|
92
|
+
target: updater.instance,
|
|
93
|
+
cacheFields: {},
|
|
94
|
+
cacheMethods: {}
|
|
92
95
|
};
|
|
93
96
|
const store = {
|
|
94
97
|
key,
|
|
@@ -96,11 +99,13 @@ function createStore(modelLike, config = {}) {
|
|
|
96
99
|
return updater.token;
|
|
97
100
|
},
|
|
98
101
|
subscribe(dispatcher) {
|
|
99
|
-
const { connect, disconnect } = updater.createTunnel(dispatcher);
|
|
102
|
+
const { connect, disconnect } = updater.createTunnel(dispatcher || noop);
|
|
100
103
|
connect();
|
|
101
104
|
return disconnect;
|
|
102
105
|
},
|
|
103
|
-
getInstance
|
|
106
|
+
getInstance() {
|
|
107
|
+
return extractInstance(updater, key.wrapper, propertiesCache);
|
|
108
|
+
},
|
|
104
109
|
update(args) {
|
|
105
110
|
const updateArgs = args != null ? args : {};
|
|
106
111
|
if (updateArgs.key) {
|
package/esm/store/instance.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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]);
|
|
6
16
|
}
|
|
7
|
-
|
|
8
|
-
return f.identifier === cacheIdentify.field;
|
|
9
|
-
},
|
|
10
|
-
method(d) {
|
|
11
|
-
if (typeof d !== "function") {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
const m = d;
|
|
15
|
-
return m.identifier === cacheIdentify.method;
|
|
16
|
-
}
|
|
17
|
+
return a;
|
|
17
18
|
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import { createProxy, shallowEqual } from "../tools";
|
|
21
|
+
import { cacheIdentify, cacheProperties } from "./cache";
|
|
18
22
|
function createField(callback, deps) {
|
|
19
23
|
const currentDeps = function computeDeps() {
|
|
20
24
|
if (deps == null) {
|
|
@@ -127,36 +131,43 @@ function wrapToField(updater, propertyName, value, onGot) {
|
|
|
127
131
|
collect(propertyName, getter);
|
|
128
132
|
return getter;
|
|
129
133
|
}
|
|
130
|
-
function extractInstance(updater,
|
|
131
|
-
const {
|
|
132
|
-
if (typeof instance !== "object" || !instance) {
|
|
133
|
-
throw new Error("The instance should be an object or array.");
|
|
134
|
-
}
|
|
135
|
-
const properties = Object.getOwnPropertyNames(instance);
|
|
134
|
+
function extractInstance(updater, wrapper, cache, opts) {
|
|
135
|
+
const { onGet } = opts || {};
|
|
136
136
|
const handleGetter = function handleGetter2(key, value) {
|
|
137
137
|
if (!onGet) {
|
|
138
138
|
return;
|
|
139
139
|
}
|
|
140
140
|
onGet(key, value);
|
|
141
141
|
};
|
|
142
|
-
|
|
142
|
+
const { instance } = updater;
|
|
143
|
+
if (typeof instance !== "object" || !instance) {
|
|
144
|
+
throw new Error("The instance should be an object or array.");
|
|
145
|
+
}
|
|
146
|
+
const properties = Object.getOwnPropertyNames(instance);
|
|
147
|
+
const proxiedInstance = createProxy(instance, {
|
|
143
148
|
get(target, p) {
|
|
144
149
|
const value = target[p];
|
|
145
150
|
if (typeof value === "function" && properties.indexOf(p) >= 0) {
|
|
146
151
|
const actionMethod = wrapToActionMethod(updater, p);
|
|
147
152
|
Object.assign(actionMethod, value);
|
|
148
|
-
handleGetter(p, actionMethod);
|
|
149
153
|
return actionMethod;
|
|
150
154
|
}
|
|
151
|
-
return wrapToField(updater, p, value
|
|
155
|
+
return wrapToField(updater, p, value);
|
|
152
156
|
},
|
|
153
157
|
set() {
|
|
154
158
|
return false;
|
|
155
159
|
}
|
|
156
160
|
});
|
|
161
|
+
function generateInstance() {
|
|
162
|
+
return proxiedInstance;
|
|
163
|
+
}
|
|
164
|
+
const wrapped = wrapper(generateInstance);
|
|
165
|
+
if (typeof wrapped === "object" && wrapped != null) {
|
|
166
|
+
return cacheProperties(__spreadProps(__spreadValues({}, cache), { target: wrapped }), handleGetter)();
|
|
167
|
+
}
|
|
168
|
+
return wrapped;
|
|
157
169
|
}
|
|
158
170
|
export {
|
|
159
|
-
cacheIdentify,
|
|
160
171
|
createField,
|
|
161
172
|
createMethod,
|
|
162
173
|
extractInstance
|