@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.
- package/README.md +175 -48
- package/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/state/SKILL.md +202 -19
- 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 +2 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -1
- package/build/index.js.map +1 -1
- package/build/resource.d.ts +11 -4
- package/build/resource.d.ts.map +1 -1
- package/build/resource.js +332 -151
- package/build/resource.js.map +1 -1
- package/build/types.d.ts +98 -29
- package/build/types.d.ts.map +1 -1
- package/build/utils/model.d.ts +22 -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/package.json +5 -4
- package/src/errors.ts +16 -13
- package/src/helper.ts +17 -0
- package/src/index.ts +2 -1
- package/src/resource.ts +407 -166
- package/src/types.ts +103 -30
- package/src/utils/model.ts +48 -28
- package/tests/resource.spec.ts +419 -0
- package/tsconfig.json +6 -1
- package/build/.gitkeep +0 -0
- package/build/consts.d.ts +0 -3
- package/build/consts.d.ts.map +0 -1
- package/build/consts.js +0 -3
- package/build/consts.js.map +0 -1
- package/build/utils/index.d.ts +0 -2
- package/build/utils/index.d.ts.map +0 -1
- package/build/utils/index.js +0 -2
- package/build/utils/index.js.map +0 -1
- package/src/consts.ts +0 -3
- package/src/utils/index.ts +0 -2
package/build/types.d.ts
CHANGED
|
@@ -1,39 +1,108 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
18
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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
|
|
36
|
-
|
|
37
|
-
|
|
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
|
package/build/types.d.ts.map
CHANGED
|
@@ -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,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,EAAE,YAAY,EAAE,cAAc,EAAE,QAAQ,EAAE,cAAc,EACjE,MAAM,oBAAoB,CAAA;AAE3B;;;;;GAKG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,cAAc;IACnD,wDAAwD;IACxD,EAAE,CAAC,EAAE,MAAM,CAAC,GAAG,MAAM,CAAA;IACrB;;;;OAIG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,CAAA;CAClB;AAED,0DAA0D;AAC1D,MAAM,WAAW,UAAU,CAAC,CAAC,SAAS,cAAc;IAClD,IAAI,EAAE,KAAK,GAAG,QAAQ,CAAA;IACtB,OAAO,EAAE,CAAC,EAAE,CAAA;CACb;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,SAAS,cAAc,CAAE,SAAQ,QAAQ,CAAC,CAAC,CAAC,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IACzG,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAAA;IAE/B;;;;OAIG;IACH,OAAO,CAAC,OAAO,EAAE,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAEpC,yBAAyB;IACzB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IAEtB;;;;;;;;;;OAUG;IACH,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,SAAS,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,MAAM,IAAI,CAAA;IAEnF;;;;;;;;OAQG;IACH,KAAK,CACH,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,SAAS,EAC9B,QAAQ,EAAE,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,KAAK,IAAI,EAC3C,IAAI,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,GACrB,MAAM,IAAI,CAAA;CACd;AAED;;;;;GAKG;AACH,MAAM,WAAW,UAAU,CAAC,CAAC,SAAS,cAAc;IAClD,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/B;;;;OAIG;IACH,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;IACvB,sFAAsF;IACtF,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAA;IAC5B,mCAAmC;IACnC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;IACrC,iGAAiG;IACjG,MAAM,IAAI,OAAO,CAAC,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"}
|
package/build/utils/model.d.ts
CHANGED
|
@@ -1,4 +1,24 @@
|
|
|
1
1
|
import type { ResourceRecord } from '@owlmeans/resource';
|
|
2
|
-
import type { StateModel
|
|
3
|
-
|
|
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;
|
|
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"}
|
package/build/utils/model.js
CHANGED
|
@@ -1,28 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
commit: async () => {
|
|
22
|
+
working = await binding.write(working);
|
|
23
|
+
stored = true;
|
|
24
|
+
return working;
|
|
23
25
|
},
|
|
24
|
-
clear: () => {
|
|
25
|
-
|
|
26
|
+
clear: async () => {
|
|
27
|
+
await binding.drop();
|
|
28
|
+
stored = false;
|
|
29
|
+
working = blank();
|
|
26
30
|
}
|
|
27
31
|
};
|
|
28
32
|
return model;
|
package/build/utils/model.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../src/utils/model.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../src/utils/model.ts"],"names":[],"mappings":"AAiBA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,OAA6B,EACd,EAAE;IACjB,4EAA4E;IAC5E,MAAM,KAAK,GAAG,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.
|
|
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.
|
|
25
|
-
"@owlmeans/resource": "^0.1.18-rc.
|
|
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
|
-
|
|
5
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
14
|
-
public static
|
|
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(`
|
|
18
|
-
this.type =
|
|
21
|
+
super(`state-config:${msg}`)
|
|
22
|
+
this.type = StateConfigError.typeName
|
|
19
23
|
}
|
|
20
24
|
}
|
|
21
25
|
|
|
22
|
-
ResourceError.registerErrorClass(
|
|
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>
|