@olovyannikov/effector-models 0.2.0 → 0.2.2
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/CHANGELOG.md +52 -0
- package/README.md +5 -1
- package/dist/index.cjs +62 -48
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +63 -49
- package/dist/index.mjs.map +1 -1
- package/dist/{model-Cav66tnw.cjs → model-DajD90ci.cjs} +159 -40
- package/dist/{model-BNsi5pxp.mjs → model-kSnLIQTE.mjs} +149 -42
- package/dist/model-kSnLIQTE.mjs.map +1 -0
- package/dist/react.cjs +1 -1
- package/dist/react.d.cts +1 -1
- package/dist/react.d.mts +1 -1
- package/dist/react.mjs +1 -1
- package/dist/{relations-CpExzbHu.d.mts → relations-B6-d4bl0.d.mts} +13 -2
- package/dist/relations-B6-d4bl0.d.mts.map +1 -0
- package/dist/{relations-pJXSBtRd.d.cts → relations-E3doGM1h.d.cts} +13 -2
- package/dist/relations-E3doGM1h.d.cts.map +1 -0
- package/package.json +2 -2
- package/dist/model-BNsi5pxp.mjs.map +0 -1
- package/dist/relations-CpExzbHu.d.mts.map +0 -1
- package/dist/relations-pJXSBtRd.d.cts.map +0 -1
|
@@ -3,6 +3,17 @@ let effector = require("effector");
|
|
|
3
3
|
/** Настоящий id по алиасу; id без алиаса возвращается как есть. */ function resolveAlias(aliases, id) {
|
|
4
4
|
return Object.hasOwn(aliases, id) ? aliases[id] : id;
|
|
5
5
|
}
|
|
6
|
+
/**
|
|
7
|
+
* Записывает собственное свойство. Обычное присваивание для ключа `__proto__` меняет прототип,
|
|
8
|
+
* а не добавляет ключ, — id и алиасы приходят снаружи и могут быть любыми строками.
|
|
9
|
+
*/ function setOwn(target, key, value) {
|
|
10
|
+
Object.defineProperty(target, key, {
|
|
11
|
+
value,
|
|
12
|
+
enumerable: true,
|
|
13
|
+
writable: true,
|
|
14
|
+
configurable: true
|
|
15
|
+
});
|
|
16
|
+
}
|
|
6
17
|
const toList = (ids) => typeof ids === "string" ? [ids] : ids;
|
|
7
18
|
const toArray = (value) => Array.isArray(value) ? value : [value];
|
|
8
19
|
const withoutAliases = (aliases, isRemoved) => {
|
|
@@ -24,6 +35,24 @@ const withoutAliases = (aliases, isRemoved) => {
|
|
|
24
35
|
};
|
|
25
36
|
}
|
|
26
37
|
const factsOf = /* @__PURE__ */ new WeakMap();
|
|
38
|
+
function takeFacts(state) {
|
|
39
|
+
const facts = factsOf.get(state) ?? [];
|
|
40
|
+
factsOf.delete(state);
|
|
41
|
+
return facts;
|
|
42
|
+
}
|
|
43
|
+
function mergeFacts(facts) {
|
|
44
|
+
const merged = [];
|
|
45
|
+
const gone = facts.flatMap((fact) => fact.type === "deleted" ? fact.payload : []);
|
|
46
|
+
if (gone.length > 0) merged.push({
|
|
47
|
+
type: "deleted",
|
|
48
|
+
payload: [...new Set(gone)]
|
|
49
|
+
});
|
|
50
|
+
for (const type of ["renamed", "missed"]) {
|
|
51
|
+
const found = facts.find((fact) => fact.type === type);
|
|
52
|
+
if (found) merged.push(found);
|
|
53
|
+
}
|
|
54
|
+
return merged;
|
|
55
|
+
}
|
|
27
56
|
function commit(state, next, facts = []) {
|
|
28
57
|
const ids = next.ids ?? state.ids;
|
|
29
58
|
const instances = next.instances ?? state.instances;
|
|
@@ -37,7 +66,14 @@ function commit(state, next, facts = []) {
|
|
|
37
66
|
if (facts.length > 0) factsOf.set(result, facts);
|
|
38
67
|
return result;
|
|
39
68
|
}
|
|
40
|
-
|
|
69
|
+
let now = 0;
|
|
70
|
+
const writtenAt = /* @__PURE__ */ new WeakMap();
|
|
71
|
+
const factAt = /* @__PURE__ */ new WeakMap();
|
|
72
|
+
/** Поле экземпляра записано после факта (payload события `deleted` или `renamed`). */ function writtenAfter(instance, field, fact) {
|
|
73
|
+
const at = factAt.get(fact);
|
|
74
|
+
return at !== void 0 && (writtenAt.get(instance)?.[field] ?? 0) > at;
|
|
75
|
+
}
|
|
76
|
+
function applyTo(state, action, ids, onWrite, fn) {
|
|
41
77
|
const { instances, aliases } = state;
|
|
42
78
|
let next = instances;
|
|
43
79
|
const missing = [];
|
|
@@ -50,7 +86,8 @@ function applyTo(state, action, ids, fn) {
|
|
|
50
86
|
const updated = fn(current);
|
|
51
87
|
if (updated !== current) {
|
|
52
88
|
if (next === instances) next = { ...instances };
|
|
53
|
-
next
|
|
89
|
+
setOwn(next, id, updated);
|
|
90
|
+
onWrite(current, updated);
|
|
54
91
|
}
|
|
55
92
|
}
|
|
56
93
|
return commit(state, { instances: next }, missing.length > 0 ? [{
|
|
@@ -90,10 +127,56 @@ function model(config) {
|
|
|
90
127
|
const $instances = $state.map((state) => state.instances);
|
|
91
128
|
const $aliases = $state.map((state) => state.aliases);
|
|
92
129
|
const $ids = $state.map((state) => state.ids);
|
|
93
|
-
const
|
|
130
|
+
const facts = $state.updates.filterMap((state) => {
|
|
131
|
+
const list = takeFacts(state);
|
|
132
|
+
return list.length > 0 ? list : void 0;
|
|
133
|
+
});
|
|
134
|
+
const factOf = (type) => facts.filterMap((list) => list.find((fact) => fact.type === type)?.payload);
|
|
94
135
|
const deleted = factOf("deleted");
|
|
95
136
|
const renamed = factOf("renamed");
|
|
96
137
|
const missed = factOf("missed");
|
|
138
|
+
const hooks = {
|
|
139
|
+
deleted: [],
|
|
140
|
+
renamed: []
|
|
141
|
+
};
|
|
142
|
+
const settle = (before, after) => {
|
|
143
|
+
if (after === before) return before;
|
|
144
|
+
let current = after;
|
|
145
|
+
const queue = takeFacts(current);
|
|
146
|
+
const seen = [];
|
|
147
|
+
while (queue.length > 0) {
|
|
148
|
+
const fact = queue.shift();
|
|
149
|
+
seen.push(fact);
|
|
150
|
+
if (fact.type === "missed") continue;
|
|
151
|
+
for (const hook of hooks[fact.type]) {
|
|
152
|
+
const next = hook(current, fact.payload);
|
|
153
|
+
if (next !== current) {
|
|
154
|
+
queue.push(...takeFacts(next));
|
|
155
|
+
current = next;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (seen.length > 0) {
|
|
160
|
+
const merged = mergeFacts(seen);
|
|
161
|
+
for (const fact of merged) if (fact.type !== "missed") factAt.set(fact.payload, now);
|
|
162
|
+
factsOf.set(current, merged);
|
|
163
|
+
}
|
|
164
|
+
return current;
|
|
165
|
+
};
|
|
166
|
+
const on = (clock, reducer) => {
|
|
167
|
+
$state.on(clock, (state, payload) => {
|
|
168
|
+
now += 1;
|
|
169
|
+
return settle(state, reducer(state, payload));
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
const tracked = /* @__PURE__ */ new Set();
|
|
173
|
+
const stamp = (prev, next) => {
|
|
174
|
+
if (tracked.size === 0) return;
|
|
175
|
+
const before = prev === void 0 ? void 0 : writtenAt.get(prev);
|
|
176
|
+
const record = {};
|
|
177
|
+
for (const field of tracked) record[field] = prev !== void 0 && prev[field] === next[field] ? before?.[field] ?? 0 : now;
|
|
178
|
+
writtenAt.set(next, record);
|
|
179
|
+
};
|
|
97
180
|
const create = (0, effector.createEvent)({ name: `${name}/create` });
|
|
98
181
|
const reduceCreate = (state, params) => {
|
|
99
182
|
const items = toArray(params);
|
|
@@ -102,10 +185,12 @@ function model(config) {
|
|
|
102
185
|
const added = [];
|
|
103
186
|
for (const { id, data } of items) {
|
|
104
187
|
if (!Object.hasOwn(instances, id)) added.push(id);
|
|
105
|
-
|
|
188
|
+
const instance = {
|
|
106
189
|
...defaults,
|
|
107
190
|
...data
|
|
108
191
|
};
|
|
192
|
+
setOwn(instances, id, instance);
|
|
193
|
+
stamp(void 0, instance);
|
|
109
194
|
}
|
|
110
195
|
return commit(state, {
|
|
111
196
|
ids: added.length > 0 ? [...state.ids, ...added] : state.ids,
|
|
@@ -113,7 +198,7 @@ function model(config) {
|
|
|
113
198
|
aliases: withoutAliases(state.aliases, (alias) => items.some((item) => item.id === alias))
|
|
114
199
|
});
|
|
115
200
|
};
|
|
116
|
-
|
|
201
|
+
on(create, reduceCreate);
|
|
117
202
|
const upsert = (0, effector.createEvent)({ name: `${name}/upsert` });
|
|
118
203
|
const reduceUpsert = (state, params) => {
|
|
119
204
|
const items = toArray(params);
|
|
@@ -124,27 +209,30 @@ function model(config) {
|
|
|
124
209
|
const id = resolveAlias(state.aliases, given);
|
|
125
210
|
const exists = Object.hasOwn(instances, id);
|
|
126
211
|
if (!exists) added.push(id);
|
|
127
|
-
instances[id]
|
|
128
|
-
|
|
212
|
+
const prev = exists ? instances[id] : void 0;
|
|
213
|
+
const instance = {
|
|
214
|
+
...prev ?? defaults,
|
|
129
215
|
...data
|
|
130
216
|
};
|
|
217
|
+
setOwn(instances, id, instance);
|
|
218
|
+
stamp(prev, instance);
|
|
131
219
|
}
|
|
132
220
|
return commit(state, {
|
|
133
221
|
ids: added.length > 0 ? [...state.ids, ...added] : state.ids,
|
|
134
222
|
instances
|
|
135
223
|
});
|
|
136
224
|
};
|
|
137
|
-
|
|
225
|
+
on(upsert, reduceUpsert);
|
|
138
226
|
const update = (0, effector.createEvent)({ name: `${name}/update` });
|
|
139
|
-
|
|
227
|
+
on(update, (state, { id, data }) => applyTo(state, "update", id, stamp, (instance) => typeof data === "function" ? data(instance) : {
|
|
140
228
|
...instance,
|
|
141
229
|
...data
|
|
142
230
|
}));
|
|
143
231
|
const remove = (0, effector.createEvent)({ name: `${name}/delete` });
|
|
144
232
|
const reduceDelete = (state, ids) => removeFrom(state, toList(ids).map((id) => resolveAlias(state.aliases, id)));
|
|
145
|
-
|
|
233
|
+
on(remove, reduceDelete);
|
|
146
234
|
const rename = (0, effector.createEvent)({ name: `${name}/rename` });
|
|
147
|
-
|
|
235
|
+
on(rename, (state, params) => {
|
|
148
236
|
const from = resolveAlias(state.aliases, params.from);
|
|
149
237
|
const { to } = params;
|
|
150
238
|
if (!Object.hasOwn(state.instances, from)) return commit(state, {}, [{
|
|
@@ -155,16 +243,14 @@ function model(config) {
|
|
|
155
243
|
}
|
|
156
244
|
}]);
|
|
157
245
|
if (from === to) return state;
|
|
158
|
-
const instances = {
|
|
159
|
-
|
|
160
|
-
[to]: state.instances[from]
|
|
161
|
-
};
|
|
246
|
+
const instances = { ...state.instances };
|
|
247
|
+
setOwn(instances, to, state.instances[from]);
|
|
162
248
|
delete instances[from];
|
|
163
249
|
const ids = state.ids.filter((id) => id !== to).map((id) => id === from ? to : id);
|
|
164
250
|
let aliases = withoutAliases(state.aliases, (alias) => alias === to);
|
|
165
251
|
for (const [alias, id] of Object.entries(aliases)) if (id === from) {
|
|
166
252
|
if (aliases === state.aliases) aliases = { ...state.aliases };
|
|
167
|
-
aliases
|
|
253
|
+
setOwn(aliases, alias, to);
|
|
168
254
|
}
|
|
169
255
|
return commit(state, {
|
|
170
256
|
ids,
|
|
@@ -179,7 +265,7 @@ function model(config) {
|
|
|
179
265
|
}]);
|
|
180
266
|
});
|
|
181
267
|
const clear = (0, effector.createEvent)({ name: `${name}/clear` });
|
|
182
|
-
|
|
268
|
+
on(clear, (state) => {
|
|
183
269
|
const gone = [...state.ids, ...Object.keys(state.aliases)];
|
|
184
270
|
return gone.length === 0 ? state : commit(state, {
|
|
185
271
|
ids: [],
|
|
@@ -191,46 +277,63 @@ function model(config) {
|
|
|
191
277
|
}]);
|
|
192
278
|
});
|
|
193
279
|
const alias = (0, effector.createEvent)({ name: `${name}/alias` });
|
|
194
|
-
|
|
280
|
+
on(alias, (state, params) => {
|
|
195
281
|
let aliases = state.aliases;
|
|
196
282
|
for (const { alias: given, id } of toArray(params)) {
|
|
197
283
|
const target = resolveAlias(aliases, id);
|
|
198
284
|
if (!Object.hasOwn(state.instances, target) || Object.hasOwn(state.instances, given) || Object.hasOwn(aliases, given) && aliases[given] === target) continue;
|
|
199
285
|
if (aliases === state.aliases) aliases = { ...state.aliases };
|
|
200
|
-
aliases
|
|
286
|
+
setOwn(aliases, given, target);
|
|
201
287
|
}
|
|
202
288
|
return commit(state, { aliases });
|
|
203
289
|
});
|
|
204
290
|
const unalias = (0, effector.createEvent)({ name: `${name}/unalias` });
|
|
205
|
-
|
|
291
|
+
on(unalias, (state, ids) => {
|
|
206
292
|
const removed = toList(ids);
|
|
207
293
|
return commit(state, { aliases: withoutAliases(state.aliases, (item) => removed.includes(item)) });
|
|
208
294
|
});
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
295
|
+
const applyLink = (state, target, payload) => {
|
|
296
|
+
switch (target.to) {
|
|
297
|
+
case "create":
|
|
298
|
+
case "upsert": {
|
|
299
|
+
const params = target.fn(payload);
|
|
300
|
+
if (params === void 0) return state;
|
|
301
|
+
return target.to === "create" ? reduceCreate(state, params) : reduceUpsert(state, params);
|
|
302
|
+
}
|
|
303
|
+
case "delete": {
|
|
304
|
+
const ids = target.fn(payload);
|
|
305
|
+
return ids === void 0 ? state : reduceDelete(state, ids);
|
|
306
|
+
}
|
|
307
|
+
case "deleteWhere": {
|
|
308
|
+
const isDeleted = target.fn(payload);
|
|
309
|
+
return removeFrom(state, state.ids.filter((id) => isDeleted(state.instances[id], id)));
|
|
310
|
+
}
|
|
311
|
+
case "patch": {
|
|
312
|
+
const instances = target.fn(payload)(state.instances);
|
|
313
|
+
if (instances !== state.instances) for (const id of state.ids) {
|
|
314
|
+
const prev = state.instances[id];
|
|
315
|
+
const next = instances[id];
|
|
316
|
+
if (next !== prev) {
|
|
317
|
+
const record = writtenAt.get(prev);
|
|
318
|
+
if (record) writtenAt.set(next, record);
|
|
319
|
+
}
|
|
225
320
|
}
|
|
226
|
-
|
|
321
|
+
return commit(state, { instances });
|
|
227
322
|
}
|
|
228
|
-
}
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
const link = (clock, target) => {
|
|
326
|
+
on(clock.map((payload) => payload), (state, payload) => applyLink(state, target, payload));
|
|
327
|
+
};
|
|
328
|
+
const react = (source, fact, target) => {
|
|
329
|
+
const own = fact === "deleted" ? deleted : renamed;
|
|
330
|
+
if (source[fact] === own) hooks[fact].push((state, payload) => applyLink(state, target, payload));
|
|
331
|
+
else link(source[fact], target);
|
|
229
332
|
};
|
|
230
333
|
const events = {};
|
|
231
334
|
for (const [action, reducer] of Object.entries(reducers)) {
|
|
232
335
|
const event = (0, effector.createEvent)({ name: `${name}/${action}` });
|
|
233
|
-
|
|
336
|
+
on(event, (state, { id, payload }) => applyTo(state, action, id, stamp, (instance) => reducer(instance, payload)));
|
|
234
337
|
events[action] = event;
|
|
235
338
|
}
|
|
236
339
|
return {
|
|
@@ -252,7 +355,11 @@ function model(config) {
|
|
|
252
355
|
clear,
|
|
253
356
|
events,
|
|
254
357
|
missed,
|
|
255
|
-
"~link": link
|
|
358
|
+
"~link": link,
|
|
359
|
+
"~react": react,
|
|
360
|
+
"~track": (field) => {
|
|
361
|
+
tracked.add(field);
|
|
362
|
+
}
|
|
256
363
|
};
|
|
257
364
|
}
|
|
258
365
|
Object.defineProperty(exports, "model", {
|
|
@@ -273,3 +380,15 @@ Object.defineProperty(exports, "resolveAlias", {
|
|
|
273
380
|
return resolveAlias;
|
|
274
381
|
}
|
|
275
382
|
});
|
|
383
|
+
Object.defineProperty(exports, "setOwn", {
|
|
384
|
+
enumerable: true,
|
|
385
|
+
get: function() {
|
|
386
|
+
return setOwn;
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
Object.defineProperty(exports, "writtenAfter", {
|
|
390
|
+
enumerable: true,
|
|
391
|
+
get: function() {
|
|
392
|
+
return writtenAfter;
|
|
393
|
+
}
|
|
394
|
+
});
|
|
@@ -3,6 +3,17 @@ import { createEvent, createStore } from "effector";
|
|
|
3
3
|
/** Настоящий id по алиасу; id без алиаса возвращается как есть. */ function resolveAlias(aliases, id) {
|
|
4
4
|
return Object.hasOwn(aliases, id) ? aliases[id] : id;
|
|
5
5
|
}
|
|
6
|
+
/**
|
|
7
|
+
* Записывает собственное свойство. Обычное присваивание для ключа `__proto__` меняет прототип,
|
|
8
|
+
* а не добавляет ключ, — id и алиасы приходят снаружи и могут быть любыми строками.
|
|
9
|
+
*/ function setOwn(target, key, value) {
|
|
10
|
+
Object.defineProperty(target, key, {
|
|
11
|
+
value,
|
|
12
|
+
enumerable: true,
|
|
13
|
+
writable: true,
|
|
14
|
+
configurable: true
|
|
15
|
+
});
|
|
16
|
+
}
|
|
6
17
|
const toList = (ids) => typeof ids === "string" ? [ids] : ids;
|
|
7
18
|
const toArray = (value) => Array.isArray(value) ? value : [value];
|
|
8
19
|
const withoutAliases = (aliases, isRemoved) => {
|
|
@@ -24,6 +35,24 @@ const withoutAliases = (aliases, isRemoved) => {
|
|
|
24
35
|
};
|
|
25
36
|
}
|
|
26
37
|
const factsOf = /* @__PURE__ */ new WeakMap();
|
|
38
|
+
function takeFacts(state) {
|
|
39
|
+
const facts = factsOf.get(state) ?? [];
|
|
40
|
+
factsOf.delete(state);
|
|
41
|
+
return facts;
|
|
42
|
+
}
|
|
43
|
+
function mergeFacts(facts) {
|
|
44
|
+
const merged = [];
|
|
45
|
+
const gone = facts.flatMap((fact) => fact.type === "deleted" ? fact.payload : []);
|
|
46
|
+
if (gone.length > 0) merged.push({
|
|
47
|
+
type: "deleted",
|
|
48
|
+
payload: [...new Set(gone)]
|
|
49
|
+
});
|
|
50
|
+
for (const type of ["renamed", "missed"]) {
|
|
51
|
+
const found = facts.find((fact) => fact.type === type);
|
|
52
|
+
if (found) merged.push(found);
|
|
53
|
+
}
|
|
54
|
+
return merged;
|
|
55
|
+
}
|
|
27
56
|
function commit(state, next, facts = []) {
|
|
28
57
|
const ids = next.ids ?? state.ids;
|
|
29
58
|
const instances = next.instances ?? state.instances;
|
|
@@ -37,7 +66,14 @@ function commit(state, next, facts = []) {
|
|
|
37
66
|
if (facts.length > 0) factsOf.set(result, facts);
|
|
38
67
|
return result;
|
|
39
68
|
}
|
|
40
|
-
|
|
69
|
+
let now = 0;
|
|
70
|
+
const writtenAt = /* @__PURE__ */ new WeakMap();
|
|
71
|
+
const factAt = /* @__PURE__ */ new WeakMap();
|
|
72
|
+
/** Поле экземпляра записано после факта (payload события `deleted` или `renamed`). */ function writtenAfter(instance, field, fact) {
|
|
73
|
+
const at = factAt.get(fact);
|
|
74
|
+
return at !== void 0 && (writtenAt.get(instance)?.[field] ?? 0) > at;
|
|
75
|
+
}
|
|
76
|
+
function applyTo(state, action, ids, onWrite, fn) {
|
|
41
77
|
const { instances, aliases } = state;
|
|
42
78
|
let next = instances;
|
|
43
79
|
const missing = [];
|
|
@@ -50,7 +86,8 @@ function applyTo(state, action, ids, fn) {
|
|
|
50
86
|
const updated = fn(current);
|
|
51
87
|
if (updated !== current) {
|
|
52
88
|
if (next === instances) next = { ...instances };
|
|
53
|
-
next
|
|
89
|
+
setOwn(next, id, updated);
|
|
90
|
+
onWrite(current, updated);
|
|
54
91
|
}
|
|
55
92
|
}
|
|
56
93
|
return commit(state, { instances: next }, missing.length > 0 ? [{
|
|
@@ -90,10 +127,56 @@ function model(config) {
|
|
|
90
127
|
const $instances = $state.map((state) => state.instances);
|
|
91
128
|
const $aliases = $state.map((state) => state.aliases);
|
|
92
129
|
const $ids = $state.map((state) => state.ids);
|
|
93
|
-
const
|
|
130
|
+
const facts = $state.updates.filterMap((state) => {
|
|
131
|
+
const list = takeFacts(state);
|
|
132
|
+
return list.length > 0 ? list : void 0;
|
|
133
|
+
});
|
|
134
|
+
const factOf = (type) => facts.filterMap((list) => list.find((fact) => fact.type === type)?.payload);
|
|
94
135
|
const deleted = factOf("deleted");
|
|
95
136
|
const renamed = factOf("renamed");
|
|
96
137
|
const missed = factOf("missed");
|
|
138
|
+
const hooks = {
|
|
139
|
+
deleted: [],
|
|
140
|
+
renamed: []
|
|
141
|
+
};
|
|
142
|
+
const settle = (before, after) => {
|
|
143
|
+
if (after === before) return before;
|
|
144
|
+
let current = after;
|
|
145
|
+
const queue = takeFacts(current);
|
|
146
|
+
const seen = [];
|
|
147
|
+
while (queue.length > 0) {
|
|
148
|
+
const fact = queue.shift();
|
|
149
|
+
seen.push(fact);
|
|
150
|
+
if (fact.type === "missed") continue;
|
|
151
|
+
for (const hook of hooks[fact.type]) {
|
|
152
|
+
const next = hook(current, fact.payload);
|
|
153
|
+
if (next !== current) {
|
|
154
|
+
queue.push(...takeFacts(next));
|
|
155
|
+
current = next;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (seen.length > 0) {
|
|
160
|
+
const merged = mergeFacts(seen);
|
|
161
|
+
for (const fact of merged) if (fact.type !== "missed") factAt.set(fact.payload, now);
|
|
162
|
+
factsOf.set(current, merged);
|
|
163
|
+
}
|
|
164
|
+
return current;
|
|
165
|
+
};
|
|
166
|
+
const on = (clock, reducer) => {
|
|
167
|
+
$state.on(clock, (state, payload) => {
|
|
168
|
+
now += 1;
|
|
169
|
+
return settle(state, reducer(state, payload));
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
const tracked = /* @__PURE__ */ new Set();
|
|
173
|
+
const stamp = (prev, next) => {
|
|
174
|
+
if (tracked.size === 0) return;
|
|
175
|
+
const before = prev === void 0 ? void 0 : writtenAt.get(prev);
|
|
176
|
+
const record = {};
|
|
177
|
+
for (const field of tracked) record[field] = prev !== void 0 && prev[field] === next[field] ? before?.[field] ?? 0 : now;
|
|
178
|
+
writtenAt.set(next, record);
|
|
179
|
+
};
|
|
97
180
|
const create = createEvent({ name: `${name}/create` });
|
|
98
181
|
const reduceCreate = (state, params) => {
|
|
99
182
|
const items = toArray(params);
|
|
@@ -102,10 +185,12 @@ function model(config) {
|
|
|
102
185
|
const added = [];
|
|
103
186
|
for (const { id, data } of items) {
|
|
104
187
|
if (!Object.hasOwn(instances, id)) added.push(id);
|
|
105
|
-
|
|
188
|
+
const instance = {
|
|
106
189
|
...defaults,
|
|
107
190
|
...data
|
|
108
191
|
};
|
|
192
|
+
setOwn(instances, id, instance);
|
|
193
|
+
stamp(void 0, instance);
|
|
109
194
|
}
|
|
110
195
|
return commit(state, {
|
|
111
196
|
ids: added.length > 0 ? [...state.ids, ...added] : state.ids,
|
|
@@ -113,7 +198,7 @@ function model(config) {
|
|
|
113
198
|
aliases: withoutAliases(state.aliases, (alias) => items.some((item) => item.id === alias))
|
|
114
199
|
});
|
|
115
200
|
};
|
|
116
|
-
|
|
201
|
+
on(create, reduceCreate);
|
|
117
202
|
const upsert = createEvent({ name: `${name}/upsert` });
|
|
118
203
|
const reduceUpsert = (state, params) => {
|
|
119
204
|
const items = toArray(params);
|
|
@@ -124,27 +209,30 @@ function model(config) {
|
|
|
124
209
|
const id = resolveAlias(state.aliases, given);
|
|
125
210
|
const exists = Object.hasOwn(instances, id);
|
|
126
211
|
if (!exists) added.push(id);
|
|
127
|
-
instances[id]
|
|
128
|
-
|
|
212
|
+
const prev = exists ? instances[id] : void 0;
|
|
213
|
+
const instance = {
|
|
214
|
+
...prev ?? defaults,
|
|
129
215
|
...data
|
|
130
216
|
};
|
|
217
|
+
setOwn(instances, id, instance);
|
|
218
|
+
stamp(prev, instance);
|
|
131
219
|
}
|
|
132
220
|
return commit(state, {
|
|
133
221
|
ids: added.length > 0 ? [...state.ids, ...added] : state.ids,
|
|
134
222
|
instances
|
|
135
223
|
});
|
|
136
224
|
};
|
|
137
|
-
|
|
225
|
+
on(upsert, reduceUpsert);
|
|
138
226
|
const update = createEvent({ name: `${name}/update` });
|
|
139
|
-
|
|
227
|
+
on(update, (state, { id, data }) => applyTo(state, "update", id, stamp, (instance) => typeof data === "function" ? data(instance) : {
|
|
140
228
|
...instance,
|
|
141
229
|
...data
|
|
142
230
|
}));
|
|
143
231
|
const remove = createEvent({ name: `${name}/delete` });
|
|
144
232
|
const reduceDelete = (state, ids) => removeFrom(state, toList(ids).map((id) => resolveAlias(state.aliases, id)));
|
|
145
|
-
|
|
233
|
+
on(remove, reduceDelete);
|
|
146
234
|
const rename = createEvent({ name: `${name}/rename` });
|
|
147
|
-
|
|
235
|
+
on(rename, (state, params) => {
|
|
148
236
|
const from = resolveAlias(state.aliases, params.from);
|
|
149
237
|
const { to } = params;
|
|
150
238
|
if (!Object.hasOwn(state.instances, from)) return commit(state, {}, [{
|
|
@@ -155,16 +243,14 @@ function model(config) {
|
|
|
155
243
|
}
|
|
156
244
|
}]);
|
|
157
245
|
if (from === to) return state;
|
|
158
|
-
const instances = {
|
|
159
|
-
|
|
160
|
-
[to]: state.instances[from]
|
|
161
|
-
};
|
|
246
|
+
const instances = { ...state.instances };
|
|
247
|
+
setOwn(instances, to, state.instances[from]);
|
|
162
248
|
delete instances[from];
|
|
163
249
|
const ids = state.ids.filter((id) => id !== to).map((id) => id === from ? to : id);
|
|
164
250
|
let aliases = withoutAliases(state.aliases, (alias) => alias === to);
|
|
165
251
|
for (const [alias, id] of Object.entries(aliases)) if (id === from) {
|
|
166
252
|
if (aliases === state.aliases) aliases = { ...state.aliases };
|
|
167
|
-
aliases
|
|
253
|
+
setOwn(aliases, alias, to);
|
|
168
254
|
}
|
|
169
255
|
return commit(state, {
|
|
170
256
|
ids,
|
|
@@ -179,7 +265,7 @@ function model(config) {
|
|
|
179
265
|
}]);
|
|
180
266
|
});
|
|
181
267
|
const clear = createEvent({ name: `${name}/clear` });
|
|
182
|
-
|
|
268
|
+
on(clear, (state) => {
|
|
183
269
|
const gone = [...state.ids, ...Object.keys(state.aliases)];
|
|
184
270
|
return gone.length === 0 ? state : commit(state, {
|
|
185
271
|
ids: [],
|
|
@@ -191,46 +277,63 @@ function model(config) {
|
|
|
191
277
|
}]);
|
|
192
278
|
});
|
|
193
279
|
const alias = createEvent({ name: `${name}/alias` });
|
|
194
|
-
|
|
280
|
+
on(alias, (state, params) => {
|
|
195
281
|
let aliases = state.aliases;
|
|
196
282
|
for (const { alias: given, id } of toArray(params)) {
|
|
197
283
|
const target = resolveAlias(aliases, id);
|
|
198
284
|
if (!Object.hasOwn(state.instances, target) || Object.hasOwn(state.instances, given) || Object.hasOwn(aliases, given) && aliases[given] === target) continue;
|
|
199
285
|
if (aliases === state.aliases) aliases = { ...state.aliases };
|
|
200
|
-
aliases
|
|
286
|
+
setOwn(aliases, given, target);
|
|
201
287
|
}
|
|
202
288
|
return commit(state, { aliases });
|
|
203
289
|
});
|
|
204
290
|
const unalias = createEvent({ name: `${name}/unalias` });
|
|
205
|
-
|
|
291
|
+
on(unalias, (state, ids) => {
|
|
206
292
|
const removed = toList(ids);
|
|
207
293
|
return commit(state, { aliases: withoutAliases(state.aliases, (item) => removed.includes(item)) });
|
|
208
294
|
});
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
295
|
+
const applyLink = (state, target, payload) => {
|
|
296
|
+
switch (target.to) {
|
|
297
|
+
case "create":
|
|
298
|
+
case "upsert": {
|
|
299
|
+
const params = target.fn(payload);
|
|
300
|
+
if (params === void 0) return state;
|
|
301
|
+
return target.to === "create" ? reduceCreate(state, params) : reduceUpsert(state, params);
|
|
302
|
+
}
|
|
303
|
+
case "delete": {
|
|
304
|
+
const ids = target.fn(payload);
|
|
305
|
+
return ids === void 0 ? state : reduceDelete(state, ids);
|
|
306
|
+
}
|
|
307
|
+
case "deleteWhere": {
|
|
308
|
+
const isDeleted = target.fn(payload);
|
|
309
|
+
return removeFrom(state, state.ids.filter((id) => isDeleted(state.instances[id], id)));
|
|
310
|
+
}
|
|
311
|
+
case "patch": {
|
|
312
|
+
const instances = target.fn(payload)(state.instances);
|
|
313
|
+
if (instances !== state.instances) for (const id of state.ids) {
|
|
314
|
+
const prev = state.instances[id];
|
|
315
|
+
const next = instances[id];
|
|
316
|
+
if (next !== prev) {
|
|
317
|
+
const record = writtenAt.get(prev);
|
|
318
|
+
if (record) writtenAt.set(next, record);
|
|
319
|
+
}
|
|
225
320
|
}
|
|
226
|
-
|
|
321
|
+
return commit(state, { instances });
|
|
227
322
|
}
|
|
228
|
-
}
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
const link = (clock, target) => {
|
|
326
|
+
on(clock.map((payload) => payload), (state, payload) => applyLink(state, target, payload));
|
|
327
|
+
};
|
|
328
|
+
const react = (source, fact, target) => {
|
|
329
|
+
const own = fact === "deleted" ? deleted : renamed;
|
|
330
|
+
if (source[fact] === own) hooks[fact].push((state, payload) => applyLink(state, target, payload));
|
|
331
|
+
else link(source[fact], target);
|
|
229
332
|
};
|
|
230
333
|
const events = {};
|
|
231
334
|
for (const [action, reducer] of Object.entries(reducers)) {
|
|
232
335
|
const event = createEvent({ name: `${name}/${action}` });
|
|
233
|
-
|
|
336
|
+
on(event, (state, { id, payload }) => applyTo(state, action, id, stamp, (instance) => reducer(instance, payload)));
|
|
234
337
|
events[action] = event;
|
|
235
338
|
}
|
|
236
339
|
return {
|
|
@@ -252,9 +355,13 @@ function model(config) {
|
|
|
252
355
|
clear,
|
|
253
356
|
events,
|
|
254
357
|
missed,
|
|
255
|
-
"~link": link
|
|
358
|
+
"~link": link,
|
|
359
|
+
"~react": react,
|
|
360
|
+
"~track": (field) => {
|
|
361
|
+
tracked.add(field);
|
|
362
|
+
}
|
|
256
363
|
};
|
|
257
364
|
}
|
|
258
|
-
export { modelState as n, resolveAlias as r, model as t };
|
|
365
|
+
export { writtenAfter as a, setOwn as i, modelState as n, resolveAlias as r, model as t };
|
|
259
366
|
|
|
260
|
-
//# sourceMappingURL=model-
|
|
367
|
+
//# sourceMappingURL=model-kSnLIQTE.mjs.map
|