@sincpro/web-framework 2.4.0 → 2.5.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 +88 -60
- package/dist/core/cache.d.ts +19 -7
- package/dist/core/cache.js +81 -44
- package/dist/core/changes.d.ts +25 -0
- package/dist/core/changes.js +39 -0
- package/dist/core/names.d.ts +25 -0
- package/dist/core/names.js +79 -0
- package/dist/react/context.d.ts +15 -2
- package/dist/react/context.js +11 -4
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +1 -0
- package/dist/react/useGroups.js +4 -1
- package/dist/react/useHistory.js +4 -1
- package/dist/react/useListing.js +9 -6
- package/dist/react/useNameOf.d.ts +5 -4
- package/dist/react/useNameOf.js +25 -26
- package/dist/react/usePivot.js +4 -1
- package/dist/react/useRecord.js +10 -6
- package/dist/react/useWrites.d.ts +19 -0
- package/dist/react/useWrites.js +35 -0
- package/dist/web/index.d.ts +1 -0
- package/dist/web/index.js +1 -0
- package/dist/web/record/ActionButton.d.ts +34 -0
- package/dist/web/record/ActionButton.js +41 -0
- package/dist/web/record/CommandButton.d.ts +5 -6
- package/dist/web/record/CommandButton.js +16 -33
- package/dist/web/record/ServiceButton.d.ts +31 -0
- package/dist/web/record/ServiceButton.js +40 -0
- package/dist/web/workspace/SincproApp.d.ts +8 -2
- package/dist/web/workspace/SincproApp.js +13 -7
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -396,6 +396,30 @@ function CloseButton({ issueId }: { issueId: string }) {
|
|
|
396
396
|
}
|
|
397
397
|
```
|
|
398
398
|
|
|
399
|
+
Or the button that draws it — `CommandButton`'s gesture for a use case: off while it runs, a
|
|
400
|
+
refusal where it happened, and an action that destroys asks first what it would do (the use case
|
|
401
|
+
run dry) and shows it:
|
|
402
|
+
|
|
403
|
+
```tsx
|
|
404
|
+
<ServiceButton
|
|
405
|
+
label="Cancel"
|
|
406
|
+
run={() => projectBoundedContext.execute(new CommandCancelIssue(issue.id))}
|
|
407
|
+
rehearse={() => projectBoundedContext.execute(new CommandCancelIssue(issue.id, true))}
|
|
408
|
+
/>
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
**What a use case wrote, every screen sees.** Each write a transport of the framework sends is
|
|
412
|
+
announced on the process's change feed (`processChanges`, client-framework) — a screen's save, a
|
|
413
|
+
use case's generated client, a panel beside. The cache forgets what it touched, and every reading
|
|
414
|
+
on screen — `useListing`, `useRecord`, `useHistory`, `useGroups`, `usePivot`, `useNameOf` — reads
|
|
415
|
+
again, keeping its filter, order and page; a record being edited keeps what was typed. A screen
|
|
416
|
+
never calls `reload()` after an action. A reading of an application's own follows the same feed:
|
|
417
|
+
|
|
418
|
+
```ts
|
|
419
|
+
const writes = useWrites(route); // how many times what lies at `route` was written
|
|
420
|
+
useEffect(() => { … }, [route, writes]);
|
|
421
|
+
```
|
|
422
|
+
|
|
399
423
|
A bounded context with no sincpro_framework behind it declares its model by hand and is drawn the same
|
|
400
424
|
way:
|
|
401
425
|
|
|
@@ -442,7 +466,8 @@ hook and draw your own, or go straight to the loose piece.
|
|
|
442
466
|
| Look at the set several ways | `EntityList views` (table, rows, board, sheet) | — | web-ui's `List.*` |
|
|
443
467
|
| Cross it by two fields | `EntityList pivot` | `usePivot` | `pivotOf` |
|
|
444
468
|
| Order by several fields | the bar's criteria builder | `listing.orderBy([...])` | criteria's `order` |
|
|
445
|
-
| Act on it (with a dry run) | `CommandButton`,
|
|
469
|
+
| Act on it (with a dry run) | `ServiceButton` (a use case), `CommandButton`, row commands | `useService`, `useCommand` | `transport.send` |
|
|
470
|
+
| Read again when it was written | every reading of this package, by itself | `useWrites` | `processChanges` (client-framework) |
|
|
446
471
|
| What happened to it | `Record.Activity` fed by `useHistory` | `useHistory` | `historyOf` |
|
|
447
472
|
| The application context | `SincproApp` | `useRecordInWorkspace`, `useFollowed` | `FrameworkProvider`, `webUiSkin` |
|
|
448
473
|
|
|
@@ -461,15 +486,16 @@ The rules behind them, in short:
|
|
|
461
486
|
|
|
462
487
|
There is no store of ours to learn: each kind of state already has its owner.
|
|
463
488
|
|
|
464
|
-
| State | Lives in
|
|
465
|
-
| -------------------------------------------- |
|
|
466
|
-
| Who answers, language, known models | `FrameworkProvider` (this package)
|
|
467
|
-
| What the backend sees on every request | `processContext` (client-framework), as `sincpro-context`
|
|
468
|
-
| What an opening carries (company, a default) | composer: the reference's `params`
|
|
469
|
-
| The record a modal covers | composer: `api.over`
|
|
470
|
-
| What a chooser was asked | composer: `pick(…, { question })`
|
|
471
|
-
| What the user is on, what is selected | composer: `api.context`
|
|
472
|
-
| A screen's list, record, groups | the hooks
|
|
489
|
+
| State | Lives in | Read with |
|
|
490
|
+
| -------------------------------------------- | ---------------------------------------------------------- | ------------------------------------ |
|
|
491
|
+
| Who answers, language, known models | `FrameworkProvider` (this package) | the hooks, through context |
|
|
492
|
+
| What the backend sees on every request | `processContext` (client-framework), as `sincpro-context` | the backend's `use_context()` |
|
|
493
|
+
| What an opening carries (company, a default) | composer: the reference's `params` | `api.self.params`, `api.setParams` |
|
|
494
|
+
| The record a modal covers | composer: `api.over` | `api.over` |
|
|
495
|
+
| What a chooser was asked | composer: `pick(…, { question })` | `api.choosing` |
|
|
496
|
+
| What the user is on, what is selected | composer: `api.context` | `useWorkspaceContext(api)` |
|
|
497
|
+
| A screen's list, record, groups | the hooks | `useListing`, `useRecord`, … |
|
|
498
|
+
| What anyone wrote | `processChanges` (client-framework): every write announced | `useWrites`, the hooks by themselves |
|
|
473
499
|
|
|
474
500
|
Not yet: a new record does not take the `params` it was opened with as its initial values
|
|
475
501
|
(`ROADMAP.md`, «Estado y contexto»).
|
|
@@ -520,56 +546,58 @@ moves to the generated one — is [`docs/ADOPTION.md`](docs/ADOPTION.md).
|
|
|
520
546
|
|
|
521
547
|
## Where it stands
|
|
522
548
|
|
|
523
|
-
| Piece | What it solves
|
|
524
|
-
| --------------------------------------- |
|
|
525
|
-
| `restTransport`, `memoryTransport` | The backend, or the same engine in memory (client-framework)
|
|
526
|
-
| `defineMeta`, `metaFromSchema` | A model by hand, or completed from OpenAPI (client-framework)
|
|
527
|
-
| `useService` | A screen runs one of the application's use cases: run, busy, answer, error
|
|
528
|
-
| `./telemetry`, `./errors` | Optional: the application's traces to its OTLP endpoint, its errors to GlitchTip
|
|
529
|
-
| `selection` | Filters named as criteria 2.0 and the backend read them, the search bar's included
|
|
530
|
-
| `useListing`, `EntityList` | N records: criteria, search bar, builder, paging
|
|
531
|
-
| `useRecord`, `factsOf`, `RecordLines` | One record: facts, relations with their real total
|
|
532
|
-
| Preloaded definitions | `FrameworkProvider metas`: the models of the discovery document, or of an exported metamodel
|
|
533
|
-
| `useLookup`, `EntityPicker` | Literal search: a many2one select, ⌘K
|
|
534
|
-
| `useRecord` + `RecordField`, `dirtyOf` | Editing: hints evaluated live, preview, save with version (409), server drafts
|
|
535
|
-
| `useCommand`, `CommandButton` | An entity's actions, with a dry run before what destroys
|
|
536
|
-
| `
|
|
537
|
-
| `
|
|
538
|
-
| `
|
|
539
|
-
|
|
|
540
|
-
| `
|
|
541
|
-
| `
|
|
542
|
-
| `
|
|
543
|
-
| `
|
|
544
|
-
| `
|
|
545
|
-
|
|
|
546
|
-
| `
|
|
547
|
-
|
|
|
548
|
-
| `
|
|
549
|
-
|
|
|
550
|
-
| `
|
|
551
|
-
| `
|
|
552
|
-
| `
|
|
553
|
-
| `
|
|
554
|
-
| `
|
|
555
|
-
| `entity
|
|
556
|
-
| `
|
|
557
|
-
| `
|
|
558
|
-
| `EntityPicker
|
|
559
|
-
| `
|
|
560
|
-
| `
|
|
561
|
-
| `
|
|
562
|
-
| `
|
|
563
|
-
| `
|
|
564
|
-
| `
|
|
565
|
-
| `
|
|
566
|
-
| `
|
|
567
|
-
|
|
|
568
|
-
| `
|
|
569
|
-
|
|
|
570
|
-
| `
|
|
571
|
-
| `
|
|
572
|
-
| `
|
|
549
|
+
| Piece | What it solves | Status |
|
|
550
|
+
| --------------------------------------- | ----------------------------------------------------------------------------------------------- | ------ |
|
|
551
|
+
| `restTransport`, `memoryTransport` | The backend, or the same engine in memory (client-framework) | ✅ |
|
|
552
|
+
| `defineMeta`, `metaFromSchema` | A model by hand, or completed from OpenAPI (client-framework) | ✅ |
|
|
553
|
+
| `useService` | A screen runs one of the application's use cases: run, busy, answer, error | ✅ |
|
|
554
|
+
| `./telemetry`, `./errors` | Optional: the application's traces to its OTLP endpoint, its errors to GlitchTip | ✅ |
|
|
555
|
+
| `selection` | Filters named as criteria 2.0 and the backend read them, the search bar's included | ✅ |
|
|
556
|
+
| `useListing`, `EntityList` | N records: criteria, search bar, builder, paging | ✅ |
|
|
557
|
+
| `useRecord`, `factsOf`, `RecordLines` | One record: facts, relations with their real total | ✅ |
|
|
558
|
+
| Preloaded definitions | `FrameworkProvider metas`: the models of the discovery document, or of an exported metamodel | ✅ |
|
|
559
|
+
| `useLookup`, `EntityPicker` | Literal search: a many2one select, ⌘K | ✅ |
|
|
560
|
+
| `useRecord` + `RecordField`, `dirtyOf` | Editing: hints evaluated live, preview, save with version (409), server drafts | ✅ |
|
|
561
|
+
| `useCommand`, `CommandButton` | An entity's actions, with a dry run before what destroys | ✅ |
|
|
562
|
+
| `ServiceButton` | A use case as an action: the same gesture, its dry run the use case's own | ✅ |
|
|
563
|
+
| `useWrites`, the change feed | Every reading on screen reads again when anyone writes what it shows — a use case included | ✅ |
|
|
564
|
+
| `entity<T, C>` (`CommandBodies`) | Typed Commands: a name the entity does not declare, or a wrong body, does not compile | ✅ |
|
|
565
|
+
| `entity<T, C, A>` (`CommandAnswers`) | What each Command answers, typed: `run(…)` returns it with no type argument written | ✅ |
|
|
566
|
+
| `readRoute`, `Transport.read` | A GET of the context's own (`/me`), neither a page nor a record | ✅ |
|
|
567
|
+
| A key alone (`EntityID`), `useNameOf` | A key that names whose record it is, drawn as a many2one; names asked in one request per entity | ✅ |
|
|
568
|
+
| `versionsOf`, `onVersionDrift` | A snapshot behind the server is told once per model — never an error | ✅ |
|
|
569
|
+
| `sincpro-client generate` | A context's DTOs, client and entities out of its exported client contract | ✅ |
|
|
570
|
+
| `metasOf`, `bodyFor` (`target`) | Definitions preloaded from that snapshot; a Command's record written where its body names it | ✅ |
|
|
571
|
+
| `useHistory` | Business events in `Record.Activity` (comments: backend first) | ✅ |
|
|
572
|
+
| `entityView` | An entity as a composer view: list, record, ⌘K search | ✅ |
|
|
573
|
+
| `<SincproApp>` | The application context in one place, the workspace dressed as web-ui (`webUiSkin`) | ✅ |
|
|
574
|
+
| `useRecordInWorkspace` | A record tells the workspace its real name and its unsaved work (composer 1.0) | ✅ |
|
|
575
|
+
| Paging by pages, ceiling on «more» | One page on screen, walked by the server's cursor: a million rows never freeze it | ✅ |
|
|
576
|
+
| `cachedTransport` | Repeated reads from memory and shared in flight, forgotten when anyone writes the entity | ✅ |
|
|
577
|
+
| `EntitySearchBar` | The search bar + criteria builder as a loose piece, for any `List` | ✅ |
|
|
578
|
+
| `EntityList` views | The same set as table, rows or board | ✅ |
|
|
579
|
+
| `useRelated`, `RelatedList` | A record's relation as a live list: searched and paged by the server, inside a form | ✅ |
|
|
580
|
+
| `EntityPicker` chooser | «see all…» opens the whole set, searched on the server: a many2one over millions | ✅ |
|
|
581
|
+
| `useRecord(entity, "")`, `create` | A new record: the model's defaults, saved to `create` (or `edit`), then it IS that record | ✅ |
|
|
582
|
+
| `retryKeys`, `Idempotency-Key` | A Command or a create retried after a lost answer runs once, not twice | ✅ |
|
|
583
|
+
| `entity.newRef()`, «New» in the list | The record's form with no id, in a page or a window; replaced by the record once created | ✅ |
|
|
584
|
+
| `EntityPicker criteria` | A many2one narrowed by another field: the invoices OF the chosen customer | ✅ |
|
|
585
|
+
| `chooserIn`, `entity.chooserRef()` | «see all…» as the composer's chooser: a row answers (`api.pick`) | ✅ |
|
|
586
|
+
| `EntityPicker` / `EntityTags` in a view | Open and choose in the workspace by themselves; the chooser carries the picker's cut | ✅ |
|
|
587
|
+
| `newEntityId`, `edit` on a new record | A new record born under a UUID v7 minted here: its draft and its save go through `Edit` | ✅ |
|
|
588
|
+
| `useGroups`, «Group by» | Groups counted by the server over the whole set; a group's rows read when it is opened | ✅ |
|
|
589
|
+
| `defineList`, `listView` | A named list (a fixed scope, its columns and filters) as data, and as a workspace view | ✅ |
|
|
590
|
+
| `EntityPicker` `browse`, `columns` | The chooser always a click away, as a table searched literally by the server | ✅ |
|
|
591
|
+
| `useFollowed` | A detail panel follows the record the person is on, without knowing the list | ✅ |
|
|
592
|
+
| `commands`, `useRowCommands` | An entity's Commands on each row, idempotent, offered only where `when` holds | ✅ |
|
|
593
|
+
| `summaryOf`, `cardOf`, `relationOf` | Any record at any size: name, description, state, type, picture, who made and changed it | ✅ |
|
|
594
|
+
| `openIn`, `RelatedRecord` | A related record opens in a modal; «Abrir en…» for the rest; never a modal over a modal | ✅ |
|
|
595
|
+
| Literal search everywhere | Picker: the first ten, the server's search as you type; tables: «Buscar «X»» on Enter | ✅ |
|
|
596
|
+
| `EntityTags` | A many2many as chips over thousands: the server's search, faces, the chosen never again | ✅ |
|
|
597
|
+
| `RecordField` + `entities` | A many2one / one2many drawn from the model alone, opened where the view says | ✅ |
|
|
598
|
+
| `EntityRecord`, `formOf` | Any entity's record screen without writing it | ✅ |
|
|
599
|
+
| `Trail`, `Record.Pager` | Previous / next from a record, across the server's pages | ✅ |
|
|
600
|
+
| `EntityList sums` | Each group says how much it adds up, summed by the server over all of it | ✅ |
|
|
573
601
|
|
|
574
602
|
What it does **not** do: mobile screens (`@sincpro/mobile` draws them; what it shares with the
|
|
575
603
|
web is @sincpro/client-framework), sessions and login (the application's), routing (composer's),
|
package/dist/core/cache.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Entity, type Transport } from "@sincpro/client-framework";
|
|
1
|
+
import { type Change, type Entity, type Transport } from "@sincpro/client-framework";
|
|
2
2
|
/**
|
|
3
3
|
* A transport that remembers what it read, for a while, and forgets it when something writes.
|
|
4
4
|
*
|
|
@@ -11,20 +11,32 @@ import { type Entity, type Transport } from "@sincpro/client-framework";
|
|
|
11
11
|
*
|
|
12
12
|
* What it keys on is the question as it travels — the route and the criteria — so two
|
|
13
13
|
* different filters, orders or pages never share an answer. A read that fails is not kept.
|
|
14
|
-
* Only completed answers are remembered: a request aborted half-way is simply not cached.
|
|
15
14
|
*
|
|
16
|
-
* A
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
15
|
+
* A question asked again while its answer is on its way joins it: two panels opening one record,
|
|
16
|
+
* a picker beside a column, are one request. Each reader may give up on its own (its `signal`);
|
|
17
|
+
* the request is cancelled only when every one of them did. A write while it flies makes its
|
|
18
|
+
* answer old: it is handed to who waited, never kept, and the next reader asks again.
|
|
19
|
+
*
|
|
20
|
+
* Who wrote does not matter. A write through this transport forgets at once; one made elsewhere —
|
|
21
|
+
* a use case's generated client, another transport — is told with `changed`, which is what the
|
|
22
|
+
* process's change feed carries (`processChanges`, @sincpro/client-framework). What a write
|
|
23
|
+
* touches is `touchedBy`'s rule (`core/changes`): every collection its route names, and the
|
|
24
|
+
* history of each entity it writes under (`entities`).
|
|
25
|
+
*
|
|
26
|
+
* A run binds it like any transport of the framework (`bindableTransport`): what a use case reads
|
|
27
|
+
* through it carries the run's trace and the process's context (`sincpro-context`), whether the
|
|
28
|
+
* answer comes from memory or from the server.
|
|
20
29
|
*
|
|
21
30
|
* const transport = cachedTransport(restTransport({ base: "/v1" }), { ttl: 30_000 });
|
|
22
|
-
*
|
|
31
|
+
* processChanges.follow((change) => transport.changed(change));
|
|
32
|
+
* transport.invalidate("/facturas"); // forget an entity by hand
|
|
23
33
|
*/
|
|
24
34
|
export type CachedTransport = Transport & {
|
|
25
35
|
/** Forget every answer whose route lies under this path: `"/facturas"` forgets the list,
|
|
26
36
|
* every page and filter of it, and every record under it — not `"/facturas-events"`. */
|
|
27
37
|
invalidate: (path?: string) => void;
|
|
38
|
+
/** A write made elsewhere: forget what it touched, as a write through here does. */
|
|
39
|
+
changed: (change: Change) => void;
|
|
28
40
|
};
|
|
29
41
|
export type CacheOptions = {
|
|
30
42
|
/** How long an answer is served from memory, in milliseconds. */
|
package/dist/core/cache.js
CHANGED
|
@@ -1,60 +1,96 @@
|
|
|
1
|
-
import {} from "@sincpro/client-framework";
|
|
2
|
-
|
|
3
|
-
return route.split("?")[0] ?? route;
|
|
4
|
-
}
|
|
5
|
-
/** `route` is `path` or lies under it: `"/facturas"` holds `"/facturas/F-1"`, never `"/facturas-events"`. */
|
|
6
|
-
function under(route, path) {
|
|
7
|
-
const own = pathOf(route);
|
|
8
|
-
return own === path || own.startsWith(path.endsWith("/") ? path : `${path}/`);
|
|
9
|
-
}
|
|
10
|
-
/** What a write may have changed: every segment of its route as a collection, and the history
|
|
11
|
-
* of each entity it writes under. A segment that is a key (`/F-1`) forgets nothing. */
|
|
12
|
-
function touchedBy(route, entities) {
|
|
13
|
-
const path = pathOf(route);
|
|
14
|
-
const segments = path.split("/").filter((one) => one !== "");
|
|
15
|
-
const history = entities
|
|
16
|
-
.filter((one) => one.history !== undefined && under(path, pathOf(one.list)))
|
|
17
|
-
// The route up to its first key: `/issue-events/{id}` forgets `/issue-events`.
|
|
18
|
-
.map((one) => pathOf(one.history ?? "").split("/{")[0] ?? "");
|
|
19
|
-
return [...segments.map((one) => `/${one}`), ...history];
|
|
20
|
-
}
|
|
1
|
+
import { bindableTransport, changeOf, } from "@sincpro/client-framework";
|
|
2
|
+
import { pathOf, touchedBy, under } from "./changes.js";
|
|
21
3
|
export function cachedTransport(transport, options = {}) {
|
|
22
4
|
const ttl = options.ttl ?? 30_000;
|
|
23
5
|
const now = options.now ?? (() => Date.now());
|
|
6
|
+
const entities = options.entities ?? [];
|
|
24
7
|
const entries = new Map();
|
|
25
|
-
|
|
8
|
+
const flights = new Map();
|
|
9
|
+
/** The answer to `key`: kept while fresh; else the request already flying for it; else a new one. */
|
|
10
|
+
function remembered(key, read, signal) {
|
|
11
|
+
if (signal?.aborted)
|
|
12
|
+
return Promise.reject(signal.reason);
|
|
26
13
|
const kept = entries.get(key);
|
|
27
14
|
if (kept !== undefined && now() - kept.at < ttl)
|
|
28
|
-
return kept.answer;
|
|
29
|
-
|
|
30
|
-
entries.set(key, { at: now(), answer });
|
|
31
|
-
return answer;
|
|
15
|
+
return Promise.resolve(kept.answer);
|
|
16
|
+
return awaited(key, flights.get(key) ?? fly(key, read), signal);
|
|
32
17
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
18
|
+
function fly(key, read) {
|
|
19
|
+
const aborter = new AbortController();
|
|
20
|
+
const flight = { answer: Promise.resolve(), aborter, waiting: 0, stale: false };
|
|
21
|
+
flight.answer = read(aborter.signal)
|
|
22
|
+
.then((answer) => {
|
|
23
|
+
if (!flight.stale)
|
|
24
|
+
entries.set(key, { at: now(), answer });
|
|
25
|
+
return answer;
|
|
26
|
+
})
|
|
27
|
+
.finally(() => {
|
|
28
|
+
if (flights.get(key) === flight)
|
|
29
|
+
flights.delete(key);
|
|
30
|
+
});
|
|
31
|
+
// Every reader may have given up: the cancelled request then has nobody to tell.
|
|
32
|
+
flight.answer.catch(() => undefined);
|
|
33
|
+
flights.set(key, flight);
|
|
34
|
+
return flight;
|
|
35
|
+
}
|
|
36
|
+
/** `flight`'s answer for one reader, who may give up alone; the last one to go cancels it. */
|
|
37
|
+
function awaited(key, flight, signal) {
|
|
38
|
+
flight.waiting += 1;
|
|
39
|
+
if (signal === undefined)
|
|
40
|
+
return flight.answer;
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
const leave = () => {
|
|
43
|
+
flight.waiting -= 1;
|
|
44
|
+
if (flight.waiting === 0) {
|
|
45
|
+
if (flights.get(key) === flight)
|
|
46
|
+
flights.delete(key);
|
|
47
|
+
flight.aborter.abort(signal.reason);
|
|
48
|
+
}
|
|
49
|
+
reject(signal.reason);
|
|
50
|
+
};
|
|
51
|
+
signal.addEventListener("abort", leave, { once: true });
|
|
52
|
+
flight.answer.then((answer) => {
|
|
53
|
+
signal.removeEventListener("abort", leave);
|
|
54
|
+
resolve(answer);
|
|
55
|
+
}, (error) => {
|
|
56
|
+
signal.removeEventListener("abort", leave);
|
|
57
|
+
reject(error);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/** Forget every answer under any of `paths`, and leave behind the ones still flying there —
|
|
62
|
+
* one walk over each, whatever their number. A path with a query forgets its collection. */
|
|
36
63
|
const forget = (paths) => {
|
|
37
64
|
const targets = paths.map(pathOf);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (
|
|
65
|
+
// `list <route> <criteria>` / `one <route>`: the route is the second word.
|
|
66
|
+
const touched = (key) => targets.some((path) => under(key.split(" ", 2)[1] ?? "", path));
|
|
67
|
+
for (const key of [...entries.keys()])
|
|
68
|
+
if (touched(key))
|
|
42
69
|
entries.delete(key);
|
|
70
|
+
for (const [key, flight] of [...flights]) {
|
|
71
|
+
if (!touched(key))
|
|
72
|
+
continue;
|
|
73
|
+
flight.stale = true;
|
|
74
|
+
flights.delete(key);
|
|
43
75
|
}
|
|
44
76
|
};
|
|
77
|
+
const changed = (change) => forget(touchedBy(change.route, entities));
|
|
45
78
|
const invalidate = (path) => {
|
|
46
|
-
if (path
|
|
47
|
-
return
|
|
48
|
-
|
|
79
|
+
if (path !== undefined)
|
|
80
|
+
return forget([path]);
|
|
81
|
+
entries.clear();
|
|
82
|
+
for (const flight of flights.values())
|
|
83
|
+
flight.stale = true;
|
|
84
|
+
flights.clear();
|
|
49
85
|
};
|
|
50
|
-
return {
|
|
86
|
+
return bindableTransport({
|
|
51
87
|
// A call's own headers (a use case's trace) never change the answer: they travel, and the
|
|
52
88
|
// answer is kept by the question alone.
|
|
53
89
|
list(route, criteria, signal, options) {
|
|
54
|
-
return remembered(`list ${route} ${JSON.stringify(criteria)}`, () => transport.list(route, criteria,
|
|
90
|
+
return remembered(`list ${route} ${JSON.stringify(criteria)}`, (flying) => transport.list(route, criteria, flying, options), signal);
|
|
55
91
|
},
|
|
56
92
|
one(route, signal, options) {
|
|
57
|
-
return remembered(`one ${route}`, () => transport.one(route,
|
|
93
|
+
return remembered(`one ${route}`, (flying) => transport.one(route, flying, options), signal);
|
|
58
94
|
},
|
|
59
95
|
// A read of the context's own (`GET /me`) is not an entity's: no write forgets it, so it is
|
|
60
96
|
// never kept — it goes to the server every time.
|
|
@@ -64,17 +100,18 @@ export function cachedTransport(transport, options = {}) {
|
|
|
64
100
|
read: (route, signal, options) => transport.read(route, signal, options),
|
|
65
101
|
}),
|
|
66
102
|
async send(method, route, body, signal, sending) {
|
|
67
|
-
// A preview writes nothing; anything else may have changed what was read.
|
|
68
|
-
const preview = route.endsWith("/preview");
|
|
69
103
|
try {
|
|
70
104
|
return await transport.send(method, route, body, signal, sending);
|
|
71
105
|
}
|
|
72
106
|
finally {
|
|
73
|
-
// Even a refused write may have changed something (a 409 says someone else did)
|
|
74
|
-
|
|
75
|
-
|
|
107
|
+
// Even a refused write may have changed something (a 409 says someone else did); a
|
|
108
|
+
// preview or a dry run changes nothing (`changeOf`).
|
|
109
|
+
const change = changeOf(method, route, body);
|
|
110
|
+
if (change !== undefined)
|
|
111
|
+
changed(change);
|
|
76
112
|
}
|
|
77
113
|
},
|
|
78
114
|
invalidate,
|
|
79
|
-
|
|
115
|
+
changed,
|
|
116
|
+
});
|
|
80
117
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Change, Entity } from "@sincpro/client-framework";
|
|
2
|
+
/**
|
|
3
|
+
* What a write touches on a screen: the rule every reader of this package shares — the cache,
|
|
4
|
+
* the lists and records on screen, the names in a cell.
|
|
5
|
+
*
|
|
6
|
+
* Context: a write says only how and where it was sent (`Change`, @sincpro/client-framework). What
|
|
7
|
+
* it may have changed is a rule of REST routes, the screens' wire: every collection its route
|
|
8
|
+
* NAMES, not only the first — «leave a tenant» (`POST /users/{email}/tenants/{tenant}/leave`)
|
|
9
|
+
* changes the tenant's members as much as the user —, and the history of each entity it writes
|
|
10
|
+
* under, wherever its events live (`/issue-events/{id}`). A segment that is a key (`/F-1`) names no
|
|
11
|
+
* collection. It is generous on purpose: reading again something that did not change costs a
|
|
12
|
+
* request; showing something that did is the bug.
|
|
13
|
+
*
|
|
14
|
+
* touches({ method: "POST", route: "/issues/7/cancel" }, "/issues?project_id=acme") -> true
|
|
15
|
+
* touches({ method: "POST", route: "/issues/7/cancel" }, "/projects/acme") -> false
|
|
16
|
+
*/
|
|
17
|
+
/** A route without its query: `/projects?tenant_id=acme` → `/projects`. */
|
|
18
|
+
export declare function pathOf(route: string): string;
|
|
19
|
+
/** `route` is `path` or lies under it: `"/facturas"` holds `"/facturas/F-1"`, never `"/facturas-events"`. */
|
|
20
|
+
export declare function under(route: string, path: string): boolean;
|
|
21
|
+
/** The collections a write to `route` may have changed: each one its route names, and the
|
|
22
|
+
* history of each entity it writes under. */
|
|
23
|
+
export declare function touchedBy(route: string, entities: readonly Entity[]): string[];
|
|
24
|
+
/** Whether `change` may have changed what was read at `route`. */
|
|
25
|
+
export declare function touches(change: Change, route: string, entities: readonly Entity[]): boolean;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a write touches on a screen: the rule every reader of this package shares — the cache,
|
|
3
|
+
* the lists and records on screen, the names in a cell.
|
|
4
|
+
*
|
|
5
|
+
* Context: a write says only how and where it was sent (`Change`, @sincpro/client-framework). What
|
|
6
|
+
* it may have changed is a rule of REST routes, the screens' wire: every collection its route
|
|
7
|
+
* NAMES, not only the first — «leave a tenant» (`POST /users/{email}/tenants/{tenant}/leave`)
|
|
8
|
+
* changes the tenant's members as much as the user —, and the history of each entity it writes
|
|
9
|
+
* under, wherever its events live (`/issue-events/{id}`). A segment that is a key (`/F-1`) names no
|
|
10
|
+
* collection. It is generous on purpose: reading again something that did not change costs a
|
|
11
|
+
* request; showing something that did is the bug.
|
|
12
|
+
*
|
|
13
|
+
* touches({ method: "POST", route: "/issues/7/cancel" }, "/issues?project_id=acme") -> true
|
|
14
|
+
* touches({ method: "POST", route: "/issues/7/cancel" }, "/projects/acme") -> false
|
|
15
|
+
*/
|
|
16
|
+
/** A route without its query: `/projects?tenant_id=acme` → `/projects`. */
|
|
17
|
+
export function pathOf(route) {
|
|
18
|
+
return route.split("?")[0] ?? route;
|
|
19
|
+
}
|
|
20
|
+
/** `route` is `path` or lies under it: `"/facturas"` holds `"/facturas/F-1"`, never `"/facturas-events"`. */
|
|
21
|
+
export function under(route, path) {
|
|
22
|
+
const own = pathOf(route);
|
|
23
|
+
return own === path || own.startsWith(path.endsWith("/") ? path : `${path}/`);
|
|
24
|
+
}
|
|
25
|
+
/** The collections a write to `route` may have changed: each one its route names, and the
|
|
26
|
+
* history of each entity it writes under. */
|
|
27
|
+
export function touchedBy(route, entities) {
|
|
28
|
+
const path = pathOf(route);
|
|
29
|
+
const segments = path.split("/").filter((one) => one !== "");
|
|
30
|
+
const history = entities
|
|
31
|
+
.filter((one) => one.history !== undefined && under(path, pathOf(one.list)))
|
|
32
|
+
// The route up to its first key: `/issue-events/{id}` forgets `/issue-events`.
|
|
33
|
+
.map((one) => pathOf(one.history ?? "").split("/{")[0] ?? "");
|
|
34
|
+
return [...segments.map((one) => `/${one}`), ...history];
|
|
35
|
+
}
|
|
36
|
+
/** Whether `change` may have changed what was read at `route`. */
|
|
37
|
+
export function touches(change, route, entities) {
|
|
38
|
+
return touchedBy(change.route, entities).some((path) => under(route, path));
|
|
39
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Change, type Entity, type MetaStore, type Transport } from "@sincpro/client-framework";
|
|
2
|
+
/**
|
|
3
|
+
* The names of records by their keys — what a cell, a fact or a chip that holds only a key shows
|
|
4
|
+
* — asked in ONE request per entity, however many of them ask at once.
|
|
5
|
+
*
|
|
6
|
+
* Context: a page of 50 rows naming 50 tenants asked 50 requests, one per cell, so screens showed
|
|
7
|
+
* the key instead of the name (ROADMAP: «asking for a name per row does not scale»). Here every
|
|
8
|
+
* key asked in the same tick waits together, and one page answers them all — the entity's own
|
|
9
|
+
* list, narrowed to those keys (`in`), in slices of `SLICE` so no page is asked without a
|
|
10
|
+
* ceiling. A name is kept for the book's life, shared by every place that shows it, and forgotten
|
|
11
|
+
* when a write touches its entity (`changed`: a rename). Unnamed is no error — the place shows the
|
|
12
|
+
* key —; a request that fails is not kept, so the next place asks again. An entity listed only
|
|
13
|
+
* under another record (`/tenants/{tenant_id}/members`) has no list to ask by key: it stays
|
|
14
|
+
* unnamed.
|
|
15
|
+
*
|
|
16
|
+
* const names = nameBook(transport, metas, entities);
|
|
17
|
+
* await names.nameOf(tenants, "acme") -> "ACME S.R.L."
|
|
18
|
+
*/
|
|
19
|
+
export type NameBook = {
|
|
20
|
+
/** The name of record `id` of `entity`; `undefined` when it has none to read or it failed. */
|
|
21
|
+
nameOf(entity: Entity, id: string): Promise<string | undefined>;
|
|
22
|
+
/** A write landed: forget the names it may have changed. */
|
|
23
|
+
changed(change: Change): void;
|
|
24
|
+
};
|
|
25
|
+
export declare function nameBook(transport: Transport, metas: MetaStore, entities?: readonly Entity[]): NameBook;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { listRoute, } from "@sincpro/client-framework";
|
|
2
|
+
import { touches } from "./changes.js";
|
|
3
|
+
import { lookupOf } from "./lookup.js";
|
|
4
|
+
/** The most keys one request asks for. */
|
|
5
|
+
const SLICE = 100;
|
|
6
|
+
/** The route an entity is listed at, or `undefined` when it is listed only under a record. */
|
|
7
|
+
function routeOf(entity) {
|
|
8
|
+
try {
|
|
9
|
+
return listRoute(entity.list);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function nameBook(transport, metas, entities = []) {
|
|
16
|
+
/** By entity key: where it is listed, and by record key the name, asked or answered. */
|
|
17
|
+
const known = new Map();
|
|
18
|
+
/** By entity key: the keys waiting for the next request, and how to answer each. */
|
|
19
|
+
const asking = new Map();
|
|
20
|
+
async function ask(entityKey) {
|
|
21
|
+
const batch = asking.get(entityKey);
|
|
22
|
+
if (batch === undefined)
|
|
23
|
+
return;
|
|
24
|
+
asking.delete(entityKey);
|
|
25
|
+
const { entity, route, waiting } = batch;
|
|
26
|
+
const meta = metas.get(entity.key);
|
|
27
|
+
const field = meta?.get_id || meta?.identity || "id";
|
|
28
|
+
const keys = [...waiting.keys()];
|
|
29
|
+
for (let at = 0; at < keys.length; at += SLICE) {
|
|
30
|
+
const slice = keys.slice(at, at + SLICE);
|
|
31
|
+
const criteria = {
|
|
32
|
+
selection: { field, operator: "in", value: slice },
|
|
33
|
+
pagination: { limit: slice.length },
|
|
34
|
+
};
|
|
35
|
+
try {
|
|
36
|
+
const page = await transport.list(route, criteria);
|
|
37
|
+
const named = new Map(lookupOf(page.meta ?? meta ?? null, page.rows, entity.summary).map((one) => [
|
|
38
|
+
one.id,
|
|
39
|
+
one.label,
|
|
40
|
+
]));
|
|
41
|
+
for (const id of slice)
|
|
42
|
+
waiting.get(id)?.(named.get(id));
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
for (const id of slice) {
|
|
46
|
+
known.get(entityKey)?.names.delete(id);
|
|
47
|
+
waiting.get(id)?.(undefined);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
nameOf(entity, id) {
|
|
54
|
+
const route = routeOf(entity);
|
|
55
|
+
if (route === undefined || id === "")
|
|
56
|
+
return Promise.resolve(undefined);
|
|
57
|
+
const shelf = known.get(entity.key) ?? { route, names: new Map() };
|
|
58
|
+
known.set(entity.key, shelf);
|
|
59
|
+
const kept = shelf.names.get(id);
|
|
60
|
+
if (kept !== undefined)
|
|
61
|
+
return kept;
|
|
62
|
+
const name = new Promise((answer) => {
|
|
63
|
+
const batch = asking.get(entity.key);
|
|
64
|
+
if (batch !== undefined)
|
|
65
|
+
return void batch.waiting.set(id, answer);
|
|
66
|
+
asking.set(entity.key, { entity, route, waiting: new Map([[id, answer]]) });
|
|
67
|
+
// Every key asked in this tick goes in the same request.
|
|
68
|
+
queueMicrotask(() => void ask(entity.key));
|
|
69
|
+
});
|
|
70
|
+
shelf.names.set(id, name);
|
|
71
|
+
return name;
|
|
72
|
+
},
|
|
73
|
+
changed(change) {
|
|
74
|
+
for (const [entityKey, shelf] of [...known])
|
|
75
|
+
if (touches(change, shelf.route, entities))
|
|
76
|
+
known.delete(entityKey);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|