@sincpro/web-framework 2.4.0 → 2.6.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 +117 -86
- 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/edit.d.ts +19 -0
- package/dist/core/edit.js +46 -0
- package/dist/core/index.d.ts +3 -3
- package/dist/core/index.js +3 -3
- package/dist/core/list.d.ts +20 -0
- package/dist/core/list.js +32 -1
- package/dist/core/names.d.ts +25 -0
- package/dist/core/names.js +79 -0
- package/dist/core/present.d.ts +22 -0
- package/dist/core/present.js +51 -19
- package/dist/react/context.d.ts +15 -2
- package/dist/react/context.js +11 -4
- package/dist/react/index.d.ts +4 -0
- package/dist/react/index.js +4 -0
- package/dist/react/useCount.d.ts +18 -0
- package/dist/react/useCount.js +23 -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.d.ts +4 -4
- package/dist/react/useRecord.js +20 -13
- package/dist/react/useService.d.ts +5 -1
- package/dist/react/useServiceReading.d.ts +35 -0
- package/dist/react/useServiceReading.js +75 -0
- package/dist/react/useTextOf.d.ts +16 -0
- package/dist/react/useTextOf.js +21 -0
- 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/list/EntityList.d.ts +4 -1
- package/dist/web/list/EntityList.js +6 -3
- package/dist/web/list/EntitySearchBar.d.ts +4 -1
- package/dist/web/list/EntitySearchBar.js +2 -2
- package/dist/web/list/groups.d.ts +13 -3
- package/dist/web/list/groups.js +21 -8
- package/dist/web/list/openIn.d.ts +4 -2
- package/dist/web/list/openIn.js +9 -2
- 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/record/control.d.ts +9 -3
- package/dist/web/record/control.js +13 -4
- package/dist/web/record/edit.js +1 -1
- package/dist/web/record/lines.js +1 -1
- package/dist/web/record/record.js +36 -3
- package/dist/web/workspace/SincproApp.d.ts +8 -2
- package/dist/web/workspace/SincproApp.js +13 -7
- package/dist/web/workspace/composer.d.ts +6 -2
- package/dist/web/workspace/composer.js +31 -12
- package/dist/web/workspace/open.d.ts +1 -1
- package/dist/web/workspace/open.js +7 -1
- package/dist/web/workspace/record.d.ts +6 -2
- package/dist/web/workspace/record.js +6 -5
- package/dist/web/workspace/workspace.d.ts +7 -1
- package/dist/web/workspace/workspace.js +13 -4
- package/package.json +6 -6
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
|
|
|
@@ -418,33 +442,37 @@ const tickets = defineMeta({
|
|
|
418
442
|
Each answers one question, and each comes in the three levels — use the component, drop to the
|
|
419
443
|
hook and draw your own, or go straight to the loose piece.
|
|
420
444
|
|
|
421
|
-
| Question | Declarative | Hook | Loose piece
|
|
422
|
-
| -------------------------------- | ----------------------------------------------------------- | ------------------------------------- |
|
|
423
|
-
| Where is the data? | `entity()` (client-framework) | — | `restTransport`, `memoryTransport`
|
|
424
|
-
| The application's logic | its `Feature`s and `ApplicationService`s (client-framework) | `useService` | `project.Issues.close(…)`
|
|
425
|
-
|
|
|
426
|
-
| What
|
|
427
|
-
| What
|
|
428
|
-
| What is
|
|
429
|
-
|
|
|
430
|
-
|
|
|
431
|
-
|
|
|
432
|
-
|
|
|
433
|
-
|
|
|
434
|
-
|
|
|
435
|
-
|
|
|
436
|
-
|
|
|
437
|
-
|
|
|
438
|
-
|
|
|
439
|
-
|
|
|
440
|
-
|
|
|
441
|
-
|
|
|
442
|
-
|
|
|
443
|
-
|
|
|
444
|
-
|
|
|
445
|
-
|
|
|
446
|
-
|
|
|
447
|
-
|
|
|
445
|
+
| Question | Declarative | Hook | Loose piece |
|
|
446
|
+
| -------------------------------- | ----------------------------------------------------------- | ------------------------------------- | ----------------------------------------------------------- |
|
|
447
|
+
| Where is the data? | `entity()` (client-framework) | — | `restTransport`, `memoryTransport` |
|
|
448
|
+
| The application's logic | its `Feature`s and `ApplicationService`s (client-framework) | `useService` | `project.Issues.close(…)` |
|
|
449
|
+
| Read through a use case | its `Feature`s (client-framework) | `useServiceReading` | `project.Accounts.balance(…)` |
|
|
450
|
+
| What every use case runs with | `processContext`, `project.context` (client-framework) | `useContextValues` | `ContextStore` |
|
|
451
|
+
| What happened, what broke | `./telemetry`, `./errors` (optional) | — | `configureProcess({ tracer, reporter })` |
|
|
452
|
+
| What is each field? | `Meta` from the server, or `defineMeta` | — | `textOf`, `viewOf`, `metaFromSchema` |
|
|
453
|
+
| A value outside a model | — | `useTextOf` | `moneyTextOf`, `dayTextOf`, `instantTextOf`, `numberTextOf` |
|
|
454
|
+
| What is a record, at any size? | `cardOf`, `relationOf` | `useSummaryOf` | `summaryOf` |
|
|
455
|
+
| How does a record look? | `presented(entity, { option, row, card })` | — | `presentationOf` |
|
|
456
|
+
| N records: search, filter, page | `EntityList`, `EntitySearchBar` | `useListing`, `useGroups` | `transport.list(route, criteria)` |
|
|
457
|
+
| A named slice (to collect, …) | `defineList` + `listView` | `useListing(list)` | `scopeOf` |
|
|
458
|
+
| A record's whole screen | `EntityRecord`, `entityView` without `record` | `useRecordScreen` | `formOf` |
|
|
459
|
+
| Previous / next from a record | `EntityRecord` (from a list) | — | `placeOf`, `stepIn` (`Trail`) |
|
|
460
|
+
| How much each group adds up | `EntityList sums` | `useGroups` (`sums`) | `sumsOf`, `detailOf` |
|
|
461
|
+
| One record: read, edit, save | `RecordField`, `factsOf`, `RecordLines` | `useRecord` | `transport.one`, `transport.send` |
|
|
462
|
+
| Its lines: add, edit, remove | `RecordField name="lines"` (`LineItems`) | `useRecord` (`edit.cell`, `errorAt`) | `lineKey`, `editableLines`, `pathOf` |
|
|
463
|
+
| Its relations, inside it | `RelatedList` | `useRelated` | `relatedOf` |
|
|
464
|
+
| What other records hold of it | web-ui's `SmartButton` opening `scopedRef(entity, cut)` | `useCount` | `scopedRef` |
|
|
465
|
+
| Choose one (literal search) | `EntityPicker`, `chooserIn` | `useLookup` | `lookupOf` + criteria's `matching` |
|
|
466
|
+
| Choose several (many2many) | `EntityTags` | `useLookup` (`values`) | `lookupOf` |
|
|
467
|
+
| A relation, from the model alone | `RecordField name="customer_id"` | `useEntityFor` | `relationFieldOf`, `entityFor` |
|
|
468
|
+
| Open it where it belongs | `RelatedRecord`, `entityView`, `EntityList openTargets` | — | `openIn`, `targetsFor`, `OpenInMenu` |
|
|
469
|
+
| Look at the set several ways | `EntityList views` (table, rows, board, sheet) | — | web-ui's `List.*` |
|
|
470
|
+
| Cross it by two fields | `EntityList pivot` | `usePivot` | `pivotOf` |
|
|
471
|
+
| Order by several fields | the bar's criteria builder | `listing.orderBy([...])` | criteria's `order` |
|
|
472
|
+
| Act on it (with a dry run) | `ServiceButton` (a use case), `CommandButton`, row commands | `useService`, `useCommand` | `transport.send` |
|
|
473
|
+
| Read again when it was written | every reading of this package, by itself | `useWrites` | `processChanges` (client-framework) |
|
|
474
|
+
| What happened to it | `Record.Activity` fed by `useHistory` | `useHistory` | `historyOf` |
|
|
475
|
+
| The application context | `SincproApp` | `useRecordInWorkspace`, `useFollowed` | `FrameworkProvider`, `webUiSkin` |
|
|
448
476
|
|
|
449
477
|
The rules behind them, in short:
|
|
450
478
|
|
|
@@ -461,15 +489,16 @@ The rules behind them, in short:
|
|
|
461
489
|
|
|
462
490
|
There is no store of ours to learn: each kind of state already has its owner.
|
|
463
491
|
|
|
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
|
|
492
|
+
| State | Lives in | Read with |
|
|
493
|
+
| -------------------------------------------- | ---------------------------------------------------------- | ------------------------------------ |
|
|
494
|
+
| Who answers, language, known models | `FrameworkProvider` (this package) | the hooks, through context |
|
|
495
|
+
| What the backend sees on every request | `processContext` (client-framework), as `sincpro-context` | the backend's `use_context()` |
|
|
496
|
+
| What an opening carries (company, a default) | composer: the reference's `params` | `api.self.params`, `api.setParams` |
|
|
497
|
+
| The record a modal covers | composer: `api.over` | `api.over` |
|
|
498
|
+
| What a chooser was asked | composer: `pick(…, { question })` | `api.choosing` |
|
|
499
|
+
| What the user is on, what is selected | composer: `api.context` | `useWorkspaceContext(api)` |
|
|
500
|
+
| A screen's list, record, groups | the hooks | `useListing`, `useRecord`, … |
|
|
501
|
+
| What anyone wrote | `processChanges` (client-framework): every write announced | `useWrites`, the hooks by themselves |
|
|
473
502
|
|
|
474
503
|
Not yet: a new record does not take the `params` it was opened with as its initial values
|
|
475
504
|
(`ROADMAP.md`, «Estado y contexto»).
|
|
@@ -520,56 +549,58 @@ moves to the generated one — is [`docs/ADOPTION.md`](docs/ADOPTION.md).
|
|
|
520
549
|
|
|
521
550
|
## Where it stands
|
|
522
551
|
|
|
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
|
-
| `
|
|
552
|
+
| Piece | What it solves | Status |
|
|
553
|
+
| --------------------------------------- | ----------------------------------------------------------------------------------------------- | ------ |
|
|
554
|
+
| `restTransport`, `memoryTransport` | The backend, or the same engine in memory (client-framework) | ✅ |
|
|
555
|
+
| `defineMeta`, `metaFromSchema` | A model by hand, or completed from OpenAPI (client-framework) | ✅ |
|
|
556
|
+
| `useService` | A screen runs one of the application's use cases: run, busy, answer, error | ✅ |
|
|
557
|
+
| `./telemetry`, `./errors` | Optional: the application's traces to its OTLP endpoint, its errors to GlitchTip | ✅ |
|
|
558
|
+
| `selection` | Filters named as criteria 2.0 and the backend read them, the search bar's included | ✅ |
|
|
559
|
+
| `useListing`, `EntityList` | N records: criteria, search bar, builder, paging | ✅ |
|
|
560
|
+
| `useRecord`, `factsOf`, `RecordLines` | One record: facts, relations with their real total | ✅ |
|
|
561
|
+
| Preloaded definitions | `FrameworkProvider metas`: the models of the discovery document, or of an exported metamodel | ✅ |
|
|
562
|
+
| `useLookup`, `EntityPicker` | Literal search: a many2one select, ⌘K | ✅ |
|
|
563
|
+
| `useRecord` + `RecordField`, `dirtyOf` | Editing: hints evaluated live, preview, save with version (409), server drafts | ✅ |
|
|
564
|
+
| `useCommand`, `CommandButton` | An entity's actions, with a dry run before what destroys | ✅ |
|
|
565
|
+
| `ServiceButton` | A use case as an action: the same gesture, its dry run the use case's own | ✅ |
|
|
566
|
+
| `useWrites`, the change feed | Every reading on screen reads again when anyone writes what it shows — a use case included | ✅ |
|
|
567
|
+
| `entity<T, C>` (`CommandBodies`) | Typed Commands: a name the entity does not declare, or a wrong body, does not compile | ✅ |
|
|
568
|
+
| `entity<T, C, A>` (`CommandAnswers`) | What each Command answers, typed: `run(…)` returns it with no type argument written | ✅ |
|
|
569
|
+
| `readRoute`, `Transport.read` | A GET of the context's own (`/me`), neither a page nor a record | ✅ |
|
|
570
|
+
| A key alone (`EntityID`), `useNameOf` | A key that names whose record it is, drawn as a many2one; names asked in one request per entity | ✅ |
|
|
571
|
+
| `versionsOf`, `onVersionDrift` | A snapshot behind the server is told once per model — never an error | ✅ |
|
|
572
|
+
| `sincpro-client generate` | A context's DTOs, client and entities out of its exported client contract | ✅ |
|
|
573
|
+
| `metasOf`, `bodyFor` (`target`) | Definitions preloaded from that snapshot; a Command's record written where its body names it | ✅ |
|
|
574
|
+
| `useHistory` | Business events in `Record.Activity` (comments: backend first) | ✅ |
|
|
575
|
+
| `entityView` | An entity as a composer view: list, record, ⌘K search | ✅ |
|
|
576
|
+
| `<SincproApp>` | The application context in one place, the workspace dressed as web-ui (`webUiSkin`) | ✅ |
|
|
577
|
+
| `useRecordInWorkspace` | A record tells the workspace its real name and its unsaved work (composer 1.0) | ✅ |
|
|
578
|
+
| Paging by pages, ceiling on «more» | One page on screen, walked by the server's cursor: a million rows never freeze it | ✅ |
|
|
579
|
+
| `cachedTransport` | Repeated reads from memory and shared in flight, forgotten when anyone writes the entity | ✅ |
|
|
580
|
+
| `EntitySearchBar` | The search bar + criteria builder as a loose piece, for any `List` | ✅ |
|
|
581
|
+
| `EntityList` views | The same set as table, rows or board | ✅ |
|
|
582
|
+
| `useRelated`, `RelatedList` | A record's relation as a live list: searched and paged by the server, inside a form | ✅ |
|
|
583
|
+
| `EntityPicker` chooser | «see all…» opens the whole set, searched on the server: a many2one over millions | ✅ |
|
|
584
|
+
| `useRecord(entity, "")`, `create` | A new record: the model's defaults, saved to `create` (or `edit`), then it IS that record | ✅ |
|
|
585
|
+
| `retryKeys`, `Idempotency-Key` | A Command or a create retried after a lost answer runs once, not twice | ✅ |
|
|
586
|
+
| `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 | ✅ |
|
|
587
|
+
| `EntityPicker criteria` | A many2one narrowed by another field: the invoices OF the chosen customer | ✅ |
|
|
588
|
+
| `chooserIn`, `entity.chooserRef()` | «see all…» as the composer's chooser: a row answers (`api.pick`) | ✅ |
|
|
589
|
+
| `EntityPicker` / `EntityTags` in a view | Open and choose in the workspace by themselves; the chooser carries the picker's cut | ✅ |
|
|
590
|
+
| `newEntityId`, `edit` on a new record | A new record born under a UUID v7 minted here: its draft and its save go through `Edit` | ✅ |
|
|
591
|
+
| `useGroups`, «Group by» | Groups counted by the server over the whole set; a group's rows read when it is opened | ✅ |
|
|
592
|
+
| `defineList`, `listView` | A named list (a fixed scope, its columns and filters) as data, and as a workspace view | ✅ |
|
|
593
|
+
| `EntityPicker` `browse`, `columns` | The chooser always a click away, as a table searched literally by the server | ✅ |
|
|
594
|
+
| `useFollowed` | A detail panel follows the record the person is on, without knowing the list | ✅ |
|
|
595
|
+
| `commands`, `useRowCommands` | An entity's Commands on each row, idempotent, offered only where `when` holds | ✅ |
|
|
596
|
+
| `summaryOf`, `cardOf`, `relationOf` | Any record at any size: name, description, state, type, picture, who made and changed it | ✅ |
|
|
597
|
+
| `openIn`, `RelatedRecord` | A related record opens in a modal; «Abrir en…» for the rest; never a modal over a modal | ✅ |
|
|
598
|
+
| Literal search everywhere | Picker: the first ten, the server's search as you type; tables: «Buscar «X»» on Enter | ✅ |
|
|
599
|
+
| `EntityTags` | A many2many as chips over thousands: the server's search, faces, the chosen never again | ✅ |
|
|
600
|
+
| `RecordField` + `entities` | A many2one / one2many drawn from the model alone, opened where the view says | ✅ |
|
|
601
|
+
| `EntityRecord`, `formOf` | Any entity's record screen without writing it | ✅ |
|
|
602
|
+
| `Trail`, `Record.Pager` | Previous / next from a record, across the server's pages | ✅ |
|
|
603
|
+
| `EntityList sums` | Each group says how much it adds up, summed by the server over all of it | ✅ |
|
|
573
604
|
|
|
574
605
|
What it does **not** do: mobile screens (`@sincpro/mobile` draws them; what it shares with the
|
|
575
606
|
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
|
+
}
|
package/dist/core/edit.d.ts
CHANGED
|
@@ -104,3 +104,22 @@ export declare function defaultsOf(meta: Meta | null | undefined): AnyRecord;
|
|
|
104
104
|
* the stored record. Both travel.
|
|
105
105
|
*/
|
|
106
106
|
export declare function creationOf(meta: Meta | null | undefined, values: AnyRecord): AnyRecord;
|
|
107
|
+
/**
|
|
108
|
+
* What an edit of a STORED record sends (sincpro_framework's `Edit`): the fields the person
|
|
109
|
+
* changed, and inside them nothing the model marks `readonly` — a line keeps only its identity,
|
|
110
|
+
* the way the form names the line it edits.
|
|
111
|
+
*
|
|
112
|
+
* editedOf(invoiceMeta, record, ["discount", "lines"])
|
|
113
|
+
* -> { discount: "5", lines: [{ id: "l1", qty: 2 }, { qty: 1 }] }
|
|
114
|
+
*
|
|
115
|
+
* Context: the form used to send the record whole, as it holds it — `id`, `version`,
|
|
116
|
+
* `created_at` and each line's own among them. sincpro_framework up to 3.36 refused a field the
|
|
117
|
+
* framework writes at every level (`apply_values`); 3.37 leaves that echo out, but still
|
|
118
|
+
* refuses a relation read as a page (`{items, count, cursor}`) written back, since it would
|
|
119
|
+
* remove every record the page did not bring. So a stored record could be created and never
|
|
120
|
+
* edited. The backend keeps what it stored and the caller's draft, and puts this edit on top of
|
|
121
|
+
* both: what changed is all it needs, on either version. The model marks the fields the
|
|
122
|
+
* framework writes `readonly` (and the computed ones), so nothing here names them. A new record
|
|
123
|
+
* goes through `creationOf`.
|
|
124
|
+
*/
|
|
125
|
+
export declare function editedOf(meta: Meta | null | undefined, values: AnyRecord, changed: readonly string[]): AnyRecord;
|