@olovyannikov/effector-models 0.1.1 → 0.2.0
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 +60 -0
- package/README.md +26 -7
- package/dist/index.cjs +53 -72
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +54 -74
- package/dist/index.mjs.map +1 -1
- package/dist/model-BNsi5pxp.mjs +260 -0
- package/dist/model-BNsi5pxp.mjs.map +1 -0
- package/dist/model-Cav66tnw.cjs +275 -0
- package/dist/react.cjs +52 -20
- package/dist/react.d.cts +29 -3
- package/dist/react.d.cts.map +1 -1
- package/dist/react.d.mts +29 -3
- package/dist/react.d.mts.map +1 -1
- package/dist/react.mjs +52 -20
- package/dist/react.mjs.map +1 -1
- package/dist/{relations-Cc9Mv0m7.d.mts → relations-CpExzbHu.d.mts} +53 -5
- package/dist/relations-CpExzbHu.d.mts.map +1 -0
- package/dist/{relations-DXJ6slzh.d.cts → relations-pJXSBtRd.d.cts} +53 -5
- package/dist/relations-pJXSBtRd.d.cts.map +1 -0
- package/package.json +1 -1
- package/dist/model-DYSAS481.mjs +0 -275
- package/dist/model-DYSAS481.mjs.map +0 -1
- package/dist/model-iPgSwajA.cjs +0 -284
- package/dist/relations-Cc9Mv0m7.d.mts.map +0 -1
- package/dist/relations-DXJ6slzh.d.cts.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,65 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
A review of the core found that writes could be lost or reordered. The model
|
|
6
|
+
is rebuilt on one store with reducers, which changes how it is seeded and
|
|
7
|
+
serialized.
|
|
8
|
+
|
|
9
|
+
### Breaking
|
|
10
|
+
|
|
11
|
+
- **One writable store per model, `$state`**: `{ ids, instances, aliases }`
|
|
12
|
+
under the sid `effector-models:<name>`. `$instances`, `$ids` and `$aliases`
|
|
13
|
+
are derived from it and are no longer writable. The separate
|
|
14
|
+
`effector-models:<name>:aliases` sid is gone.
|
|
15
|
+
- Seed with `fork({ values: [[Model.$state, modelState({ ... })]] })`
|
|
16
|
+
instead of `[[Model.$instances, { ... }]]`.
|
|
17
|
+
- Serialized state has the new shape; state serialized by 0.1.x does not
|
|
18
|
+
hydrate.
|
|
19
|
+
- `clear` reports the deleted ids and aliases in `deleted`.
|
|
20
|
+
- `ref` requires a single-reference field to accept `null`
|
|
21
|
+
(`string | null`): that is what a deleted target is replaced with.
|
|
22
|
+
- `ModelLike` requires `$ids` and the internal `~link`.
|
|
23
|
+
|
|
24
|
+
### Fixed
|
|
25
|
+
|
|
26
|
+
- **Lost writes.** Commands launched one after another in the same tick —
|
|
27
|
+
`target: [a, b]`, two prepends of one event — were coalesced: only the last
|
|
28
|
+
payload of an instance event survived, and `rename` → `update` or
|
|
29
|
+
`delete` → `create` ran in the wrong order. Every write is a reducer of
|
|
30
|
+
`$state` now and is applied in order.
|
|
31
|
+
- **Lost facts in relations.** When `deleted` or `renamed` fired twice in one
|
|
32
|
+
tick, `child` and `ref` saw only the last one: a tree with a `ref` to
|
|
33
|
+
itself kept links to a deleted root, a tree with a second `child` kept the
|
|
34
|
+
orphans, `union.delete` with mixed kinds cleaned only the last kind.
|
|
35
|
+
- **Order of ids.** `$ids` and `$idsByParent` follow creation order also for
|
|
36
|
+
ids that look like numbers (`'10'` before `'2'`), and `rename` keeps the
|
|
37
|
+
instance in its place instead of moving it to the end.
|
|
38
|
+
- Ids equal to `Object.prototype` property names (`'constructor'`,
|
|
39
|
+
`'toString'`) were treated as existing instances by `update`, `upsert` and
|
|
40
|
+
the hooks.
|
|
41
|
+
- `useChildren` and `useModel` threw when a request switched from a selector
|
|
42
|
+
that returned `null` to plain children.
|
|
43
|
+
- `clear` on an empty model no longer marks the store as changed; `$ids` no
|
|
44
|
+
longer updates when only a field of an instance changes.
|
|
45
|
+
- A reducer with an optional payload (`(state, by?: number) => ...`) no
|
|
46
|
+
longer requires `payload` in the event params and in the bound event.
|
|
47
|
+
|
|
48
|
+
### Added
|
|
49
|
+
|
|
50
|
+
- `modelState(instances, aliases?)` — the value of `$state` for seeding.
|
|
51
|
+
- `ModelState` and `ModelLink` types.
|
|
52
|
+
|
|
53
|
+
## 0.1.2
|
|
54
|
+
|
|
55
|
+
### Added
|
|
56
|
+
|
|
57
|
+
- **React**: `useModel(union, { kind, id })` — the same hook for an instance
|
|
58
|
+
of a union. The result is narrowed by `kind`: `state` and `events` belong
|
|
59
|
+
to the model of that kind; `update` and `remove` go to it as well. It is
|
|
60
|
+
`undefined` when there is no such instance. `UnionModelView` is the type
|
|
61
|
+
of the result.
|
|
62
|
+
|
|
3
63
|
## 0.1.1
|
|
4
64
|
|
|
5
65
|
### Added
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Fork of [`@effector-kit/models`](https://github.com/movpushmov/effector-kit) by
|
|
|
10
10
|
| Instance data | mutated in place, `$instances` keeps its reference | immutable; untouched instances keep their reference |
|
|
11
11
|
| `map` / `combine` / `useStoreMap` over `$instances` | miss writes | just work |
|
|
12
12
|
| Runtime | global context, writes into `scope.reg` | public Effector API only |
|
|
13
|
-
| `fork({ values })` / `serialize` | `$instances` read-only in types, instance stores `serialize: 'ignore'` | one `$
|
|
13
|
+
| `fork({ values })` / `serialize` | `$instances` read-only in types, instance stores `serialize: 'ignore'` | one `$state` store with sid `effector-models:<name>` |
|
|
14
14
|
| Contract defaults on `create` | not applied | applied |
|
|
15
15
|
| `create` over existing id | silently replaces | `create` replaces, `upsert` merges |
|
|
16
16
|
| Write to missing instance | silent `Promise.reject` | ignored and reported via `missed` |
|
|
@@ -42,7 +42,7 @@ interface Document {
|
|
|
42
42
|
const defaults: Document = { documentId: null, pending: null, failed: false };
|
|
43
43
|
|
|
44
44
|
export const DocumentModel = model({
|
|
45
|
-
name: 'document', // unique: sid of
|
|
45
|
+
name: 'document', // unique: sid of the state is `effector-models:document`
|
|
46
46
|
defaults,
|
|
47
47
|
events: {
|
|
48
48
|
patched: (doc, patch: Patch) => ({
|
|
@@ -61,14 +61,15 @@ A reducer returning the same object means "nothing changed": no store update, no
|
|
|
61
61
|
|
|
62
62
|
| Unit | Payload | Behaviour |
|
|
63
63
|
| --------------- | -------------------------------------------------- | --------------------------------------------------------------------------------- |
|
|
64
|
-
| `$
|
|
65
|
-
| `$
|
|
64
|
+
| `$state` | — | `{ ids, instances, aliases }`: the single writable store, has a sid |
|
|
65
|
+
| `$instances` | — | `Record<id, State>`, derived from `$state` |
|
|
66
|
+
| `$ids` | — | `string[]` in creation order; updates only when the set or the order changes |
|
|
66
67
|
| `create` | `{ id, data? }` or array | creates with defaults; replaces existing id |
|
|
67
68
|
| `upsert` | `{ id, data? }` or array | merges into existing or creates with defaults |
|
|
68
69
|
| `update` | `{ id, data: Partial<State> \| (state) => State }` | existing only |
|
|
69
70
|
| `delete` | `id` or `id[]` | |
|
|
70
71
|
| `rename` | `{ from, to }` | moves instance in one step (e.g. draft → created document) |
|
|
71
|
-
| `clear` | — |
|
|
72
|
+
| `clear` | — | deletes everything; the ids are reported in `deleted` |
|
|
72
73
|
| `events.<name>` | `{ id \| id[], payload }` | applies reducer synchronously |
|
|
73
74
|
| `missed` | `{ action, ids }` | writes addressed to instances that do not exist |
|
|
74
75
|
| `deleted` | `string[]` | fact: real ids of deleted instances, then their aliases |
|
|
@@ -93,7 +94,7 @@ sample({
|
|
|
93
94
|
});
|
|
94
95
|
```
|
|
95
96
|
|
|
96
|
-
Tests seed
|
|
97
|
+
Tests seed a model like any store: `fork({ values: [[DocumentModel.$state, modelState({ a: { ... } })]] })`.
|
|
97
98
|
|
|
98
99
|
## Relations
|
|
99
100
|
|
|
@@ -188,7 +189,7 @@ and follow the life of instances.
|
|
|
188
189
|
|
|
189
190
|
```ts
|
|
190
191
|
Chats.alias({ alias: 'route-id', id: 'server-id' });
|
|
191
|
-
Chats.$aliases; // Store<Record<alias, id
|
|
192
|
+
Chats.$aliases; // Store<Record<alias, id>>
|
|
192
193
|
|
|
193
194
|
// Everything that takes an id takes an alias.
|
|
194
195
|
Chats.events.renamed({ id: 'route-id', payload: 'New title' });
|
|
@@ -278,6 +279,20 @@ changes — an aggregate only when its result does. The actions keep their
|
|
|
278
279
|
references while the id is the same. Keep the set of keys constant between
|
|
279
280
|
renders, and pass a stable selector to `childrenOf` to avoid re-subscribing.
|
|
280
281
|
|
|
282
|
+
A [union](#relations) takes a reference instead of an id. The result is narrowed
|
|
283
|
+
by `kind` — the state and the events are those of the model of that kind —
|
|
284
|
+
and is `undefined` when there is no such instance:
|
|
285
|
+
|
|
286
|
+
```tsx
|
|
287
|
+
const passenger = useModel(passengers, { kind: 'kid', id });
|
|
288
|
+
|
|
289
|
+
if (passenger?.kind === 'kid') {
|
|
290
|
+
passenger.state.age;
|
|
291
|
+
passenger.events.aged(7);
|
|
292
|
+
passenger.remove();
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
281
296
|
The hooks below are the parts `useModel` is made of. Use them when a
|
|
282
297
|
component needs one thing only — for example `useInstance` with a selector to
|
|
283
298
|
subscribe to a single field.
|
|
@@ -307,6 +322,10 @@ const Slides = () =>
|
|
|
307
322
|
- `useResolvedId(model, id)` — the real id for an id that may be an alias. The other hooks resolve aliases themselves; use it when the real id is needed as data, for example as a `key`.
|
|
308
323
|
- `useUnionInstance(union, { kind, id })` — `{ kind, id, state }` or `undefined`; `kind` narrows the type of `state`.
|
|
309
324
|
|
|
325
|
+
## AI agents
|
|
326
|
+
|
|
327
|
+
An [agent skill](https://github.com/Olovyannikov/effector-kit/tree/main/skills/effector-models) and [`llms-full.txt`](https://olovyannikov.github.io/effector-kit/llms-full.txt) teach coding agents the rules of the library. See [Working with AI agents](https://olovyannikov.github.io/effector-kit/guide/agents).
|
|
328
|
+
|
|
310
329
|
## Releasing
|
|
311
330
|
|
|
312
331
|
Bump `version` in `package.json`, describe the changes in `CHANGELOG.md` and merge into `main`. The `models-next` workflow checks the package, installs the packed tarball into an empty project and publishes the version if it is not on npm yet: a prerelease gets the dist-tag `next`, a stable version gets `latest`.
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_model = require("./model-
|
|
2
|
+
const require_model = require("./model-Cav66tnw.cjs");
|
|
3
3
|
let effector = require("effector");
|
|
4
4
|
// lib/aliases.ts
|
|
5
5
|
/**
|
|
@@ -30,24 +30,28 @@ let effector = require("effector");
|
|
|
30
30
|
id,
|
|
31
31
|
data
|
|
32
32
|
});
|
|
33
|
+
const refsOf = (refs, kind) => (Array.isArray(refs) ? refs : [refs]).filter((item) => item.kind === kind).map((item) => item.id);
|
|
33
34
|
for (const [kind, member] of Object.entries(models)) {
|
|
34
|
-
(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
member["~link"](create, {
|
|
36
|
+
to: "create",
|
|
37
|
+
fn: (params) => {
|
|
38
|
+
const own = paramsOf(params, kind);
|
|
39
|
+
return own.length > 0 ? own : void 0;
|
|
40
|
+
}
|
|
39
41
|
});
|
|
40
|
-
(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
member["~link"](upsert, {
|
|
43
|
+
to: "upsert",
|
|
44
|
+
fn: (params) => {
|
|
45
|
+
const own = paramsOf(params, kind);
|
|
46
|
+
return own.length > 0 ? own : void 0;
|
|
47
|
+
}
|
|
45
48
|
});
|
|
46
|
-
(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
member["~link"](remove, {
|
|
50
|
+
to: "delete",
|
|
51
|
+
fn: (refs) => {
|
|
52
|
+
const own = refsOf(refs, kind);
|
|
53
|
+
return own.length > 0 ? own : void 0;
|
|
54
|
+
}
|
|
51
55
|
});
|
|
52
56
|
}
|
|
53
57
|
return {
|
|
@@ -69,55 +73,35 @@ function isUnion(value) {
|
|
|
69
73
|
*/ function child(config) {
|
|
70
74
|
const { parent, model, key } = config;
|
|
71
75
|
const parentOf = (state) => state[key];
|
|
72
|
-
const $idsByParent = (0, effector.combine)(model.$instances, parent.$aliases, (instances, aliases) => {
|
|
76
|
+
const $idsByParent = (0, effector.combine)(model.$ids, model.$instances, parent.$aliases, (ids, instances, aliases) => {
|
|
73
77
|
const index = {};
|
|
74
|
-
for (const
|
|
75
|
-
const
|
|
78
|
+
for (const id of ids) {
|
|
79
|
+
const state = instances[id];
|
|
80
|
+
const parentId = state === void 0 ? void 0 : parentOf(state);
|
|
76
81
|
if (typeof parentId === "string") (index[require_model.resolveAlias(aliases, parentId)] ??= []).push(id);
|
|
77
82
|
}
|
|
78
83
|
return index;
|
|
79
84
|
});
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const descendantsOf = (instances, ids) => {
|
|
86
|
-
const found = /* @__PURE__ */ new Set();
|
|
87
|
-
let level = new Set(ids);
|
|
88
|
-
while (level.size > 0) {
|
|
89
|
-
const next = /* @__PURE__ */ new Set();
|
|
90
|
-
for (const id of childrenOf(instances, (parentId) => level.has(parentId))) if (!found.has(id)) {
|
|
91
|
-
found.add(id);
|
|
92
|
-
next.add(id);
|
|
93
|
-
}
|
|
94
|
-
level = isTree ? next : /* @__PURE__ */ new Set();
|
|
85
|
+
model["~link"](parent.deleted, {
|
|
86
|
+
to: "deleteWhere",
|
|
87
|
+
fn: (ids) => (state) => {
|
|
88
|
+
const parentId = parentOf(state);
|
|
89
|
+
return typeof parentId === "string" && ids.includes(parentId);
|
|
95
90
|
}
|
|
96
|
-
return [...found];
|
|
97
|
-
};
|
|
98
|
-
(0, effector.sample)({
|
|
99
|
-
clock: parent.deleted,
|
|
100
|
-
source: model.$instances,
|
|
101
|
-
filter: (instances, ids) => descendantsOf(instances, ids).length > 0,
|
|
102
|
-
fn: (instances, ids) => descendantsOf(instances, ids),
|
|
103
|
-
target: model.delete
|
|
104
91
|
});
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
data: { [key]: to }
|
|
119
|
-
}),
|
|
120
|
-
target: model.update
|
|
92
|
+
model["~link"](parent.renamed, {
|
|
93
|
+
to: "patch",
|
|
94
|
+
fn: ({ from, to }) => (instances) => {
|
|
95
|
+
let next = instances;
|
|
96
|
+
for (const [id, state] of Object.entries(instances)) if (parentOf(state) === from) {
|
|
97
|
+
if (next === instances) next = { ...instances };
|
|
98
|
+
next[id] = {
|
|
99
|
+
...state,
|
|
100
|
+
[key]: to
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return next;
|
|
104
|
+
}
|
|
121
105
|
});
|
|
122
106
|
return {
|
|
123
107
|
model,
|
|
@@ -127,10 +111,10 @@ function isUnion(value) {
|
|
|
127
111
|
}
|
|
128
112
|
function modelSignals(target, wrap, read) {
|
|
129
113
|
return {
|
|
130
|
-
removed:
|
|
114
|
+
removed: target.deleted.map((deleted) => (item) => {
|
|
131
115
|
const id = read(item);
|
|
132
116
|
return id !== void 0 && deleted.includes(id);
|
|
133
|
-
}),
|
|
117
|
+
}),
|
|
134
118
|
replaced: target.renamed.map(({ from, to }) => (item) => read(item) === from ? wrap(to) : item)
|
|
135
119
|
};
|
|
136
120
|
}
|
|
@@ -167,31 +151,28 @@ function targetSignals(target) {
|
|
|
167
151
|
}
|
|
168
152
|
return next;
|
|
169
153
|
};
|
|
170
|
-
(
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
fn: (instances, isRemoved) => rewrite(instances, (value) => {
|
|
154
|
+
from["~link"](signals.removed, {
|
|
155
|
+
to: "patch",
|
|
156
|
+
fn: (isRemoved) => (instances) => rewrite(instances, (value) => {
|
|
174
157
|
if (Array.isArray(value)) return value.some(isRemoved) ? value.filter((item) => !isRemoved(item)) : value;
|
|
175
158
|
return value !== null && isRemoved(value) ? null : value;
|
|
176
|
-
})
|
|
177
|
-
target: from.$instances
|
|
159
|
+
})
|
|
178
160
|
});
|
|
179
|
-
(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
fn: (instances, replace) => rewrite(instances, (value) => {
|
|
161
|
+
from["~link"](signals.replaced, {
|
|
162
|
+
to: "patch",
|
|
163
|
+
fn: (replace) => (instances) => rewrite(instances, (value) => {
|
|
183
164
|
if (Array.isArray(value)) {
|
|
184
165
|
const replaced = value.map(replace);
|
|
185
166
|
return replaced.some((item, index) => item !== value[index]) ? replaced : value;
|
|
186
167
|
}
|
|
187
168
|
return value === null ? value : replace(value);
|
|
188
|
-
})
|
|
189
|
-
target: from.$instances
|
|
169
|
+
})
|
|
190
170
|
});
|
|
191
171
|
}
|
|
192
172
|
exports.aliases = aliases;
|
|
193
173
|
exports.child = child;
|
|
194
174
|
exports.model = require_model.model;
|
|
175
|
+
exports.modelState = require_model.modelState;
|
|
195
176
|
exports.ref = ref;
|
|
196
177
|
exports.resolveAlias = require_model.resolveAlias;
|
|
197
178
|
exports.union = union;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EventCallable, Store } from "effector";
|
|
2
|
-
import { C as
|
|
2
|
+
import { C as Reducers, D as modelState, E as model, O as resolveAlias, S as Reducer, T as UpdateParams, _ as Instances, a as Union, b as ModelConfig, c as UnionModels, d as ref, f as union, g as InstanceEventParams, h as Ids, i as RefConfig, l as UnionRef, m as CreateParams, n as ChildConfig, o as UnionCreateParams, p as AliasParams, r as ModelLike, s as UnionInstances, t as Child, u as child, v as Missed, w as RenameParams, x as ModelState, y as Model } from "./relations-pJXSBtRd.cjs";
|
|
3
3
|
/** Часть модели, нужная алиасам. */
|
|
4
4
|
type AliasTarget = Pick<Model<object, Record<never, unknown>>, "$aliases" | "alias" | "unalias">;
|
|
5
5
|
interface Aliases {
|
|
@@ -17,5 +17,5 @@ interface Aliases {
|
|
|
17
17
|
* а события и хуки принимают алиас вместо id. Обёртка оставлена для кода, написанного под alpha.2.
|
|
18
18
|
*/
|
|
19
19
|
export declare function aliases(model: AliasTarget): Aliases;
|
|
20
|
-
export { type AliasParams, type AliasTarget, type Aliases, type Child, type ChildConfig, type CreateParams, type Ids, type InstanceEventParams, type Instances, type Missed, type Model, type ModelConfig, type ModelLike, type Reducer, type Reducers, type RefConfig, type RenameParams, type Union, type UnionCreateParams, type UnionInstances, type UnionModels, type UnionRef, type UpdateParams, child, model, ref, resolveAlias, union };
|
|
20
|
+
export { type AliasParams, type AliasTarget, type Aliases, type Child, type ChildConfig, type CreateParams, type Ids, type InstanceEventParams, type Instances, type Missed, type Model, type ModelConfig, type ModelLike, type ModelState, type Reducer, type Reducers, type RefConfig, type RenameParams, type Union, type UnionCreateParams, type UnionInstances, type UnionModels, type UnionRef, type UpdateParams, child, model, modelState, ref, resolveAlias, union };
|
|
21
21
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EventCallable, Store } from "effector";
|
|
2
|
-
import { C as
|
|
2
|
+
import { C as Reducers, D as modelState, E as model, O as resolveAlias, S as Reducer, T as UpdateParams, _ as Instances, a as Union, b as ModelConfig, c as UnionModels, d as ref, f as union, g as InstanceEventParams, h as Ids, i as RefConfig, l as UnionRef, m as CreateParams, n as ChildConfig, o as UnionCreateParams, p as AliasParams, r as ModelLike, s as UnionInstances, t as Child, u as child, v as Missed, w as RenameParams, x as ModelState, y as Model } from "./relations-CpExzbHu.mjs";
|
|
3
3
|
/** Часть модели, нужная алиасам. */
|
|
4
4
|
type AliasTarget = Pick<Model<object, Record<never, unknown>>, "$aliases" | "alias" | "unalias">;
|
|
5
5
|
interface Aliases {
|
|
@@ -17,5 +17,5 @@ interface Aliases {
|
|
|
17
17
|
* а события и хуки принимают алиас вместо id. Обёртка оставлена для кода, написанного под alpha.2.
|
|
18
18
|
*/
|
|
19
19
|
export declare function aliases(model: AliasTarget): Aliases;
|
|
20
|
-
export { type AliasParams, type AliasTarget, type Aliases, type Child, type ChildConfig, type CreateParams, type Ids, type InstanceEventParams, type Instances, type Missed, type Model, type ModelConfig, type ModelLike, type Reducer, type Reducers, type RefConfig, type RenameParams, type Union, type UnionCreateParams, type UnionInstances, type UnionModels, type UnionRef, type UpdateParams, child, model, ref, resolveAlias, union };
|
|
20
|
+
export { type AliasParams, type AliasTarget, type Aliases, type Child, type ChildConfig, type CreateParams, type Ids, type InstanceEventParams, type Instances, type Missed, type Model, type ModelConfig, type ModelLike, type ModelState, type Reducer, type Reducers, type RefConfig, type RenameParams, type Union, type UnionCreateParams, type UnionInstances, type UnionModels, type UnionRef, type UpdateParams, child, model, modelState, ref, resolveAlias, union };
|
|
21
21
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as resolveAlias, t as model } from "./model-
|
|
2
|
-
import { combine, createEvent, merge
|
|
1
|
+
import { n as modelState, r as resolveAlias, t as model } from "./model-BNsi5pxp.mjs";
|
|
2
|
+
import { combine, createEvent, merge } from "effector";
|
|
3
3
|
// lib/aliases.ts
|
|
4
4
|
/**
|
|
5
5
|
* @deprecated Алиасы встроены в модель: `model.$aliases`, `model.alias`, `model.unalias`,
|
|
@@ -29,24 +29,28 @@ import { combine, createEvent, merge, sample } from "effector";
|
|
|
29
29
|
id,
|
|
30
30
|
data
|
|
31
31
|
});
|
|
32
|
+
const refsOf = (refs, kind) => (Array.isArray(refs) ? refs : [refs]).filter((item) => item.kind === kind).map((item) => item.id);
|
|
32
33
|
for (const [kind, member] of Object.entries(models)) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
member["~link"](create, {
|
|
35
|
+
to: "create",
|
|
36
|
+
fn: (params) => {
|
|
37
|
+
const own = paramsOf(params, kind);
|
|
38
|
+
return own.length > 0 ? own : void 0;
|
|
39
|
+
}
|
|
38
40
|
});
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
member["~link"](upsert, {
|
|
42
|
+
to: "upsert",
|
|
43
|
+
fn: (params) => {
|
|
44
|
+
const own = paramsOf(params, kind);
|
|
45
|
+
return own.length > 0 ? own : void 0;
|
|
46
|
+
}
|
|
44
47
|
});
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
member["~link"](remove, {
|
|
49
|
+
to: "delete",
|
|
50
|
+
fn: (refs) => {
|
|
51
|
+
const own = refsOf(refs, kind);
|
|
52
|
+
return own.length > 0 ? own : void 0;
|
|
53
|
+
}
|
|
50
54
|
});
|
|
51
55
|
}
|
|
52
56
|
return {
|
|
@@ -68,55 +72,35 @@ function isUnion(value) {
|
|
|
68
72
|
*/ function child(config) {
|
|
69
73
|
const { parent, model, key } = config;
|
|
70
74
|
const parentOf = (state) => state[key];
|
|
71
|
-
const $idsByParent = combine(model.$instances, parent.$aliases, (instances, aliases) => {
|
|
75
|
+
const $idsByParent = combine(model.$ids, model.$instances, parent.$aliases, (ids, instances, aliases) => {
|
|
72
76
|
const index = {};
|
|
73
|
-
for (const
|
|
74
|
-
const
|
|
77
|
+
for (const id of ids) {
|
|
78
|
+
const state = instances[id];
|
|
79
|
+
const parentId = state === void 0 ? void 0 : parentOf(state);
|
|
75
80
|
if (typeof parentId === "string") (index[resolveAlias(aliases, parentId)] ??= []).push(id);
|
|
76
81
|
}
|
|
77
82
|
return index;
|
|
78
83
|
});
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const descendantsOf = (instances, ids) => {
|
|
85
|
-
const found = /* @__PURE__ */ new Set();
|
|
86
|
-
let level = new Set(ids);
|
|
87
|
-
while (level.size > 0) {
|
|
88
|
-
const next = /* @__PURE__ */ new Set();
|
|
89
|
-
for (const id of childrenOf(instances, (parentId) => level.has(parentId))) if (!found.has(id)) {
|
|
90
|
-
found.add(id);
|
|
91
|
-
next.add(id);
|
|
92
|
-
}
|
|
93
|
-
level = isTree ? next : /* @__PURE__ */ new Set();
|
|
84
|
+
model["~link"](parent.deleted, {
|
|
85
|
+
to: "deleteWhere",
|
|
86
|
+
fn: (ids) => (state) => {
|
|
87
|
+
const parentId = parentOf(state);
|
|
88
|
+
return typeof parentId === "string" && ids.includes(parentId);
|
|
94
89
|
}
|
|
95
|
-
return [...found];
|
|
96
|
-
};
|
|
97
|
-
sample({
|
|
98
|
-
clock: parent.deleted,
|
|
99
|
-
source: model.$instances,
|
|
100
|
-
filter: (instances, ids) => descendantsOf(instances, ids).length > 0,
|
|
101
|
-
fn: (instances, ids) => descendantsOf(instances, ids),
|
|
102
|
-
target: model.delete
|
|
103
90
|
});
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
data: { [key]: to }
|
|
118
|
-
}),
|
|
119
|
-
target: model.update
|
|
91
|
+
model["~link"](parent.renamed, {
|
|
92
|
+
to: "patch",
|
|
93
|
+
fn: ({ from, to }) => (instances) => {
|
|
94
|
+
let next = instances;
|
|
95
|
+
for (const [id, state] of Object.entries(instances)) if (parentOf(state) === from) {
|
|
96
|
+
if (next === instances) next = { ...instances };
|
|
97
|
+
next[id] = {
|
|
98
|
+
...state,
|
|
99
|
+
[key]: to
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
return next;
|
|
103
|
+
}
|
|
120
104
|
});
|
|
121
105
|
return {
|
|
122
106
|
model,
|
|
@@ -126,10 +110,10 @@ function isUnion(value) {
|
|
|
126
110
|
}
|
|
127
111
|
function modelSignals(target, wrap, read) {
|
|
128
112
|
return {
|
|
129
|
-
removed:
|
|
113
|
+
removed: target.deleted.map((deleted) => (item) => {
|
|
130
114
|
const id = read(item);
|
|
131
115
|
return id !== void 0 && deleted.includes(id);
|
|
132
|
-
}),
|
|
116
|
+
}),
|
|
133
117
|
replaced: target.renamed.map(({ from, to }) => (item) => read(item) === from ? wrap(to) : item)
|
|
134
118
|
};
|
|
135
119
|
}
|
|
@@ -166,28 +150,24 @@ function targetSignals(target) {
|
|
|
166
150
|
}
|
|
167
151
|
return next;
|
|
168
152
|
};
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
fn: (instances, isRemoved) => rewrite(instances, (value) => {
|
|
153
|
+
from["~link"](signals.removed, {
|
|
154
|
+
to: "patch",
|
|
155
|
+
fn: (isRemoved) => (instances) => rewrite(instances, (value) => {
|
|
173
156
|
if (Array.isArray(value)) return value.some(isRemoved) ? value.filter((item) => !isRemoved(item)) : value;
|
|
174
157
|
return value !== null && isRemoved(value) ? null : value;
|
|
175
|
-
})
|
|
176
|
-
target: from.$instances
|
|
158
|
+
})
|
|
177
159
|
});
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
fn: (instances, replace) => rewrite(instances, (value) => {
|
|
160
|
+
from["~link"](signals.replaced, {
|
|
161
|
+
to: "patch",
|
|
162
|
+
fn: (replace) => (instances) => rewrite(instances, (value) => {
|
|
182
163
|
if (Array.isArray(value)) {
|
|
183
164
|
const replaced = value.map(replace);
|
|
184
165
|
return replaced.some((item, index) => item !== value[index]) ? replaced : value;
|
|
185
166
|
}
|
|
186
167
|
return value === null ? value : replace(value);
|
|
187
|
-
})
|
|
188
|
-
target: from.$instances
|
|
168
|
+
})
|
|
189
169
|
});
|
|
190
170
|
}
|
|
191
|
-
export { aliases, child, model, ref, resolveAlias, union };
|
|
171
|
+
export { aliases, child, model, modelState, ref, resolveAlias, union };
|
|
192
172
|
|
|
193
173
|
//# sourceMappingURL=index.mjs.map
|