@owlmeans/state 0.1.18-rc.8 → 0.1.18
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 +173 -68
- package/agent-meta/manifest.json +3 -3
- package/agent-meta/skills/state/SKILL.md +137 -58
- package/build/consts.local.d.ts +10 -0
- package/build/consts.local.d.ts.map +1 -0
- package/build/consts.local.js +10 -0
- package/build/consts.local.js.map +1 -0
- package/build/errors.d.ts +12 -5
- package/build/errors.d.ts.map +1 -1
- package/build/errors.js +16 -13
- package/build/errors.js.map +1 -1
- package/build/helper.d.ts +16 -0
- package/build/helper.d.ts.map +1 -0
- package/build/helper.js +14 -0
- package/build/helper.js.map +1 -0
- package/build/index.d.ts +3 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -2
- package/build/index.js.map +1 -1
- package/build/resource.d.ts +12 -5
- package/build/resource.d.ts.map +1 -1
- package/build/resource.js +317 -207
- package/build/resource.js.map +1 -1
- package/build/types.d.ts +91 -50
- package/build/types.d.ts.map +1 -1
- package/build/types.local.d.ts +15 -0
- package/build/types.local.d.ts.map +1 -0
- package/build/types.local.js +2 -0
- package/build/types.local.js.map +1 -0
- package/build/utils/criteria.d.ts.map +1 -1
- package/build/utils/criteria.js.map +1 -1
- package/build/utils/model.d.ts +10 -2
- package/build/utils/model.d.ts.map +1 -1
- package/build/utils/model.js +26 -22
- package/build/utils/model.js.map +1 -1
- package/build/utils/types.d.ts +15 -0
- package/build/utils/types.d.ts.map +1 -0
- package/build/utils/types.js +2 -0
- package/build/utils/types.js.map +1 -0
- package/package.json +4 -4
- package/src/consts.local.ts +11 -0
- package/src/errors.ts +16 -13
- package/src/helper.ts +17 -0
- package/src/index.ts +3 -2
- package/src/resource.ts +359 -241
- package/src/types.local.ts +16 -0
- package/src/types.ts +98 -55
- package/src/utils/model.ts +35 -28
- package/src/utils/types.ts +15 -0
- package/tests/resource.spec.ts +347 -90
- package/build/.gitkeep +0 -0
- package/src/consts.ts +0 -3
- package/src/utils/criteria.ts +0 -252
- package/src/utils/index.ts +0 -3
- package/tests/criteria.spec.ts +0 -121
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Criteria, FirstOptions, ResourceRecord } from '@owlmeans/resource'
|
|
2
|
+
import type { StateEvent, StateModel } from './types.js'
|
|
3
|
+
|
|
4
|
+
export interface QueryWatch<T extends ResourceRecord> {
|
|
5
|
+
where?: Criteria<T>
|
|
6
|
+
opts?: FirstOptions<T>
|
|
7
|
+
listener: (models: StateModel<T>[]) => void
|
|
8
|
+
last: StateModel<T>[]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface Subscription<T extends ResourceRecord> {
|
|
12
|
+
handler: (value: StateEvent<T>) => void | Promise<void>
|
|
13
|
+
channel: string
|
|
14
|
+
once: boolean
|
|
15
|
+
timer?: ReturnType<typeof setTimeout>
|
|
16
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,79 +1,122 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
Criteria, FirstOptions, PubSubResource, Resource, ResourceRecord
|
|
3
|
+
} from '@owlmeans/resource'
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
/**
|
|
6
|
+
* How a state resource is keyed and what it shows before anything is loaded.
|
|
7
|
+
*
|
|
8
|
+
* Everything here is optional: an unconfigured resource keys records by `id`, holds as many of
|
|
9
|
+
* them as it is given, and shows an empty object until a record arrives.
|
|
10
|
+
*/
|
|
11
|
+
export interface StateConfig<T extends ResourceRecord> {
|
|
12
|
+
/** The field records are keyed by. Defaults to `id`. */
|
|
13
|
+
id?: keyof T & string
|
|
4
14
|
/**
|
|
5
|
-
*
|
|
15
|
+
* The resource holds exactly ONE record, which therefore needs no id — the current user, the
|
|
16
|
+
* active session, a wizard being filled in. It is what makes `watch(undefined, ...)` (and so
|
|
17
|
+
* `useStoreModel()` with no id) answerable: elsewhere there is no "the record" to address.
|
|
6
18
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
19
|
+
single?: boolean
|
|
20
|
+
/**
|
|
21
|
+
* What {@link StateModel.record} shows while the model is empty. A screen that binds to a
|
|
22
|
+
* record before it has arrived reads the default instead of guarding every field — and the
|
|
23
|
+
* store still holds nothing, so nothing renders it as a row.
|
|
24
|
+
*/
|
|
25
|
+
default?: () => T
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** One change to the store, as its subscribers see it. */
|
|
29
|
+
export interface StateEvent<T extends ResourceRecord> {
|
|
30
|
+
type: 'set' | 'remove'
|
|
31
|
+
records: T[]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The framework's client store: a `Resource` like any other, registered ON the context — which is
|
|
36
|
+
* what separates it from a store held beside the app, since a screen, a service and a guard all
|
|
37
|
+
* reach the same records through the same container.
|
|
38
|
+
*
|
|
39
|
+
* Reads and writes are the resource vocabulary; `watch` and `query` are the live half, and they
|
|
40
|
+
* are synchronous on purpose — a React subscriber has to have its value before it renders.
|
|
41
|
+
*/
|
|
42
|
+
export interface StateResource<T extends ResourceRecord> extends Resource<T>, PubSubResource<StateEvent<T>> {
|
|
43
|
+
readonly config: StateConfig<T>
|
|
10
44
|
|
|
11
45
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* the same data without the envelope, for the overwhelmingly common case of "give me what is
|
|
16
|
-
* in the store". Destructuring the wrong one of the two is a silent empty render, so both
|
|
17
|
-
* spellings exist rather than one being the trap the other rescues.
|
|
46
|
+
* Make the store agree with an authoritative list: every record given is written, and every
|
|
47
|
+
* record the list does not name is dropped. That is the shape of "the server just told us what
|
|
48
|
+
* exists" — saving each record one by one leaves the ones deleted elsewhere behind.
|
|
18
49
|
*/
|
|
19
|
-
|
|
50
|
+
replace(records: T[]): Promise<void>
|
|
51
|
+
|
|
52
|
+
/** Drop every record. */
|
|
53
|
+
clear(): Promise<void>
|
|
20
54
|
|
|
21
55
|
/**
|
|
22
|
-
* The
|
|
56
|
+
* Follow one record. The listener is called with the current model straight away — before
|
|
57
|
+
* `watch` returns — and again on every change to that record, including its removal.
|
|
58
|
+
*
|
|
59
|
+
* `undefined` addresses the one record of a `single` resource; on any other it throws, since
|
|
60
|
+
* there is nothing for it to mean.
|
|
23
61
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
62
|
+
* An absent id on a listed resource watches nothing and reports an empty model — a screen
|
|
63
|
+
* binds before the record supplying the id exists, and that is a loading state, not an error.
|
|
64
|
+
* @returns unsubscribe
|
|
27
65
|
*/
|
|
28
|
-
|
|
29
|
-
}
|
|
66
|
+
watch(id: string | undefined, listener: (model: StateModel<T>) => void): () => void
|
|
30
67
|
|
|
31
|
-
export interface StateSubscriptionOption<T extends ResourceRecord> {
|
|
32
|
-
id?: string | string[]
|
|
33
|
-
_systemId?: string
|
|
34
68
|
/**
|
|
35
|
-
*
|
|
69
|
+
* Follow a live QUERY. The listener is called with the matching models straight away and again
|
|
70
|
+
* whenever a write changes the answer, so a list screen never recomputes ids and never
|
|
71
|
+
* re-subscribes to keep up. `undefined` matches every record.
|
|
36
72
|
*
|
|
37
|
-
*
|
|
38
|
-
* a create, update or delete changes that answer — so a list screen never recomputes ids and
|
|
39
|
-
* never re-subscribes to keep up. Ignored when `id` is given: a subscription is either to
|
|
40
|
-
* named records or to a query, never to both.
|
|
73
|
+
* A query subscription creates nothing: an empty store yields an empty list.
|
|
41
74
|
*
|
|
42
|
-
*
|
|
43
|
-
* rather than a placeholder record.
|
|
75
|
+
* @returns unsubscribe
|
|
44
76
|
*/
|
|
45
|
-
query
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
export interface StateListener<T extends ResourceRecord> {
|
|
51
|
-
(record: StateModel<T>[], systemId?: string): void | Promise<void>
|
|
77
|
+
query(
|
|
78
|
+
where: Criteria<T> | undefined,
|
|
79
|
+
listener: (models: StateModel<T>[]) => void,
|
|
80
|
+
opts?: FirstOptions<T>
|
|
81
|
+
): () => void
|
|
52
82
|
}
|
|
53
83
|
|
|
84
|
+
/**
|
|
85
|
+
* One record, as something bound to it can hold: what it currently says, and how to change it.
|
|
86
|
+
*
|
|
87
|
+
* `record` is a snapshot — assigning into it changes nothing anyone can see. `update` is how a
|
|
88
|
+
* change reaches the store and every other subscriber.
|
|
89
|
+
*/
|
|
54
90
|
export interface StateModel<T extends ResourceRecord> {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
91
|
+
readonly id: string | undefined
|
|
92
|
+
/**
|
|
93
|
+
* Nothing is loaded yet: the store holds no record under this model's key. This — not a
|
|
94
|
+
* sentinel id, and not a placeholder record — is what "not there" looks like, so a subscription
|
|
95
|
+
* to an unknown id leaves the store exactly as empty as it found it.
|
|
96
|
+
*/
|
|
97
|
+
readonly empty: boolean
|
|
98
|
+
/** The record, or the configured `default` while {@link StateModel.empty} is true. */
|
|
99
|
+
readonly record: Readonly<T>
|
|
100
|
+
/** Merge and write in one step. */
|
|
101
|
+
update(patch: Partial<T>): Promise<T>
|
|
102
|
+
/** Write what {@link StateModel.record} currently holds — including a default not yet stored. */
|
|
103
|
+
commit(): Promise<T>
|
|
104
|
+
clear(): Promise<void>
|
|
66
105
|
}
|
|
67
106
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
107
|
+
/**
|
|
108
|
+
* A state alias that remembers the record type it addresses, so `getStateResource(TASKS)` is
|
|
109
|
+
* typed without repeating `<Task>` at every call site. Built by {@link stateAlias}; it is a plain
|
|
110
|
+
* string at runtime, so it works anywhere an alias is expected.
|
|
111
|
+
*/
|
|
112
|
+
export type StateAlias<T extends ResourceRecord> = string & { readonly _state?: T }
|
|
71
113
|
|
|
72
|
-
export interface
|
|
73
|
-
<T extends ResourceRecord>(
|
|
114
|
+
export interface GetStateResource {
|
|
115
|
+
<T extends ResourceRecord>(alias: StateAlias<T>): StateResource<T>
|
|
116
|
+
<T extends ResourceRecord = ResourceRecord>(alias?: string): StateResource<T>
|
|
74
117
|
}
|
|
75
118
|
|
|
76
|
-
export interface
|
|
77
|
-
|
|
78
|
-
|
|
119
|
+
export interface StateResourceAppend {
|
|
120
|
+
/** The state resource under `alias`, or the first one appended to the context. */
|
|
121
|
+
getStateResource: GetStateResource
|
|
79
122
|
}
|
package/src/utils/model.ts
CHANGED
|
@@ -1,40 +1,47 @@
|
|
|
1
1
|
import type { ResourceRecord } from '@owlmeans/resource'
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
2
|
+
import type { StateModel } from '../types.js'
|
|
3
|
+
import type { StateModelBinding } from './types.js'
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Wrap one record — or its absence — as something a screen can bind to.
|
|
7
|
+
*
|
|
8
|
+
* The working copy is replaced rather than mutated on every write, so the record a caller is
|
|
9
|
+
* holding never changes underneath it and two models of the same record stay comparable by
|
|
10
|
+
* reference.
|
|
11
|
+
*/
|
|
5
12
|
export const createStateModel = <T extends ResourceRecord>(
|
|
6
|
-
|
|
13
|
+
binding: StateModelBinding<T>
|
|
7
14
|
): StateModel<T> => {
|
|
8
|
-
|
|
15
|
+
/** What an empty model shows: the configured default, or nothing at all. */
|
|
16
|
+
const blank = (): T => binding.default?.() ?? {} as T
|
|
17
|
+
|
|
18
|
+
let working: T = binding.record ?? blank()
|
|
19
|
+
let stored = binding.record != null
|
|
20
|
+
|
|
9
21
|
const model: StateModel<T> = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
if (record.id == null) {
|
|
23
|
-
throw new MisshapedRecord('id')
|
|
24
|
-
}
|
|
25
|
-
before = model.record
|
|
26
|
-
void resource.load(record.id).then(exists => {
|
|
27
|
-
exists != null && resource.update(model.record)
|
|
28
|
-
})
|
|
22
|
+
get id() { return binding.id },
|
|
23
|
+
|
|
24
|
+
get empty() { return !stored },
|
|
25
|
+
|
|
26
|
+
get record() { return working },
|
|
27
|
+
|
|
28
|
+
update: async patch => {
|
|
29
|
+
working = { ...working, ...patch } as T
|
|
30
|
+
|
|
31
|
+
return model.commit()
|
|
29
32
|
},
|
|
30
33
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
commit: async () => {
|
|
35
|
+
working = await binding.write(working)
|
|
36
|
+
stored = true
|
|
37
|
+
|
|
38
|
+
return working
|
|
34
39
|
},
|
|
35
40
|
|
|
36
|
-
clear: () => {
|
|
37
|
-
|
|
41
|
+
clear: async () => {
|
|
42
|
+
await binding.drop()
|
|
43
|
+
stored = false
|
|
44
|
+
working = blank()
|
|
38
45
|
}
|
|
39
46
|
}
|
|
40
47
|
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ResourceRecord } from '@owlmeans/resource'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a model needs from the resource that made it: which record it stands for, and the two
|
|
5
|
+
* writes it can perform. The key stays on the resource side, so a model bound to the one record
|
|
6
|
+
* of a `single` resource works the same as one bound to an id.
|
|
7
|
+
*/
|
|
8
|
+
export interface StateModelBinding<T extends ResourceRecord> {
|
|
9
|
+
id: string | undefined
|
|
10
|
+
/** The stored record, or `undefined` when the store holds none — an EMPTY model. */
|
|
11
|
+
record: T | undefined
|
|
12
|
+
default?: () => T
|
|
13
|
+
write: (record: T) => Promise<T>
|
|
14
|
+
drop: () => Promise<void>
|
|
15
|
+
}
|