@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.
Files changed (55) hide show
  1. package/README.md +173 -68
  2. package/agent-meta/manifest.json +3 -3
  3. package/agent-meta/skills/state/SKILL.md +137 -58
  4. package/build/consts.local.d.ts +10 -0
  5. package/build/consts.local.d.ts.map +1 -0
  6. package/build/consts.local.js +10 -0
  7. package/build/consts.local.js.map +1 -0
  8. package/build/errors.d.ts +12 -5
  9. package/build/errors.d.ts.map +1 -1
  10. package/build/errors.js +16 -13
  11. package/build/errors.js.map +1 -1
  12. package/build/helper.d.ts +16 -0
  13. package/build/helper.d.ts.map +1 -0
  14. package/build/helper.js +14 -0
  15. package/build/helper.js.map +1 -0
  16. package/build/index.d.ts +3 -2
  17. package/build/index.d.ts.map +1 -1
  18. package/build/index.js +2 -2
  19. package/build/index.js.map +1 -1
  20. package/build/resource.d.ts +12 -5
  21. package/build/resource.d.ts.map +1 -1
  22. package/build/resource.js +317 -207
  23. package/build/resource.js.map +1 -1
  24. package/build/types.d.ts +91 -50
  25. package/build/types.d.ts.map +1 -1
  26. package/build/types.local.d.ts +15 -0
  27. package/build/types.local.d.ts.map +1 -0
  28. package/build/types.local.js +2 -0
  29. package/build/types.local.js.map +1 -0
  30. package/build/utils/criteria.d.ts.map +1 -1
  31. package/build/utils/criteria.js.map +1 -1
  32. package/build/utils/model.d.ts +10 -2
  33. package/build/utils/model.d.ts.map +1 -1
  34. package/build/utils/model.js +26 -22
  35. package/build/utils/model.js.map +1 -1
  36. package/build/utils/types.d.ts +15 -0
  37. package/build/utils/types.d.ts.map +1 -0
  38. package/build/utils/types.js +2 -0
  39. package/build/utils/types.js.map +1 -0
  40. package/package.json +4 -4
  41. package/src/consts.local.ts +11 -0
  42. package/src/errors.ts +16 -13
  43. package/src/helper.ts +17 -0
  44. package/src/index.ts +3 -2
  45. package/src/resource.ts +359 -241
  46. package/src/types.local.ts +16 -0
  47. package/src/types.ts +98 -55
  48. package/src/utils/model.ts +35 -28
  49. package/src/utils/types.ts +15 -0
  50. package/tests/resource.spec.ts +347 -90
  51. package/build/.gitkeep +0 -0
  52. package/src/consts.ts +0 -3
  53. package/src/utils/criteria.ts +0 -252
  54. package/src/utils/index.ts +0 -3
  55. 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 { ListCriteria, Resource, ResourceRecord } from '@owlmeans/resource'
1
+ import type {
2
+ Criteria, FirstOptions, PubSubResource, Resource, ResourceRecord
3
+ } from '@owlmeans/resource'
2
4
 
3
- export interface StateResource<T extends ResourceRecord> extends Resource<T> {
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
- * @returns unsubscribe function
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
- subscribe: (params: StateSubscriptionOption<T>) => [() => void, StateModel<T>[]]
8
- listen: (listener: StateListener<T>) => () => void
9
- erase: () => Promise<void>
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
- * Every record held, as a plain array.
13
- *
14
- * `list()` answers the `Resource` contract and therefore returns `{ items, pager }`; this is
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
- all: () => Promise<T[]>
50
+ replace(records: T[]): Promise<void>
51
+
52
+ /** Drop every record. */
53
+ clear(): Promise<void>
20
54
 
21
55
  /**
22
- * The records the criteria accepts, as a plain array.
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
- * Same criteria language as the server resources (`@owlmeans/postgres-resource`), so a filter
25
- * written for an endpoint means the same thing applied locally. Records the store does not
26
- * hold cannot match — this filters what has been loaded, it never fetches.
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
- match: (criteria?: ListCriteria) => Promise<T[]>
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
- * Subscribe to a live QUERY instead of to fixed ids.
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
- * The listener is called with every record the criteria currently accepts, and again whenever
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
- * Unlike an id subscription, this one creates nothing: an empty store yields an empty list
43
- * rather than a placeholder record.
75
+ * @returns unsubscribe
44
76
  */
45
- query?: ListCriteria
46
- default?: Partial<T>
47
- listener: StateListener<T>
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
- record: T,
56
-
57
- commit: (force?: boolean) => void
58
-
59
- update: (data?: Partial<T>) => void
60
-
61
- clear: () => void
62
- }
63
-
64
- export interface StateResourceAppend {
65
- getStateResource: <T extends ResourceRecord>(alias?: string) => StateResource<T>
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
- export interface UseStoreHelper {
69
- <T extends ResourceRecord>(id?: string | UseStoreHelperOptions<T>, opts?: string | boolean | UseStoreHelperOptions<T>): StateModel<T>
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 UseStoreListHelper {
73
- <T extends ResourceRecord>(id?: string | string[] | UseStoreHelperOptions<T>, opts?: string | boolean | UseStoreHelperOptions<T>): StateModel<T>[]
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 UseStoreHelperOptions<T extends ResourceRecord> extends Omit<StateSubscriptionOption<T>, "listener"> {
77
- listen?: boolean
78
- resource?: string
119
+ export interface StateResourceAppend {
120
+ /** The state resource under `alias`, or the first one appended to the context. */
121
+ getStateResource: GetStateResource
79
122
  }
@@ -1,40 +1,47 @@
1
1
  import type { ResourceRecord } from '@owlmeans/resource'
2
- import { MisshapedRecord } from '@owlmeans/resource'
3
- import type { StateModel, StateResource } from '../types.js'
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
- record: T, resource: StateResource<T>
13
+ binding: StateModelBinding<T>
7
14
  ): StateModel<T> => {
8
- let before: T = record
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
- record: { ...record },
11
-
12
- commit: force => {
13
- if (force !== true) {
14
- if (!Object.entries(before).reduce(
15
- (changed, [key, value]) => changed || model.record[key as keyof T] !== value, false
16
- ) && !Object.entries(model.record).reduce(
17
- (changed, [key, value]) => changed || before[key as keyof T] !== value, false
18
- )) {
19
- return
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
- update: data => {
32
- Object.assign(model.record, data)
33
- model.commit()
34
+ commit: async () => {
35
+ working = await binding.write(working)
36
+ stored = true
37
+
38
+ return working
34
39
  },
35
40
 
36
- clear: () => {
37
- void resource.delete(record)
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
+ }