as-model 0.1.0 → 0.1.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/dist/index.js +382 -165
- package/esm/identifiers/index.js +10 -0
- package/esm/index.js +2 -1
- package/esm/key/index.js +3 -3
- package/esm/model/index.js +22 -7
- package/esm/store/enhance/index.js +6 -0
- package/esm/store/enhance/selector.js +169 -0
- package/esm/store/enhance/signal.js +81 -0
- package/esm/store/index.js +43 -97
- package/esm/store/instance.js +2 -30
- package/esm/tools/index.js +10 -0
- package/esm/tools/proxy.js +43 -0
- package/esm/tools/shallowEqual.js +34 -0
- package/esm/updater/index.js +8 -8
- package/esm/updater/notifier.js +40 -19
- package/esm/validation/index.js +15 -0
- package/package.json +1 -1
package/esm/index.js
CHANGED
|
@@ -36,11 +36,12 @@ function config() {
|
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
38
|
var _config = config(), createStore = _config.createStore, createKey = _config.createKey, createStores = _config.createStores, model = _config.model;
|
|
39
|
-
import { createSignal } from "./store";
|
|
39
|
+
import { createSignal, createSelector } from "./store";
|
|
40
40
|
import { validations } from "./validation";
|
|
41
41
|
export {
|
|
42
42
|
config,
|
|
43
43
|
createKey,
|
|
44
|
+
createSelector,
|
|
44
45
|
createSignal,
|
|
45
46
|
createStore,
|
|
46
47
|
createStores,
|
package/esm/key/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
2
|
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
3
|
-
import { createStore, createPrimaryKey
|
|
3
|
+
import { createStore, createPrimaryKey } from "../store";
|
|
4
|
+
import { isModelKey } from "../validation";
|
|
4
5
|
function createKey(model) {
|
|
5
6
|
var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
6
|
-
var
|
|
7
|
-
var wrapModel = hasDefaultState ? createPrimaryKey(model, config.state) : createPrimaryKey(model);
|
|
7
|
+
var wrapModel = createPrimaryKey(model, config);
|
|
8
8
|
wrapModel.createStore = function createKeyStore() {
|
|
9
9
|
var storeConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
10
10
|
return createStore(wrapModel, _object_spread({}, config, storeConfig));
|
package/esm/model/index.js
CHANGED
|
@@ -2,21 +2,36 @@ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
|
2
2
|
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
3
3
|
import { createStore, createField, createMethod } from "../store";
|
|
4
4
|
import { createKey } from "../key";
|
|
5
|
+
import { modelUsageIdentifier } from "../identifiers";
|
|
5
6
|
function configModel(config) {
|
|
6
|
-
var model = function model2(modelFn) {
|
|
7
|
+
var model = function model2(modelFn, selector) {
|
|
8
|
+
var currentSelector = selector !== null && selector !== void 0 ? selector : function defaultSelector(i) {
|
|
9
|
+
return i();
|
|
10
|
+
};
|
|
7
11
|
var modelWrapper = function modelWrapper2(state) {
|
|
8
12
|
return modelFn(state);
|
|
9
13
|
};
|
|
14
|
+
modelWrapper.select = function select(s) {
|
|
15
|
+
return model2(modelFn, s);
|
|
16
|
+
};
|
|
10
17
|
modelWrapper.createKey = function createModelKey(state) {
|
|
11
|
-
return createKey(
|
|
12
|
-
state
|
|
13
|
-
|
|
18
|
+
return createKey(modelFn, arguments.length ? _object_spread_props(_object_spread({}, config), {
|
|
19
|
+
state,
|
|
20
|
+
selector: currentSelector
|
|
21
|
+
}) : _object_spread_props(_object_spread({}, config), {
|
|
22
|
+
selector: currentSelector
|
|
23
|
+
}));
|
|
14
24
|
};
|
|
15
25
|
modelWrapper.createStore = function createModelStore(state) {
|
|
16
|
-
return createStore(
|
|
17
|
-
state
|
|
18
|
-
|
|
26
|
+
return createStore(modelFn, arguments.length ? _object_spread_props(_object_spread({}, config), {
|
|
27
|
+
state,
|
|
28
|
+
selector: currentSelector
|
|
29
|
+
}) : _object_spread_props(_object_spread({}, config), {
|
|
30
|
+
selector: currentSelector
|
|
31
|
+
}));
|
|
19
32
|
};
|
|
33
|
+
modelWrapper.selector = currentSelector;
|
|
34
|
+
modelWrapper.modelUsageIdentifier = modelUsageIdentifier;
|
|
20
35
|
return modelWrapper;
|
|
21
36
|
};
|
|
22
37
|
model.createField = createField;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
2
|
+
import { _ as _type_of } from "@swc/helpers/_/_type_of";
|
|
3
|
+
import { cacheIdentify } from "../instance";
|
|
4
|
+
import { createProxy, shallowEqual } from "../../tools";
|
|
5
|
+
function wrapToField(cache, propertyName, value, onGot) {
|
|
6
|
+
var cacheFields = cache.cacheFields;
|
|
7
|
+
if (!cacheIdentify.field(value)) {
|
|
8
|
+
if (onGot) {
|
|
9
|
+
onGot(propertyName, value);
|
|
10
|
+
}
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
var field = value;
|
|
14
|
+
var cachedField = cacheFields[propertyName];
|
|
15
|
+
if (cachedField) {
|
|
16
|
+
return cachedField.getter;
|
|
17
|
+
}
|
|
18
|
+
var getter = {
|
|
19
|
+
get: function get() {
|
|
20
|
+
var currentField = cache.target[propertyName];
|
|
21
|
+
if (!cacheIdentify.field(currentField)) {
|
|
22
|
+
throw new Error("Field should always be field.");
|
|
23
|
+
}
|
|
24
|
+
var current = currentField.get();
|
|
25
|
+
var fieldInCache = cache.cacheFields[propertyName];
|
|
26
|
+
if (!currentField.deps || fieldInCache == null) {
|
|
27
|
+
cacheFields[propertyName] = {
|
|
28
|
+
getter,
|
|
29
|
+
value: current,
|
|
30
|
+
deps: currentField.deps
|
|
31
|
+
};
|
|
32
|
+
return current;
|
|
33
|
+
}
|
|
34
|
+
if (shallowEqual(currentField.deps, fieldInCache.deps)) {
|
|
35
|
+
return fieldInCache.value;
|
|
36
|
+
}
|
|
37
|
+
cacheFields[propertyName] = {
|
|
38
|
+
getter,
|
|
39
|
+
value: current,
|
|
40
|
+
deps: currentField.deps
|
|
41
|
+
};
|
|
42
|
+
return current;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
cacheFields[propertyName] = {
|
|
46
|
+
getter,
|
|
47
|
+
value: field.value,
|
|
48
|
+
deps: field.deps
|
|
49
|
+
};
|
|
50
|
+
return getter;
|
|
51
|
+
}
|
|
52
|
+
function wrapToActionMethod(cache, methodName) {
|
|
53
|
+
var cacheMethods = cache.cacheMethods;
|
|
54
|
+
var cachedMethod = cacheMethods[methodName];
|
|
55
|
+
if (typeof cachedMethod === "function") {
|
|
56
|
+
return cachedMethod;
|
|
57
|
+
}
|
|
58
|
+
var actionMethod = function actionMethod2() {
|
|
59
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
60
|
+
args[_key] = arguments[_key];
|
|
61
|
+
}
|
|
62
|
+
var target = cache.target;
|
|
63
|
+
var method = target[methodName];
|
|
64
|
+
if (typeof method !== "function") {
|
|
65
|
+
throw new Error("Can not change methods in runtime.");
|
|
66
|
+
}
|
|
67
|
+
return method.apply(void 0, _to_consumable_array(args));
|
|
68
|
+
};
|
|
69
|
+
cacheMethods[methodName] = actionMethod;
|
|
70
|
+
return actionMethod;
|
|
71
|
+
}
|
|
72
|
+
var cacheProperties = function(cache, onGet) {
|
|
73
|
+
return function createCachePropertiesProxy() {
|
|
74
|
+
var instance = cache.target;
|
|
75
|
+
var properties = Object.getOwnPropertyNames(instance);
|
|
76
|
+
var handleGetter = function handleGetter2(key, value) {
|
|
77
|
+
if (!onGet) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
onGet(key, value);
|
|
81
|
+
};
|
|
82
|
+
return createProxy(instance, {
|
|
83
|
+
get: function get(target, p) {
|
|
84
|
+
var value = target[p];
|
|
85
|
+
if (typeof value === "function" && properties.indexOf(p) >= 0) {
|
|
86
|
+
var actionMethod = wrapToActionMethod(cache, p);
|
|
87
|
+
Object.assign(actionMethod, value);
|
|
88
|
+
handleGetter(p, actionMethod);
|
|
89
|
+
return actionMethod;
|
|
90
|
+
}
|
|
91
|
+
return wrapToField(cache, p, value, handleGetter);
|
|
92
|
+
},
|
|
93
|
+
set: function set() {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
function createSelector(store, opts) {
|
|
100
|
+
var equality = (opts !== null && opts !== void 0 ? opts : {}).equality;
|
|
101
|
+
var selectStore = {
|
|
102
|
+
selectedInstance: store.getInstance()
|
|
103
|
+
};
|
|
104
|
+
var cache = {
|
|
105
|
+
equality,
|
|
106
|
+
setSelect: function setSelect(selector) {
|
|
107
|
+
cache.selector = selector;
|
|
108
|
+
if (!selector) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
var currentSelectedInstance = selectStore.selectedInstance;
|
|
112
|
+
var nextSelectedInstance = selector(store.getInstance);
|
|
113
|
+
if (currentSelectedInstance === nextSelectedInstance || equality && equality(currentSelectedInstance, nextSelectedInstance)) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
selectStore.selectedInstance = nextSelectedInstance;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
var propertiesCache = {
|
|
120
|
+
target: store.getInstance(),
|
|
121
|
+
cacheFields: {},
|
|
122
|
+
cacheMethods: {}
|
|
123
|
+
};
|
|
124
|
+
var generateSelectedInstance = function generateSelectedInstance2(getInstance) {
|
|
125
|
+
var storeKey = store.key;
|
|
126
|
+
if (cache.selector) {
|
|
127
|
+
return cache.selector(getInstance);
|
|
128
|
+
}
|
|
129
|
+
if (!(storeKey === null || storeKey === void 0 ? void 0 : storeKey.selector)) {
|
|
130
|
+
return getInstance();
|
|
131
|
+
}
|
|
132
|
+
var result = storeKey === null || storeKey === void 0 ? void 0 : storeKey.selector(getInstance);
|
|
133
|
+
if (result == null || (typeof result === "undefined" ? "undefined" : _type_of(result)) !== "object") {
|
|
134
|
+
throw new Error("The default selector result should be an object or array");
|
|
135
|
+
}
|
|
136
|
+
propertiesCache.target = result;
|
|
137
|
+
return cacheProperties(propertiesCache)();
|
|
138
|
+
};
|
|
139
|
+
selectStore.selectedInstance = generateSelectedInstance(store.getInstance);
|
|
140
|
+
var enhance = function(dispatcher) {
|
|
141
|
+
return function(action) {
|
|
142
|
+
var currentSelectedInstance = selectStore.selectedInstance;
|
|
143
|
+
var nextSelectedInstance = generateSelectedInstance(store.getInstance);
|
|
144
|
+
if (currentSelectedInstance === nextSelectedInstance || cache.equality && cache.equality(currentSelectedInstance, nextSelectedInstance)) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
selectStore.selectedInstance = nextSelectedInstance;
|
|
148
|
+
if (dispatcher == null) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
dispatcher(action);
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
function select(selector) {
|
|
155
|
+
cache.setSelect(selector);
|
|
156
|
+
return selectStore.selectedInstance;
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
key: store.key,
|
|
160
|
+
subscribe: function subscribe(dispatcher) {
|
|
161
|
+
return store.subscribe(enhance(dispatcher));
|
|
162
|
+
},
|
|
163
|
+
select
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
export {
|
|
167
|
+
cacheProperties,
|
|
168
|
+
createSelector
|
|
169
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { cacheIdentify, extractInstance } from "../instance";
|
|
2
|
+
import { shallowEqual } from "../../tools";
|
|
3
|
+
function createSignal(store) {
|
|
4
|
+
var signalStore = {
|
|
5
|
+
collection: null,
|
|
6
|
+
started: false,
|
|
7
|
+
enabled: false
|
|
8
|
+
};
|
|
9
|
+
var enhance = function(dispatcher) {
|
|
10
|
+
return function(action) {
|
|
11
|
+
if (!signalStore.enabled) {
|
|
12
|
+
dispatcher === null || dispatcher === void 0 ? void 0 : dispatcher(action);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
var collection = signalStore.collection;
|
|
16
|
+
if (collection == null) {
|
|
17
|
+
dispatcher === null || dispatcher === void 0 ? void 0 : dispatcher(action);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
var current = extractInstance(store.updater);
|
|
21
|
+
var keys = Object.keys(collection);
|
|
22
|
+
var currentCollectionEntries = keys.map(function(key) {
|
|
23
|
+
var field = current[key];
|
|
24
|
+
if (cacheIdentify.field(field)) {
|
|
25
|
+
return [
|
|
26
|
+
key,
|
|
27
|
+
field.get()
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
return [
|
|
31
|
+
key,
|
|
32
|
+
field
|
|
33
|
+
];
|
|
34
|
+
});
|
|
35
|
+
var currentCollection = Object.fromEntries(currentCollectionEntries);
|
|
36
|
+
if (!shallowEqual(collection, currentCollection)) {
|
|
37
|
+
dispatcher === null || dispatcher === void 0 ? void 0 : dispatcher(action);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
var storeKey = store.key;
|
|
42
|
+
return {
|
|
43
|
+
key: storeKey,
|
|
44
|
+
subscribe: function subscribe(dispatcher) {
|
|
45
|
+
return store.subscribe(enhance(dispatcher));
|
|
46
|
+
},
|
|
47
|
+
getSignal: function getSignal() {
|
|
48
|
+
var collectUsedFields = function collectUsedFields2(key, val) {
|
|
49
|
+
if (!signalStore.started) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
signalStore.collection = signalStore.collection || {};
|
|
53
|
+
signalStore.collection[key] = val;
|
|
54
|
+
};
|
|
55
|
+
var getInstance = function getInstance2() {
|
|
56
|
+
return extractInstance(store.updater, collectUsedFields);
|
|
57
|
+
};
|
|
58
|
+
var signal = function signal2() {
|
|
59
|
+
return getInstance();
|
|
60
|
+
};
|
|
61
|
+
signal.startStatistics = function startStatistics() {
|
|
62
|
+
signalStore.started = true;
|
|
63
|
+
};
|
|
64
|
+
signal.stopStatistics = function stopStatistics() {
|
|
65
|
+
signalStore.started = false;
|
|
66
|
+
};
|
|
67
|
+
signal.subscribe = function subscribe(dispatchCallback) {
|
|
68
|
+
return store.subscribe(dispatchCallback);
|
|
69
|
+
};
|
|
70
|
+
signal.payload = function payload(callback) {
|
|
71
|
+
return store.payload(callback);
|
|
72
|
+
};
|
|
73
|
+
signalStore.enabled = true;
|
|
74
|
+
signalStore.started = true;
|
|
75
|
+
return signal;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
createSignal
|
|
81
|
+
};
|
package/esm/store/index.js
CHANGED
|
@@ -1,33 +1,35 @@
|
|
|
1
1
|
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
2
|
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
3
|
+
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
3
4
|
import { createUpdater } from "../updater";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var model = isModelKey(modelFn) ? modelFn.source : modelFn;
|
|
5
|
+
import { isModelKey, isModelUsage } from "../validation";
|
|
6
|
+
import { modelKeyIdentifier } from "../identifiers";
|
|
7
|
+
import { extractInstance, createField as createInstanceField, createMethod as createInstanceMethod } from "./instance";
|
|
8
|
+
function createPrimaryKey(modelFn) {
|
|
9
|
+
var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
10
|
+
var ifModelKey = isModelKey(modelFn);
|
|
11
|
+
var ifModelUsage = isModelUsage(modelFn);
|
|
12
|
+
var model = ifModelKey ? modelFn.source : modelFn;
|
|
13
|
+
var _config_selector;
|
|
14
|
+
var selector = (_config_selector = config.selector) !== null && _config_selector !== void 0 ? _config_selector : ifModelKey || ifModelUsage ? modelFn.selector : function defaultSelector(i) {
|
|
15
|
+
return i();
|
|
16
|
+
};
|
|
17
17
|
var wrapModel = function wrapModel2(state) {
|
|
18
18
|
return model(state);
|
|
19
19
|
};
|
|
20
20
|
wrapModel.source = model;
|
|
21
|
+
wrapModel.selector = selector;
|
|
21
22
|
wrapModel.modelKeyIdentifier = modelKeyIdentifier;
|
|
22
|
-
if (
|
|
23
|
-
wrapModel.defaultState =
|
|
23
|
+
if ("state" in config) {
|
|
24
|
+
wrapModel.defaultState = config.state;
|
|
24
25
|
}
|
|
25
26
|
return wrapModel;
|
|
26
27
|
}
|
|
27
28
|
function createStore(modelLike) {
|
|
28
29
|
var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
29
|
-
var
|
|
30
|
-
var
|
|
30
|
+
var ifModelKey = isModelKey(modelLike);
|
|
31
|
+
var model = ifModelKey ? modelLike.source : modelLike;
|
|
32
|
+
var modelKey = ifModelKey ? modelLike : void 0;
|
|
31
33
|
var conf = function computeConfig() {
|
|
32
34
|
var hasConfigState = "state" in config;
|
|
33
35
|
var hasKeyState = !!modelKey && "defaultState" in modelKey;
|
|
@@ -41,17 +43,33 @@ function createStore(modelLike) {
|
|
|
41
43
|
}
|
|
42
44
|
return config;
|
|
43
45
|
}();
|
|
44
|
-
var
|
|
46
|
+
var combinedMiddleWare = function combinedMiddleWare2(s) {
|
|
47
|
+
return function updaterMiddleWare(next) {
|
|
48
|
+
var middleWares = conf.middleWares;
|
|
49
|
+
if (middleWares == null) {
|
|
50
|
+
return next;
|
|
51
|
+
}
|
|
52
|
+
var updateMiddleWares = _to_consumable_array(middleWares).reverse().map(function(middleWare) {
|
|
53
|
+
return middleWare(s);
|
|
54
|
+
});
|
|
55
|
+
return updateMiddleWares.reduce(function(finalDispatcher, um) {
|
|
56
|
+
return um(finalDispatcher);
|
|
57
|
+
}, next);
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
var updater = createUpdater(model, combinedMiddleWare, conf);
|
|
61
|
+
var key = modelKey !== null && modelKey !== void 0 ? modelKey : createPrimaryKey(model, config);
|
|
62
|
+
var getInstance = function getInstance2() {
|
|
63
|
+
return extractInstance(updater);
|
|
64
|
+
};
|
|
45
65
|
var store = {
|
|
46
|
-
key
|
|
66
|
+
key,
|
|
47
67
|
subscribe: function subscribe(dispatcher) {
|
|
48
68
|
var _updater_createTunnel = updater.createTunnel(dispatcher), connect = _updater_createTunnel.connect, disconnect = _updater_createTunnel.disconnect;
|
|
49
69
|
connect();
|
|
50
70
|
return disconnect;
|
|
51
71
|
},
|
|
52
|
-
getInstance
|
|
53
|
-
return extractInstance(updater);
|
|
54
|
-
},
|
|
72
|
+
getInstance,
|
|
55
73
|
update: function update(args) {
|
|
56
74
|
updater.update(args);
|
|
57
75
|
},
|
|
@@ -68,86 +86,14 @@ function createStore(modelLike) {
|
|
|
68
86
|
};
|
|
69
87
|
return store;
|
|
70
88
|
}
|
|
71
|
-
function createSignal(store) {
|
|
72
|
-
var signalStore = {
|
|
73
|
-
collection: null,
|
|
74
|
-
started: false,
|
|
75
|
-
enabled: false
|
|
76
|
-
};
|
|
77
|
-
var middleWare = function(dispatcher) {
|
|
78
|
-
return function(action) {
|
|
79
|
-
if (!signalStore.enabled) {
|
|
80
|
-
dispatcher(action);
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
var collection = signalStore.collection;
|
|
84
|
-
if (collection == null) {
|
|
85
|
-
dispatcher(action);
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
var current = extractInstance(store.updater);
|
|
89
|
-
var keys = Object.keys(collection);
|
|
90
|
-
var currentCollectionEntries = keys.map(function(key) {
|
|
91
|
-
var field = current[key];
|
|
92
|
-
if (cacheIdentify.field(field)) {
|
|
93
|
-
return [
|
|
94
|
-
key,
|
|
95
|
-
field.get()
|
|
96
|
-
];
|
|
97
|
-
}
|
|
98
|
-
return [
|
|
99
|
-
key,
|
|
100
|
-
field
|
|
101
|
-
];
|
|
102
|
-
});
|
|
103
|
-
var currentCollection = Object.fromEntries(currentCollectionEntries);
|
|
104
|
-
if (!shallowEqual(collection, currentCollection)) {
|
|
105
|
-
dispatcher(action);
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
};
|
|
109
|
-
return {
|
|
110
|
-
key: store.key,
|
|
111
|
-
subscribe: function subscribe(dispatcher) {
|
|
112
|
-
return store.subscribe(middleWare(dispatcher));
|
|
113
|
-
},
|
|
114
|
-
getSignal: function getSignal() {
|
|
115
|
-
var collectUsedFields = function collectUsedFields2(key, val) {
|
|
116
|
-
if (!signalStore.started) {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
signalStore.collection = signalStore.collection || {};
|
|
120
|
-
signalStore.collection[key] = val;
|
|
121
|
-
};
|
|
122
|
-
var signal = function signal2() {
|
|
123
|
-
return extractInstance(store.updater, collectUsedFields);
|
|
124
|
-
};
|
|
125
|
-
signal.startStatistics = function startStatistics() {
|
|
126
|
-
signalStore.started = true;
|
|
127
|
-
};
|
|
128
|
-
signal.stopStatistics = function stopStatistics() {
|
|
129
|
-
signalStore.started = false;
|
|
130
|
-
};
|
|
131
|
-
signal.subscribe = function subscribe(dispatchCallback) {
|
|
132
|
-
return store.subscribe(dispatchCallback);
|
|
133
|
-
};
|
|
134
|
-
signal.payload = function payload(callback) {
|
|
135
|
-
return store.payload(callback);
|
|
136
|
-
};
|
|
137
|
-
signalStore.enabled = true;
|
|
138
|
-
signalStore.started = true;
|
|
139
|
-
return signal;
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
89
|
var createField = createInstanceField;
|
|
144
90
|
var createMethod = createInstanceMethod;
|
|
91
|
+
import { createSignal, createSelector } from "./enhance";
|
|
145
92
|
export {
|
|
146
93
|
createField,
|
|
147
94
|
createMethod,
|
|
148
95
|
createPrimaryKey,
|
|
96
|
+
createSelector,
|
|
149
97
|
createSignal,
|
|
150
|
-
createStore
|
|
151
|
-
isModelKey,
|
|
152
|
-
modelKeyIdentifier
|
|
98
|
+
createStore
|
|
153
99
|
};
|
package/esm/store/instance.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
-
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
3
1
|
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
4
2
|
import { _ as _type_of } from "@swc/helpers/_/_type_of";
|
|
5
3
|
import { createProxy, shallowEqual } from "../tools";
|
|
@@ -68,7 +66,7 @@ function wrapToActionMethod(updater, methodName) {
|
|
|
68
66
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
69
67
|
args[_key] = arguments[_key];
|
|
70
68
|
}
|
|
71
|
-
var instance = updater.instance,
|
|
69
|
+
var instance = updater.instance, model = updater.model;
|
|
72
70
|
var method2 = instance[methodName];
|
|
73
71
|
if (typeof method2 !== "function") {
|
|
74
72
|
throw new Error("Can not change methods in runtime.");
|
|
@@ -77,9 +75,6 @@ function wrapToActionMethod(updater, methodName) {
|
|
|
77
75
|
return method2.apply(void 0, _to_consumable_array(args));
|
|
78
76
|
}
|
|
79
77
|
var state = method2.apply(void 0, _to_consumable_array(args));
|
|
80
|
-
if (isDestroyed) {
|
|
81
|
-
return state;
|
|
82
|
-
}
|
|
83
78
|
var action = {
|
|
84
79
|
type: methodName,
|
|
85
80
|
state,
|
|
@@ -88,30 +83,7 @@ function wrapToActionMethod(updater, methodName) {
|
|
|
88
83
|
prevInstance: updater.instance,
|
|
89
84
|
method: actionMethod2
|
|
90
85
|
};
|
|
91
|
-
|
|
92
|
-
updater.notify(action);
|
|
93
|
-
return state;
|
|
94
|
-
}
|
|
95
|
-
var prevState = updater.state;
|
|
96
|
-
var prevInstance = updater.instance;
|
|
97
|
-
var newestInstance = model(state);
|
|
98
|
-
updater.mutate(function(up, effect) {
|
|
99
|
-
effect(function(u) {
|
|
100
|
-
u.notify({
|
|
101
|
-
type: methodName,
|
|
102
|
-
state,
|
|
103
|
-
prevState,
|
|
104
|
-
instance: newestInstance,
|
|
105
|
-
prevInstance,
|
|
106
|
-
method: actionMethod2
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
return _object_spread_props(_object_spread({}, up), {
|
|
110
|
-
instance: newestInstance,
|
|
111
|
-
state,
|
|
112
|
-
version: up.version + 1
|
|
113
|
-
});
|
|
114
|
-
});
|
|
86
|
+
updater.notify(action);
|
|
115
87
|
return state;
|
|
116
88
|
};
|
|
117
89
|
cacheMethods[methodName] = actionMethod;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
+
function getDescriptors(target, receiver, ownOrPrototype, handler) {
|
|
3
|
+
var it = Object.keys(ownOrPrototype);
|
|
4
|
+
var result = {};
|
|
5
|
+
it.forEach(function(key) {
|
|
6
|
+
result[key] = {
|
|
7
|
+
get: function() {
|
|
8
|
+
if (!handler.get) {
|
|
9
|
+
return target[key];
|
|
10
|
+
}
|
|
11
|
+
return handler.get(target, key, receiver);
|
|
12
|
+
},
|
|
13
|
+
set: function(v) {
|
|
14
|
+
if (!handler.set) {
|
|
15
|
+
target[key] = v;
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
var valid = handler.set(target, key, v, receiver);
|
|
19
|
+
if (!valid) {
|
|
20
|
+
throw new Error("".concat(key, " in proxy target is not mutable"));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
var createSimpleProxy = function(target, handler) {
|
|
28
|
+
var proxy = {};
|
|
29
|
+
var own = getDescriptors(target, proxy, target, handler);
|
|
30
|
+
var prototype = getDescriptors(target, proxy, Object.getPrototypeOf(target), handler);
|
|
31
|
+
Object.defineProperties(proxy, _object_spread({}, prototype, own));
|
|
32
|
+
return proxy;
|
|
33
|
+
};
|
|
34
|
+
var createProxy = function(target, handler) {
|
|
35
|
+
if (typeof Proxy !== "function") {
|
|
36
|
+
return createSimpleProxy(target, handler);
|
|
37
|
+
}
|
|
38
|
+
return new Proxy(target, handler);
|
|
39
|
+
};
|
|
40
|
+
export {
|
|
41
|
+
createProxy,
|
|
42
|
+
createSimpleProxy
|
|
43
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { _ as _type_of } from "@swc/helpers/_/_type_of";
|
|
2
|
+
function isObject(data) {
|
|
3
|
+
return data && (typeof data === "undefined" ? "undefined" : _type_of(data)) === "object";
|
|
4
|
+
}
|
|
5
|
+
function shallowEqual(prev, current) {
|
|
6
|
+
if (Object.is(prev, current)) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
if (!isObject(prev) || !isObject(current)) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
var prevKeys = Object.keys(prev);
|
|
13
|
+
var currentKeys = Object.keys(current);
|
|
14
|
+
if (prevKeys.length !== currentKeys.length) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
var pre = prev;
|
|
18
|
+
var curr = current;
|
|
19
|
+
var hasDiffKey = prevKeys.some(function(key) {
|
|
20
|
+
return !Object.prototype.hasOwnProperty.call(curr, key);
|
|
21
|
+
});
|
|
22
|
+
if (hasDiffKey) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
var hasDiffValue = currentKeys.some(function(key) {
|
|
26
|
+
var currentValue = curr[key];
|
|
27
|
+
var prevValue = pre[key];
|
|
28
|
+
return !Object.is(currentValue, prevValue);
|
|
29
|
+
});
|
|
30
|
+
return !hasDiffValue;
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
shallowEqual
|
|
34
|
+
};
|