@owlmeans/state 0.1.18-rc.2 → 0.1.18-rc.21

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 (45) hide show
  1. package/README.md +175 -48
  2. package/agent-meta/manifest.json +2 -2
  3. package/agent-meta/skills/state/SKILL.md +202 -19
  4. package/build/errors.d.ts +12 -5
  5. package/build/errors.d.ts.map +1 -1
  6. package/build/errors.js +16 -13
  7. package/build/errors.js.map +1 -1
  8. package/build/helper.d.ts +16 -0
  9. package/build/helper.d.ts.map +1 -0
  10. package/build/helper.js +14 -0
  11. package/build/helper.js.map +1 -0
  12. package/build/index.d.ts +2 -1
  13. package/build/index.d.ts.map +1 -1
  14. package/build/index.js +2 -1
  15. package/build/index.js.map +1 -1
  16. package/build/resource.d.ts +11 -4
  17. package/build/resource.d.ts.map +1 -1
  18. package/build/resource.js +332 -151
  19. package/build/resource.js.map +1 -1
  20. package/build/types.d.ts +98 -29
  21. package/build/types.d.ts.map +1 -1
  22. package/build/utils/model.d.ts +22 -2
  23. package/build/utils/model.d.ts.map +1 -1
  24. package/build/utils/model.js +26 -22
  25. package/build/utils/model.js.map +1 -1
  26. package/package.json +5 -4
  27. package/src/errors.ts +16 -13
  28. package/src/helper.ts +17 -0
  29. package/src/index.ts +2 -1
  30. package/src/resource.ts +407 -166
  31. package/src/types.ts +103 -30
  32. package/src/utils/model.ts +48 -28
  33. package/tests/resource.spec.ts +419 -0
  34. package/tsconfig.json +6 -1
  35. package/build/.gitkeep +0 -0
  36. package/build/consts.d.ts +0 -3
  37. package/build/consts.d.ts.map +0 -1
  38. package/build/consts.js +0 -3
  39. package/build/consts.js.map +0 -1
  40. package/build/utils/index.d.ts +0 -2
  41. package/build/utils/index.d.ts.map +0 -1
  42. package/build/utils/index.js +0 -2
  43. package/build/utils/index.js.map +0 -1
  44. package/src/consts.ts +0 -3
  45. package/src/utils/index.ts +0 -2
package/build/types.d.ts CHANGED
@@ -1,39 +1,108 @@
1
- import type { ListCriteria, Resource, ResourceRecord } from '@owlmeans/resource';
2
- export interface StateResource<T extends ResourceRecord> extends Resource<T> {
1
+ import type { Criteria, FirstOptions, PubSubResource, Resource, ResourceRecord } from '@owlmeans/resource';
2
+ /**
3
+ * How a state resource is keyed and what it shows before anything is loaded.
4
+ *
5
+ * Everything here is optional: an unconfigured resource keys records by `id`, holds as many of
6
+ * them as it is given, and shows an empty object until a record arrives.
7
+ */
8
+ export interface StateConfig<T extends ResourceRecord> {
9
+ /** The field records are keyed by. Defaults to `id`. */
10
+ id?: keyof T & string;
3
11
  /**
4
- * @returns unsubscribe function
12
+ * The resource holds exactly ONE record, which therefore needs no id — the current user, the
13
+ * active session, a wizard being filled in. It is what makes `watch(undefined, ...)` (and so
14
+ * `useStoreModel()` with no id) answerable: elsewhere there is no "the record" to address.
5
15
  */
6
- subscribe: (params: StateSubscriptionOption<T>) => [() => void, StateModel<T>[]];
7
- listen: (listener: StateListener<T>) => () => void;
8
- erase: () => Promise<void>;
16
+ single?: boolean;
17
+ /**
18
+ * What {@link StateModel.record} shows while the model is empty. A screen that binds to a
19
+ * record before it has arrived reads the default instead of guarding every field — and the
20
+ * store still holds nothing, so nothing renders it as a row.
21
+ */
22
+ default?: () => T;
9
23
  }
10
- export interface StateSubscriptionOption<T extends ResourceRecord> {
11
- id?: string | string[];
12
- _systemId?: string;
13
- query?: ListCriteria;
14
- default?: Partial<T>;
15
- listener: StateListener<T>;
24
+ /** One change to the store, as its subscribers see it. */
25
+ export interface StateEvent<T extends ResourceRecord> {
26
+ type: 'set' | 'remove';
27
+ records: T[];
16
28
  }
17
- export interface StateListener<T extends ResourceRecord> {
18
- (record: StateModel<T>[], systemId?: string): void | Promise<void>;
29
+ /**
30
+ * The framework's client store: a `Resource` like any other, registered ON the context — which is
31
+ * what separates it from a store held beside the app, since a screen, a service and a guard all
32
+ * reach the same records through the same container.
33
+ *
34
+ * Reads and writes are the resource vocabulary; `watch` and `query` are the live half, and they
35
+ * are synchronous on purpose — a React subscriber has to have its value before it renders.
36
+ */
37
+ export interface StateResource<T extends ResourceRecord> extends Resource<T>, PubSubResource<StateEvent<T>> {
38
+ readonly config: StateConfig<T>;
39
+ /**
40
+ * Make the store agree with an authoritative list: every record given is written, and every
41
+ * record the list does not name is dropped. That is the shape of "the server just told us what
42
+ * exists" — saving each record one by one leaves the ones deleted elsewhere behind.
43
+ */
44
+ replace(records: T[]): Promise<void>;
45
+ /** Drop every record. */
46
+ clear(): Promise<void>;
47
+ /**
48
+ * Follow one record. The listener is called with the current model straight away — before
49
+ * `watch` returns — and again on every change to that record, including its removal.
50
+ *
51
+ * `undefined` addresses the one record of a `single` resource; on any other it throws, since
52
+ * there is nothing for it to mean.
53
+ *
54
+ * An absent id on a listed resource watches nothing and reports an empty model — a screen
55
+ * binds before the record supplying the id exists, and that is a loading state, not an error.
56
+ * @returns unsubscribe
57
+ */
58
+ watch(id: string | undefined, listener: (model: StateModel<T>) => void): () => void;
59
+ /**
60
+ * Follow a live QUERY. The listener is called with the matching models straight away and again
61
+ * whenever a write changes the answer, so a list screen never recomputes ids and never
62
+ * re-subscribes to keep up. `undefined` matches every record.
63
+ *
64
+ * A query subscription creates nothing: an empty store yields an empty list.
65
+ *
66
+ * @returns unsubscribe
67
+ */
68
+ query(where: Criteria<T> | undefined, listener: (models: StateModel<T>[]) => void, opts?: FirstOptions<T>): () => void;
19
69
  }
70
+ /**
71
+ * One record, as something bound to it can hold: what it currently says, and how to change it.
72
+ *
73
+ * `record` is a snapshot — assigning into it changes nothing anyone can see. `update` is how a
74
+ * change reaches the store and every other subscriber.
75
+ */
20
76
  export interface StateModel<T extends ResourceRecord> {
21
- record: T;
22
- commit: (force?: boolean) => void;
23
- update: (data?: Partial<T>) => void;
24
- clear: () => void;
25
- }
26
- export interface StateResourceAppend {
27
- getStateResource: <T extends ResourceRecord>(alias?: string) => StateResource<T>;
28
- }
29
- export interface UseStoreHelper {
30
- <T extends ResourceRecord>(id?: string | UseStoreHelperOptions<T>, opts?: string | boolean | UseStoreHelperOptions<T>): StateModel<T>;
77
+ readonly id: string | undefined;
78
+ /**
79
+ * Nothing is loaded yet: the store holds no record under this model's key. This — not a
80
+ * sentinel id, and not a placeholder record — is what "not there" looks like, so a subscription
81
+ * to an unknown id leaves the store exactly as empty as it found it.
82
+ */
83
+ readonly empty: boolean;
84
+ /** The record, or the configured `default` while {@link StateModel.empty} is true. */
85
+ readonly record: Readonly<T>;
86
+ /** Merge and write in one step. */
87
+ update(patch: Partial<T>): Promise<T>;
88
+ /** Write what {@link StateModel.record} currently holds — including a default not yet stored. */
89
+ commit(): Promise<T>;
90
+ clear(): Promise<void>;
31
91
  }
32
- export interface UseStoreListHelper {
33
- <T extends ResourceRecord>(id?: string | string[] | UseStoreHelperOptions<T>, opts?: string | boolean | UseStoreHelperOptions<T>): StateModel<T>[];
92
+ /**
93
+ * A state alias that remembers the record type it addresses, so `getStateResource(TASKS)` is
94
+ * typed without repeating `<Task>` at every call site. Built by {@link stateAlias}; it is a plain
95
+ * string at runtime, so it works anywhere an alias is expected.
96
+ */
97
+ export type StateAlias<T extends ResourceRecord> = string & {
98
+ readonly _state?: T;
99
+ };
100
+ export interface GetStateResource {
101
+ <T extends ResourceRecord>(alias: StateAlias<T>): StateResource<T>;
102
+ <T extends ResourceRecord = ResourceRecord>(alias?: string): StateResource<T>;
34
103
  }
35
- export interface UseStoreHelperOptions<T extends ResourceRecord> extends Omit<StateSubscriptionOption<T>, "listener"> {
36
- listen?: boolean;
37
- resource?: string;
104
+ export interface StateResourceAppend {
105
+ /** The state resource under `alias`, or the first one appended to the context. */
106
+ getStateResource: GetStateResource;
38
107
  }
39
108
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAEhF,MAAM,WAAW,aA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CAAC,CAAC,CAAA;IACpB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACvB;AAED;;;;GAIG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,cAAc,IAAI,MAAM,GAAG;IAAE,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;CAAE,CAAA;AAEnF,MAAM,WAAW,gBAAgB;IAC/B,CAAC,CAAC,SAAS,cAAc,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAA;IAClE,CAAC,CAAC,SAAS,cAAc,GAAG,cAAc,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC,CAAA;CAC9E;AAED,MAAM,WAAW,mBAAmB;IAClC,kFAAkF;IAClF,gBAAgB,EAAE,gBAAgB,CAAA;CACnC"}
@@ -1,4 +1,24 @@
1
1
  import type { ResourceRecord } from '@owlmeans/resource';
2
- import type { StateModel, StateResource } from '../types.js';
3
- export declare const createStateModel: <T extends ResourceRecord>(record: T, resource: StateResource<T>) => StateModel<T>;
2
+ import type { StateModel } from '../types.js';
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
+ }
16
+ /**
17
+ * Wrap one record — or its absence — as something a screen can bind to.
18
+ *
19
+ * The working copy is replaced rather than mutated on every write, so the record a caller is
20
+ * holding never changes underneath it and two models of the same record stay comparable by
21
+ * reference.
22
+ */
23
+ export declare const createStateModel: <T extends ResourceRecord>(binding: StateModelBinding<T>) => StateModel<T>;
4
24
  //# sourceMappingURL=model.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/utils/model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAExD,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAE5D,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,cAAc,UAC/C,CAAC,YAAY,aAAa,CAAC,CAAC,CAAC,KACpC,UAAU,CAAC,CAAC,CAmCd,CAAA"}
1
+ {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/utils/model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAE7C;;;;GAIG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC,SAAS,cAAc;IACzD,EAAE,EAAE,MAAM,GAAG,SAAS,CAAA;IACtB,oFAAoF;IACpF,MAAM,EAAE,CAAC,GAAG,SAAS,CAAA;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC,CAAA;IACjB,KAAK,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;IAChC,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC1B;AAED;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,cAAc,WAC9C,iBAAiB,CAAC,CAAC,CAAC,KAC5B,UAAU,CAAC,CAAC,CAmCd,CAAA"}
@@ -1,28 +1,32 @@
1
- import { MisshapedRecord } from '@owlmeans/resource';
2
- export const createStateModel = (record, resource) => {
3
- let before = record;
1
+ /**
2
+ * Wrap one record — or its absence — as something a screen can bind to.
3
+ *
4
+ * The working copy is replaced rather than mutated on every write, so the record a caller is
5
+ * holding never changes underneath it and two models of the same record stay comparable by
6
+ * reference.
7
+ */
8
+ export const createStateModel = (binding) => {
9
+ /** What an empty model shows: the configured default, or nothing at all. */
10
+ const blank = () => binding.default?.() ?? {};
11
+ let working = binding.record ?? blank();
12
+ let stored = binding.record != null;
4
13
  const model = {
5
- record: { ...record },
6
- commit: force => {
7
- if (force !== true) {
8
- if (!Object.entries(before).reduce((changed, [key, value]) => changed || model.record[key] !== value, false) && !Object.entries(model.record).reduce((changed, [key, value]) => changed || before[key] !== value, false)) {
9
- return;
10
- }
11
- }
12
- if (record.id == null) {
13
- throw new MisshapedRecord('id');
14
- }
15
- before = model.record;
16
- void resource.load(record.id).then(exists => {
17
- exists != null && resource.update(model.record);
18
- });
14
+ get id() { return binding.id; },
15
+ get empty() { return !stored; },
16
+ get record() { return working; },
17
+ update: async (patch) => {
18
+ working = { ...working, ...patch };
19
+ return model.commit();
19
20
  },
20
- update: data => {
21
- Object.assign(model.record, data);
22
- model.commit();
21
+ commit: async () => {
22
+ working = await binding.write(working);
23
+ stored = true;
24
+ return working;
23
25
  },
24
- clear: () => {
25
- void resource.delete(record);
26
+ clear: async () => {
27
+ await binding.drop();
28
+ stored = false;
29
+ working = blank();
26
30
  }
27
31
  };
28
32
  return model;
@@ -1 +1 @@
1
- {"version":3,"file":"model.js","sourceRoot":"","sources":["../../src/utils/model.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAGpD,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,MAAS,EAAE,QAA0B,EACtB,EAAE;IACjB,IAAI,MAAM,GAAM,MAAM,CAAA;IACtB,MAAM,KAAK,GAAkB;QAC3B,MAAM,EAAE,EAAE,GAAG,MAAM,EAAE;QAErB,MAAM,EAAE,KAAK,CAAC,EAAE;YACd,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACnB,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,CAChC,CAAC,OAAO,EAAE,CAAC,GAAG,EAA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AAG,GAAM,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,IAAI,EAAO,CAAA;IAErD,IAAI,OAAO,GAAM,OAAO,CAAC,MAAM,IAAI,KAAK,EAAE,CAAA;IAC1C,IAAI,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,IAAI,CAAA;IAEnC,MAAM,KAAK,GAAkB;QAC3B,IAAI,EAAE,KAAK,OAAO,OAAO,CAAC,EAAE,CAAA,CAAC,CAAC;QAE9B,IAAI,KAAK,KAAK,OAAO,CAAC,MAAM,CAAA,CAAC,CAAC;QAE9B,IAAI,MAAM,KAAK,OAAO,OAAO,CAAA,CAAC,CAAC;QAE/B,MAAM,EAAE,KAAK,EAAC,KAAK,EAAC,EAAE;YACpB,OAAO,GAAG,EAAE,GAAG,OAAO,EAAE,GAAG,KAAK,EAAO,CAAA;YAEvC,OAAO,KAAK,CAAC,MAAM,EAAE,CAAA;QACvB,CAAC;QAED,MAAM,EAAE,KAAK,IAAI,EAAE;YACjB,OAAO,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;YACtC,MAAM,GAAG,IAAI,CAAA;YAEb,OAAO,OAAO,CAAA;QAChB,CAAC;QAED,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,MAAM,OAAO,CAAC,IAAI,EAAE,CAAA;YACpB,MAAM,GAAG,KAAK,CAAA;YACd,OAAO,GAAG,KAAK,EAAE,CAAA;QACnB,CAAC;KACF,CAAA;IAED,OAAO,KAAK,CAAA;AACd,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@owlmeans/state",
3
- "version": "0.1.18-rc.2",
3
+ "version": "0.1.18-rc.21",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "tsc -b",
8
8
  "dev": "sleep 342 && nodemon -e ts,tsx,json --watch src --exec \"tsc -p ./tsconfig.json\"",
9
- "watch": "tsc -b -w --preserveWatchOutput --pretty"
9
+ "watch": "tsc -b -w --preserveWatchOutput --pretty",
10
+ "test": "bun test ./tests"
10
11
  },
11
12
  "main": "build/index.js",
12
13
  "module": "build/index.js",
@@ -21,8 +22,8 @@
21
22
  }
22
23
  },
23
24
  "dependencies": {
24
- "@owlmeans/context": "^0.1.18-rc.2",
25
- "@owlmeans/resource": "^0.1.18-rc.2"
25
+ "@owlmeans/context": "^0.1.18-rc.19",
26
+ "@owlmeans/resource": "^0.1.18-rc.20"
26
27
  },
27
28
  "devDependencies": {
28
29
  "@owlmeans/dep-config": "workspace:*",
package/src/errors.ts CHANGED
@@ -1,23 +1,26 @@
1
1
 
2
2
  import { ResourceError } from '@owlmeans/resource'
3
3
 
4
- export class StateToolingError extends ResourceError {
5
- public static override typeName = `${ResourceError.typeName}Tooling`
4
+ /**
5
+ * The resource was asked for something its {@link StateConfig} does not allow. Both cases are
6
+ * wiring mistakes rather than missing data, so they throw instead of answering with nothing.
7
+ */
8
+ export class StateConfigError extends ResourceError {
9
+ public static override typeName = `${ResourceError.typeName}StateConfig`
6
10
 
7
- constructor(msg: string) {
8
- super(`tooling:${msg}`)
9
- this.type = StateToolingError.typeName
10
- }
11
- }
11
+ /**
12
+ * A record was addressed without an id on a resource that holds many of them. Only a `single`
13
+ * resource has a record that needs no naming.
14
+ */
15
+ public static readonly NonSingle: string = 'non-single'
12
16
 
13
- export class StateListenerError extends StateToolingError {
14
- public static override typeName = `${StateToolingError.typeName}Listener`
17
+ /** A write carried no value for the resource's id field, and nothing here mints one. */
18
+ public static readonly NoId: string = 'no-id'
15
19
 
16
20
  constructor(msg: string) {
17
- super(`listener:${msg}`)
18
- this.type = StateListenerError.typeName
21
+ super(`state-config:${msg}`)
22
+ this.type = StateConfigError.typeName
19
23
  }
20
24
  }
21
25
 
22
- ResourceError.registerErrorClass(StateToolingError)
23
- ResourceError.registerErrorClass(StateListenerError)
26
+ ResourceError.registerErrorClass(StateConfigError)
package/src/helper.ts ADDED
@@ -0,0 +1,17 @@
1
+ import type { ResourceRecord } from '@owlmeans/resource'
2
+ import type { StateAlias } from './types.js'
3
+
4
+ /**
5
+ * Name a state resource once, with the record type it holds attached:
6
+ *
7
+ * ```typescript
8
+ * export const TASKS = stateAlias<Task>('tasks')
9
+ * const tasks = context.getStateResource(TASKS) // StateResource<Task>
10
+ * ```
11
+ *
12
+ * The handle is the string itself at runtime — the type rides along only so that every reader of
13
+ * the alias gets the record type without repeating it, and so that a mismatch is a compile error
14
+ * instead of a record shaped like nothing anyone expected.
15
+ */
16
+ export const stateAlias = <T extends ResourceRecord>(alias: string): StateAlias<T> =>
17
+ alias as StateAlias<T>
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
 
2
2
  export type * from './types.js'
3
3
 
4
- export * from './consts.js'
5
4
  export * from './errors.js'
5
+ export * from './helper.js'
6
6
  export * from './resource.js'
7
+ export * from './utils/model.js'