@akanjs/store 0.0.46 → 0.0.47
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/index.js +2 -1086
- package/package.json +1 -1
- package/src/index.js +18 -0
- package/src/storeDecorators.js +1098 -0
- package/src/types.js +15 -0
package/index.js
CHANGED
|
@@ -2,10 +2,6 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
5
|
var __copyProps = (to, from, except, desc) => {
|
|
10
6
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
7
|
for (let key of __getOwnPropNames(from))
|
|
@@ -14,1088 +10,8 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
14
10
|
}
|
|
15
11
|
return to;
|
|
16
12
|
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
17
14
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// pkgs/@akanjs/store/index.ts
|
|
20
15
|
var store_exports = {};
|
|
21
|
-
__export(store_exports, {
|
|
22
|
-
MixStore: () => MixStore,
|
|
23
|
-
Store: () => Store,
|
|
24
|
-
Toast: () => Toast,
|
|
25
|
-
createActions: () => createActions,
|
|
26
|
-
createState: () => createState,
|
|
27
|
-
makeStore: () => makeStore,
|
|
28
|
-
rootStoreOf: () => rootStoreOf,
|
|
29
|
-
scalarStateOf: () => scalarStateOf,
|
|
30
|
-
st: () => st,
|
|
31
|
-
stateOf: () => stateOf
|
|
32
|
-
});
|
|
33
16
|
module.exports = __toCommonJS(store_exports);
|
|
34
|
-
|
|
35
|
-
// pkgs/@akanjs/store/src/storeDecorators.ts
|
|
36
|
-
var import_base = require("@akanjs/base");
|
|
37
|
-
var import_common = require("@akanjs/common");
|
|
38
|
-
var import_constant = require("@akanjs/constant");
|
|
39
|
-
var import_dictionary = require("@akanjs/dictionary");
|
|
40
|
-
var import_signal = require("@akanjs/signal");
|
|
41
|
-
var import_react = require("react");
|
|
42
|
-
var import_zustand = require("zustand");
|
|
43
|
-
var import_middleware = require("zustand/middleware");
|
|
44
|
-
var import_immer = require("zustand/middleware/immer");
|
|
45
|
-
var st = {};
|
|
46
|
-
var StoreStorage = class {
|
|
47
|
-
};
|
|
48
|
-
var getStoreMeta = (storeName) => {
|
|
49
|
-
const storeMeta = Reflect.getMetadata(storeName, StoreStorage.prototype);
|
|
50
|
-
if (!storeMeta)
|
|
51
|
-
throw new Error(`storeMeta is not defined: ${storeName}`);
|
|
52
|
-
return storeMeta;
|
|
53
|
-
};
|
|
54
|
-
var setStoreMeta = (storeName, storeMeta) => {
|
|
55
|
-
Reflect.defineMetadata(storeName, storeMeta, StoreStorage.prototype);
|
|
56
|
-
};
|
|
57
|
-
var getStoreNames = () => {
|
|
58
|
-
const storeNames = Reflect.getMetadataKeys(StoreStorage.prototype);
|
|
59
|
-
if (!storeNames)
|
|
60
|
-
throw new Error(`storeNames is not defined`);
|
|
61
|
-
return storeNames;
|
|
62
|
-
};
|
|
63
|
-
var createState = (gql) => {
|
|
64
|
-
const [fieldName, className] = [(0, import_common.lowerlize)(gql.refName), (0, import_common.capitalize)(gql.refName)];
|
|
65
|
-
const names = {
|
|
66
|
-
model: fieldName,
|
|
67
|
-
Model: className,
|
|
68
|
-
modelLoading: `${fieldName}Loading`,
|
|
69
|
-
modelForm: `${fieldName}Form`,
|
|
70
|
-
modelFormLoading: `${fieldName}FormLoading`,
|
|
71
|
-
modelSubmit: `${fieldName}Submit`,
|
|
72
|
-
modelViewAt: `${fieldName}ViewAt`,
|
|
73
|
-
modelModal: `${fieldName}Modal`,
|
|
74
|
-
modelOperation: `${fieldName}Operation`,
|
|
75
|
-
defaultModel: `default${className}`,
|
|
76
|
-
defaultModelInsight: `default${className}Insight`,
|
|
77
|
-
modelList: `${fieldName}List`,
|
|
78
|
-
modelListLoading: `${fieldName}ListLoading`,
|
|
79
|
-
modelInitList: `${fieldName}InitList`,
|
|
80
|
-
modelInitAt: `${fieldName}InitAt`,
|
|
81
|
-
modelSelection: `${fieldName}Selection`,
|
|
82
|
-
modelInsight: `${fieldName}Insight`,
|
|
83
|
-
lastPageOfModel: `lastPageOf${className}`,
|
|
84
|
-
pageOfModel: `pageOf${className}`,
|
|
85
|
-
limitOfModel: `limitOf${className}`,
|
|
86
|
-
queryArgsOfModel: `queryArgsOf${className}`,
|
|
87
|
-
sortOfModel: `sortOf${className}`
|
|
88
|
-
};
|
|
89
|
-
const baseState = {
|
|
90
|
-
[names.model]: null,
|
|
91
|
-
[names.modelLoading]: true,
|
|
92
|
-
[names.modelForm]: { ...gql[names.defaultModel] },
|
|
93
|
-
[names.modelFormLoading]: true,
|
|
94
|
-
[names.modelSubmit]: { disabled: true, loading: false, times: 0 },
|
|
95
|
-
[names.modelViewAt]: /* @__PURE__ */ new Date(0),
|
|
96
|
-
[names.modelModal]: null,
|
|
97
|
-
[names.modelOperation]: "sleep"
|
|
98
|
-
};
|
|
99
|
-
const sliceState = gql.slices.reduce((acc, { sliceName, defaultArgs }) => {
|
|
100
|
-
const SliceName = (0, import_common.capitalize)(sliceName);
|
|
101
|
-
const namesOfSlice = {
|
|
102
|
-
defaultModel: SliceName.replace(names.Model, names.defaultModel),
|
|
103
|
-
//clusterInSelf Cluster
|
|
104
|
-
modelList: sliceName.replace(names.model, names.modelList),
|
|
105
|
-
modelListLoading: sliceName.replace(names.model, names.modelListLoading),
|
|
106
|
-
modelInitList: sliceName.replace(names.model, names.modelInitList),
|
|
107
|
-
modelInitAt: sliceName.replace(names.model, names.modelInitAt),
|
|
108
|
-
modelSelection: sliceName.replace(names.model, names.modelSelection),
|
|
109
|
-
modelInsight: sliceName.replace(names.model, names.modelInsight),
|
|
110
|
-
lastPageOfModel: SliceName.replace(names.Model, names.lastPageOfModel),
|
|
111
|
-
pageOfModel: SliceName.replace(names.Model, names.pageOfModel),
|
|
112
|
-
limitOfModel: SliceName.replace(names.Model, names.limitOfModel),
|
|
113
|
-
queryArgsOfModel: SliceName.replace(names.Model, names.queryArgsOfModel),
|
|
114
|
-
sortOfModel: SliceName.replace(names.Model, names.sortOfModel)
|
|
115
|
-
};
|
|
116
|
-
const singleSliceState = {
|
|
117
|
-
[namesOfSlice.defaultModel]: { ...gql[names.defaultModel] },
|
|
118
|
-
[namesOfSlice.modelList]: new import_base.DataList(),
|
|
119
|
-
[namesOfSlice.modelListLoading]: true,
|
|
120
|
-
[namesOfSlice.modelInitList]: new import_base.DataList(),
|
|
121
|
-
[namesOfSlice.modelInitAt]: /* @__PURE__ */ new Date(0),
|
|
122
|
-
[namesOfSlice.modelSelection]: new import_base.DataList(),
|
|
123
|
-
[namesOfSlice.modelInsight]: gql[names.defaultModelInsight],
|
|
124
|
-
[namesOfSlice.lastPageOfModel]: 1,
|
|
125
|
-
[namesOfSlice.pageOfModel]: 1,
|
|
126
|
-
[namesOfSlice.limitOfModel]: 20,
|
|
127
|
-
[namesOfSlice.queryArgsOfModel]: defaultArgs,
|
|
128
|
-
[namesOfSlice.sortOfModel]: "latest"
|
|
129
|
-
};
|
|
130
|
-
return Object.assign(acc, singleSliceState);
|
|
131
|
-
}, {});
|
|
132
|
-
return { ...baseState, ...sliceState };
|
|
133
|
-
};
|
|
134
|
-
var createActions = (gql) => {
|
|
135
|
-
const formSetterActions = makeFormSetter(gql);
|
|
136
|
-
const baseActions = makeActions(gql);
|
|
137
|
-
return { ...formSetterActions, ...baseActions };
|
|
138
|
-
};
|
|
139
|
-
var makeFormSetter = (gql) => {
|
|
140
|
-
const fileGql = (0, import_signal.getGqlOnStorage)("file");
|
|
141
|
-
const [fieldName, className] = [(0, import_common.lowerlize)(gql.refName), (0, import_common.capitalize)(gql.refName)];
|
|
142
|
-
const modelRef = (0, import_constant.getFullModelRef)(gql.refName);
|
|
143
|
-
const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
|
|
144
|
-
const names = {
|
|
145
|
-
model: fieldName,
|
|
146
|
-
Model: className,
|
|
147
|
-
modelForm: `${fieldName}Form`,
|
|
148
|
-
writeOnModel: `writeOn${className}`,
|
|
149
|
-
addModelFiles: `add${className}Files`
|
|
150
|
-
};
|
|
151
|
-
const baseSetAction = {
|
|
152
|
-
[names.writeOnModel]: function(path, value) {
|
|
153
|
-
this.set((state) => {
|
|
154
|
-
(0, import_common.pathSet)(state[names.modelForm], path, value);
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
const fieldSetAction = fieldMetas.reduce((acc, fieldMeta) => {
|
|
159
|
-
const [fieldKeyName, classKeyName] = [(0, import_common.lowerlize)(fieldMeta.key), (0, import_common.capitalize)(fieldMeta.key)];
|
|
160
|
-
const namesOfField = {
|
|
161
|
-
field: fieldKeyName,
|
|
162
|
-
Field: classKeyName,
|
|
163
|
-
setFieldOnModel: `set${classKeyName}On${className}`,
|
|
164
|
-
addFieldOnModel: `add${classKeyName}On${className}`,
|
|
165
|
-
subFieldOnModel: `sub${classKeyName}On${className}`,
|
|
166
|
-
addOrSubFieldOnModel: `addOrSub${classKeyName}On${className}`,
|
|
167
|
-
uploadFieldOnModel: `upload${classKeyName}On${className}`
|
|
168
|
-
};
|
|
169
|
-
const singleFieldSetAction = {
|
|
170
|
-
[namesOfField.setFieldOnModel]: function(value) {
|
|
171
|
-
this.set((state) => {
|
|
172
|
-
const setValue = fieldMeta.isClass ? (0, import_signal.immerify)(fieldMeta.modelRef, value) : value;
|
|
173
|
-
state[names.modelForm][namesOfField.field] = setValue;
|
|
174
|
-
});
|
|
175
|
-
},
|
|
176
|
-
...fieldMeta.isArray ? {
|
|
177
|
-
[namesOfField.addFieldOnModel]: function(value, options = {}) {
|
|
178
|
-
const form = this.get()[names.modelForm];
|
|
179
|
-
const length = form[namesOfField.field].length;
|
|
180
|
-
if (options.limit && options.limit <= length)
|
|
181
|
-
return;
|
|
182
|
-
const idx = options.idx ?? length;
|
|
183
|
-
const setValue = fieldMeta.isClass ? (0, import_signal.immerify)(fieldMeta.modelRef, value) : value;
|
|
184
|
-
this.set((state) => {
|
|
185
|
-
state[names.modelForm][namesOfField.field] = [
|
|
186
|
-
...form[namesOfField.field].slice(0, idx),
|
|
187
|
-
...Array.isArray(setValue) ? setValue : [setValue],
|
|
188
|
-
...form[namesOfField.field].slice(idx)
|
|
189
|
-
];
|
|
190
|
-
});
|
|
191
|
-
},
|
|
192
|
-
[namesOfField.subFieldOnModel]: function(idx) {
|
|
193
|
-
const form = this.get()[names.modelForm];
|
|
194
|
-
this.set((state) => {
|
|
195
|
-
state[names.modelForm][namesOfField.field] = typeof idx === "number" ? form[namesOfField.field].filter((_, i) => i !== idx) : form[namesOfField.field].filter((_, i) => !idx.includes(i));
|
|
196
|
-
});
|
|
197
|
-
},
|
|
198
|
-
[namesOfField.addOrSubFieldOnModel]: function(value, options = {}) {
|
|
199
|
-
const { [names.modelForm]: form } = this.get();
|
|
200
|
-
const index = form[namesOfField.field].findIndex((v) => v === value);
|
|
201
|
-
if (index === -1)
|
|
202
|
-
this[namesOfField.addFieldOnModel](value, options);
|
|
203
|
-
else
|
|
204
|
-
this[namesOfField.subFieldOnModel](index);
|
|
205
|
-
}
|
|
206
|
-
} : {},
|
|
207
|
-
...fieldMeta.name === "File" ? {
|
|
208
|
-
[namesOfField.uploadFieldOnModel]: async function(fileList, index) {
|
|
209
|
-
const form = this.get()[names.modelForm];
|
|
210
|
-
if (!fileList.length)
|
|
211
|
-
return;
|
|
212
|
-
const files = await gql[names.addModelFiles](
|
|
213
|
-
fileList,
|
|
214
|
-
form.id
|
|
215
|
-
);
|
|
216
|
-
if (fieldMeta.isArray) {
|
|
217
|
-
const idx = index ?? form[namesOfField.field].length;
|
|
218
|
-
this.set((state) => {
|
|
219
|
-
state[names.modelForm][namesOfField.field] = [
|
|
220
|
-
...form[namesOfField.field].slice(0, idx),
|
|
221
|
-
...files,
|
|
222
|
-
...form[namesOfField.field].slice(idx)
|
|
223
|
-
];
|
|
224
|
-
});
|
|
225
|
-
} else {
|
|
226
|
-
this.set((state) => {
|
|
227
|
-
state[names.modelForm][namesOfField.field] = files[0];
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
files.map((file) => {
|
|
231
|
-
const intervalKey = setInterval(() => {
|
|
232
|
-
void (async () => {
|
|
233
|
-
const currentFile = await fileGql.file(file.id);
|
|
234
|
-
if (fieldMeta.isArray)
|
|
235
|
-
this.set((state) => {
|
|
236
|
-
state[names.modelForm][namesOfField.field] = state[names.modelForm][namesOfField.field].map(
|
|
237
|
-
(file2) => file2.id === currentFile.id ? currentFile : file2
|
|
238
|
-
);
|
|
239
|
-
});
|
|
240
|
-
else
|
|
241
|
-
this.set((state) => {
|
|
242
|
-
state[names.modelForm][namesOfField.field] = currentFile;
|
|
243
|
-
});
|
|
244
|
-
if (currentFile.status !== "uploading")
|
|
245
|
-
clearInterval(intervalKey);
|
|
246
|
-
})();
|
|
247
|
-
}, 3e3);
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
} : {}
|
|
251
|
-
};
|
|
252
|
-
return Object.assign(acc, singleFieldSetAction);
|
|
253
|
-
}, {});
|
|
254
|
-
return Object.assign(fieldSetAction, baseSetAction);
|
|
255
|
-
};
|
|
256
|
-
var makeActions = (gql) => {
|
|
257
|
-
const [fieldName, className] = [(0, import_common.lowerlize)(gql.refName), (0, import_common.capitalize)(gql.refName)];
|
|
258
|
-
const modelRef = (0, import_constant.getFullModelRef)(className);
|
|
259
|
-
const lightModelRef = (0, import_constant.getLightModelRef)(modelRef);
|
|
260
|
-
const names = {
|
|
261
|
-
model: fieldName,
|
|
262
|
-
_model: `_${fieldName}`,
|
|
263
|
-
Model: className,
|
|
264
|
-
purifyModel: `purify${className}`,
|
|
265
|
-
crystalizeModel: `crystalize${className}`,
|
|
266
|
-
lightCrystalizeModel: `lightCrystalize${className}`,
|
|
267
|
-
crystalizeInsight: `crystalize${className}Insight`,
|
|
268
|
-
modelOperation: `${fieldName}Operation`,
|
|
269
|
-
defaultModel: `default${className}`,
|
|
270
|
-
modelInsight: `${fieldName}Insight`,
|
|
271
|
-
modelForm: `${fieldName}Form`,
|
|
272
|
-
modelSubmit: `${fieldName}Submit`,
|
|
273
|
-
modelLoading: `${fieldName}Loading`,
|
|
274
|
-
modelFormLoading: `${fieldName}FormLoading`,
|
|
275
|
-
modelList: `${fieldName}List`,
|
|
276
|
-
modelListLoading: `${fieldName}ListLoading`,
|
|
277
|
-
modelSelection: `${fieldName}Selection`,
|
|
278
|
-
createModelInForm: `create${className}InForm`,
|
|
279
|
-
updateModelInForm: `modify${className}InForm`,
|
|
280
|
-
createModel: `create${className}`,
|
|
281
|
-
updateModel: `update${className}`,
|
|
282
|
-
removeModel: `remove${className}`,
|
|
283
|
-
checkModelSubmitable: `check${className}Submitable`,
|
|
284
|
-
submitModel: `submit${className}`,
|
|
285
|
-
newModel: `new${className}`,
|
|
286
|
-
editModel: `edit${className}`,
|
|
287
|
-
mergeModel: `merge${className}`,
|
|
288
|
-
viewModel: `view${className}`,
|
|
289
|
-
setModel: `set${className}`,
|
|
290
|
-
resetModel: `reset${className}`,
|
|
291
|
-
modelViewAt: `${fieldName}ViewAt`,
|
|
292
|
-
modelModal: `${fieldName}Modal`,
|
|
293
|
-
initModel: `init${className}`,
|
|
294
|
-
modelInitList: `${fieldName}InitList`,
|
|
295
|
-
modelInitAt: `${fieldName}InitAt`,
|
|
296
|
-
refreshModel: `refresh${className}`,
|
|
297
|
-
selectModel: `select${className}`,
|
|
298
|
-
setPageOfModel: `setPageOf${className}`,
|
|
299
|
-
addPageOfModel: `addPageOf${className}`,
|
|
300
|
-
setLimitOfModel: `setLimitOf${className}`,
|
|
301
|
-
setQueryArgsOfModel: `setQueryArgsOf${className}`,
|
|
302
|
-
setSortOfModel: `setSortOf${className}`,
|
|
303
|
-
lastPageOfModel: `lastPageOf${className}`,
|
|
304
|
-
pageOfModel: `pageOf${className}`,
|
|
305
|
-
limitOfModel: `limitOf${className}`,
|
|
306
|
-
queryArgsOfModel: `queryArgsOf${className}`,
|
|
307
|
-
sortOfModel: `sortOf${className}`
|
|
308
|
-
};
|
|
309
|
-
const baseAction = {
|
|
310
|
-
[names.createModelInForm]: async function({ idx, path, modal, sliceName = names.model, onError, onSuccess } = {}) {
|
|
311
|
-
const SliceName = (0, import_common.capitalize)(sliceName);
|
|
312
|
-
const namesOfSlice = {
|
|
313
|
-
defaultModel: SliceName.replace(names.Model, names.defaultModel),
|
|
314
|
-
modelList: sliceName.replace(names.model, names.modelList),
|
|
315
|
-
modelListLoading: sliceName.replace(names.model, names.modelListLoading),
|
|
316
|
-
modelInsight: sliceName.replace(names.model, names.modelInsight)
|
|
317
|
-
};
|
|
318
|
-
const currentState = this.get();
|
|
319
|
-
const modelForm = currentState[names.modelForm];
|
|
320
|
-
const modelList = currentState[namesOfSlice.modelList];
|
|
321
|
-
const modelListLoading = currentState[namesOfSlice.modelListLoading];
|
|
322
|
-
const modelInsight = currentState[namesOfSlice.modelInsight];
|
|
323
|
-
const defaultModel = currentState[namesOfSlice.defaultModel];
|
|
324
|
-
const modelInput = gql[names.purifyModel](modelForm);
|
|
325
|
-
if (!modelInput)
|
|
326
|
-
return;
|
|
327
|
-
this.set({ [names.modelLoading]: true });
|
|
328
|
-
const model = await gql[names.createModel](modelInput, { onError });
|
|
329
|
-
const newModelList = modelListLoading ? modelList : new import_base.DataList([...modelList.slice(0, idx ?? 0), model, ...modelList.slice(idx ?? 0)]);
|
|
330
|
-
const newModelInsight = gql[names.crystalizeInsight]({
|
|
331
|
-
...modelInsight,
|
|
332
|
-
count: modelInsight.count + 1
|
|
333
|
-
});
|
|
334
|
-
this.set({
|
|
335
|
-
[names.modelForm]: (0, import_signal.immerify)(modelRef, defaultModel),
|
|
336
|
-
[names.model]: model,
|
|
337
|
-
[names.modelLoading]: false,
|
|
338
|
-
[namesOfSlice.modelList]: newModelList,
|
|
339
|
-
[namesOfSlice.modelInsight]: newModelInsight,
|
|
340
|
-
[names.modelViewAt]: /* @__PURE__ */ new Date(),
|
|
341
|
-
[names.modelModal]: modal ?? null,
|
|
342
|
-
...typeof path === "string" && path ? { [path]: model } : {}
|
|
343
|
-
});
|
|
344
|
-
await onSuccess?.(model);
|
|
345
|
-
},
|
|
346
|
-
[names.updateModelInForm]: async function({ path, modal, sliceName = names.model, onError, onSuccess } = {}) {
|
|
347
|
-
const SliceName = (0, import_common.capitalize)(sliceName);
|
|
348
|
-
const namesOfSlice = {
|
|
349
|
-
defaultModel: SliceName.replace(names.Model, names.defaultModel)
|
|
350
|
-
};
|
|
351
|
-
const currentState = this.get();
|
|
352
|
-
const model = currentState[names.model];
|
|
353
|
-
const modelForm = currentState[names.modelForm];
|
|
354
|
-
const defaultModel = currentState[namesOfSlice.defaultModel];
|
|
355
|
-
const modelInput = gql[names.purifyModel](modelForm);
|
|
356
|
-
if (!modelInput)
|
|
357
|
-
return;
|
|
358
|
-
if (model?.id === modelForm.id)
|
|
359
|
-
this.set({ [names.modelLoading]: modelForm.id });
|
|
360
|
-
const updatedModel = await gql[names.updateModel](modelForm.id, modelInput, {
|
|
361
|
-
onError
|
|
362
|
-
});
|
|
363
|
-
this.set({
|
|
364
|
-
...model?.id === updatedModel.id ? { [names.model]: updatedModel, [names.modelLoading]: false, [names.modelViewAt]: /* @__PURE__ */ new Date() } : {},
|
|
365
|
-
[names.modelForm]: (0, import_signal.immerify)(modelRef, defaultModel),
|
|
366
|
-
[names.modelModal]: modal ?? null,
|
|
367
|
-
...typeof path === "string" && path ? { [path]: updatedModel } : {}
|
|
368
|
-
});
|
|
369
|
-
const updatedLightModel = gql[names.lightCrystalizeModel](updatedModel);
|
|
370
|
-
gql.slices.forEach(({ sliceName: sliceName2 }) => {
|
|
371
|
-
const namesOfSlice2 = {
|
|
372
|
-
modelList: sliceName2.replace(names.model, names.modelList),
|
|
373
|
-
modelListLoading: sliceName2.replace(names.model, names.modelListLoading)
|
|
374
|
-
};
|
|
375
|
-
const currentState2 = this.get();
|
|
376
|
-
const modelList = currentState2[namesOfSlice2.modelList];
|
|
377
|
-
const modelListLoading = currentState2[namesOfSlice2.modelListLoading];
|
|
378
|
-
if (modelListLoading || !modelList.has(updatedModel.id))
|
|
379
|
-
return;
|
|
380
|
-
const newModelList = new import_base.DataList(modelList).set(updatedLightModel);
|
|
381
|
-
this.set({ [namesOfSlice2.modelList]: newModelList });
|
|
382
|
-
});
|
|
383
|
-
await onSuccess?.(updatedModel);
|
|
384
|
-
},
|
|
385
|
-
[names.createModel]: async function(data, { idx, path, modal, sliceName = names.model, onError, onSuccess } = {}) {
|
|
386
|
-
const SliceName = (0, import_common.capitalize)(sliceName);
|
|
387
|
-
const namesOfSlice = {
|
|
388
|
-
defaultModel: SliceName.replace(names.Model, names.defaultModel),
|
|
389
|
-
modelList: sliceName.replace(names.model, names.modelList),
|
|
390
|
-
modelListLoading: sliceName.replace(names.model, names.modelListLoading),
|
|
391
|
-
modelInsight: sliceName.replace(names.model, names.modelInsight)
|
|
392
|
-
};
|
|
393
|
-
const currentState = this.get();
|
|
394
|
-
const modelList = currentState[namesOfSlice.modelList];
|
|
395
|
-
const modelListLoading = currentState[namesOfSlice.modelListLoading];
|
|
396
|
-
const modelInsight = currentState[namesOfSlice.modelInsight];
|
|
397
|
-
const modelInput = gql[names.purifyModel](data);
|
|
398
|
-
if (!modelInput)
|
|
399
|
-
return;
|
|
400
|
-
this.set({ [names.modelLoading]: true });
|
|
401
|
-
const model = await gql[names.createModel](modelInput, { onError });
|
|
402
|
-
const newModelList = modelListLoading ? modelList : new import_base.DataList([...modelList.slice(0, idx ?? 0), model, ...modelList.slice(idx ?? 0)]);
|
|
403
|
-
const newModelInsight = gql[names.crystalizeInsight]({
|
|
404
|
-
...modelInsight,
|
|
405
|
-
count: modelInsight.count + 1
|
|
406
|
-
});
|
|
407
|
-
this.set({
|
|
408
|
-
[names.model]: model,
|
|
409
|
-
[names.modelLoading]: false,
|
|
410
|
-
[namesOfSlice.modelList]: newModelList,
|
|
411
|
-
[namesOfSlice.modelInsight]: newModelInsight,
|
|
412
|
-
[names.modelViewAt]: /* @__PURE__ */ new Date(),
|
|
413
|
-
[names.modelModal]: modal ?? null,
|
|
414
|
-
...typeof path === "string" && path ? { [path]: model } : {}
|
|
415
|
-
});
|
|
416
|
-
await onSuccess?.(model);
|
|
417
|
-
},
|
|
418
|
-
[names.updateModel]: async function(id, data, { idx, path, modal, sliceName = names.model, onError, onSuccess } = {}) {
|
|
419
|
-
const currentState = this.get();
|
|
420
|
-
const model = currentState[names.model];
|
|
421
|
-
const modelInput = gql[names.purifyModel](data);
|
|
422
|
-
if (!modelInput)
|
|
423
|
-
return;
|
|
424
|
-
if (model?.id === id)
|
|
425
|
-
this.set({ [names.modelLoading]: id });
|
|
426
|
-
const updatedModel = await gql[names.updateModel](id, modelInput, { onError });
|
|
427
|
-
this.set({
|
|
428
|
-
...model?.id === updatedModel.id ? { [names.model]: updatedModel, [names.modelLoading]: false, [names.modelViewAt]: /* @__PURE__ */ new Date() } : {},
|
|
429
|
-
[names.modelModal]: modal ?? null,
|
|
430
|
-
...typeof path === "string" && path ? { [path]: updatedModel } : {}
|
|
431
|
-
});
|
|
432
|
-
const updatedLightModel = gql[names.lightCrystalizeModel](updatedModel);
|
|
433
|
-
gql.slices.forEach(({ sliceName: sliceName2 }) => {
|
|
434
|
-
const namesOfSlice = {
|
|
435
|
-
modelList: sliceName2.replace(names.model, names.modelList),
|
|
436
|
-
modelListLoading: sliceName2.replace(names.model, names.modelListLoading)
|
|
437
|
-
};
|
|
438
|
-
const currentState2 = this.get();
|
|
439
|
-
const modelList = currentState2[namesOfSlice.modelList];
|
|
440
|
-
const modelListLoading = currentState2[namesOfSlice.modelListLoading];
|
|
441
|
-
if (modelListLoading || !modelList.has(updatedModel.id))
|
|
442
|
-
return;
|
|
443
|
-
const newModelList = new import_base.DataList(modelList).set(updatedLightModel);
|
|
444
|
-
this.set({ [namesOfSlice.modelList]: newModelList });
|
|
445
|
-
});
|
|
446
|
-
await onSuccess?.(updatedModel);
|
|
447
|
-
},
|
|
448
|
-
[names.removeModel]: async function(id, options) {
|
|
449
|
-
const { modal, ...fetchPolicyOptions } = options ?? {};
|
|
450
|
-
const model = await gql[names.removeModel](
|
|
451
|
-
id,
|
|
452
|
-
fetchPolicyOptions
|
|
453
|
-
);
|
|
454
|
-
const lightModel = gql[names.lightCrystalizeModel](model);
|
|
455
|
-
gql.slices.forEach(({ sliceName }) => {
|
|
456
|
-
const namesOfSlice = {
|
|
457
|
-
modelList: sliceName.replace(names.model, names.modelList),
|
|
458
|
-
modelListLoading: sliceName.replace(names.model, names.modelListLoading),
|
|
459
|
-
modelSelection: sliceName.replace(names.model, names.modelSelection),
|
|
460
|
-
modelInsight: sliceName.replace(names.model, names.modelInsight)
|
|
461
|
-
};
|
|
462
|
-
const currentState = this.get();
|
|
463
|
-
const modelList = currentState[namesOfSlice.modelList];
|
|
464
|
-
const modelListLoading = currentState[namesOfSlice.modelListLoading];
|
|
465
|
-
const modelSelection = currentState[namesOfSlice.modelSelection];
|
|
466
|
-
const modelInsight = currentState[namesOfSlice.modelInsight];
|
|
467
|
-
if (modelListLoading || !modelList.has(model.id))
|
|
468
|
-
return;
|
|
469
|
-
const newModelList = new import_base.DataList(modelList);
|
|
470
|
-
if (model.removedAt) {
|
|
471
|
-
newModelList.delete(id);
|
|
472
|
-
const newModelInsight = gql[names.crystalizeInsight]({
|
|
473
|
-
...modelInsight,
|
|
474
|
-
count: modelInsight.count - 1
|
|
475
|
-
});
|
|
476
|
-
const newModelSelection = new import_base.DataList(modelSelection);
|
|
477
|
-
newModelSelection.delete(id);
|
|
478
|
-
this.set({
|
|
479
|
-
[namesOfSlice.modelList]: newModelList,
|
|
480
|
-
[namesOfSlice.modelInsight]: newModelInsight,
|
|
481
|
-
...modelSelection.has(model.id) ? { [namesOfSlice.modelSelection]: newModelSelection } : {},
|
|
482
|
-
...modal !== void 0 ? { [names.modelModal]: modal } : {}
|
|
483
|
-
});
|
|
484
|
-
} else {
|
|
485
|
-
newModelList.set(lightModel);
|
|
486
|
-
this.set({
|
|
487
|
-
[namesOfSlice.modelList]: newModelList,
|
|
488
|
-
...modal !== void 0 ? { [names.modelModal]: modal } : {}
|
|
489
|
-
});
|
|
490
|
-
}
|
|
491
|
-
});
|
|
492
|
-
},
|
|
493
|
-
[names.checkModelSubmitable]: function(disabled) {
|
|
494
|
-
const currentState = this.get();
|
|
495
|
-
const modelForm = currentState[names.modelForm];
|
|
496
|
-
const modelSubmit = currentState[names.modelSubmit];
|
|
497
|
-
const modelInput = gql[names.purifyModel](modelForm);
|
|
498
|
-
this.set({ [names.modelSubmit]: { ...modelSubmit, disabled: !modelInput || disabled } });
|
|
499
|
-
},
|
|
500
|
-
[names.submitModel]: async function(option) {
|
|
501
|
-
const currentState = this.get();
|
|
502
|
-
const modelForm = currentState[names.modelForm];
|
|
503
|
-
const modelSubmit = currentState[names.modelSubmit];
|
|
504
|
-
this.set({ [names.modelSubmit]: { ...modelSubmit, loading: true } });
|
|
505
|
-
if (modelForm.id)
|
|
506
|
-
await this[names.updateModelInForm](option);
|
|
507
|
-
else
|
|
508
|
-
await this[names.createModelInForm](option);
|
|
509
|
-
this.set({ [names.modelSubmit]: { ...modelSubmit, loading: false, times: modelSubmit.times + 1 } });
|
|
510
|
-
},
|
|
511
|
-
[names.newModel]: function(partial = {}, { modal, setDefault, sliceName = names.model } = {}) {
|
|
512
|
-
const SliceName = (0, import_common.capitalize)(sliceName);
|
|
513
|
-
const namesOfSlice = {
|
|
514
|
-
defaultModel: SliceName.replace(names.Model, names.defaultModel)
|
|
515
|
-
};
|
|
516
|
-
const currentState = this.get();
|
|
517
|
-
const defaultModel = currentState[namesOfSlice.defaultModel];
|
|
518
|
-
this.set({
|
|
519
|
-
[names.modelForm]: (0, import_signal.immerify)(modelRef, { ...defaultModel, ...partial }),
|
|
520
|
-
[namesOfSlice.defaultModel]: setDefault ? (0, import_signal.immerify)(modelRef, { ...defaultModel, ...partial }) : defaultModel,
|
|
521
|
-
[names.model]: null,
|
|
522
|
-
[names.modelModal]: modal ?? "edit",
|
|
523
|
-
[names.modelFormLoading]: false
|
|
524
|
-
});
|
|
525
|
-
},
|
|
526
|
-
[names.editModel]: async function(modelOrId, { modal, onError } = {}) {
|
|
527
|
-
const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
|
|
528
|
-
this.set({ [names.modelFormLoading]: id, [names.modelModal]: modal ?? "edit" });
|
|
529
|
-
this.set((state) => {
|
|
530
|
-
state[names.modelForm].id = id;
|
|
531
|
-
});
|
|
532
|
-
const model = await gql[names.model](id, { onError });
|
|
533
|
-
const modelForm = (0, import_common.deepObjectify)(model);
|
|
534
|
-
this.set({
|
|
535
|
-
[names.model]: model,
|
|
536
|
-
[names.modelFormLoading]: false,
|
|
537
|
-
[names.modelViewAt]: /* @__PURE__ */ new Date(),
|
|
538
|
-
[names.modelForm]: modelForm
|
|
539
|
-
});
|
|
540
|
-
},
|
|
541
|
-
[names.mergeModel]: async function(modelOrId, data, options) {
|
|
542
|
-
const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
|
|
543
|
-
const currentState = this.get();
|
|
544
|
-
const model = currentState[names.model];
|
|
545
|
-
if (id === model?.id)
|
|
546
|
-
this.set({ modelLoading: id });
|
|
547
|
-
const updatedModel = await gql[names.mergeModel](modelOrId, data, options);
|
|
548
|
-
this.set({
|
|
549
|
-
[names.model]: id === model?.id ? updatedModel : model,
|
|
550
|
-
[names.modelLoading]: false
|
|
551
|
-
});
|
|
552
|
-
const updatedLightModel = gql[names.lightCrystalizeModel](updatedModel);
|
|
553
|
-
gql.slices.forEach(({ sliceName }) => {
|
|
554
|
-
const namesOfSlice = {
|
|
555
|
-
modelList: sliceName.replace(names.model, names.modelList),
|
|
556
|
-
modelListLoading: sliceName.replace(names.model, names.modelListLoading)
|
|
557
|
-
};
|
|
558
|
-
const currentState2 = this.get();
|
|
559
|
-
const modelList = currentState2[namesOfSlice.modelList];
|
|
560
|
-
const modelListLoading = currentState2[namesOfSlice.modelListLoading];
|
|
561
|
-
if (modelListLoading || !modelList.has(updatedModel.id))
|
|
562
|
-
return;
|
|
563
|
-
const newModelList = new import_base.DataList(modelList).set(updatedLightModel);
|
|
564
|
-
this.set({ [namesOfSlice.modelList]: newModelList });
|
|
565
|
-
});
|
|
566
|
-
},
|
|
567
|
-
[names.viewModel]: async function(modelOrId, { modal, onError } = {}) {
|
|
568
|
-
const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
|
|
569
|
-
this.set({ [names.modelModal]: modal ?? "view", [names.modelLoading]: id });
|
|
570
|
-
const model = await gql[names.model](id, { onError });
|
|
571
|
-
this.set({ [names.model]: model, [names.modelViewAt]: /* @__PURE__ */ new Date(), [names.modelLoading]: false });
|
|
572
|
-
},
|
|
573
|
-
[names.setModel]: function(fullOrLightModel) {
|
|
574
|
-
const currentState = this.get();
|
|
575
|
-
const model = currentState[names.model];
|
|
576
|
-
const isFull = fullOrLightModel instanceof modelRef;
|
|
577
|
-
if (isFull) {
|
|
578
|
-
const crystalizedModel = gql[names.crystalizeModel](fullOrLightModel);
|
|
579
|
-
this.set({ [names.model]: crystalizedModel });
|
|
580
|
-
} else if (model?.id === fullOrLightModel.id) {
|
|
581
|
-
const crystalizedModel = gql[names.crystalizeModel]({ ...model, ...fullOrLightModel });
|
|
582
|
-
this.set({ [names.model]: crystalizedModel });
|
|
583
|
-
}
|
|
584
|
-
const lightModel = gql[names.lightCrystalizeModel](fullOrLightModel);
|
|
585
|
-
gql.slices.forEach(({ sliceName }) => {
|
|
586
|
-
const namesOfSlice = {
|
|
587
|
-
modelList: sliceName.replace(names.model, names.modelList),
|
|
588
|
-
modelListLoading: sliceName.replace(names.model, names.modelListLoading)
|
|
589
|
-
};
|
|
590
|
-
const modelList = currentState[namesOfSlice.modelList];
|
|
591
|
-
const modelListLoading = currentState[namesOfSlice.modelListLoading];
|
|
592
|
-
if (modelListLoading || !modelList.has(lightModel.id))
|
|
593
|
-
return;
|
|
594
|
-
this.set({ [namesOfSlice.modelList]: modelList.set(lightModel).save() });
|
|
595
|
-
});
|
|
596
|
-
},
|
|
597
|
-
[names.resetModel]: function(model) {
|
|
598
|
-
const currentState = this.get();
|
|
599
|
-
const defaultModel = currentState[names.defaultModel];
|
|
600
|
-
this.set({
|
|
601
|
-
[names.model]: model ?? null,
|
|
602
|
-
[names.modelViewAt]: /* @__PURE__ */ new Date(0),
|
|
603
|
-
[names.modelForm]: (0, import_signal.immerify)(modelRef, defaultModel),
|
|
604
|
-
[names.modelModal]: null
|
|
605
|
-
});
|
|
606
|
-
return model ?? null;
|
|
607
|
-
}
|
|
608
|
-
};
|
|
609
|
-
const sliceAction = gql.slices.reduce((acc, { sliceName, argLength }) => {
|
|
610
|
-
const SliceName = (0, import_common.capitalize)(sliceName);
|
|
611
|
-
const namesOfSlice = {
|
|
612
|
-
defaultModel: SliceName.replace(names.Model, names.defaultModel),
|
|
613
|
-
modelInsight: sliceName.replace(names.model, names.modelInsight),
|
|
614
|
-
modelList: sliceName.replace(names.model, names.modelList),
|
|
615
|
-
modelListLoading: sliceName.replace(names.model, names.modelListLoading),
|
|
616
|
-
initModel: SliceName.replace(names.Model, names.initModel),
|
|
617
|
-
modelInitList: SliceName.replace(names.Model, names.modelInitList),
|
|
618
|
-
modelInitAt: SliceName.replace(names.Model, names.modelInitAt),
|
|
619
|
-
refreshModel: SliceName.replace(names.Model, names.refreshModel),
|
|
620
|
-
selectModel: SliceName.replace(names.Model, names.selectModel),
|
|
621
|
-
setPageOfModel: SliceName.replace(names.Model, names.setPageOfModel),
|
|
622
|
-
addPageOfModel: SliceName.replace(names.Model, names.addPageOfModel),
|
|
623
|
-
setLimitOfModel: SliceName.replace(names.Model, names.setLimitOfModel),
|
|
624
|
-
setQueryArgsOfModel: SliceName.replace(names.Model, names.setQueryArgsOfModel),
|
|
625
|
-
setSortOfModel: SliceName.replace(names.Model, names.setSortOfModel),
|
|
626
|
-
lastPageOfModel: SliceName.replace(names.Model, names.lastPageOfModel),
|
|
627
|
-
pageOfModel: SliceName.replace(names.Model, names.pageOfModel),
|
|
628
|
-
limitOfModel: SliceName.replace(names.Model, names.limitOfModel),
|
|
629
|
-
queryArgsOfModel: SliceName.replace(names.Model, names.queryArgsOfModel),
|
|
630
|
-
sortOfModel: SliceName.replace(names.Model, names.sortOfModel),
|
|
631
|
-
modelSelection: SliceName.replace(names.Model, names.modelSelection)
|
|
632
|
-
};
|
|
633
|
-
const singleSliceAction = {
|
|
634
|
-
[namesOfSlice.initModel]: async function(...args) {
|
|
635
|
-
const initArgLength = Math.min(args.length, argLength);
|
|
636
|
-
const initForm = { invalidate: false, ...args[argLength] ?? {} };
|
|
637
|
-
const queryArgs = new Array(initArgLength).fill(null).map((_, i) => args[i]);
|
|
638
|
-
const defaultModel = (0, import_signal.immerify)(modelRef, { ...gql[names.defaultModel], ...initForm.default ?? {} });
|
|
639
|
-
this.set({ [names.defaultModel]: defaultModel });
|
|
640
|
-
await this[namesOfSlice.refreshModel](
|
|
641
|
-
...initArgLength === argLength ? [...queryArgs, initForm] : queryArgs
|
|
642
|
-
);
|
|
643
|
-
},
|
|
644
|
-
[namesOfSlice.refreshModel]: async function(...args) {
|
|
645
|
-
const refreshArgLength = Math.min(args.length, argLength);
|
|
646
|
-
const currentState = this.get();
|
|
647
|
-
const existingQueryArgs = currentState[namesOfSlice.queryArgsOfModel];
|
|
648
|
-
const queryArgs = [
|
|
649
|
-
...new Array(refreshArgLength).fill(null).map((_, i) => args[i]),
|
|
650
|
-
...existingQueryArgs.slice(refreshArgLength, argLength)
|
|
651
|
-
];
|
|
652
|
-
const initForm = args[argLength] ?? {};
|
|
653
|
-
const {
|
|
654
|
-
page = currentState[namesOfSlice.pageOfModel],
|
|
655
|
-
limit = currentState[namesOfSlice.limitOfModel],
|
|
656
|
-
sort = currentState[namesOfSlice.sortOfModel],
|
|
657
|
-
invalidate = true
|
|
658
|
-
} = initForm;
|
|
659
|
-
const modelOperation = currentState[names.modelOperation];
|
|
660
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
|
|
661
|
-
const pageOfModel = currentState[namesOfSlice.pageOfModel];
|
|
662
|
-
const limitOfModel = currentState[namesOfSlice.limitOfModel];
|
|
663
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel];
|
|
664
|
-
if (!invalidate && !["sleep", "reset"].includes(modelOperation) && (0, import_common.isQueryEqual)(queryArgs, queryArgsOfModel) && page === pageOfModel && limit === limitOfModel && (0, import_common.isQueryEqual)(sort, sortOfModel))
|
|
665
|
-
return;
|
|
666
|
-
else
|
|
667
|
-
this.set({ [namesOfSlice.modelListLoading]: true });
|
|
668
|
-
const [modelDataList, modelInsight] = await Promise.all([
|
|
669
|
-
gql[namesOfSlice.modelList](
|
|
670
|
-
...queryArgs,
|
|
671
|
-
(page - 1) * limit,
|
|
672
|
-
limit,
|
|
673
|
-
sort,
|
|
674
|
-
{ onError: initForm.onError }
|
|
675
|
-
),
|
|
676
|
-
gql[namesOfSlice.modelInsight](...queryArgs, {
|
|
677
|
-
onError: initForm.onError
|
|
678
|
-
})
|
|
679
|
-
]);
|
|
680
|
-
const modelList = new import_base.DataList(modelDataList);
|
|
681
|
-
this.set({
|
|
682
|
-
[namesOfSlice.modelList]: modelList,
|
|
683
|
-
[namesOfSlice.modelListLoading]: false,
|
|
684
|
-
[namesOfSlice.modelInsight]: modelInsight,
|
|
685
|
-
[namesOfSlice.modelInitList]: modelList,
|
|
686
|
-
[namesOfSlice.modelInitAt]: /* @__PURE__ */ new Date(),
|
|
687
|
-
[namesOfSlice.lastPageOfModel]: Math.max(Math.floor((modelInsight.count - 1) / limit) + 1, 1),
|
|
688
|
-
[namesOfSlice.limitOfModel]: limit,
|
|
689
|
-
[namesOfSlice.queryArgsOfModel]: queryArgs,
|
|
690
|
-
[namesOfSlice.sortOfModel]: sort,
|
|
691
|
-
[namesOfSlice.pageOfModel]: page,
|
|
692
|
-
[names.modelOperation]: "idle"
|
|
693
|
-
});
|
|
694
|
-
},
|
|
695
|
-
[namesOfSlice.selectModel]: function(model, { refresh, remove } = {}) {
|
|
696
|
-
const models = Array.isArray(model) ? model : [model];
|
|
697
|
-
const currentState = this.get();
|
|
698
|
-
const modelSelection = currentState[namesOfSlice.modelSelection];
|
|
699
|
-
if (refresh)
|
|
700
|
-
this.set({ [namesOfSlice.modelSelection]: new import_base.DataList(models) });
|
|
701
|
-
else if (remove) {
|
|
702
|
-
const newModelSelection = new import_base.DataList(modelSelection);
|
|
703
|
-
models.map((model2) => newModelSelection.delete(model2.id));
|
|
704
|
-
this.set({ [namesOfSlice.modelSelection]: newModelSelection });
|
|
705
|
-
} else {
|
|
706
|
-
this.set({ [namesOfSlice.modelSelection]: new import_base.DataList([...modelSelection.values, ...models]) });
|
|
707
|
-
}
|
|
708
|
-
},
|
|
709
|
-
[namesOfSlice.setPageOfModel]: async function(page, options) {
|
|
710
|
-
const currentState = this.get();
|
|
711
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
|
|
712
|
-
const pageOfModel = currentState[namesOfSlice.pageOfModel];
|
|
713
|
-
const limitOfModel = currentState[namesOfSlice.limitOfModel];
|
|
714
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel];
|
|
715
|
-
if (pageOfModel === page)
|
|
716
|
-
return;
|
|
717
|
-
this.set({ [namesOfSlice.modelListLoading]: true });
|
|
718
|
-
const modelDataList = await gql[namesOfSlice.modelList](
|
|
719
|
-
...queryArgsOfModel,
|
|
720
|
-
(page - 1) * limitOfModel,
|
|
721
|
-
limitOfModel,
|
|
722
|
-
sortOfModel,
|
|
723
|
-
options
|
|
724
|
-
);
|
|
725
|
-
const modelList = new import_base.DataList(modelDataList);
|
|
726
|
-
this.set({
|
|
727
|
-
[namesOfSlice.modelList]: modelList,
|
|
728
|
-
[namesOfSlice.pageOfModel]: page,
|
|
729
|
-
[namesOfSlice.modelListLoading]: false
|
|
730
|
-
});
|
|
731
|
-
},
|
|
732
|
-
[namesOfSlice.addPageOfModel]: async function(page, options) {
|
|
733
|
-
const currentState = this.get();
|
|
734
|
-
const modelList = currentState[namesOfSlice.modelList];
|
|
735
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
|
|
736
|
-
const pageOfModel = currentState[namesOfSlice.pageOfModel];
|
|
737
|
-
const limitOfModel = currentState[namesOfSlice.limitOfModel];
|
|
738
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel];
|
|
739
|
-
if (pageOfModel === page)
|
|
740
|
-
return;
|
|
741
|
-
const addFront = page < pageOfModel;
|
|
742
|
-
const modelDataList = await gql[namesOfSlice.modelList](
|
|
743
|
-
...queryArgsOfModel,
|
|
744
|
-
(page - 1) * limitOfModel,
|
|
745
|
-
limitOfModel,
|
|
746
|
-
sortOfModel,
|
|
747
|
-
options
|
|
748
|
-
);
|
|
749
|
-
const newModelList = new import_base.DataList(
|
|
750
|
-
addFront ? [...modelDataList, ...modelList] : [...modelList, ...modelDataList]
|
|
751
|
-
);
|
|
752
|
-
this.set({ [namesOfSlice.modelList]: newModelList, [namesOfSlice.pageOfModel]: page });
|
|
753
|
-
},
|
|
754
|
-
[namesOfSlice.setLimitOfModel]: async function(limit, options) {
|
|
755
|
-
const currentState = this.get();
|
|
756
|
-
const modelInsight = currentState[namesOfSlice.modelInsight];
|
|
757
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
|
|
758
|
-
const pageOfModel = currentState[namesOfSlice.pageOfModel];
|
|
759
|
-
const limitOfModel = currentState[namesOfSlice.limitOfModel];
|
|
760
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel];
|
|
761
|
-
if (limitOfModel === limit)
|
|
762
|
-
return;
|
|
763
|
-
const skip = (pageOfModel - 1) * limitOfModel;
|
|
764
|
-
const page = Math.max(Math.floor((skip - 1) / limit) + 1, 1);
|
|
765
|
-
const modelDataList = await gql[namesOfSlice.modelList](
|
|
766
|
-
...queryArgsOfModel,
|
|
767
|
-
(page - 1) * limit,
|
|
768
|
-
limit,
|
|
769
|
-
sortOfModel,
|
|
770
|
-
options
|
|
771
|
-
);
|
|
772
|
-
const modelList = new import_base.DataList(modelDataList);
|
|
773
|
-
this.set({
|
|
774
|
-
[namesOfSlice.modelList]: modelList,
|
|
775
|
-
[namesOfSlice.lastPageOfModel]: Math.max(Math.floor((modelInsight.count - 1) / limit) + 1, 1),
|
|
776
|
-
[namesOfSlice.limitOfModel]: limit,
|
|
777
|
-
[namesOfSlice.pageOfModel]: page
|
|
778
|
-
});
|
|
779
|
-
},
|
|
780
|
-
[namesOfSlice.setQueryArgsOfModel]: async function(...args) {
|
|
781
|
-
const queryArgs = new Array(argLength).fill(null).map((_, i) => args[i]);
|
|
782
|
-
const options = args[argLength];
|
|
783
|
-
const currentState = this.get();
|
|
784
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
|
|
785
|
-
const limitOfModel = currentState[namesOfSlice.limitOfModel];
|
|
786
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel];
|
|
787
|
-
if ((0, import_common.isQueryEqual)(queryArgsOfModel, queryArgs)) {
|
|
788
|
-
import_common.Logger.trace(`${namesOfSlice.queryArgsOfModel} store-level cache hit`);
|
|
789
|
-
return;
|
|
790
|
-
}
|
|
791
|
-
this.set({ [namesOfSlice.modelListLoading]: true });
|
|
792
|
-
const [modelDataList, modelInsight] = await Promise.all([
|
|
793
|
-
gql[namesOfSlice.modelList](
|
|
794
|
-
...queryArgs,
|
|
795
|
-
0,
|
|
796
|
-
limitOfModel,
|
|
797
|
-
sortOfModel,
|
|
798
|
-
options
|
|
799
|
-
),
|
|
800
|
-
gql[namesOfSlice.modelInsight](...queryArgs, options)
|
|
801
|
-
]);
|
|
802
|
-
const modelList = new import_base.DataList(modelDataList);
|
|
803
|
-
this.set({
|
|
804
|
-
[namesOfSlice.queryArgsOfModel]: queryArgs,
|
|
805
|
-
[namesOfSlice.modelList]: modelList,
|
|
806
|
-
[namesOfSlice.modelInsight]: modelInsight,
|
|
807
|
-
[namesOfSlice.lastPageOfModel]: Math.max(Math.floor((modelInsight.count - 1) / limitOfModel) + 1, 1),
|
|
808
|
-
[namesOfSlice.pageOfModel]: 1,
|
|
809
|
-
[namesOfSlice.modelSelection]: /* @__PURE__ */ new Map(),
|
|
810
|
-
[namesOfSlice.modelListLoading]: false
|
|
811
|
-
});
|
|
812
|
-
},
|
|
813
|
-
[namesOfSlice.setSortOfModel]: async function(sort, options) {
|
|
814
|
-
const currentState = this.get();
|
|
815
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel];
|
|
816
|
-
const limitOfModel = currentState[namesOfSlice.limitOfModel];
|
|
817
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel];
|
|
818
|
-
if (sortOfModel === sort)
|
|
819
|
-
return;
|
|
820
|
-
this.set({ [namesOfSlice.modelListLoading]: true });
|
|
821
|
-
const modelDataList = await gql[namesOfSlice.modelList](
|
|
822
|
-
...queryArgsOfModel,
|
|
823
|
-
0,
|
|
824
|
-
limitOfModel,
|
|
825
|
-
sort,
|
|
826
|
-
options
|
|
827
|
-
);
|
|
828
|
-
const modelList = new import_base.DataList(modelDataList);
|
|
829
|
-
this.set({
|
|
830
|
-
[namesOfSlice.modelList]: modelList,
|
|
831
|
-
[namesOfSlice.sortOfModel]: sort,
|
|
832
|
-
[namesOfSlice.pageOfModel]: 1,
|
|
833
|
-
[namesOfSlice.modelListLoading]: false
|
|
834
|
-
});
|
|
835
|
-
}
|
|
836
|
-
};
|
|
837
|
-
return Object.assign(acc, singleSliceAction);
|
|
838
|
-
}, {});
|
|
839
|
-
return { ...baseAction, ...sliceAction };
|
|
840
|
-
};
|
|
841
|
-
var stateOf = (gql, state) => {
|
|
842
|
-
const applyState = Object.assign(createState(gql), state);
|
|
843
|
-
const applyAction = createActions(gql);
|
|
844
|
-
setStoreMeta(gql.refName, {
|
|
845
|
-
refName: gql.refName,
|
|
846
|
-
useKeys: Object.keys(applyState),
|
|
847
|
-
doKeys: Object.keys(applyAction),
|
|
848
|
-
slices: gql.slices
|
|
849
|
-
});
|
|
850
|
-
const applyStore = { ...applyState, ...applyAction };
|
|
851
|
-
class StateStore {
|
|
852
|
-
get;
|
|
853
|
-
set;
|
|
854
|
-
pick;
|
|
855
|
-
}
|
|
856
|
-
Object.keys(applyStore).forEach(
|
|
857
|
-
(key) => Object.defineProperty(StateStore.prototype, key, { value: applyStore[key] })
|
|
858
|
-
);
|
|
859
|
-
return StateStore;
|
|
860
|
-
};
|
|
861
|
-
var scalarStateOf = (refName, state) => {
|
|
862
|
-
const applyState = state;
|
|
863
|
-
setStoreMeta(refName, { refName, useKeys: Object.keys(applyState), doKeys: [], slices: [] });
|
|
864
|
-
class StateStore {
|
|
865
|
-
get;
|
|
866
|
-
set;
|
|
867
|
-
pick;
|
|
868
|
-
}
|
|
869
|
-
Object.keys(applyState).forEach(
|
|
870
|
-
(key) => Object.defineProperty(StateStore.prototype, key, { value: applyState[key] })
|
|
871
|
-
);
|
|
872
|
-
return StateStore;
|
|
873
|
-
};
|
|
874
|
-
var Store = (returnsOrObj) => {
|
|
875
|
-
const refName = typeof returnsOrObj === "object" ? returnsOrObj.name : (0, import_common.lowerlize)((0, import_constant.getClassMeta)(returnsOrObj()).refName);
|
|
876
|
-
const storeMeta = getStoreMeta(refName);
|
|
877
|
-
return function(target) {
|
|
878
|
-
const customDoKeys = Object.getOwnPropertyNames(target.prototype).filter((key) => key !== "constructor");
|
|
879
|
-
setStoreMeta(refName, { ...storeMeta, doKeys: [...storeMeta.doKeys, ...customDoKeys] });
|
|
880
|
-
};
|
|
881
|
-
};
|
|
882
|
-
var createSelectors = (_store, store = {}) => {
|
|
883
|
-
store.get = _store.getState;
|
|
884
|
-
store.set = (s) => {
|
|
885
|
-
if (typeof s === "function")
|
|
886
|
-
_store.setState((st2) => {
|
|
887
|
-
s(st2);
|
|
888
|
-
});
|
|
889
|
-
else
|
|
890
|
-
_store.setState(s);
|
|
891
|
-
};
|
|
892
|
-
store.sel = (selectFn, equals) => _store(selectFn, equals);
|
|
893
|
-
const state = store.get();
|
|
894
|
-
store.sub = _store.subscribe;
|
|
895
|
-
const useReference = (selectFn) => {
|
|
896
|
-
const ref = (0, import_react.useRef)(selectFn(store.get()));
|
|
897
|
-
(0, import_react.useEffect)(() => {
|
|
898
|
-
return store.sub(selectFn, (val) => ref.current = val);
|
|
899
|
-
}, []);
|
|
900
|
-
return ref;
|
|
901
|
-
};
|
|
902
|
-
store.ref = useReference;
|
|
903
|
-
const existingUse = store.use;
|
|
904
|
-
const existingDo = store.do;
|
|
905
|
-
const existingSlice = store.slice;
|
|
906
|
-
if (!existingUse)
|
|
907
|
-
Object.assign(store, { use: {} });
|
|
908
|
-
if (!existingDo)
|
|
909
|
-
Object.assign(store, { do: {} });
|
|
910
|
-
if (!existingSlice)
|
|
911
|
-
Object.assign(store, { slice: {} });
|
|
912
|
-
for (const k of Object.keys(state)) {
|
|
913
|
-
if (typeof state[k] !== "function") {
|
|
914
|
-
store.use[k] = () => store.sel((s) => s[k]);
|
|
915
|
-
const setKey = `set${(0, import_common.capitalize)(k)}`;
|
|
916
|
-
if (!state[setKey])
|
|
917
|
-
store.do[setKey] = (value) => {
|
|
918
|
-
store.set({ [k]: value });
|
|
919
|
-
};
|
|
920
|
-
} else {
|
|
921
|
-
store.do[k] = async (...args) => {
|
|
922
|
-
try {
|
|
923
|
-
import_common.Logger.verbose(`${k} action loading...`);
|
|
924
|
-
const start = Date.now();
|
|
925
|
-
await state[k](...args);
|
|
926
|
-
const end = Date.now();
|
|
927
|
-
import_common.Logger.verbose(`=> ${k} action dispatched (${end - start}ms)`);
|
|
928
|
-
} catch (e) {
|
|
929
|
-
const errKey = typeof e === "string" ? e : e.message;
|
|
930
|
-
import_dictionary.msg.error(errKey, { key: k });
|
|
931
|
-
throw e;
|
|
932
|
-
}
|
|
933
|
-
};
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
const storeNames = getStoreNames();
|
|
937
|
-
for (const storeName of storeNames) {
|
|
938
|
-
const [fieldName, className] = [(0, import_common.lowerlize)(storeName), (0, import_common.capitalize)(storeName)];
|
|
939
|
-
const names = {
|
|
940
|
-
model: fieldName,
|
|
941
|
-
Model: className,
|
|
942
|
-
defaultModel: `default${className}`,
|
|
943
|
-
modelInsight: `${fieldName}Insight`,
|
|
944
|
-
modelList: `${fieldName}List`,
|
|
945
|
-
modelListLoading: `${fieldName}ListLoading`,
|
|
946
|
-
modelInitList: `${fieldName}InitList`,
|
|
947
|
-
modelInitAt: `${fieldName}InitAt`,
|
|
948
|
-
pageOfModel: `pageOf${className}`,
|
|
949
|
-
limitOfModel: `limitOf${className}`,
|
|
950
|
-
queryArgsOfModel: `queryArgsOf${className}`,
|
|
951
|
-
sortOfModel: `sortOf${className}`,
|
|
952
|
-
modelSelection: `${fieldName}Selection`,
|
|
953
|
-
initModel: `init${className}`,
|
|
954
|
-
refreshModel: `refresh${className}`,
|
|
955
|
-
selectModel: `select${className}`,
|
|
956
|
-
setPageOfModel: `setPageOf${className}`,
|
|
957
|
-
addPageOfModel: `addPageOf${className}`,
|
|
958
|
-
setLimitOfModel: `setLimitOf${className}`,
|
|
959
|
-
setQueryArgsOfModel: `setQueryArgsOf${className}`,
|
|
960
|
-
setSortOfModel: `setSortOf${className}`,
|
|
961
|
-
lastPageOfModel: `lastPageOf${className}`
|
|
962
|
-
};
|
|
963
|
-
const storeMeta = getStoreMeta(storeName);
|
|
964
|
-
storeMeta.slices.forEach(({ sliceName, argLength, refName }) => {
|
|
965
|
-
const SliceName = (0, import_common.capitalize)(sliceName);
|
|
966
|
-
const namesOfSliceState = {
|
|
967
|
-
defaultModel: SliceName.replace(names.Model, names.defaultModel),
|
|
968
|
-
modelInitList: SliceName.replace(names.Model, names.modelInitList),
|
|
969
|
-
modelInsight: sliceName.replace(names.model, names.modelInsight),
|
|
970
|
-
modelList: sliceName.replace(names.model, names.modelList),
|
|
971
|
-
modelListLoading: sliceName.replace(names.model, names.modelListLoading),
|
|
972
|
-
modelInitAt: SliceName.replace(names.Model, names.modelInitAt),
|
|
973
|
-
lastPageOfModel: SliceName.replace(names.Model, names.lastPageOfModel),
|
|
974
|
-
pageOfModel: SliceName.replace(names.Model, names.pageOfModel),
|
|
975
|
-
limitOfModel: SliceName.replace(names.Model, names.limitOfModel),
|
|
976
|
-
queryArgsOfModel: SliceName.replace(names.Model, names.queryArgsOfModel),
|
|
977
|
-
sortOfModel: SliceName.replace(names.Model, names.sortOfModel),
|
|
978
|
-
modelSelection: SliceName.replace(names.Model, names.modelSelection)
|
|
979
|
-
};
|
|
980
|
-
const namesOfSliceAction = {
|
|
981
|
-
initModel: SliceName.replace(names.Model, names.initModel),
|
|
982
|
-
refreshModel: SliceName.replace(names.Model, names.refreshModel),
|
|
983
|
-
selectModel: SliceName.replace(names.Model, names.selectModel),
|
|
984
|
-
setPageOfModel: SliceName.replace(names.Model, names.setPageOfModel),
|
|
985
|
-
addPageOfModel: SliceName.replace(names.Model, names.addPageOfModel),
|
|
986
|
-
setLimitOfModel: SliceName.replace(names.Model, names.setLimitOfModel),
|
|
987
|
-
setQueryArgsOfModel: SliceName.replace(names.Model, names.setQueryArgsOfModel),
|
|
988
|
-
setSortOfModel: SliceName.replace(names.Model, names.setSortOfModel)
|
|
989
|
-
};
|
|
990
|
-
store.slice[sliceName] = { do: {}, use: {} };
|
|
991
|
-
const targetSlice = store.slice[sliceName];
|
|
992
|
-
Object.keys(namesOfSliceAction).forEach((key) => {
|
|
993
|
-
targetSlice.do[names[key]] = store.do[namesOfSliceAction[key]];
|
|
994
|
-
});
|
|
995
|
-
Object.keys(namesOfSliceState).map((key) => {
|
|
996
|
-
targetSlice.use[names[key]] = store.use[namesOfSliceState[key]];
|
|
997
|
-
targetSlice.do[`set${(0, import_common.capitalize)(names[key])}`] = store.do[`set${(0, import_common.capitalize)(namesOfSliceState[key])}`];
|
|
998
|
-
});
|
|
999
|
-
targetSlice.sliceName = sliceName;
|
|
1000
|
-
targetSlice.refName = refName;
|
|
1001
|
-
targetSlice.argLength = argLength;
|
|
1002
|
-
});
|
|
1003
|
-
}
|
|
1004
|
-
return store;
|
|
1005
|
-
};
|
|
1006
|
-
var makePicker = (set, get) => (...fields) => {
|
|
1007
|
-
const state = get();
|
|
1008
|
-
const ret = {};
|
|
1009
|
-
for (const field of fields) {
|
|
1010
|
-
const val = state[field];
|
|
1011
|
-
if (!val)
|
|
1012
|
-
throw new Error(`Field ${field} is not ready`);
|
|
1013
|
-
if (typeof val === "string" && val.length === 0)
|
|
1014
|
-
throw new Error(`Field is empty string (${field})`);
|
|
1015
|
-
else if (["self", "me"].includes(field) && !state[field].id?.length)
|
|
1016
|
-
throw new Error("Self or Me Id is not defined");
|
|
1017
|
-
ret[field] = val;
|
|
1018
|
-
}
|
|
1019
|
-
return ret;
|
|
1020
|
-
};
|
|
1021
|
-
var makeStore = (st2, storeRef, { library } = {}) => {
|
|
1022
|
-
if (library)
|
|
1023
|
-
return st2;
|
|
1024
|
-
const zustandStore = (0, import_zustand.create)(
|
|
1025
|
-
(0, import_middleware.devtools)(
|
|
1026
|
-
(0, import_middleware.subscribeWithSelector)(
|
|
1027
|
-
(0, import_immer.immer)((set, get) => {
|
|
1028
|
-
const store = {};
|
|
1029
|
-
const pick = makePicker(set, get);
|
|
1030
|
-
Object.getOwnPropertyNames(storeRef.prototype).forEach((key) => {
|
|
1031
|
-
const descriptor = Object.getOwnPropertyDescriptor(storeRef.prototype, key);
|
|
1032
|
-
if (descriptor)
|
|
1033
|
-
store[key] = descriptor.value;
|
|
1034
|
-
});
|
|
1035
|
-
Object.assign(store, { set, get, pick });
|
|
1036
|
-
return store;
|
|
1037
|
-
})
|
|
1038
|
-
),
|
|
1039
|
-
{ name: "root", anonymousActionType: "root", type: "root" }
|
|
1040
|
-
)
|
|
1041
|
-
);
|
|
1042
|
-
return createSelectors(zustandStore, st2);
|
|
1043
|
-
};
|
|
1044
|
-
var MixStore = (s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20, s21, s22, s23, s24, s25, s26, s27, s28, s29, s30) => {
|
|
1045
|
-
const stores = [
|
|
1046
|
-
s1,
|
|
1047
|
-
s2,
|
|
1048
|
-
s3,
|
|
1049
|
-
s4,
|
|
1050
|
-
s5,
|
|
1051
|
-
s6,
|
|
1052
|
-
s7,
|
|
1053
|
-
s8,
|
|
1054
|
-
s9,
|
|
1055
|
-
s10,
|
|
1056
|
-
s11,
|
|
1057
|
-
s12,
|
|
1058
|
-
s13,
|
|
1059
|
-
s14,
|
|
1060
|
-
s15,
|
|
1061
|
-
s16,
|
|
1062
|
-
s17,
|
|
1063
|
-
s18,
|
|
1064
|
-
s19,
|
|
1065
|
-
s20,
|
|
1066
|
-
s21,
|
|
1067
|
-
s22,
|
|
1068
|
-
s23,
|
|
1069
|
-
s24,
|
|
1070
|
-
s25,
|
|
1071
|
-
s26,
|
|
1072
|
-
s27,
|
|
1073
|
-
s28,
|
|
1074
|
-
s29,
|
|
1075
|
-
s30
|
|
1076
|
-
].filter((s) => !!s);
|
|
1077
|
-
class Mix {
|
|
1078
|
-
}
|
|
1079
|
-
(0, import_common.applyMixins)(Mix, stores);
|
|
1080
|
-
return Mix;
|
|
1081
|
-
};
|
|
1082
|
-
var rootStoreOf = (store) => {
|
|
1083
|
-
return Object.getPrototypeOf(store);
|
|
1084
|
-
};
|
|
1085
|
-
var Toast = ({ root, duration = 3 } = {}) => {
|
|
1086
|
-
return function(target, key, descriptor) {
|
|
1087
|
-
const originMethod = descriptor.value;
|
|
1088
|
-
descriptor.value = async function(...args) {
|
|
1089
|
-
try {
|
|
1090
|
-
import_dictionary.msg.loading(`${root ? `${root}.` : ""}${key}-loading`, { key, duration });
|
|
1091
|
-
const result = await originMethod.apply(this, args);
|
|
1092
|
-
import_dictionary.msg.success(`${root ? `${root}.` : ""}${key}-success`, { key, duration });
|
|
1093
|
-
return result;
|
|
1094
|
-
} catch (err) {
|
|
1095
|
-
const errKey = typeof err === "string" ? err : err.message;
|
|
1096
|
-
import_dictionary.msg.error(errKey, { key, duration });
|
|
1097
|
-
import_common.Logger.error(`${key} action error return: ${err}`);
|
|
1098
|
-
}
|
|
1099
|
-
};
|
|
1100
|
-
};
|
|
1101
|
-
};
|
|
17
|
+
__reExport(store_exports, require("./src"), module.exports);
|