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
package/dist/index.js
CHANGED
|
@@ -747,7 +747,7 @@
|
|
|
747
747
|
return updater;
|
|
748
748
|
}
|
|
749
749
|
|
|
750
|
-
// src/store/
|
|
750
|
+
// src/store/cache.ts
|
|
751
751
|
var cacheIdentify = {
|
|
752
752
|
field: function field(d) {
|
|
753
753
|
if (!d) {
|
|
@@ -764,6 +764,102 @@
|
|
|
764
764
|
return m.identifier === cacheIdentify.method;
|
|
765
765
|
}
|
|
766
766
|
};
|
|
767
|
+
function wrapToField(cache, propertyName, value, onGot) {
|
|
768
|
+
var cacheFields = cache.cacheFields;
|
|
769
|
+
if (!cacheIdentify.field(value)) {
|
|
770
|
+
if (onGot) {
|
|
771
|
+
onGot(propertyName, value);
|
|
772
|
+
}
|
|
773
|
+
return value;
|
|
774
|
+
}
|
|
775
|
+
var field2 = value;
|
|
776
|
+
var cachedField = cacheFields[propertyName];
|
|
777
|
+
if (cachedField) {
|
|
778
|
+
return cachedField.getter;
|
|
779
|
+
}
|
|
780
|
+
var getter = {
|
|
781
|
+
get: function get() {
|
|
782
|
+
var currentField = cache.target[propertyName];
|
|
783
|
+
if (!cacheIdentify.field(currentField)) {
|
|
784
|
+
throw new Error("Field should always be field.");
|
|
785
|
+
}
|
|
786
|
+
var current = currentField.get();
|
|
787
|
+
var fieldInCache = cache.cacheFields[propertyName];
|
|
788
|
+
if (!currentField.deps || fieldInCache == null) {
|
|
789
|
+
cacheFields[propertyName] = {
|
|
790
|
+
getter,
|
|
791
|
+
value: current,
|
|
792
|
+
deps: currentField.deps
|
|
793
|
+
};
|
|
794
|
+
return current;
|
|
795
|
+
}
|
|
796
|
+
if (shallowEqual(currentField.deps, fieldInCache.deps)) {
|
|
797
|
+
return fieldInCache.value;
|
|
798
|
+
}
|
|
799
|
+
cacheFields[propertyName] = {
|
|
800
|
+
getter,
|
|
801
|
+
value: current,
|
|
802
|
+
deps: currentField.deps
|
|
803
|
+
};
|
|
804
|
+
return current;
|
|
805
|
+
}
|
|
806
|
+
};
|
|
807
|
+
cacheFields[propertyName] = {
|
|
808
|
+
getter,
|
|
809
|
+
value: field2.value,
|
|
810
|
+
deps: field2.deps
|
|
811
|
+
};
|
|
812
|
+
return getter;
|
|
813
|
+
}
|
|
814
|
+
function wrapToActionMethod(cache, methodName) {
|
|
815
|
+
var cacheMethods = cache.cacheMethods;
|
|
816
|
+
var cachedMethod = cacheMethods[methodName];
|
|
817
|
+
if (typeof cachedMethod === "function") {
|
|
818
|
+
return cachedMethod;
|
|
819
|
+
}
|
|
820
|
+
var actionMethod = function actionMethod2() {
|
|
821
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
822
|
+
args[_key] = arguments[_key];
|
|
823
|
+
}
|
|
824
|
+
var target = cache.target;
|
|
825
|
+
var method2 = target[methodName];
|
|
826
|
+
if (typeof method2 !== "function") {
|
|
827
|
+
throw new Error("Can not change methods in runtime.");
|
|
828
|
+
}
|
|
829
|
+
return method2.apply(void 0, _to_consumable_array(args));
|
|
830
|
+
};
|
|
831
|
+
cacheMethods[methodName] = actionMethod;
|
|
832
|
+
return actionMethod;
|
|
833
|
+
}
|
|
834
|
+
var cacheProperties = function(cache, onGet) {
|
|
835
|
+
return function createCachePropertiesProxy() {
|
|
836
|
+
var instance = cache.target;
|
|
837
|
+
var properties = Object.getOwnPropertyNames(instance);
|
|
838
|
+
var handleGetter = function handleGetter2(key, value) {
|
|
839
|
+
if (!onGet) {
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
onGet(key, value);
|
|
843
|
+
};
|
|
844
|
+
return createProxy(instance, {
|
|
845
|
+
get: function get(target, p) {
|
|
846
|
+
var value = target[p];
|
|
847
|
+
if (typeof value === "function" && properties.indexOf(p) >= 0) {
|
|
848
|
+
var actionMethod = wrapToActionMethod(cache, p);
|
|
849
|
+
Object.assign(actionMethod, value);
|
|
850
|
+
handleGetter(p, actionMethod);
|
|
851
|
+
return actionMethod;
|
|
852
|
+
}
|
|
853
|
+
return wrapToField(cache, p, value, handleGetter);
|
|
854
|
+
},
|
|
855
|
+
set: function set() {
|
|
856
|
+
return false;
|
|
857
|
+
}
|
|
858
|
+
});
|
|
859
|
+
};
|
|
860
|
+
};
|
|
861
|
+
|
|
862
|
+
// src/store/instance.ts
|
|
767
863
|
function createField(callback, deps) {
|
|
768
864
|
var currentDeps = function computeDeps() {
|
|
769
865
|
if (deps == null) {
|
|
@@ -802,7 +898,7 @@
|
|
|
802
898
|
replace.identifier = cacheIdentify.method;
|
|
803
899
|
return replace;
|
|
804
900
|
}
|
|
805
|
-
function
|
|
901
|
+
function wrapToActionMethod2(updater, methodName) {
|
|
806
902
|
var cacheMethods = updater.cacheMethods;
|
|
807
903
|
var cachedMethod = cacheMethods[methodName];
|
|
808
904
|
if (typeof cachedMethod === "function") {
|
|
@@ -835,7 +931,7 @@
|
|
|
835
931
|
cacheMethods[methodName] = actionMethod;
|
|
836
932
|
return actionMethod;
|
|
837
933
|
}
|
|
838
|
-
function
|
|
934
|
+
function wrapToField2(updater, propertyName, value, onGot) {
|
|
839
935
|
function collect(pName, v) {
|
|
840
936
|
if (onGot) {
|
|
841
937
|
onGot(pName, v);
|
|
@@ -888,33 +984,43 @@
|
|
|
888
984
|
collect(propertyName, getter);
|
|
889
985
|
return getter;
|
|
890
986
|
}
|
|
891
|
-
function extractInstance(updater,
|
|
892
|
-
var
|
|
893
|
-
if ((typeof instance === "undefined" ? "undefined" : _type_of(instance)) !== "object" || !instance) {
|
|
894
|
-
throw new Error("The instance should be an object or array.");
|
|
895
|
-
}
|
|
896
|
-
var properties = Object.getOwnPropertyNames(instance);
|
|
987
|
+
function extractInstance(updater, wrapper, cache, opts) {
|
|
988
|
+
var onGet = (opts || {}).onGet;
|
|
897
989
|
var handleGetter = function handleGetter2(key, value) {
|
|
898
990
|
if (!onGet) {
|
|
899
991
|
return;
|
|
900
992
|
}
|
|
901
993
|
onGet(key, value);
|
|
902
994
|
};
|
|
903
|
-
|
|
995
|
+
var instance = updater.instance;
|
|
996
|
+
if ((typeof instance === "undefined" ? "undefined" : _type_of(instance)) !== "object" || !instance) {
|
|
997
|
+
throw new Error("The instance should be an object or array.");
|
|
998
|
+
}
|
|
999
|
+
var properties = Object.getOwnPropertyNames(instance);
|
|
1000
|
+
var proxiedInstance = createProxy(instance, {
|
|
904
1001
|
get: function get(target, p) {
|
|
905
1002
|
var value = target[p];
|
|
906
1003
|
if (typeof value === "function" && properties.indexOf(p) >= 0) {
|
|
907
|
-
var actionMethod =
|
|
1004
|
+
var actionMethod = wrapToActionMethod2(updater, p);
|
|
908
1005
|
Object.assign(actionMethod, value);
|
|
909
|
-
handleGetter(p, actionMethod);
|
|
910
1006
|
return actionMethod;
|
|
911
1007
|
}
|
|
912
|
-
return
|
|
1008
|
+
return wrapToField2(updater, p, value);
|
|
913
1009
|
},
|
|
914
1010
|
set: function set() {
|
|
915
1011
|
return false;
|
|
916
1012
|
}
|
|
917
1013
|
});
|
|
1014
|
+
function generateInstance() {
|
|
1015
|
+
return proxiedInstance;
|
|
1016
|
+
}
|
|
1017
|
+
var wrapped = wrapper(generateInstance);
|
|
1018
|
+
if ((typeof wrapped === "undefined" ? "undefined" : _type_of(wrapped)) === "object" && wrapped != null) {
|
|
1019
|
+
return cacheProperties(_object_spread_props(_object_spread({}, cache), {
|
|
1020
|
+
target: wrapped
|
|
1021
|
+
}), handleGetter)();
|
|
1022
|
+
}
|
|
1023
|
+
return wrapped;
|
|
918
1024
|
}
|
|
919
1025
|
|
|
920
1026
|
// src/store/enhance/signal.ts
|
|
@@ -924,6 +1030,11 @@
|
|
|
924
1030
|
started: false,
|
|
925
1031
|
enabled: false
|
|
926
1032
|
};
|
|
1033
|
+
var propertiesCache = {
|
|
1034
|
+
target: store.getInstance(),
|
|
1035
|
+
cacheFields: {},
|
|
1036
|
+
cacheMethods: {}
|
|
1037
|
+
};
|
|
927
1038
|
var enhance = function(dispatcher) {
|
|
928
1039
|
return function(action) {
|
|
929
1040
|
if (!signalStore.enabled) {
|
|
@@ -934,7 +1045,7 @@
|
|
|
934
1045
|
if (collection == null) {
|
|
935
1046
|
return;
|
|
936
1047
|
}
|
|
937
|
-
var storeInstance = extractInstance(store.updater);
|
|
1048
|
+
var storeInstance = extractInstance(store.updater, store.key.wrapper, propertiesCache);
|
|
938
1049
|
var keys = Object.keys(collection);
|
|
939
1050
|
if (!keys.length) {
|
|
940
1051
|
return;
|
|
@@ -970,7 +1081,9 @@
|
|
|
970
1081
|
};
|
|
971
1082
|
var getInstance = function getInstance2(options) {
|
|
972
1083
|
var cutOff = (options !== null && options !== void 0 ? options : {}).cutOff;
|
|
973
|
-
return extractInstance(store.updater,
|
|
1084
|
+
return extractInstance(store.updater, store.key.wrapper, propertiesCache, {
|
|
1085
|
+
onGet: cutOff ? void 0 : collectUsedFields
|
|
1086
|
+
});
|
|
974
1087
|
};
|
|
975
1088
|
var signal = function signal2(options) {
|
|
976
1089
|
return getInstance(options);
|
|
@@ -994,100 +1107,6 @@
|
|
|
994
1107
|
}
|
|
995
1108
|
|
|
996
1109
|
// src/store/enhance/selector.ts
|
|
997
|
-
function wrapToField2(cache, propertyName, value, onGot) {
|
|
998
|
-
var cacheFields = cache.cacheFields;
|
|
999
|
-
if (!cacheIdentify.field(value)) {
|
|
1000
|
-
if (onGot) {
|
|
1001
|
-
onGot(propertyName, value);
|
|
1002
|
-
}
|
|
1003
|
-
return value;
|
|
1004
|
-
}
|
|
1005
|
-
var field2 = value;
|
|
1006
|
-
var cachedField = cacheFields[propertyName];
|
|
1007
|
-
if (cachedField) {
|
|
1008
|
-
return cachedField.getter;
|
|
1009
|
-
}
|
|
1010
|
-
var getter = {
|
|
1011
|
-
get: function get() {
|
|
1012
|
-
var currentField = cache.target[propertyName];
|
|
1013
|
-
if (!cacheIdentify.field(currentField)) {
|
|
1014
|
-
throw new Error("Field should always be field.");
|
|
1015
|
-
}
|
|
1016
|
-
var current = currentField.get();
|
|
1017
|
-
var fieldInCache = cache.cacheFields[propertyName];
|
|
1018
|
-
if (!currentField.deps || fieldInCache == null) {
|
|
1019
|
-
cacheFields[propertyName] = {
|
|
1020
|
-
getter,
|
|
1021
|
-
value: current,
|
|
1022
|
-
deps: currentField.deps
|
|
1023
|
-
};
|
|
1024
|
-
return current;
|
|
1025
|
-
}
|
|
1026
|
-
if (shallowEqual(currentField.deps, fieldInCache.deps)) {
|
|
1027
|
-
return fieldInCache.value;
|
|
1028
|
-
}
|
|
1029
|
-
cacheFields[propertyName] = {
|
|
1030
|
-
getter,
|
|
1031
|
-
value: current,
|
|
1032
|
-
deps: currentField.deps
|
|
1033
|
-
};
|
|
1034
|
-
return current;
|
|
1035
|
-
}
|
|
1036
|
-
};
|
|
1037
|
-
cacheFields[propertyName] = {
|
|
1038
|
-
getter,
|
|
1039
|
-
value: field2.value,
|
|
1040
|
-
deps: field2.deps
|
|
1041
|
-
};
|
|
1042
|
-
return getter;
|
|
1043
|
-
}
|
|
1044
|
-
function wrapToActionMethod2(cache, methodName) {
|
|
1045
|
-
var cacheMethods = cache.cacheMethods;
|
|
1046
|
-
var cachedMethod = cacheMethods[methodName];
|
|
1047
|
-
if (typeof cachedMethod === "function") {
|
|
1048
|
-
return cachedMethod;
|
|
1049
|
-
}
|
|
1050
|
-
var actionMethod = function actionMethod2() {
|
|
1051
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
1052
|
-
args[_key] = arguments[_key];
|
|
1053
|
-
}
|
|
1054
|
-
var target = cache.target;
|
|
1055
|
-
var method2 = target[methodName];
|
|
1056
|
-
if (typeof method2 !== "function") {
|
|
1057
|
-
throw new Error("Can not change methods in runtime.");
|
|
1058
|
-
}
|
|
1059
|
-
return method2.apply(void 0, _to_consumable_array(args));
|
|
1060
|
-
};
|
|
1061
|
-
cacheMethods[methodName] = actionMethod;
|
|
1062
|
-
return actionMethod;
|
|
1063
|
-
}
|
|
1064
|
-
var cacheProperties = function(cache, onGet) {
|
|
1065
|
-
return function createCachePropertiesProxy() {
|
|
1066
|
-
var instance = cache.target;
|
|
1067
|
-
var properties = Object.getOwnPropertyNames(instance);
|
|
1068
|
-
var handleGetter = function handleGetter2(key, value) {
|
|
1069
|
-
if (!onGet) {
|
|
1070
|
-
return;
|
|
1071
|
-
}
|
|
1072
|
-
onGet(key, value);
|
|
1073
|
-
};
|
|
1074
|
-
return createProxy(instance, {
|
|
1075
|
-
get: function get(target, p) {
|
|
1076
|
-
var value = target[p];
|
|
1077
|
-
if (typeof value === "function" && properties.indexOf(p) >= 0) {
|
|
1078
|
-
var actionMethod = wrapToActionMethod2(cache, p);
|
|
1079
|
-
Object.assign(actionMethod, value);
|
|
1080
|
-
handleGetter(p, actionMethod);
|
|
1081
|
-
return actionMethod;
|
|
1082
|
-
}
|
|
1083
|
-
return wrapToField2(cache, p, value, handleGetter);
|
|
1084
|
-
},
|
|
1085
|
-
set: function set() {
|
|
1086
|
-
return false;
|
|
1087
|
-
}
|
|
1088
|
-
});
|
|
1089
|
-
};
|
|
1090
|
-
};
|
|
1091
1110
|
function createSelector(store, opts) {
|
|
1092
1111
|
var equality = (opts !== null && opts !== void 0 ? opts : {}).equality;
|
|
1093
1112
|
var selectStore = {
|
|
@@ -1108,25 +1127,11 @@
|
|
|
1108
1127
|
selectStore.selectedInstance = nextSelectedInstance;
|
|
1109
1128
|
}
|
|
1110
1129
|
};
|
|
1111
|
-
var propertiesCache = {
|
|
1112
|
-
target: store.getInstance(),
|
|
1113
|
-
cacheFields: {},
|
|
1114
|
-
cacheMethods: {}
|
|
1115
|
-
};
|
|
1116
1130
|
var generateSelectedInstance = function generateSelectedInstance2(getInstance) {
|
|
1117
|
-
var storeKey = store.key;
|
|
1118
1131
|
if (cache.selector) {
|
|
1119
1132
|
return cache.selector(getInstance);
|
|
1120
1133
|
}
|
|
1121
|
-
|
|
1122
|
-
return getInstance();
|
|
1123
|
-
}
|
|
1124
|
-
var result = storeKey === null || storeKey === void 0 ? void 0 : storeKey.selector(getInstance);
|
|
1125
|
-
if (result == null || (typeof result === "undefined" ? "undefined" : _type_of(result)) !== "object") {
|
|
1126
|
-
throw new Error("The default selector result should be an object or array");
|
|
1127
|
-
}
|
|
1128
|
-
propertiesCache.target = result;
|
|
1129
|
-
return cacheProperties(propertiesCache)();
|
|
1134
|
+
return getInstance();
|
|
1130
1135
|
};
|
|
1131
1136
|
selectStore.selectedInstance = generateSelectedInstance(store.getInstance);
|
|
1132
1137
|
var enhance = function(dispatcher) {
|
|
@@ -1165,15 +1170,15 @@
|
|
|
1165
1170
|
var ifModelKey = isModelKey(modelFn);
|
|
1166
1171
|
var ifModelUsage = isModelUsage(modelFn);
|
|
1167
1172
|
var model2 = ifModelKey ? modelFn.source : modelFn;
|
|
1168
|
-
var
|
|
1169
|
-
var
|
|
1173
|
+
var _config_wrapper;
|
|
1174
|
+
var wrapper = (_config_wrapper = config2.wrapper) !== null && _config_wrapper !== void 0 ? _config_wrapper : ifModelKey || ifModelUsage ? modelFn.wrapper : function defaultSelector(i) {
|
|
1170
1175
|
return i();
|
|
1171
1176
|
};
|
|
1172
1177
|
var wrapModel = function wrapModel2(state) {
|
|
1173
1178
|
return model2(state);
|
|
1174
1179
|
};
|
|
1175
1180
|
wrapModel.source = model2;
|
|
1176
|
-
wrapModel.
|
|
1181
|
+
wrapModel.wrapper = wrapper;
|
|
1177
1182
|
wrapModel.modelKeyIdentifier = modelKeyIdentifier;
|
|
1178
1183
|
if ("state" in config2) {
|
|
1179
1184
|
wrapModel.defaultState = config2.state;
|
|
@@ -1214,8 +1219,10 @@
|
|
|
1214
1219
|
};
|
|
1215
1220
|
var updater = createUpdater(model2, combinedMiddleWare, conf);
|
|
1216
1221
|
var key = modelKey !== null && modelKey !== void 0 ? modelKey : createPrimaryKey(model2, config2);
|
|
1217
|
-
var
|
|
1218
|
-
|
|
1222
|
+
var propertiesCache = {
|
|
1223
|
+
target: updater.instance,
|
|
1224
|
+
cacheFields: {},
|
|
1225
|
+
cacheMethods: {}
|
|
1219
1226
|
};
|
|
1220
1227
|
var store = {
|
|
1221
1228
|
key,
|
|
@@ -1223,11 +1230,13 @@
|
|
|
1223
1230
|
return updater.token;
|
|
1224
1231
|
},
|
|
1225
1232
|
subscribe: function subscribe(dispatcher) {
|
|
1226
|
-
var _updater_createTunnel = updater.createTunnel(dispatcher), connect = _updater_createTunnel.connect, disconnect = _updater_createTunnel.disconnect;
|
|
1233
|
+
var _updater_createTunnel = updater.createTunnel(dispatcher || noop), connect = _updater_createTunnel.connect, disconnect = _updater_createTunnel.disconnect;
|
|
1227
1234
|
connect();
|
|
1228
1235
|
return disconnect;
|
|
1229
1236
|
},
|
|
1230
|
-
getInstance
|
|
1237
|
+
getInstance: function getInstance() {
|
|
1238
|
+
return extractInstance(updater, key.wrapper, propertiesCache);
|
|
1239
|
+
},
|
|
1231
1240
|
update: function update(args) {
|
|
1232
1241
|
var updateArgs = args !== null && args !== void 0 ? args : {};
|
|
1233
1242
|
if (updateArgs.key) {
|
|
@@ -1351,36 +1360,36 @@
|
|
|
1351
1360
|
|
|
1352
1361
|
// src/model/index.ts
|
|
1353
1362
|
function configModel(config2) {
|
|
1354
|
-
var model2 = function model3(modelFn,
|
|
1355
|
-
var currentSelector =
|
|
1363
|
+
var model2 = function model3(modelFn, wrapper) {
|
|
1364
|
+
var currentSelector = wrapper !== null && wrapper !== void 0 ? wrapper : function defaultSelector(i) {
|
|
1356
1365
|
return i();
|
|
1357
1366
|
};
|
|
1358
1367
|
var modelWrapper = function modelWrapper2(state) {
|
|
1359
1368
|
return modelFn(state);
|
|
1360
1369
|
};
|
|
1361
|
-
modelWrapper.
|
|
1370
|
+
modelWrapper.pipe = function pipe(s) {
|
|
1362
1371
|
return model3(modelFn, s);
|
|
1363
1372
|
};
|
|
1364
1373
|
modelWrapper.createKey = function createModelKey(state) {
|
|
1365
1374
|
return createKey(modelFn, arguments.length ? _object_spread_props(_object_spread({}, config2), {
|
|
1366
1375
|
state,
|
|
1367
|
-
|
|
1376
|
+
wrapper: currentSelector
|
|
1368
1377
|
}) : _object_spread_props(_object_spread({}, config2), {
|
|
1369
|
-
|
|
1378
|
+
wrapper: currentSelector
|
|
1370
1379
|
}));
|
|
1371
1380
|
};
|
|
1372
1381
|
modelWrapper.createStore = function createModelStore(state) {
|
|
1373
1382
|
return createStore(modelFn, arguments.length ? _object_spread_props(_object_spread({}, config2), {
|
|
1374
1383
|
state,
|
|
1375
|
-
|
|
1384
|
+
wrapper: currentSelector
|
|
1376
1385
|
}) : _object_spread_props(_object_spread({}, config2), {
|
|
1377
|
-
|
|
1386
|
+
wrapper: currentSelector
|
|
1378
1387
|
}));
|
|
1379
1388
|
};
|
|
1380
1389
|
modelWrapper.extends = function extendsModelUsage(e) {
|
|
1381
1390
|
return Object.assign(modelWrapper, e);
|
|
1382
1391
|
};
|
|
1383
|
-
modelWrapper.
|
|
1392
|
+
modelWrapper.wrapper = currentSelector;
|
|
1384
1393
|
modelWrapper.modelUsageIdentifier = modelUsageIdentifier;
|
|
1385
1394
|
return modelWrapper;
|
|
1386
1395
|
};
|
package/esm/model/index.js
CHANGED
|
@@ -21,32 +21,32 @@ import { createStore, createField, createMethod } from "../store";
|
|
|
21
21
|
import { createKey } from "../key";
|
|
22
22
|
import { modelUsageIdentifier } from "../identifiers";
|
|
23
23
|
function configModel(config) {
|
|
24
|
-
const model = function model2(modelFn,
|
|
25
|
-
const currentSelector =
|
|
24
|
+
const model = function model2(modelFn, wrapper) {
|
|
25
|
+
const currentSelector = wrapper != null ? wrapper : function defaultSelector(i) {
|
|
26
26
|
return i();
|
|
27
27
|
};
|
|
28
28
|
const modelWrapper = function modelWrapper2(state) {
|
|
29
29
|
return modelFn(state);
|
|
30
30
|
};
|
|
31
|
-
modelWrapper.
|
|
31
|
+
modelWrapper.pipe = function pipe(s) {
|
|
32
32
|
return model2(modelFn, s);
|
|
33
33
|
};
|
|
34
34
|
modelWrapper.createKey = function createModelKey(state) {
|
|
35
35
|
return createKey(
|
|
36
36
|
modelFn,
|
|
37
|
-
arguments.length ? __spreadProps(__spreadValues({}, config), { state,
|
|
37
|
+
arguments.length ? __spreadProps(__spreadValues({}, config), { state, wrapper: currentSelector }) : __spreadProps(__spreadValues({}, config), { wrapper: currentSelector })
|
|
38
38
|
);
|
|
39
39
|
};
|
|
40
40
|
modelWrapper.createStore = function createModelStore(state) {
|
|
41
41
|
return createStore(
|
|
42
42
|
modelFn,
|
|
43
|
-
arguments.length ? __spreadProps(__spreadValues({}, config), { state,
|
|
43
|
+
arguments.length ? __spreadProps(__spreadValues({}, config), { state, wrapper: currentSelector }) : __spreadProps(__spreadValues({}, config), { wrapper: currentSelector })
|
|
44
44
|
);
|
|
45
45
|
};
|
|
46
46
|
modelWrapper.extends = function extendsModelUsage(e) {
|
|
47
47
|
return Object.assign(modelWrapper, e);
|
|
48
48
|
};
|
|
49
|
-
modelWrapper.
|
|
49
|
+
modelWrapper.wrapper = currentSelector;
|
|
50
50
|
modelWrapper.modelUsageIdentifier = modelUsageIdentifier;
|
|
51
51
|
return modelWrapper;
|
|
52
52
|
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { createProxy, shallowEqual } from "../tools";
|
|
2
|
+
const cacheIdentify = {
|
|
3
|
+
field(d) {
|
|
4
|
+
if (!d) {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
const f = d;
|
|
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
|
+
};
|
|
18
|
+
function wrapToField(cache, propertyName, value, onGot) {
|
|
19
|
+
const { cacheFields } = cache;
|
|
20
|
+
if (!cacheIdentify.field(value)) {
|
|
21
|
+
if (onGot) {
|
|
22
|
+
onGot(propertyName, value);
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
const field = value;
|
|
27
|
+
const cachedField = cacheFields[propertyName];
|
|
28
|
+
if (cachedField) {
|
|
29
|
+
return cachedField.getter;
|
|
30
|
+
}
|
|
31
|
+
const getter = {
|
|
32
|
+
get() {
|
|
33
|
+
const currentField = cache.target[propertyName];
|
|
34
|
+
if (!cacheIdentify.field(currentField)) {
|
|
35
|
+
throw new Error("Field should always be field.");
|
|
36
|
+
}
|
|
37
|
+
const current = currentField.get();
|
|
38
|
+
const fieldInCache = cache.cacheFields[propertyName];
|
|
39
|
+
if (!currentField.deps || fieldInCache == null) {
|
|
40
|
+
cacheFields[propertyName] = {
|
|
41
|
+
getter,
|
|
42
|
+
value: current,
|
|
43
|
+
deps: currentField.deps
|
|
44
|
+
};
|
|
45
|
+
return current;
|
|
46
|
+
}
|
|
47
|
+
if (shallowEqual(currentField.deps, fieldInCache.deps)) {
|
|
48
|
+
return fieldInCache.value;
|
|
49
|
+
}
|
|
50
|
+
cacheFields[propertyName] = {
|
|
51
|
+
getter,
|
|
52
|
+
value: current,
|
|
53
|
+
deps: currentField.deps
|
|
54
|
+
};
|
|
55
|
+
return current;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
cacheFields[propertyName] = { getter, value: field.value, deps: field.deps };
|
|
59
|
+
return getter;
|
|
60
|
+
}
|
|
61
|
+
function wrapToActionMethod(cache, methodName) {
|
|
62
|
+
const { cacheMethods } = cache;
|
|
63
|
+
const cachedMethod = cacheMethods[methodName];
|
|
64
|
+
if (typeof cachedMethod === "function") {
|
|
65
|
+
return cachedMethod;
|
|
66
|
+
}
|
|
67
|
+
const actionMethod = function actionMethod2(...args) {
|
|
68
|
+
const { target } = cache;
|
|
69
|
+
const method = target[methodName];
|
|
70
|
+
if (typeof method !== "function") {
|
|
71
|
+
throw new Error("Can not change methods in runtime.");
|
|
72
|
+
}
|
|
73
|
+
return method(...args);
|
|
74
|
+
};
|
|
75
|
+
cacheMethods[methodName] = actionMethod;
|
|
76
|
+
return actionMethod;
|
|
77
|
+
}
|
|
78
|
+
const cacheProperties = (cache, onGet) => {
|
|
79
|
+
return function createCachePropertiesProxy() {
|
|
80
|
+
const instance = cache.target;
|
|
81
|
+
const properties = Object.getOwnPropertyNames(instance);
|
|
82
|
+
const handleGetter = function handleGetter2(key, value) {
|
|
83
|
+
if (!onGet) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
onGet(key, value);
|
|
87
|
+
};
|
|
88
|
+
return createProxy(instance, {
|
|
89
|
+
get(target, p) {
|
|
90
|
+
const value = target[p];
|
|
91
|
+
if (typeof value === "function" && properties.indexOf(p) >= 0) {
|
|
92
|
+
const actionMethod = wrapToActionMethod(cache, p);
|
|
93
|
+
Object.assign(actionMethod, value);
|
|
94
|
+
handleGetter(p, actionMethod);
|
|
95
|
+
return actionMethod;
|
|
96
|
+
}
|
|
97
|
+
return wrapToField(cache, p, value, handleGetter);
|
|
98
|
+
},
|
|
99
|
+
set() {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
export {
|
|
106
|
+
cacheIdentify,
|
|
107
|
+
cacheProperties
|
|
108
|
+
};
|