@olovyannikov/effector-models 1.0.0-alpha.1 → 1.0.0-alpha.3
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 +43 -0
- package/README.md +67 -14
- package/dist/index.cjs +31 -174
- package/dist/index.d.cts +21 -2
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +21 -2
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +30 -175
- package/dist/index.mjs.map +1 -1
- package/dist/model-DYSAS481.mjs +275 -0
- package/dist/model-DYSAS481.mjs.map +1 -0
- package/dist/model-iPgSwajA.cjs +284 -0
- package/dist/react.cjs +26 -6
- package/dist/react.d.cts +13 -7
- package/dist/react.d.cts.map +1 -1
- package/dist/react.d.mts +13 -7
- package/dist/react.d.mts.map +1 -1
- package/dist/react.mjs +26 -7
- package/dist/react.mjs.map +1 -1
- package/dist/{relations-DF1qw49e.d.mts → relations-Cc9Mv0m7.d.mts} +36 -7
- package/dist/relations-Cc9Mv0m7.d.mts.map +1 -0
- package/dist/{relations-D1pFQ4IC.d.cts → relations-DXJ6slzh.d.cts} +36 -7
- package/dist/relations-DXJ6slzh.d.cts.map +1 -0
- package/package.json +1 -1
- package/dist/relations-D1pFQ4IC.d.cts.map +0 -1
- package/dist/relations-DF1qw49e.d.mts.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,48 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.0.0-alpha.3
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- **Aliases in the core.** Every model has `$aliases`, `alias` and `unalias`.
|
|
8
|
+
`update`, `upsert`, `delete`, `rename` and instance events accept an alias
|
|
9
|
+
wherever they accept an id; so do `useInstance`, `useInstanceEvents`,
|
|
10
|
+
`useChildIds`, `useChildren` and `useUnionInstance`.
|
|
11
|
+
- **Facts**: `model.deleted` (real ids of the deleted instances followed by
|
|
12
|
+
their aliases) and `model.renamed` (`{ from, to }` with the real `from`).
|
|
13
|
+
They fire only when something was actually deleted or moved.
|
|
14
|
+
- Relations see through aliases: a child or a reference that stores an alias
|
|
15
|
+
of its target is cascaded and cleaned up like one that stores the real id.
|
|
16
|
+
`$idsByParent` is always keyed by the real parent id.
|
|
17
|
+
- `union.$aliases`; `Child` has a `parent` field.
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
|
|
21
|
+
- `upsert` with an alias id extends the aliased instance instead of creating
|
|
22
|
+
a new one. `create` still displaces the alias.
|
|
23
|
+
- `rename` to the instance's own id is a no-op. It used to delete the
|
|
24
|
+
instance.
|
|
25
|
+
- `ModelLike` also requires `$aliases`, `deleted` and `renamed`.
|
|
26
|
+
- `useUnionInstance` returns the real id when the reference holds an alias.
|
|
27
|
+
|
|
28
|
+
### Deprecated
|
|
29
|
+
|
|
30
|
+
- `aliases(model)` is a thin wrapper over the model now: `add` is
|
|
31
|
+
`model.alias`, `remove` is `model.unalias`, `resolved(event)` returns the
|
|
32
|
+
event itself. The sid of the alias store did not change, so serialized
|
|
33
|
+
state from alpha.2 hydrates as is.
|
|
34
|
+
|
|
35
|
+
## 1.0.0-alpha.2
|
|
36
|
+
|
|
37
|
+
### Added
|
|
38
|
+
|
|
39
|
+
- **Aliases**: `aliases(model)` — one instance reachable by several ids:
|
|
40
|
+
`$aliases`, `add`, `remove`, and `resolved(event)` to write through an
|
|
41
|
+
alias. Aliases follow `delete`, `clear` and `rename` of the instances.
|
|
42
|
+
`resolveAlias(aliases, id)` is exported for use in `sample`.
|
|
43
|
+
- **React**: `useResolvedId(aliases, id)` — the real id for an id that may
|
|
44
|
+
be an alias.
|
|
45
|
+
|
|
3
46
|
## 1.0.0-alpha.1
|
|
4
47
|
|
|
5
48
|
### Added
|
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ Fork of [`@effector-kit/models`](https://github.com/movpushmov/effector-kit) by
|
|
|
16
16
|
| Write to missing instance | silent `Promise.reject` | ignored and reported via `missed` |
|
|
17
17
|
| React `useModel(lens)` | re-renders on any instance change, new object every render | subscribed to its own instance, stable reference, selector |
|
|
18
18
|
|
|
19
|
-
`child`, `ref` and `union` are available as [relations](#relations) between models: plain data plus integrity rules.
|
|
19
|
+
`child`, `ref` and `union` are available as [relations](#relations) between models: plain data plus integrity rules. Aliases are built into the model: see [aliases](#aliases--one-instance-several-ids). Not carried over: `fn` with arbitrary graphs inside an instance — side effects stay outside, as usual in Effector.
|
|
20
20
|
|
|
21
21
|
Documentation, a step-by-step tutorial and live examples: <https://olovyannikov.github.io/effector-kit/guide/keyed-models> (also in Russian).
|
|
22
22
|
|
|
@@ -59,18 +59,23 @@ A reducer returning the same object means "nothing changed": no store update, no
|
|
|
59
59
|
|
|
60
60
|
## API
|
|
61
61
|
|
|
62
|
-
| Unit | Payload | Behaviour
|
|
63
|
-
| --------------- | -------------------------------------------------- |
|
|
64
|
-
| `$instances` | — | `Record<id, State>`, writable, has sid
|
|
65
|
-
| `$ids` | — | `string[]`
|
|
66
|
-
| `create` | `{ id, data? }` or array | creates with defaults; replaces existing id
|
|
67
|
-
| `upsert` | `{ id, data? }` or array | merges into existing or creates with defaults
|
|
68
|
-
| `update` | `{ id, data: Partial<State> \| (state) => State }` | existing only
|
|
69
|
-
| `delete` | `id` or `id[]` |
|
|
70
|
-
| `rename` | `{ from, to }` | moves instance in one step (e.g. draft → created document)
|
|
71
|
-
| `clear` | — |
|
|
72
|
-
| `events.<name>` | `{ id \| id[], payload }` | applies reducer synchronously
|
|
73
|
-
| `missed` | `{ action, ids }` | writes addressed to instances that do not exist
|
|
62
|
+
| Unit | Payload | Behaviour |
|
|
63
|
+
| --------------- | -------------------------------------------------- | --------------------------------------------------------------------------------- |
|
|
64
|
+
| `$instances` | — | `Record<id, State>`, writable, has sid |
|
|
65
|
+
| `$ids` | — | `string[]` |
|
|
66
|
+
| `create` | `{ id, data? }` or array | creates with defaults; replaces existing id |
|
|
67
|
+
| `upsert` | `{ id, data? }` or array | merges into existing or creates with defaults |
|
|
68
|
+
| `update` | `{ id, data: Partial<State> \| (state) => State }` | existing only |
|
|
69
|
+
| `delete` | `id` or `id[]` | |
|
|
70
|
+
| `rename` | `{ from, to }` | moves instance in one step (e.g. draft → created document) |
|
|
71
|
+
| `clear` | — | |
|
|
72
|
+
| `events.<name>` | `{ id \| id[], payload }` | applies reducer synchronously |
|
|
73
|
+
| `missed` | `{ action, ids }` | writes addressed to instances that do not exist |
|
|
74
|
+
| `deleted` | `string[]` | fact: real ids of deleted instances, then their aliases |
|
|
75
|
+
| `renamed` | `{ from, to }` | fact: the instance moved, `from` is the real id |
|
|
76
|
+
| `$aliases` | — | `Record<alias, id>`, has a sid; see [aliases](#aliases--one-instance-several-ids) |
|
|
77
|
+
| `alias` | `{ alias, id }` or an array | adds aliases to existing instances |
|
|
78
|
+
| `unalias` | `alias` or `alias[]` | removes aliases, instances stay |
|
|
74
79
|
|
|
75
80
|
```ts
|
|
76
81
|
sample({
|
|
@@ -175,6 +180,49 @@ const Bookings = model({
|
|
|
175
180
|
ref({ from: Bookings, field: 'passengers', to: passengers });
|
|
176
181
|
```
|
|
177
182
|
|
|
183
|
+
### Aliases — one instance, several ids
|
|
184
|
+
|
|
185
|
+
One instance reachable by several ids — a route id next to the server id.
|
|
186
|
+
The instance is still stored once; aliases live in the model, in `$aliases`,
|
|
187
|
+
and follow the life of instances.
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
Chats.alias({ alias: 'route-id', id: 'server-id' });
|
|
191
|
+
Chats.$aliases; // Store<Record<alias, id>>, has a sid
|
|
192
|
+
|
|
193
|
+
// Everything that takes an id takes an alias.
|
|
194
|
+
Chats.events.renamed({ id: 'route-id', payload: 'New title' });
|
|
195
|
+
Chats.update({ id: 'route-id', data: { title: 'New title' } });
|
|
196
|
+
Chats.delete('route-id');
|
|
197
|
+
|
|
198
|
+
Chats.unalias('route-id'); // the instance stays
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
```tsx
|
|
202
|
+
const chat = useInstance(Chats, routeId);
|
|
203
|
+
const { renamed } = useInstanceEvents(Chats, routeId);
|
|
204
|
+
const messageIds = useChildIds(messages, routeId);
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
- `update`, `upsert`, `delete`, `rename`, instance events and all hooks
|
|
208
|
+
resolve aliases; `create` does not — an instance created with the id of an
|
|
209
|
+
alias displaces the alias, a real id always wins
|
|
210
|
+
- deleting or clearing instances removes their aliases, `rename` moves them
|
|
211
|
+
- an alias to a missing instance, or with the id of an existing one, is not
|
|
212
|
+
created
|
|
213
|
+
- relations see through aliases: a child or a reference may store an alias
|
|
214
|
+
of its target and is still cascaded and cleaned up
|
|
215
|
+
- `unalias` does not touch references that store the alias — keep real ids
|
|
216
|
+
in data when you can; `useResolvedId(model, id)` and
|
|
217
|
+
`resolveAlias(aliases, id)` give the real id
|
|
218
|
+
|
|
219
|
+
To react to changes, subscribe to the facts instead of the commands:
|
|
220
|
+
`model.deleted` carries the real ids of deleted instances followed by their
|
|
221
|
+
aliases, `model.renamed` carries `{ from, to }` with the real `from`.
|
|
222
|
+
|
|
223
|
+
Use `rename` when an instance simply moves to a new id, and aliases when
|
|
224
|
+
both ids must keep working.
|
|
225
|
+
|
|
178
226
|
## React
|
|
179
227
|
|
|
180
228
|
```tsx
|
|
@@ -194,13 +242,18 @@ const Slides = () =>
|
|
|
194
242
|
useInstanceIds(SlideModel).map(id => <Slide key={id} id={id} />);
|
|
195
243
|
```
|
|
196
244
|
|
|
197
|
-
- `useInstance(model, id)` — instance or `undefined
|
|
245
|
+
- `useInstance(model, id)` — instance or `undefined`, `id` may be an alias; `useInstance(model, id, selector, isEqual?)` — selected value. Pass a stable selector to avoid re-subscribing.
|
|
198
246
|
- `useInstanceIds(model)` — re-renders only when the set or order of ids changes.
|
|
199
247
|
- `useInstanceEvents(model, id)` — instance events bound to the id and to the scope from `Provider`.
|
|
200
248
|
- `useChildIds(relation, parentId)` — ids of the children of one parent; re-renders only when their set or order changes.
|
|
201
249
|
- `useChildren(relation, parentId)` — the children themselves; `useChildren(relation, parentId, selector, isEqual?)` — an aggregate over them (a total, a counter) that re-renders only when the selected value changes.
|
|
250
|
+
- `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`.
|
|
202
251
|
- `useUnionInstance(union, { kind, id })` — `{ kind, id, state }` or `undefined`; `kind` narrows the type of `state`.
|
|
203
252
|
|
|
253
|
+
## Releasing
|
|
254
|
+
|
|
255
|
+
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`.
|
|
256
|
+
|
|
204
257
|
## License
|
|
205
258
|
|
|
206
259
|
MIT. Original work © movpushmov.
|
package/dist/index.cjs
CHANGED
|
@@ -1,171 +1,27 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_model = require("./model-iPgSwajA.cjs");
|
|
2
3
|
let effector = require("effector");
|
|
3
|
-
// lib/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const missing = [];
|
|
9
|
-
for (const id of toList$1(ids)) {
|
|
10
|
-
const current = instances[id];
|
|
11
|
-
if (current === void 0) {
|
|
12
|
-
missing.push(id);
|
|
13
|
-
continue;
|
|
14
|
-
}
|
|
15
|
-
const updated = fn(current);
|
|
16
|
-
if (updated !== current) {
|
|
17
|
-
if (next === instances) next = { ...instances };
|
|
18
|
-
next[id] = updated;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return {
|
|
22
|
-
instances: next,
|
|
23
|
-
missing
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
function model(config) {
|
|
27
|
-
const { name, defaults } = config;
|
|
28
|
-
const reducers = config.events ?? {};
|
|
29
|
-
const $instances = (0, effector.createStore)({}, {
|
|
30
|
-
sid: `effector-models:${name}`,
|
|
31
|
-
name: `${name}/$instances`
|
|
32
|
-
});
|
|
33
|
-
const $ids = $instances.map((instances) => Object.keys(instances));
|
|
34
|
-
const missed = (0, effector.createEvent)({ name: `${name}/missed` });
|
|
35
|
-
const applied = (0, effector.createEvent)();
|
|
36
|
-
(0, effector.sample)({
|
|
37
|
-
clock: applied,
|
|
38
|
-
source: $instances,
|
|
39
|
-
filter: (instances, result) => result.instances !== instances,
|
|
40
|
-
fn: (_, { instances }) => instances,
|
|
41
|
-
target: $instances
|
|
42
|
-
});
|
|
43
|
-
(0, effector.sample)({
|
|
44
|
-
clock: applied,
|
|
45
|
-
filter: ({ missing }) => missing.length > 0,
|
|
46
|
-
fn: ({ action, missing }) => ({
|
|
47
|
-
action,
|
|
48
|
-
ids: missing
|
|
49
|
-
}),
|
|
50
|
-
target: missed
|
|
51
|
-
});
|
|
52
|
-
const create = (0, effector.createEvent)({ name: `${name}/create` });
|
|
53
|
-
(0, effector.sample)({
|
|
54
|
-
clock: create,
|
|
55
|
-
source: $instances,
|
|
56
|
-
fn: (instances, params) => {
|
|
57
|
-
const next = { ...instances };
|
|
58
|
-
for (const { id, data } of toArray(params)) next[id] = {
|
|
59
|
-
...defaults,
|
|
60
|
-
...data
|
|
61
|
-
};
|
|
62
|
-
return next;
|
|
63
|
-
},
|
|
64
|
-
target: $instances
|
|
65
|
-
});
|
|
66
|
-
const upsert = (0, effector.createEvent)({ name: `${name}/upsert` });
|
|
67
|
-
(0, effector.sample)({
|
|
68
|
-
clock: upsert,
|
|
69
|
-
source: $instances,
|
|
70
|
-
fn: (instances, params) => {
|
|
71
|
-
const next = { ...instances };
|
|
72
|
-
for (const { id, data } of toArray(params)) next[id] = {
|
|
73
|
-
...next[id] ?? defaults,
|
|
74
|
-
...data
|
|
75
|
-
};
|
|
76
|
-
return next;
|
|
77
|
-
},
|
|
78
|
-
target: $instances
|
|
79
|
-
});
|
|
80
|
-
const update = (0, effector.createEvent)({ name: `${name}/update` });
|
|
81
|
-
(0, effector.sample)({
|
|
82
|
-
clock: update,
|
|
83
|
-
source: $instances,
|
|
84
|
-
fn: (instances, { id, data }) => ({
|
|
85
|
-
action: "update",
|
|
86
|
-
...applyTo(instances, id, (state) => typeof data === "function" ? data(state) : {
|
|
87
|
-
...state,
|
|
88
|
-
...data
|
|
89
|
-
})
|
|
90
|
-
}),
|
|
91
|
-
target: applied
|
|
92
|
-
});
|
|
93
|
-
const remove = (0, effector.createEvent)({ name: `${name}/delete` });
|
|
94
|
-
(0, effector.sample)({
|
|
95
|
-
clock: remove,
|
|
96
|
-
source: $instances,
|
|
97
|
-
filter: (instances, ids) => toList$1(ids).some((id) => id in instances),
|
|
98
|
-
fn: (instances, ids) => {
|
|
99
|
-
const next = { ...instances };
|
|
100
|
-
for (const id of toList$1(ids)) delete next[id];
|
|
101
|
-
return next;
|
|
102
|
-
},
|
|
103
|
-
target: $instances
|
|
104
|
-
});
|
|
105
|
-
const rename = (0, effector.createEvent)({ name: `${name}/rename` });
|
|
106
|
-
(0, effector.sample)({
|
|
107
|
-
clock: rename,
|
|
108
|
-
source: $instances,
|
|
109
|
-
fn: (instances, { from, to }) => {
|
|
110
|
-
const current = instances[from];
|
|
111
|
-
if (current === void 0) return {
|
|
112
|
-
action: "rename",
|
|
113
|
-
instances,
|
|
114
|
-
missing: [from]
|
|
115
|
-
};
|
|
116
|
-
const next = {
|
|
117
|
-
...instances,
|
|
118
|
-
[to]: current
|
|
119
|
-
};
|
|
120
|
-
delete next[from];
|
|
121
|
-
return {
|
|
122
|
-
action: "rename",
|
|
123
|
-
instances: next,
|
|
124
|
-
missing: []
|
|
125
|
-
};
|
|
126
|
-
},
|
|
127
|
-
target: applied
|
|
128
|
-
});
|
|
129
|
-
const clear = (0, effector.createEvent)({ name: `${name}/clear` });
|
|
130
|
-
(0, effector.sample)({
|
|
131
|
-
clock: clear,
|
|
132
|
-
fn: () => ({}),
|
|
133
|
-
target: $instances
|
|
134
|
-
});
|
|
135
|
-
const events = {};
|
|
136
|
-
for (const [action, reducer] of Object.entries(reducers)) {
|
|
137
|
-
const event = (0, effector.createEvent)({ name: `${name}/${action}` });
|
|
138
|
-
(0, effector.sample)({
|
|
139
|
-
clock: event,
|
|
140
|
-
source: $instances,
|
|
141
|
-
fn: (instances, { id, payload }) => ({
|
|
142
|
-
action,
|
|
143
|
-
...applyTo(instances, id, (state) => reducer(state, payload))
|
|
144
|
-
}),
|
|
145
|
-
target: applied
|
|
146
|
-
});
|
|
147
|
-
events[action] = event;
|
|
148
|
-
}
|
|
4
|
+
// lib/aliases.ts
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated Алиасы встроены в модель: `model.$aliases`, `model.alias`, `model.unalias`,
|
|
7
|
+
* а события и хуки принимают алиас вместо id. Обёртка оставлена для кода, написанного под alpha.2.
|
|
8
|
+
*/ function aliases(model) {
|
|
149
9
|
return {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
create,
|
|
155
|
-
upsert,
|
|
156
|
-
update,
|
|
157
|
-
delete: remove,
|
|
158
|
-
rename,
|
|
159
|
-
clear,
|
|
160
|
-
events,
|
|
161
|
-
missed
|
|
10
|
+
$aliases: model.$aliases,
|
|
11
|
+
add: model.alias,
|
|
12
|
+
remove: model.unalias,
|
|
13
|
+
resolved: (target) => target
|
|
162
14
|
};
|
|
163
15
|
}
|
|
164
16
|
// lib/relations.ts
|
|
165
|
-
const toList = (ids) => typeof ids === "string" ? [ids] : ids;
|
|
166
17
|
/** Несколько моделей как одна коллекция: экземпляр адресуется парой `{ kind, id }`. */ function union(models) {
|
|
167
18
|
const stores = {};
|
|
168
|
-
|
|
19
|
+
const aliasStores = {};
|
|
20
|
+
for (const [kind, member] of Object.entries(models)) {
|
|
21
|
+
stores[kind] = member.$instances;
|
|
22
|
+
aliasStores[kind] = member.$aliases;
|
|
23
|
+
}
|
|
24
|
+
const $aliases = (0, effector.combine)(aliasStores);
|
|
169
25
|
const $instances = (0, effector.combine)(stores);
|
|
170
26
|
const remove = (0, effector.createEvent)();
|
|
171
27
|
const create = (0, effector.createEvent)();
|
|
@@ -198,6 +54,7 @@ const toList = (ids) => typeof ids === "string" ? [ids] : ids;
|
|
|
198
54
|
"~kind": "union",
|
|
199
55
|
models,
|
|
200
56
|
$instances,
|
|
57
|
+
$aliases,
|
|
201
58
|
create,
|
|
202
59
|
upsert,
|
|
203
60
|
delete: remove
|
|
@@ -212,11 +69,11 @@ function isUnion(value) {
|
|
|
212
69
|
*/ function child(config) {
|
|
213
70
|
const { parent, model, key } = config;
|
|
214
71
|
const parentOf = (state) => state[key];
|
|
215
|
-
const $idsByParent = model.$instances
|
|
72
|
+
const $idsByParent = (0, effector.combine)(model.$instances, parent.$aliases, (instances, aliases) => {
|
|
216
73
|
const index = {};
|
|
217
74
|
for (const [id, state] of Object.entries(instances)) {
|
|
218
75
|
const parentId = parentOf(state);
|
|
219
|
-
if (typeof parentId === "string") (index[parentId] ??= []).push(id);
|
|
76
|
+
if (typeof parentId === "string") (index[require_model.resolveAlias(aliases, parentId)] ??= []).push(id);
|
|
220
77
|
}
|
|
221
78
|
return index;
|
|
222
79
|
});
|
|
@@ -239,10 +96,10 @@ function isUnion(value) {
|
|
|
239
96
|
return [...found];
|
|
240
97
|
};
|
|
241
98
|
(0, effector.sample)({
|
|
242
|
-
clock: parent.
|
|
99
|
+
clock: parent.deleted,
|
|
243
100
|
source: model.$instances,
|
|
244
|
-
filter: (instances, ids) => descendantsOf(instances,
|
|
245
|
-
fn: (instances, ids) => descendantsOf(instances,
|
|
101
|
+
filter: (instances, ids) => descendantsOf(instances, ids).length > 0,
|
|
102
|
+
fn: (instances, ids) => descendantsOf(instances, ids),
|
|
246
103
|
target: model.delete
|
|
247
104
|
});
|
|
248
105
|
if (!isTree) (0, effector.sample)({
|
|
@@ -253,7 +110,7 @@ function isUnion(value) {
|
|
|
253
110
|
target: model.delete
|
|
254
111
|
});
|
|
255
112
|
(0, effector.sample)({
|
|
256
|
-
clock: parent.
|
|
113
|
+
clock: parent.renamed,
|
|
257
114
|
source: model.$instances,
|
|
258
115
|
filter: (instances, { from }) => childrenOf(instances, (parentId) => parentId === from).length > 0,
|
|
259
116
|
fn: (instances, { from, to }) => ({
|
|
@@ -264,19 +121,17 @@ function isUnion(value) {
|
|
|
264
121
|
});
|
|
265
122
|
return {
|
|
266
123
|
model,
|
|
124
|
+
parent: { $aliases: parent.$aliases },
|
|
267
125
|
$idsByParent
|
|
268
126
|
};
|
|
269
127
|
}
|
|
270
128
|
function modelSignals(target, wrap, read) {
|
|
271
129
|
return {
|
|
272
|
-
removed: (0, effector.merge)([target.
|
|
273
|
-
const
|
|
274
|
-
return (
|
|
275
|
-
const id = read(item);
|
|
276
|
-
return id !== void 0 && deleted.includes(id);
|
|
277
|
-
};
|
|
130
|
+
removed: (0, effector.merge)([target.deleted.map((deleted) => (item) => {
|
|
131
|
+
const id = read(item);
|
|
132
|
+
return id !== void 0 && deleted.includes(id);
|
|
278
133
|
}), target.clear.map(() => (item) => read(item) !== void 0)]),
|
|
279
|
-
replaced: target.
|
|
134
|
+
replaced: target.renamed.map(({ from, to }) => (item) => read(item) === from ? wrap(to) : item)
|
|
280
135
|
};
|
|
281
136
|
}
|
|
282
137
|
function targetSignals(target) {
|
|
@@ -334,7 +189,9 @@ function targetSignals(target) {
|
|
|
334
189
|
target: from.$instances
|
|
335
190
|
});
|
|
336
191
|
}
|
|
192
|
+
exports.aliases = aliases;
|
|
337
193
|
exports.child = child;
|
|
338
|
-
exports.model = model;
|
|
194
|
+
exports.model = require_model.model;
|
|
339
195
|
exports.ref = ref;
|
|
196
|
+
exports.resolveAlias = require_model.resolveAlias;
|
|
340
197
|
exports.union = union;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,2 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { EventCallable, Store } from "effector";
|
|
2
|
+
import { C as RenameParams, E as resolveAlias, S as Reducers, T as model, _ 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 UpdateParams, x as Reducer, y as Model } from "./relations-DXJ6slzh.cjs";
|
|
3
|
+
/** Часть модели, нужная алиасам. */
|
|
4
|
+
type AliasTarget = Pick<Model<object, Record<never, unknown>>, "$aliases" | "alias" | "unalias">;
|
|
5
|
+
interface Aliases {
|
|
6
|
+
/** Алиас → настоящий id экземпляра. */
|
|
7
|
+
readonly $aliases: Store<Record<string, string>>;
|
|
8
|
+
readonly add: EventCallable<AliasParams | AliasParams[]>;
|
|
9
|
+
readonly remove: EventCallable<Ids>;
|
|
10
|
+
/** Возвращает то же событие: события модели сами принимают алиасы. */
|
|
11
|
+
readonly resolved: <Params extends {
|
|
12
|
+
id: Ids;
|
|
13
|
+
}>(target: EventCallable<Params>) => EventCallable<Params>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated Алиасы встроены в модель: `model.$aliases`, `model.alias`, `model.unalias`,
|
|
17
|
+
* а события и хуки принимают алиас вместо id. Обёртка оставлена для кода, написанного под alpha.2.
|
|
18
|
+
*/
|
|
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 };
|
|
21
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../lib/aliases.ts"],"mappings":";;;KAKY,cAAc,KACxB,cAAc;UAIC;;WAEN,UAAU,MAAM;WAChB,KAAK,cAAc,cAAc;WACjC,QAAQ,cAAc;;WAEtB,WAAW;IAAiB,IAAI;KACvC,QAAQ,cAAc,YACnB,cAAc;;;;;;wBAOL,QAAQ,OAAO,cAAc"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,2 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { EventCallable, Store } from "effector";
|
|
2
|
+
import { C as RenameParams, E as resolveAlias, S as Reducers, T as model, _ 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 UpdateParams, x as Reducer, y as Model } from "./relations-Cc9Mv0m7.mjs";
|
|
3
|
+
/** Часть модели, нужная алиасам. */
|
|
4
|
+
type AliasTarget = Pick<Model<object, Record<never, unknown>>, "$aliases" | "alias" | "unalias">;
|
|
5
|
+
interface Aliases {
|
|
6
|
+
/** Алиас → настоящий id экземпляра. */
|
|
7
|
+
readonly $aliases: Store<Record<string, string>>;
|
|
8
|
+
readonly add: EventCallable<AliasParams | AliasParams[]>;
|
|
9
|
+
readonly remove: EventCallable<Ids>;
|
|
10
|
+
/** Возвращает то же событие: события модели сами принимают алиасы. */
|
|
11
|
+
readonly resolved: <Params extends {
|
|
12
|
+
id: Ids;
|
|
13
|
+
}>(target: EventCallable<Params>) => EventCallable<Params>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated Алиасы встроены в модель: `model.$aliases`, `model.alias`, `model.unalias`,
|
|
17
|
+
* а события и хуки принимают алиас вместо id. Обёртка оставлена для кода, написанного под alpha.2.
|
|
18
|
+
*/
|
|
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 };
|
|
21
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":["EventCallable","Store","AliasParams","Ids","Model","AliasTarget","Record","Pick","Aliases","Params","aliases"],"sources":["../lib/aliases.ts"],"mappings":";;;KAKYK,cAAcE,KACxBH,cAAcE;UAICE;;WAEN,UAAUP,MAAMK;WAChB,KAAKN,cAAcE,cAAcA;WACjC,QAAQF,cAAcG;;WAEtB,WAAW;IAAiB,IAAIA;KACvC,QAAQH,cAAcS,YACnBT,cAAcS;;;;;;wBAOLC,QAAQ,OAAOL,cAAcG"}
|