nanoeffects 0.29.0 → 0.31.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/README.md +6 -5
- package/dist/{action-creator-jeEqIKgK.d.ts → action-creator-6rlBTmH9.d.ts} +27 -2
- package/dist/actions.d.ts +3 -3
- package/dist/actions.js +1 -1
- package/dist/{bus-0uYn_kRj.js → bus-DFlmNJ6h.js} +1 -1
- package/dist/context-B5Z2RpJt.js +1 -0
- package/dist/{context-types-Bme1mzkn.d.ts → context-types-DKNc5mkS.d.ts} +1 -1
- package/dist/context.d.ts +2 -2
- package/dist/dev.d.ts +49 -3
- package/dist/dev.js +2 -1
- package/dist/effects.d.ts +2 -2
- package/dist/effects.js +1 -1
- package/dist/emit-key-C6FL7Ewn.js +1 -0
- package/dist/guard.d.ts +59 -0
- package/dist/guard.js +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/match.d.ts +1 -1
- package/dist/{module-BWMW83qk.d.ts → module-G39MaVOt.d.ts} +2 -2
- package/dist/notrace/{action-creator-xpQ_I2h8.d.ts → action-creator-BrMSO3RP.d.ts} +1 -0
- package/dist/notrace/actions.d.ts +2 -2
- package/dist/notrace/effects.d.ts +2 -2
- package/dist/notrace/index.d.ts +1 -1
- package/dist/notrace/{plugin-6LhNx0th.d.ts → plugin-7GUL2EW5.d.ts} +1 -1
- package/dist/notrace/policies.d.ts +1 -1
- package/dist/notrace/redux.d.ts +1 -1
- package/dist/observable.d.ts +1 -1
- package/dist/oop.d.ts +1 -1
- package/dist/oop.js +1 -1
- package/dist/{plugin-BaErevLy.d.ts → plugin-BWNP23hr.d.ts} +2 -2
- package/dist/policies.d.ts +2 -2
- package/dist/policies.js +1 -1
- package/dist/production/dev.js +1 -1
- package/dist/react.d.ts +2 -2
- package/dist/react.js +1 -1
- package/dist/redux.d.ts +1 -1
- package/dist/scope.d.ts +2 -2
- package/dist/signal.d.ts +22 -2
- package/dist/signal.js +1 -1
- package/dist/site-DLvUoXs3.js +1 -0
- package/dist/svelte.d.ts +1 -1
- package/dist/trace.d.ts +151 -2
- package/dist/trace.js +3 -1
- package/dist/{types-D5xsR37j.d.ts → types-N_cdRr1r.d.ts} +2 -2
- package/dist/vue.d.ts +1 -1
- package/dist/{with-policy-C6BIiPIi.js → with-policy-CeM1rtES.js} +1 -1
- package/dist/{with-policy-BZVRBUOm.d.ts → with-policy-CthiBp_z.d.ts} +1 -1
- package/docs/README.md +62 -31
- package/docs/actions.md +44 -27
- package/docs/cancel-stale-requests.md +2 -0
- package/docs/chat-example.md +3 -1
- package/docs/chat-widget.md +2 -0
- package/docs/context.md +322 -9
- package/docs/debounce-and-abort.md +3 -1
- package/docs/debugging-loops.md +318 -0
- package/docs/effects.md +255 -138
- package/docs/errors.md +4 -2
- package/docs/examples.md +60 -0
- package/docs/guide.md +39 -22
- package/docs/hmr.md +35 -1
- package/docs/loop-guard.md +109 -0
- package/docs/migrating-from-effector.md +4 -2
- package/docs/migrating-from-mitt.md +6 -4
- package/docs/migrating-from-rxjs.md +2 -0
- package/docs/migrating-from-saga.md +2 -0
- package/docs/motivation.md +14 -21
- package/docs/observable.md +2 -0
- package/docs/oop.md +3 -1
- package/docs/package.md +4 -1
- package/docs/pass-the-signal.md +346 -0
- package/docs/plugins.md +22 -5
- package/docs/policies.md +73 -46
- package/docs/prevent-double-submit.md +3 -1
- package/docs/query-libraries.md +3 -1
- package/docs/react.md +42 -13
- package/docs/recipes.md +528 -0
- package/docs/ru/README.md +62 -0
- package/docs/ru/actions.md +412 -0
- package/docs/ru/cancel-stale-requests.md +106 -0
- package/docs/ru/chat-example.md +259 -0
- package/docs/ru/chat-widget.md +131 -0
- package/docs/ru/context.md +402 -0
- package/docs/ru/debounce-and-abort.md +105 -0
- package/docs/ru/debugging-loops.md +318 -0
- package/docs/ru/effects.md +538 -0
- package/docs/ru/errors.md +36 -0
- package/docs/ru/examples.md +60 -0
- package/docs/ru/guide.md +530 -0
- package/docs/ru/hmr.md +83 -0
- package/docs/ru/loop-guard.md +109 -0
- package/docs/ru/migrating-from-effector.md +339 -0
- package/docs/ru/migrating-from-mitt.md +99 -0
- package/docs/ru/migrating-from-rxjs.md +197 -0
- package/docs/ru/migrating-from-saga.md +97 -0
- package/docs/ru/motivation.md +47 -0
- package/docs/ru/observable.md +233 -0
- package/docs/ru/oop.md +431 -0
- package/docs/ru/package.md +42 -0
- package/docs/ru/pass-the-signal.md +346 -0
- package/docs/ru/plugins.md +398 -0
- package/docs/ru/policies.md +201 -0
- package/docs/ru/prevent-double-submit.md +122 -0
- package/docs/ru/query-libraries.md +392 -0
- package/docs/ru/react.md +309 -0
- package/docs/ru/recipes.md +528 -0
- package/docs/ru/semantics.md +19 -0
- package/docs/ru/server.md +204 -0
- package/docs/ru/starter.md +118 -0
- package/docs/ru/stores.md +673 -0
- package/docs/ru/svelte.md +78 -0
- package/docs/ru/tracing.md +466 -0
- package/docs/ru/typescript.md +170 -0
- package/docs/ru/vue.md +92 -0
- package/docs/semantics.md +2 -0
- package/docs/server.md +2 -0
- package/docs/starter.md +2 -0
- package/docs/stores.md +2 -0
- package/docs/svelte.md +3 -1
- package/docs/tracing.md +58 -1
- package/docs/typescript.md +2 -0
- package/docs/vue.md +3 -1
- package/llms-full.txt +2087 -418
- package/llms.txt +11 -8
- package/package.json +23 -6
- package/dist/context-DtUnzC-1.js +0 -1
- package/docs/scope.md +0 -278
- package/docs/stability.md +0 -90
package/docs/chat-widget.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Build a chat widget that listens only while it is open
|
|
2
2
|
|
|
3
|
+
> **Level D.** Read first: [Guide](guide.md#1-create-a-bus) steps 1 to 3.
|
|
4
|
+
|
|
3
5
|
Use this page when a site has a chat with a bot behind a launcher button, and the chat must cost nothing while it is closed. Closed means no subscriptions on the bus, no polling, no keyboard listener, and no request still running. Opening turns all of it on, and closing turns all of it off in one call.
|
|
4
6
|
|
|
5
7
|
The code is in [`examples/chat-widget`](../examples/chat-widget): plain DOM and TypeScript, a fake bot in the browser, no server. Run it on [StackBlitz](https://stackblitz.com/github/nanoeffects/nanoeffects/tree/main/examples/chat-widget), or on your machine:
|
package/docs/context.md
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Working with context
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **Level D.** Read first: [Guide](guide.md#1-create-a-bus).
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Optional: put a store, client or logger on every task, or give fields to one action, module or call. Skip until many handlers need the same dependency.
|
|
6
|
+
|
|
7
|
+
## On this page
|
|
8
|
+
|
|
9
|
+
- [Context fields](#context-fields)
|
|
10
|
+
- [Pass values at runtime](#pass-values-at-runtime)
|
|
11
|
+
- [Type the fields (optional)](#type-the-fields-optional)
|
|
12
|
+
- [Build a field from the signal of each task](#build-a-field-from-the-signal-of-each-task)
|
|
13
|
+
- [Scope: fields for an action, a module and a call](#scope-fields-for-an-action-a-module-and-a-call)
|
|
14
|
+
- [Turn scopes on](#turn-scopes-on)
|
|
15
|
+
- [Give an action its own fields](#give-an-action-its-own-fields)
|
|
16
|
+
- [Bind an effect to the task](#bind-an-effect-to-the-task)
|
|
17
|
+
- [Replace a field for one call](#replace-a-field-for-one-call)
|
|
18
|
+
- [Give an action, a job or a module its own fields](#give-an-action-a-job-or-a-module-its-own-fields)
|
|
19
|
+
- [Which value wins](#which-value-wins)
|
|
20
|
+
- [Keep a generic effect generic](#keep-a-generic-effect-generic)
|
|
21
|
+
- [Type the fields of a scope](#type-the-fields-of-a-scope)
|
|
22
|
+
- [Limits](#limits)
|
|
23
|
+
|
|
24
|
+
## Context fields
|
|
25
|
+
|
|
26
|
+
### Pass values at runtime
|
|
27
|
+
|
|
28
|
+
<!-- docs-check: new -->
|
|
6
29
|
|
|
7
30
|
```js
|
|
8
31
|
import { createBus } from "nanoeffects";
|
|
@@ -15,15 +38,17 @@ bus.on("save", (draft, { store }) => {
|
|
|
15
38
|
});
|
|
16
39
|
```
|
|
17
40
|
|
|
18
|
-
That is enough in JavaScript. The fields appear on handlers, `ctx.run`, `ctx.on`, actions, modules, and child tasks of effects and policies.
|
|
41
|
+
Here `store` is your own object: a store, an API client, a logger, anything. That is enough in JavaScript. The fields appear on handlers, `ctx.run`, `ctx.on`, actions, modules, and child tasks of effects and policies.
|
|
19
42
|
|
|
20
43
|
Library fields (`signal`, `emit`, `run`, …) win on name clashes. `context` may be a function `(task) => fields` when the value depends on the task.
|
|
21
44
|
|
|
22
|
-
To get `race`, `all`, `take` and the like on every task without passing `ctx`, see [`effectsPlugin`](effects.md#
|
|
45
|
+
To get `race`, `all`, `take` and the like on every task without passing `ctx`, see [`effectsPlugin`](effects.md#effectsplugin).
|
|
46
|
+
|
|
47
|
+
To give fields to **one** action, job or module, or to replace them for one `emit`, see [scopes](context.md#scope-fields-for-an-action-a-module-and-a-call).
|
|
23
48
|
|
|
24
|
-
|
|
49
|
+
### Type the fields (optional)
|
|
25
50
|
|
|
26
|
-
|
|
51
|
+
<!-- docs-check: new -->
|
|
27
52
|
|
|
28
53
|
In TypeScript, `contextPlugin` (from `nanoeffects/context`) puts the fields on every task of **this** bus and takes their types from the value you pass. The event map holds only events. It does not wire anything across the app — another bus can have different fields or none.
|
|
29
54
|
|
|
@@ -45,7 +70,9 @@ bus.on("save", (draft, { store }) => store.put(draft));
|
|
|
45
70
|
- A name that the bus already has is a type error. The fields of the library (`signal`, `emit`, `run`, …) win at runtime, and a field that the context already has is not replaced.
|
|
46
71
|
- The plugin returns the same bus, so keep the result of the chain: `const bus = createBus().use(contextPlugin({ store }))`.
|
|
47
72
|
|
|
48
|
-
|
|
73
|
+
### Build a field from the signal of each task
|
|
74
|
+
|
|
75
|
+
<!-- docs-check: new -->
|
|
49
76
|
|
|
50
77
|
Problem: an HTTP client or a database handle should stop when the task is aborted. Without help, every handler passes `ctx.signal` to it by hand, and one forgotten call leaves a request running.
|
|
51
78
|
|
|
@@ -72,12 +99,13 @@ bus.on("user/open", async (id, ctx) => {
|
|
|
72
99
|
});
|
|
73
100
|
```
|
|
74
101
|
|
|
102
|
+
- `signalFetch` from the same entry is a `fetch` bound to a signal, so `fetch: fromSignal(signalFetch)` gives every handler a `ctx.fetch` that stops with the task. [Bind the signal once](pass-the-signal.md#bind-the-signal-once)
|
|
75
103
|
- `make` runs at the first read, so a handler that never uses `ctx.http` pays nothing for it.
|
|
76
104
|
- A task that `ctx.run` starts builds its own value from its own signal, so aborting the child stops only the child's requests.
|
|
77
105
|
- The field is not copied by `{ ...ctx }` and `Object.keys(ctx)` does not list it. Read `ctx.http` first if you copy a context.
|
|
78
106
|
- Without `contextPlugin` a `fromSignal` marker is only an object.
|
|
79
107
|
|
|
80
|
-
|
|
108
|
+
#### Name the fields in the event map instead
|
|
81
109
|
|
|
82
110
|
`createBus<Events>({ context: { store } })` also types the fields, but through the event map: `WithContext` is checker-only and says which fields the handlers of this bus have. Use it when you cannot call `use`, for example for the bus that `createEffectsMiddleware` makes.
|
|
83
111
|
|
|
@@ -89,3 +117,288 @@ type Events = { save: Draft } & WithContext<{ store: Store }>;
|
|
|
89
117
|
|
|
90
118
|
const bus = createBus<Events>({ context: { store } });
|
|
91
119
|
```
|
|
120
|
+
|
|
121
|
+
## Scope: fields for an action, a module and a call
|
|
122
|
+
|
|
123
|
+
Optional, for domain logic. Use it when one action needs its own client, one module its own logger, or one test its own fake, and a field on the whole bus ([context fields](context.md)) is too wide.
|
|
124
|
+
|
|
125
|
+
A **scope** is a plain object. Every key goes on `ctx` as it is (`cache`, `api`, `logger`), except one: `$effects`. Its functions take `{ signal }` as the first parameter, like `retry(ctx, fn)`, and go on `ctx` bound to the task, so you call `ctx.fetchJson(url)` instead of `fetchJson(ctx, url)`.
|
|
126
|
+
|
|
127
|
+
You give a scope to the thing that uses it. A call can replace it. Nothing else changes: handlers still get `(payload, ctx)`.
|
|
128
|
+
|
|
129
|
+
### Turn scopes on
|
|
130
|
+
|
|
131
|
+
<!-- docs-check: new -->
|
|
132
|
+
|
|
133
|
+
Scopes are a plugin, so a bus that does not use them pays nothing.
|
|
134
|
+
|
|
135
|
+
```js
|
|
136
|
+
// bus.js
|
|
137
|
+
import { createBus } from "nanoeffects";
|
|
138
|
+
import { scopePlugin } from "nanoeffects/scope";
|
|
139
|
+
|
|
140
|
+
// scopePlugin — makes the scopes below work on this bus; returns the same bus
|
|
141
|
+
export const bus = createBus().use(scopePlugin());
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
The bus is in one file and the actions in files named after their domain, so the next example imports `bus` from here. Each section after it keeps its own `bus.js` and domain file, as if it were an app of its own.
|
|
145
|
+
|
|
146
|
+
- Without `scopePlugin` a scope is **ignored**: no error and no warning. If a field is missing from `ctx`, check this first.
|
|
147
|
+
- Use it in any order with the other plugins: a scope wins over `effectsPlugin`, `contextPlugin` and `createBus({ context })`.
|
|
148
|
+
|
|
149
|
+
### Give an action its own fields
|
|
150
|
+
|
|
151
|
+
<!-- docs-check: new -->
|
|
152
|
+
|
|
153
|
+
<!-- docs-check: continue -->
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
// user.js
|
|
157
|
+
import { createAction } from "nanoeffects/actions";
|
|
158
|
+
|
|
159
|
+
// createAction — third argument is the scope of this action
|
|
160
|
+
export const getUser = createAction(
|
|
161
|
+
"user/get",
|
|
162
|
+
(id, { api, cache }) => {
|
|
163
|
+
// api and cache are on ctx for this action only
|
|
164
|
+
return cache.get(id) ?? api.getUser(id);
|
|
165
|
+
},
|
|
166
|
+
{ api, cache: new Map() },
|
|
167
|
+
);
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
```js
|
|
171
|
+
import { bus } from "./bus";
|
|
172
|
+
import { getUser } from "./user";
|
|
173
|
+
|
|
174
|
+
const user = await bus.emit(getUser("1")); // the handler has api and cache
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
The listeners of `"user/get"` get the same fields when the action is emitted, because the fields belong to the call.
|
|
178
|
+
|
|
179
|
+
### Bind an effect to the task
|
|
180
|
+
|
|
181
|
+
<!-- docs-check: new -->
|
|
182
|
+
|
|
183
|
+
An effect is a function with `{ signal }` as its first parameter. In `$effects` it is bound to the task, so it stops with the task.
|
|
184
|
+
|
|
185
|
+
```js
|
|
186
|
+
// feed.js
|
|
187
|
+
import { createAction } from "nanoeffects/actions";
|
|
188
|
+
import { delay, race } from "nanoeffects/effects";
|
|
189
|
+
|
|
190
|
+
// your own effect: first parameter is the task (only its signal is used)
|
|
191
|
+
const fetchJson = (ctx, url) =>
|
|
192
|
+
fetch(url, { signal: ctx.signal }).then((response) => response.json());
|
|
193
|
+
|
|
194
|
+
export const loadFeed = createAction(
|
|
195
|
+
"feed/load",
|
|
196
|
+
(url, { fetchJson, race, delay }) =>
|
|
197
|
+
// race, delay, fetchJson — bound to this task: no ctx argument
|
|
198
|
+
race({
|
|
199
|
+
feed: () => fetchJson(url),
|
|
200
|
+
timeout: () => delay(5000),
|
|
201
|
+
}),
|
|
202
|
+
// $effects — built-in and your own functions share one object
|
|
203
|
+
{ $effects: { fetchJson, race, delay } },
|
|
204
|
+
);
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
```js
|
|
208
|
+
// bus.js
|
|
209
|
+
import { createBus } from "nanoeffects";
|
|
210
|
+
import { scopePlugin } from "nanoeffects/scope";
|
|
211
|
+
|
|
212
|
+
export const bus = createBus().use(scopePlugin());
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
```js
|
|
216
|
+
import { bus } from "./bus";
|
|
217
|
+
import { loadFeed } from "./feed";
|
|
218
|
+
|
|
219
|
+
const result = await bus.emit(loadFeed("/feed"));
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
- Put in `$effects` only functions that take `ctx` first. A plain function (a formatter, a logger) belongs in `context`.
|
|
223
|
+
- A function under any other key is copied as it is; a function in `$effects` is bound.
|
|
224
|
+
- `$effects` is the one name with a meaning. A field called `effects` is an ordinary field: `{ effects: { retries: 3 } }` puts that object on `ctx`, and nothing in it is bound.
|
|
225
|
+
|
|
226
|
+
### Replace a field for one call
|
|
227
|
+
|
|
228
|
+
<!-- docs-check: new -->
|
|
229
|
+
|
|
230
|
+
The second argument of `emit` is a scope for the tasks of **that** call. It is the usual way to give a test a fake.
|
|
231
|
+
|
|
232
|
+
```js
|
|
233
|
+
// bus.js
|
|
234
|
+
import { createBus } from "nanoeffects";
|
|
235
|
+
import { scopePlugin } from "nanoeffects/scope";
|
|
236
|
+
|
|
237
|
+
export const bus = createBus().use(scopePlugin());
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
```js
|
|
241
|
+
// user.js
|
|
242
|
+
import { createAction } from "nanoeffects/actions";
|
|
243
|
+
|
|
244
|
+
export const getUser = createAction(
|
|
245
|
+
"user/get",
|
|
246
|
+
(id, { api }) => api.getUser(id),
|
|
247
|
+
{ api },
|
|
248
|
+
);
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
```js
|
|
252
|
+
import { bus } from "./bus";
|
|
253
|
+
import { getUser } from "./user";
|
|
254
|
+
|
|
255
|
+
// the real api for this call
|
|
256
|
+
await bus.emit(getUser("1"));
|
|
257
|
+
|
|
258
|
+
// the second argument — a fake api, for this call only
|
|
259
|
+
const fakeApi = { getUser: async (id) => ({ id, name: "test" }) };
|
|
260
|
+
await bus.emit(getUser("1"), { api: fakeApi });
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Rules for the second argument:
|
|
264
|
+
|
|
265
|
+
- It works with the call of an action (`emit(getUser("1"), scope)`, `ctx.emit(loaded(user), scope)`).
|
|
266
|
+
- It does not work with the string form: in `emit("user/get", payload)` the second argument is the payload.
|
|
267
|
+
- It can replace `$effects` too: `{ $effects: { delay: instantDelay } }`.
|
|
268
|
+
- Fields of the library (`signal`, `emit`, …) in it are ignored.
|
|
269
|
+
|
|
270
|
+
### Give an action, a job or a module its own fields
|
|
271
|
+
|
|
272
|
+
<!-- docs-check: new -->
|
|
273
|
+
|
|
274
|
+
```js
|
|
275
|
+
// user.js
|
|
276
|
+
import { createAction, createJob } from "nanoeffects/actions";
|
|
277
|
+
|
|
278
|
+
// createAction — the listeners of this action's calls get the fields
|
|
279
|
+
export const userLoaded = createAction("user/loaded", { store });
|
|
280
|
+
|
|
281
|
+
// createJob — the scope is the fourth argument, after the outcomes;
|
|
282
|
+
// the handler and the listeners of the outcome get its fields
|
|
283
|
+
export const loadUser = createJob(
|
|
284
|
+
"user/load",
|
|
285
|
+
fetchUser,
|
|
286
|
+
{ success: userLoaded, fail: "user/failed" },
|
|
287
|
+
{ logger },
|
|
288
|
+
);
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
```js
|
|
292
|
+
// audit-module.js
|
|
293
|
+
import { createModule } from "nanoeffects/actions";
|
|
294
|
+
import { userLoaded } from "./user";
|
|
295
|
+
|
|
296
|
+
// createModule — third argument; everything the module owns gets the fields:
|
|
297
|
+
// m.on listeners, m.handle handlers, m.spawn tasks
|
|
298
|
+
export const auditModule = createModule(
|
|
299
|
+
"audit",
|
|
300
|
+
(m) => {
|
|
301
|
+
m.on(userLoaded, (user, { logger }) => logger.info(user.id));
|
|
302
|
+
},
|
|
303
|
+
{ logger },
|
|
304
|
+
);
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
If the outcome action (`success` or `fail`) has a scope of its own, its listeners see the job's fields with the action's own fields over them; the handler of the job sees only the job's.
|
|
308
|
+
|
|
309
|
+
A module scope does not leak: a listener registered outside the module does not see it.
|
|
310
|
+
|
|
311
|
+
### Which value wins
|
|
312
|
+
|
|
313
|
+
<!-- docs-check: new -->
|
|
314
|
+
|
|
315
|
+
When several places set the same field, the strongest one wins:
|
|
316
|
+
|
|
317
|
+
| Strength | Where it is set |
|
|
318
|
+
| -------- | --------------- |
|
|
319
|
+
| 1 (strongest) | the second argument of `emit` |
|
|
320
|
+
| 2 | the scope of the action or job |
|
|
321
|
+
| 3 | the scope of the module that owns the handler |
|
|
322
|
+
| 4 (weakest) | the bus: `createBus({ context })`, `contextPlugin`, `effectsPlugin` |
|
|
323
|
+
|
|
324
|
+
The fields of the library (`signal`, `abort`, `emit`, `on`, `off`, `run`, `type`, `payload`) are never replaced. The name `traceGroupId` is reserved: the tracer reads it, and it is not put on `ctx` ([Follow one flow](tracing.md#follow-one-flow-with-a-tracegroupid)). A child task (`ctx.run`, an attempt of `retry`) has the fields of its parent.
|
|
325
|
+
|
|
326
|
+
### Keep a generic effect generic
|
|
327
|
+
|
|
328
|
+
<!-- docs-check: new -->
|
|
329
|
+
|
|
330
|
+
In `$effects` a function loses its first parameter, and TypeScript cannot keep a generic through that step: `<T>(items: T[]) => T` becomes `(items: unknown[]) => unknown`. JavaScript code is not affected. If you want the exact type, use one of two ways.
|
|
331
|
+
|
|
332
|
+
`effect(fn)`: mark the function where you declare it, then put it in the scope like any other value. No annotation, so a JavaScript editor shows the exact hint too.
|
|
333
|
+
|
|
334
|
+
```js
|
|
335
|
+
// pick.js
|
|
336
|
+
import { createAction } from "nanoeffects/actions";
|
|
337
|
+
import { effect } from "nanoeffects/scope";
|
|
338
|
+
|
|
339
|
+
// effect — marks the function, so a scope binds it to the task; the editor keeps its generic
|
|
340
|
+
const first = effect((ctx, items) => items[0]);
|
|
341
|
+
|
|
342
|
+
export const pick = createAction("pick", (items, { first }) => first(items), {
|
|
343
|
+
first, // a plain key of the scope, not an entry of `$effects`
|
|
344
|
+
});
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
Call `effect` where you declare the function, as above, and not inline in the scope: TypeScript does not infer the scope from a generic call written there.
|
|
348
|
+
|
|
349
|
+
`Effect<F>` (TypeScript): declare how the effect looks **after** it is bound, and keep it in `$effects`.
|
|
350
|
+
|
|
351
|
+
```ts
|
|
352
|
+
import { createAction } from "nanoeffects/actions";
|
|
353
|
+
import type { Effect } from "nanoeffects/effects";
|
|
354
|
+
|
|
355
|
+
// First — checker-only: how a handler calls the effect (no ctx)
|
|
356
|
+
type First = <T>(items: T[]) => T;
|
|
357
|
+
|
|
358
|
+
// Effect<First> — the function takes the task first and is typed as First once bound
|
|
359
|
+
const first: Effect<First> = (ctx, items) => items[0];
|
|
360
|
+
|
|
361
|
+
const pick = createAction(
|
|
362
|
+
"pick",
|
|
363
|
+
(id: string, { first }) => first([id]), // string
|
|
364
|
+
{ $effects: { first } },
|
|
365
|
+
);
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
`Effect<F>` and `effect` change what the checker sees. At run time the function is the same.
|
|
369
|
+
|
|
370
|
+
### Type the fields of a scope
|
|
371
|
+
|
|
372
|
+
<!-- docs-check: new -->
|
|
373
|
+
|
|
374
|
+
A handler of an action, job or module with a scope sees the fields of the scope in `ctx`, typed from the values you gave. You write no `WithContext`.
|
|
375
|
+
|
|
376
|
+
```ts
|
|
377
|
+
import { createAction } from "nanoeffects/actions";
|
|
378
|
+
|
|
379
|
+
type User = { id: string; name: string };
|
|
380
|
+
|
|
381
|
+
const getUser = createAction(
|
|
382
|
+
"user/get",
|
|
383
|
+
async (id: string, ctx) => {
|
|
384
|
+
ctx.cache; // Map<string, User>, from the scope below
|
|
385
|
+
return ctx.cache.get(id);
|
|
386
|
+
},
|
|
387
|
+
{ cache: new Map<string, User>() },
|
|
388
|
+
);
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
- A field of a scope replaces a field of the same name from the bus in the type too.
|
|
392
|
+
- An override in `emit` must match the type of the field it replaces: `emit(getUser("1"), { cache: 1 })` is an error.
|
|
393
|
+
- A field that no scope and no bus declares has no type to check against, so `emit(call, { unknown: 1 })` is an error in TypeScript.
|
|
394
|
+
- `bus.on(creator, fn)` for an action with a scope types `ctx` with its fields.
|
|
395
|
+
|
|
396
|
+
### Limits
|
|
397
|
+
|
|
398
|
+
<!-- docs-check: new -->
|
|
399
|
+
|
|
400
|
+
- No plugin, no scope: see [Turn scopes on](#turn-scopes-on).
|
|
401
|
+
- The fields of the bus are not in the handler of an action made with `createAction(…, scope)` unless you type `ctx` yourself; the fields of the scope are.
|
|
402
|
+
- The string form of `emit` takes no scope.
|
|
403
|
+
- `effect(fn)` works only as a value of a scope, not as an entry of `$effects`.
|
|
404
|
+
- A scope on an action, job or module is typed without the events of a bus, so `take` in its `$effects` takes any event name.
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Pause between requests
|
|
2
|
+
|
|
3
|
+
> **Level B.** Read first: [Policies](policies.md).
|
|
2
4
|
|
|
3
5
|
Read this page if you debounce user input (a search box, an autosave, a resize handler) and you also want the request in flight to stop when a new one starts. `debounce` alone waits for a pause. It does not cancel a request that already started. This page shows the two policies together, the order to put them in, and when to use `throttle` instead.
|
|
4
6
|
|