@asyra/system-context 0.2.5 → 0.5.1

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 (154) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +49 -1
  3. package/dist/apis/index.d.ts +11 -10
  4. package/dist/apis/index.js +3 -11
  5. package/dist/apis/managed-property-state.d.ts +39 -0
  6. package/dist/apis/managed-property-state.js +53 -0
  7. package/dist/apis/root.d.ts +1 -1
  8. package/dist/apis/root.js +18 -11
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.js +1 -1
  11. package/dist/states/index.d.ts +2 -6
  12. package/dist/states/index.js +2 -6
  13. package/dist/states/managed-property-state.d.ts +46 -0
  14. package/dist/states/managed-property-state.js +192 -0
  15. package/dist/system-context.d.ts +11 -11
  16. package/dist/system-context.js +13 -20
  17. package/dist/types/deps.d.ts +2 -10
  18. package/dist/types/index.d.ts +5 -9
  19. package/dist/types/index.js +1 -1
  20. package/dist/types/managed-property-state.d.ts +15 -0
  21. package/package.json +30 -6
  22. package/.turbo/turbo-test$colon$ci.log +0 -77
  23. package/.turbo/turbo-test$colon$local.log +0 -23
  24. package/.turbo/turbo-test.log +0 -359
  25. package/CHANGELOG.md +0 -46
  26. package/coverage/base.css +0 -224
  27. package/coverage/block-navigation.js +0 -87
  28. package/coverage/clover.xml +0 -345
  29. package/coverage/coverage-final.json +0 -5831
  30. package/coverage/favicon.png +0 -0
  31. package/coverage/index.html +0 -176
  32. package/coverage/prettify.css +0 -1
  33. package/coverage/prettify.js +0 -2
  34. package/coverage/sort-arrow-sprite.png +0 -0
  35. package/coverage/sorter.js +0 -196
  36. package/coverage/src/apis/index.html +0 -206
  37. package/coverage/src/apis/index.ts.html +0 -133
  38. package/coverage/src/apis/key-state.ts.html +0 -124
  39. package/coverage/src/apis/mouse-state.ts.html +0 -124
  40. package/coverage/src/apis/primary-tool-state.ts.html +0 -124
  41. package/coverage/src/apis/root.ts.html +0 -139
  42. package/coverage/src/apis/system-state.ts.html +0 -112
  43. package/coverage/src/apis/target-state.ts.html +0 -121
  44. package/coverage/src/index.html +0 -131
  45. package/coverage/src/index.ts.html +0 -97
  46. package/coverage/src/states/index.html +0 -191
  47. package/coverage/src/states/index.ts.html +0 -139
  48. package/coverage/src/states/key-state.ts.html +0 -148
  49. package/coverage/src/states/mouse-state.ts.html +0 -184
  50. package/coverage/src/states/primary-tool-state.ts.html +0 -130
  51. package/coverage/src/states/system-state.ts.html +0 -130
  52. package/coverage/src/states/target-state.ts.html +0 -154
  53. package/coverage/src/subscribe/index.html +0 -191
  54. package/coverage/src/subscribe/index.ts.html +0 -187
  55. package/coverage/src/subscribe/key-state.ts.html +0 -109
  56. package/coverage/src/subscribe/mouse-state.ts.html +0 -109
  57. package/coverage/src/subscribe/primary-tool-state.ts.html +0 -136
  58. package/coverage/src/subscribe/system-state.ts.html +0 -94
  59. package/coverage/src/subscribe/target-state.ts.html +0 -109
  60. package/coverage/src/system-context.ts.html +0 -247
  61. package/coverage/src/types/deps.ts.html +0 -145
  62. package/coverage/src/types/index.html +0 -221
  63. package/coverage/src/types/index.ts.html +0 -157
  64. package/coverage/src/types/key-state.ts.html +0 -118
  65. package/coverage/src/types/mouse-state.ts.html +0 -118
  66. package/coverage/src/types/primary-tool-state.ts.html +0 -121
  67. package/coverage/src/types/root.ts.html +0 -106
  68. package/coverage/src/types/system-state.ts.html +0 -106
  69. package/coverage/src/types/target-state.ts.html +0 -118
  70. package/dist/__tests__/system-context.test.js +0 -63
  71. package/dist/apis/key-state.d.ts +0 -2
  72. package/dist/apis/key-state.js +0 -8
  73. package/dist/apis/mouse-state copy.d.ts +0 -2
  74. package/dist/apis/mouse-state copy.js +0 -8
  75. package/dist/apis/mouse-state.d.ts +0 -2
  76. package/dist/apis/mouse-state.js +0 -8
  77. package/dist/apis/primary-tool-state.d.ts +0 -2
  78. package/dist/apis/primary-tool-state.js +0 -8
  79. package/dist/apis/system-state.d.ts +0 -2
  80. package/dist/apis/system-state.js +0 -5
  81. package/dist/apis/target-state.d.ts +0 -2
  82. package/dist/apis/target-state.js +0 -8
  83. package/dist/states/key-state.d.ts +0 -9
  84. package/dist/states/key-state.js +0 -16
  85. package/dist/states/mouse-state copy.d.ts +0 -9
  86. package/dist/states/mouse-state copy.js +0 -28
  87. package/dist/states/mouse-state.d.ts +0 -9
  88. package/dist/states/mouse-state.js +0 -28
  89. package/dist/states/primary-tool-state.d.ts +0 -8
  90. package/dist/states/primary-tool-state.js +0 -11
  91. package/dist/states/system-state.d.ts +0 -8
  92. package/dist/states/system-state.js +0 -11
  93. package/dist/states/target-state.d.ts +0 -9
  94. package/dist/states/target-state.js +0 -17
  95. package/dist/subscribe/index.d.ts +0 -2
  96. package/dist/subscribe/index.js +0 -24
  97. package/dist/subscribe/key-state.d.ts +0 -2
  98. package/dist/subscribe/key-state.js +0 -6
  99. package/dist/subscribe/mouse-state copy.d.ts +0 -2
  100. package/dist/subscribe/mouse-state copy.js +0 -6
  101. package/dist/subscribe/mouse-state.d.ts +0 -2
  102. package/dist/subscribe/mouse-state.js +0 -6
  103. package/dist/subscribe/primary-tool-state.d.ts +0 -2
  104. package/dist/subscribe/primary-tool-state.js +0 -10
  105. package/dist/subscribe/root.d.ts +0 -2
  106. package/dist/subscribe/root.js +0 -7
  107. package/dist/subscribe/system-state.d.ts +0 -1
  108. package/dist/subscribe/system-state.js +0 -3
  109. package/dist/subscribe/target-state.d.ts +0 -2
  110. package/dist/subscribe/target-state.js +0 -6
  111. package/dist/types/key-state.d.ts +0 -8
  112. package/dist/types/key-state.js +0 -1
  113. package/dist/types/mouse-state copy.d.ts +0 -8
  114. package/dist/types/mouse-state copy.js +0 -1
  115. package/dist/types/mouse-state.d.ts +0 -8
  116. package/dist/types/mouse-state.js +0 -1
  117. package/dist/types/primary-tool-state.d.ts +0 -8
  118. package/dist/types/primary-tool-state.js +0 -1
  119. package/dist/types/system-state.d.ts +0 -5
  120. package/dist/types/system-state.js +0 -1
  121. package/dist/types/target-state.d.ts +0 -8
  122. package/dist/types/target-state.js +0 -1
  123. package/src/__tests__/system-context.test.ts +0 -79
  124. package/src/apis/index.ts +0 -16
  125. package/src/apis/key-state.ts +0 -13
  126. package/src/apis/mouse-state.ts +0 -13
  127. package/src/apis/primary-tool-state.ts +0 -13
  128. package/src/apis/root.ts +0 -18
  129. package/src/apis/system-state.ts +0 -9
  130. package/src/apis/target-state.ts +0 -12
  131. package/src/index.ts +0 -4
  132. package/src/states/index.ts +0 -18
  133. package/src/states/key-state.ts +0 -21
  134. package/src/states/mouse-state.ts +0 -33
  135. package/src/states/primary-tool-state.ts +0 -15
  136. package/src/states/system-state.ts +0 -15
  137. package/src/states/target-state.ts +0 -23
  138. package/src/subscribe/index.ts +0 -34
  139. package/src/subscribe/key-state.ts +0 -8
  140. package/src/subscribe/mouse-state.ts +0 -8
  141. package/src/subscribe/primary-tool-state.ts +0 -17
  142. package/src/subscribe/system-state.ts +0 -3
  143. package/src/subscribe/target-state.ts +0 -8
  144. package/src/system-context.ts +0 -54
  145. package/src/types/deps.ts +0 -20
  146. package/src/types/index.ts +0 -24
  147. package/src/types/key-state.ts +0 -11
  148. package/src/types/mouse-state.ts +0 -11
  149. package/src/types/primary-tool-state.ts +0 -12
  150. package/src/types/root.ts +0 -7
  151. package/src/types/system-state.ts +0 -7
  152. package/src/types/target-state.ts +0 -11
  153. package/tsconfig.json +0 -17
  154. /package/dist/{__tests__/system-context.test.d.ts → types/managed-property-state.js} +0 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Asa Tsai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1 +1,49 @@
1
- # event-emitter
1
+ # `@asyra/system-context`
2
+
3
+ Registered managed global/runtime properties for modes, viewport values, and App/system flags.
4
+
5
+ ## Requirements
6
+
7
+ - Node.js 24.x
8
+ - Yarn 4.3.1 for this repository's maintained workflows
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ npm install @asyra/system-context
14
+ ```
15
+
16
+ ```ts
17
+ import systemContext from '@asyra/system-context'
18
+ ```
19
+
20
+ Use only the package root and the explicitly documented public subpaths.
21
+
22
+ ## Owns
23
+
24
+ - managed property registration, validation, observable values, snapshots, persistence eligibility, and one-shot load artifacts
25
+
26
+ ## Does not own
27
+
28
+ - entity graphs, component properties, UI binding, render output, default event subscriptions, or App command policy
29
+
30
+ ## Start here
31
+
32
+ Use it for small global values that are not entity/property graph data. Define value, validation, and runtime-only policy before use.
33
+
34
+ ## Lifecycle and composition
35
+
36
+ Runtime writes validate before update. Load produces an instance-bound artifact that applies once without validator replay. Missing, foreign, stale, reused, or invalid artifacts fail before mutation; runtime-only values remain outside persistence.
37
+
38
+ ## Learn more
39
+
40
+ - [Complete package guide](https://github.com/karote00/asyra/blob/main/docs/public/reference/packages/system-context.md)
41
+ - [Framework release support](https://github.com/karote00/asyra/blob/main/docs/ai/framework/RELEASE_SUPPORT.md)
42
+
43
+ ## Support and policy
44
+
45
+ This repository does not accept external issues or contributions. You may use,
46
+ inspect, and fork the package under the [MIT License](https://github.com/karote00/asyra/blob/main/LICENSE).
47
+ Follow the [security policy](https://github.com/karote00/asyra/blob/main/SECURITY.md) for
48
+ security-sensitive reports and the [root policy](https://github.com/karote00/asyra) for the
49
+ current support boundary.
@@ -1,13 +1,14 @@
1
- import { HandlerDeps } from '../types';
1
+ import { HandlerDeps } from '../types/index.js';
2
2
  export declare const createAllAPIs: (deps: HandlerDeps) => {
3
- updateHoveredElementId: (elementId: string | null) => void;
4
- getTargetState: () => import("@asyra/utils").TargetSnapshot;
5
- updateKeyState: (keySnapshot: import("@asyra/utils").KeySnapshot) => void;
6
- getKeyState: () => import("@asyra/utils").KeySnapshot;
7
- getSystemMode: () => import("@asyra/utils").SystemMode;
8
- updateMouseState: (mouseSnapshot: import("@asyra/utils").MouseSnapshot) => void;
9
- getMouseState: () => import("@asyra/utils").MouseSnapshot;
10
- getCurrentPrimaryTool: () => import("@asyra/utils").PrimaryToolType;
11
- switchPrimaryTool: (tool: import("@asyra/utils").PrimaryToolType) => void;
3
+ registerProperty: <T>(key: string, defaultValue: T, options?: import("../states/managed-property-state.js").ManagedPropertyRegistrationOptions<T>) => import("rxjs").BehaviorSubject<T>;
4
+ getManagedProperty: <T>(key: string) => T | undefined;
5
+ setManagedProperty: <T>(key: string, value: T) => void;
6
+ getManagedPropertyObservable: <T>(key: string) => import("rxjs").BehaviorSubject<T> | undefined;
7
+ hasManagedProperty: (key: string) => boolean;
8
+ unregisterProperty: (key: string) => boolean;
9
+ validateManagedProperties: (data: unknown) => import("../states/managed-property-state.js").ManagedPropertyLoadValidationResult;
10
+ applyValidatedManagedProperties: (result: ReturnType<import("../states/managed-property-state.js").ManagedPropertyState["validateLoadData"]>) => void;
11
+ loadManagedProperties: (data: unknown) => import("../states/managed-property-state.js").ManagedPropertyLoadDiagnostic[];
12
+ saveManagedProperties: () => Record<string, unknown>;
12
13
  getSystemContextSnapshot: () => import("@asyra/utils").SystemContextSnapshot;
13
14
  };
@@ -1,14 +1,6 @@
1
- import { createRootAPIs } from './root';
2
- import { createMouseStateAPIs } from './mouse-state';
3
- import { createPrimaryToolStateAPIs } from './primary-tool-state';
4
- import { createSystemAPIs } from './system-state';
5
- import { createKeyStateAPIs } from './key-state';
6
- import { createTargetStateAPIs } from './target-state';
1
+ import { createRootAPIs } from './root.js';
2
+ import { createManagedPropertyStateAPIs } from './managed-property-state.js';
7
3
  export const createAllAPIs = (deps) => ({
8
4
  ...createRootAPIs(deps),
9
- ...createPrimaryToolStateAPIs(deps.primaryToolState),
10
- ...createMouseStateAPIs(deps.mouseState),
11
- ...createSystemAPIs(deps.systemState),
12
- ...createKeyStateAPIs(deps.keyState),
13
- ...createTargetStateAPIs(deps.targetState)
5
+ ...createManagedPropertyStateAPIs(deps.managedPropertyState)
14
6
  });
@@ -0,0 +1,39 @@
1
+ import { ManagedPropertyState } from '../states/managed-property-state.js';
2
+ import type { ManagedPropertyRegistrationOptions } from '../states/managed-property-state.js';
3
+ export declare const createManagedPropertyStateAPIs: (managedPropertyState: ManagedPropertyState) => {
4
+ /**
5
+ * Register a new managed property in system context
6
+ * Creates an internal observable for the property
7
+ *
8
+ * @param key - Property name
9
+ * @param defaultValue - Default value
10
+ * @returns The BehaviorSubject for the property
11
+ */
12
+ registerProperty: <T>(key: string, defaultValue: T, options?: ManagedPropertyRegistrationOptions<T>) => import("rxjs").BehaviorSubject<T>;
13
+ /**
14
+ * Get a managed property value
15
+ */
16
+ getManagedProperty: <T>(key: string) => T | undefined;
17
+ /**
18
+ * Set a managed property value
19
+ */
20
+ setManagedProperty: <T>(key: string, value: T) => void;
21
+ /**
22
+ * Get the observable for a managed property
23
+ */
24
+ getManagedPropertyObservable: <T>(key: string) => import("rxjs").BehaviorSubject<T> | undefined;
25
+ hasManagedProperty: (key: string) => boolean;
26
+ unregisterProperty: (key: string) => boolean;
27
+ validateManagedProperties: (data: unknown) => import("../states/managed-property-state.js").ManagedPropertyLoadValidationResult;
28
+ /** Apply only an artifact returned by validateManagedProperties; validators do not rerun. */
29
+ applyValidatedManagedProperties: (result: ReturnType<ManagedPropertyState["validateLoadData"]>) => void;
30
+ /**
31
+ * Load managed properties with registration/type guards.
32
+ * Unknown keys and invalid values are ignored with diagnostics.
33
+ */
34
+ loadManagedProperties: (data: unknown) => import("../states/managed-property-state.js").ManagedPropertyLoadDiagnostic[];
35
+ /**
36
+ * Save currently registered managed properties as plain object data.
37
+ */
38
+ saveManagedProperties: () => Record<string, unknown>;
39
+ };
@@ -0,0 +1,53 @@
1
+ export const createManagedPropertyStateAPIs = (managedPropertyState) => ({
2
+ /**
3
+ * Register a new managed property in system context
4
+ * Creates an internal observable for the property
5
+ *
6
+ * @param key - Property name
7
+ * @param defaultValue - Default value
8
+ * @returns The BehaviorSubject for the property
9
+ */
10
+ registerProperty: (key, defaultValue, options) => {
11
+ return managedPropertyState.register(key, defaultValue, options);
12
+ },
13
+ /**
14
+ * Get a managed property value
15
+ */
16
+ getManagedProperty: (key) => {
17
+ return managedPropertyState.get(key);
18
+ },
19
+ /**
20
+ * Set a managed property value
21
+ */
22
+ setManagedProperty: (key, value) => {
23
+ managedPropertyState.set(key, value);
24
+ },
25
+ /**
26
+ * Get the observable for a managed property
27
+ */
28
+ getManagedPropertyObservable: (key) => {
29
+ return managedPropertyState.getObservable(key);
30
+ },
31
+ hasManagedProperty: (key) => managedPropertyState.has(key),
32
+ unregisterProperty: (key) => managedPropertyState.unregister(key),
33
+ validateManagedProperties: (data) => {
34
+ return managedPropertyState.validateLoadData(data);
35
+ },
36
+ /** Apply only an artifact returned by validateManagedProperties; validators do not rerun. */
37
+ applyValidatedManagedProperties: (result) => {
38
+ managedPropertyState.applyValidatedData(result);
39
+ },
40
+ /**
41
+ * Load managed properties with registration/type guards.
42
+ * Unknown keys and invalid values are ignored with diagnostics.
43
+ */
44
+ loadManagedProperties: (data) => {
45
+ return managedPropertyState.load(data);
46
+ },
47
+ /**
48
+ * Save currently registered managed properties as plain object data.
49
+ */
50
+ saveManagedProperties: () => {
51
+ return managedPropertyState.save();
52
+ }
53
+ });
@@ -1,2 +1,2 @@
1
- import { HandlerDeps, RootAPIs } from '../types';
1
+ import { HandlerDeps, RootAPIs } from '../types/index.js';
2
2
  export declare const createRootAPIs: (deps: HandlerDeps) => RootAPIs;
package/dist/apis/root.js CHANGED
@@ -1,15 +1,22 @@
1
+ const cloneManagedValue = (value) => {
2
+ if (Array.isArray(value)) {
3
+ return value.map((item) => cloneManagedValue(item));
4
+ }
5
+ if (value && typeof value === 'object') {
6
+ const objectValue = value;
7
+ const cloned = Object.fromEntries(Object.entries(objectValue).map(([key, item]) => [
8
+ key,
9
+ cloneManagedValue(item)
10
+ ]));
11
+ return cloned;
12
+ }
13
+ return value;
14
+ };
1
15
  export const createRootAPIs = (deps) => ({
2
16
  getSystemContextSnapshot() {
3
- return {
4
- primaryTool: deps.primaryToolState.current,
5
- mouse: deps.mouseState.current,
6
- system: {
7
- mode: deps.systemState.mode,
8
- featureFlags: {},
9
- permissions: {}
10
- },
11
- key: deps.keyState.current,
12
- target: deps.targetState.current
13
- };
17
+ return deps.managedPropertyState.getAllKeys().reduce((snapshot, key) => {
18
+ snapshot[key] = cloneManagedValue(deps.managedPropertyState.get(key));
19
+ return snapshot;
20
+ }, {});
14
21
  }
15
22
  });
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import systemContext, { SystemContext } from './system-context';
1
+ import systemContext, { SystemContext } from './system-context.js';
2
2
  export { SystemContext };
3
3
  export default systemContext;
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import systemContext, { SystemContext } from './system-context';
1
+ import systemContext, { SystemContext } from './system-context.js';
2
2
  export { SystemContext };
3
3
  export default systemContext;
@@ -1,6 +1,2 @@
1
- import systemState, { SystemState } from './system-state';
2
- import primaryToolState, { PrimaryToolState } from './primary-tool-state';
3
- import mouseState, { MouseState } from './mouse-state';
4
- import keyState, { KeyState } from './key-state';
5
- import targetState, { TargetState } from './target-state';
6
- export { systemState, SystemState, primaryToolState, PrimaryToolState, mouseState, MouseState, keyState, KeyState, targetState, TargetState };
1
+ import managedPropertyState, { ManagedPropertyState } from './managed-property-state.js';
2
+ export { managedPropertyState, ManagedPropertyState };
@@ -1,6 +1,2 @@
1
- import systemState, { SystemState } from './system-state';
2
- import primaryToolState, { PrimaryToolState } from './primary-tool-state';
3
- import mouseState, { MouseState } from './mouse-state';
4
- import keyState, { KeyState } from './key-state';
5
- import targetState, { TargetState } from './target-state';
6
- export { systemState, SystemState, primaryToolState, PrimaryToolState, mouseState, MouseState, keyState, KeyState, targetState, TargetState };
1
+ import managedPropertyState, { ManagedPropertyState } from './managed-property-state.js';
2
+ export { managedPropertyState, ManagedPropertyState };
@@ -0,0 +1,46 @@
1
+ import { BehaviorSubject } from 'rxjs';
2
+ import { type LoadDiagnostic } from '@asyra/utils';
3
+ export type ManagedPropertyValidator<T> = (value: unknown) => value is T;
4
+ export interface ManagedPropertyRegistrationOptions<T> {
5
+ silent?: boolean;
6
+ validate?: ManagedPropertyValidator<T>;
7
+ /**
8
+ * Runtime-only property flag.
9
+ * - true: property is not persisted by save()/load()
10
+ * - false: property is included in save()/load()
11
+ */
12
+ runtime?: boolean;
13
+ }
14
+ export interface ManagedProperty<T> {
15
+ key: string;
16
+ state: BehaviorSubject<T>;
17
+ validate: (value: unknown) => boolean;
18
+ runtime: boolean;
19
+ }
20
+ export interface ManagedPropertyLoadDiagnostic extends LoadDiagnostic {
21
+ key: string;
22
+ }
23
+ export interface ManagedPropertyLoadValidationResult {
24
+ data: Record<string, unknown>;
25
+ diagnostics: ManagedPropertyLoadDiagnostic[];
26
+ }
27
+ export declare class ManagedPropertyState {
28
+ private properties;
29
+ private validatedLoadArtifacts;
30
+ private createLoadValidationResult;
31
+ register<T>(key: string, defaultValue: T, options?: ManagedPropertyRegistrationOptions<T>): BehaviorSubject<T>;
32
+ get<T>(key: string): T | undefined;
33
+ set<T>(key: string, value: T): void;
34
+ setIfRegistered<T>(key: string, value: T): void;
35
+ validateLoadData(data: unknown): ManagedPropertyLoadValidationResult;
36
+ applyValidatedData(result: ManagedPropertyLoadValidationResult): void;
37
+ load(data: unknown): ManagedPropertyLoadDiagnostic[];
38
+ save(): Record<string, unknown>;
39
+ getProperty<T>(key: string): ManagedProperty<T> | undefined;
40
+ getObservable<T>(key: string): BehaviorSubject<T> | undefined;
41
+ has(key: string): boolean;
42
+ unregister(key: string): boolean;
43
+ getAllKeys(): string[];
44
+ }
45
+ declare const _default: ManagedPropertyState;
46
+ export default _default;
@@ -0,0 +1,192 @@
1
+ import { BehaviorSubject } from 'rxjs';
2
+ import { isRecord } from '@asyra/utils';
3
+ const cloneLoadData = (data) => {
4
+ if (typeof globalThis.structuredClone === 'function') {
5
+ return globalThis.structuredClone(data);
6
+ }
7
+ return JSON.parse(JSON.stringify(data));
8
+ };
9
+ const createValidatorFromDefault = (defaultValue) => {
10
+ if (Array.isArray(defaultValue)) {
11
+ return ((value) => Array.isArray(value));
12
+ }
13
+ if (defaultValue === null) {
14
+ // Null defaults cannot infer a stable runtime type. Keep permissive behavior
15
+ // unless a custom validator is explicitly provided by registration options.
16
+ return ((_) => true);
17
+ }
18
+ if (defaultValue === undefined) {
19
+ return ((_) => true);
20
+ }
21
+ if (typeof defaultValue === 'number') {
22
+ return ((value) => typeof value === 'number' && Number.isFinite(value));
23
+ }
24
+ if (typeof defaultValue === 'object') {
25
+ return ((value) => isRecord(value));
26
+ }
27
+ const expectedType = typeof defaultValue;
28
+ return ((value) => typeof value === expectedType);
29
+ };
30
+ export class ManagedPropertyState {
31
+ properties = new Map();
32
+ validatedLoadArtifacts = new WeakMap();
33
+ createLoadValidationResult(data, diagnostics, properties) {
34
+ const validatedSnapshot = cloneLoadData(data);
35
+ const result = {
36
+ data: cloneLoadData(validatedSnapshot),
37
+ diagnostics
38
+ };
39
+ this.validatedLoadArtifacts.set(result, {
40
+ data: validatedSnapshot,
41
+ properties
42
+ });
43
+ return result;
44
+ }
45
+ register(key, defaultValue, options) {
46
+ const silent = options?.silent ?? true;
47
+ if (this.properties.has(key)) {
48
+ if (!silent) {
49
+ console.warn(`[ManagedPropertyState] Property "${key}" already registered, returning existing`);
50
+ }
51
+ const existing = this.getProperty(key);
52
+ if (existing) {
53
+ return existing.state;
54
+ }
55
+ }
56
+ const state = new BehaviorSubject(defaultValue);
57
+ const validate = options?.validate ?? createValidatorFromDefault(defaultValue);
58
+ const runtime = options?.runtime ?? true;
59
+ this.properties.set(key, {
60
+ key,
61
+ state,
62
+ validate: (value) => validate(value),
63
+ runtime
64
+ });
65
+ return state;
66
+ }
67
+ get(key) {
68
+ const prop = this.properties.get(key);
69
+ return prop?.state.getValue();
70
+ }
71
+ set(key, value) {
72
+ const prop = this.properties.get(key);
73
+ if (prop) {
74
+ if (!prop.validate(value)) {
75
+ console.warn(`[ManagedPropertyState] Rejected invalid value for "${key}" during runtime set`);
76
+ return;
77
+ }
78
+ prop.state.next(value);
79
+ }
80
+ else {
81
+ console.warn(`[ManagedPropertyState] Property "${key}" not found`);
82
+ }
83
+ }
84
+ setIfRegistered(key, value) {
85
+ const prop = this.properties.get(key);
86
+ if (prop) {
87
+ if (!prop.validate(value)) {
88
+ return;
89
+ }
90
+ prop.state.next(value);
91
+ }
92
+ }
93
+ validateLoadData(data) {
94
+ const diagnostics = [];
95
+ const sanitized = {};
96
+ const validatedProperties = new Map();
97
+ if (data === undefined) {
98
+ return this.createLoadValidationResult(sanitized, diagnostics, validatedProperties);
99
+ }
100
+ if (!isRecord(data)) {
101
+ diagnostics.push({
102
+ key: '__root__',
103
+ path: 'systemContext',
104
+ message: 'Expected object map for managed properties'
105
+ });
106
+ return this.createLoadValidationResult(sanitized, diagnostics, validatedProperties);
107
+ }
108
+ Object.entries(data).forEach(([key, value]) => {
109
+ const prop = this.properties.get(key);
110
+ if (!prop) {
111
+ diagnostics.push({
112
+ key,
113
+ path: `systemContext.${key}`,
114
+ message: 'Ignored unregistered managed property during load'
115
+ });
116
+ return;
117
+ }
118
+ if (prop.runtime) {
119
+ diagnostics.push({
120
+ key,
121
+ path: `systemContext.${key}`,
122
+ message: 'Ignored runtime-only managed property during load'
123
+ });
124
+ return;
125
+ }
126
+ if (!prop.validate(value)) {
127
+ diagnostics.push({
128
+ key,
129
+ path: `systemContext.${key}`,
130
+ message: 'Ignored invalid managed property value during load'
131
+ });
132
+ return;
133
+ }
134
+ sanitized[key] = value;
135
+ validatedProperties.set(key, prop);
136
+ });
137
+ return this.createLoadValidationResult(sanitized, diagnostics, validatedProperties);
138
+ }
139
+ applyValidatedData(result) {
140
+ const artifact = this.validatedLoadArtifacts.get(result);
141
+ if (!artifact) {
142
+ throw new Error('[ManagedPropertyState] Expected an owner-issued one-shot validated load artifact');
143
+ }
144
+ artifact.properties.forEach((property, key) => {
145
+ if (this.properties.get(key) !== property) {
146
+ throw new Error('[ManagedPropertyState] Expected an owner-issued one-shot validated load artifact with current registrations');
147
+ }
148
+ });
149
+ this.validatedLoadArtifacts.delete(result);
150
+ Object.entries(artifact.data).forEach(([key, value]) => {
151
+ artifact.properties.get(key)?.state.next(value);
152
+ });
153
+ }
154
+ load(data) {
155
+ const result = this.validateLoadData(data);
156
+ this.applyValidatedData(result);
157
+ return result.diagnostics;
158
+ }
159
+ save() {
160
+ const result = {};
161
+ this.properties.forEach((property, key) => {
162
+ if (property.runtime) {
163
+ return;
164
+ }
165
+ result[key] = property.state.getValue();
166
+ });
167
+ return result;
168
+ }
169
+ getProperty(key) {
170
+ return this.properties.get(key);
171
+ }
172
+ getObservable(key) {
173
+ const prop = this.properties.get(key);
174
+ return prop?.state;
175
+ }
176
+ has(key) {
177
+ return this.properties.has(key);
178
+ }
179
+ unregister(key) {
180
+ const property = this.properties.get(key);
181
+ if (!property) {
182
+ return false;
183
+ }
184
+ this.properties.delete(key);
185
+ property.state.complete();
186
+ return true;
187
+ }
188
+ getAllKeys() {
189
+ return Array.from(this.properties.keys());
190
+ }
191
+ }
192
+ export default new ManagedPropertyState();
@@ -1,16 +1,16 @@
1
- import { HandlerDeps, PrimaryToolStateAPIs, MouseStateAPIs, SystemStateAPIs, SystemContextAPIs, RootAPIs, KeyStateAPIs, TargetStateAPIs } from './types';
1
+ import { HandlerDeps, SystemContextAPIs, RootAPIs, ManagedPropertyStateAPIs } from './types/index.js';
2
2
  export declare class SystemContext implements SystemContextAPIs {
3
- private deps;
4
- getSystemMode: SystemStateAPIs['getSystemMode'];
5
- getCurrentPrimaryTool: PrimaryToolStateAPIs['getCurrentPrimaryTool'];
6
- switchPrimaryTool: PrimaryToolStateAPIs['switchPrimaryTool'];
7
- getMouseState: MouseStateAPIs['getMouseState'];
8
- updateMouseState: MouseStateAPIs['updateMouseState'];
9
- getKeyState: KeyStateAPIs['getKeyState'];
10
- updateKeyState: KeyStateAPIs['updateKeyState'];
11
- getTargetState: TargetStateAPIs['getTargetState'];
12
- updateHoveredElementId: TargetStateAPIs['updateHoveredElementId'];
13
3
  getSystemContextSnapshot: RootAPIs['getSystemContextSnapshot'];
4
+ registerProperty: ManagedPropertyStateAPIs['registerProperty'];
5
+ getManagedProperty: ManagedPropertyStateAPIs['getManagedProperty'];
6
+ setManagedProperty: ManagedPropertyStateAPIs['setManagedProperty'];
7
+ getManagedPropertyObservable: ManagedPropertyStateAPIs['getManagedPropertyObservable'];
8
+ hasManagedProperty: ManagedPropertyStateAPIs['hasManagedProperty'];
9
+ unregisterProperty: ManagedPropertyStateAPIs['unregisterProperty'];
10
+ validateManagedProperties: ManagedPropertyStateAPIs['validateManagedProperties'];
11
+ applyValidatedManagedProperties: ManagedPropertyStateAPIs['applyValidatedManagedProperties'];
12
+ loadManagedProperties: ManagedPropertyStateAPIs['loadManagedProperties'];
13
+ saveManagedProperties: ManagedPropertyStateAPIs['saveManagedProperties'];
14
14
  constructor(deps: HandlerDeps);
15
15
  }
16
16
  declare const systemContext: SystemContext;
@@ -1,30 +1,23 @@
1
- import { createAllAPIs } from './apis';
2
- import { systemState, primaryToolState, mouseState, keyState, targetState } from './states';
3
- import { initSystemContextSubscribe } from './subscribe';
1
+ import { createAllAPIs } from './apis/index.js';
2
+ import { managedPropertyState } from './states/index.js';
4
3
  export class SystemContext {
5
- deps;
6
- getSystemMode;
7
- getCurrentPrimaryTool;
8
- switchPrimaryTool;
9
- getMouseState;
10
- updateMouseState;
11
- getKeyState;
12
- updateKeyState;
13
- getTargetState;
14
- updateHoveredElementId;
15
4
  getSystemContextSnapshot;
5
+ registerProperty;
6
+ getManagedProperty;
7
+ setManagedProperty;
8
+ getManagedPropertyObservable;
9
+ hasManagedProperty;
10
+ unregisterProperty;
11
+ validateManagedProperties;
12
+ applyValidatedManagedProperties;
13
+ loadManagedProperties;
14
+ saveManagedProperties;
16
15
  constructor(deps) {
17
- this.deps = deps;
18
16
  const apis = createAllAPIs(deps);
19
- initSystemContextSubscribe(apis);
20
17
  Object.assign(this, apis);
21
18
  }
22
19
  }
23
20
  const systemContext = new SystemContext({
24
- systemState,
25
- primaryToolState,
26
- mouseState,
27
- keyState,
28
- targetState
21
+ managedPropertyState
29
22
  });
30
23
  export default systemContext;
@@ -1,12 +1,4 @@
1
- import type { SystemState, PrimaryToolState, MouseState, KeyState, TargetState } from '../states';
1
+ import type { ManagedPropertyState } from '../states/index.js';
2
2
  export interface HandlerDeps {
3
- systemState: SystemState;
4
- primaryToolState: PrimaryToolState;
5
- mouseState: MouseState;
6
- keyState: KeyState;
7
- targetState: TargetState;
8
- }
9
- export interface SystemDeps {
10
- primaryToolState: PrimaryToolState;
11
- mouseState: MouseState;
3
+ managedPropertyState: ManagedPropertyState;
12
4
  }
@@ -1,9 +1,5 @@
1
- import { RootAPIs } from './root';
2
- import { SystemStateAPIs } from './system-state';
3
- import { PrimaryToolStateAPIs } from './primary-tool-state';
4
- import { MouseStateAPIs } from './mouse-state';
5
- import { KeyStateAPIs } from './key-state';
6
- import { TargetStateAPIs } from './target-state';
7
- export { RootAPIs, SystemStateAPIs, PrimaryToolStateAPIs, MouseStateAPIs, KeyStateAPIs, TargetStateAPIs };
8
- export type SystemContextAPIs = RootAPIs & SystemStateAPIs & PrimaryToolStateAPIs & MouseStateAPIs & KeyStateAPIs & TargetStateAPIs;
9
- export * from './deps';
1
+ import { RootAPIs } from './root.js';
2
+ import { ManagedPropertyStateAPIs } from './managed-property-state.js';
3
+ export { RootAPIs, ManagedPropertyStateAPIs };
4
+ export type SystemContextAPIs = RootAPIs & ManagedPropertyStateAPIs;
5
+ export * from './deps.js';
@@ -1 +1 @@
1
- export * from './deps';
1
+ export * from './deps.js';
@@ -0,0 +1,15 @@
1
+ import { BehaviorSubject } from 'rxjs';
2
+ import type { ManagedPropertyLoadDiagnostic, ManagedPropertyLoadValidationResult, ManagedPropertyRegistrationOptions } from '../states/managed-property-state.js';
3
+ export interface ManagedPropertyStateAPIs {
4
+ registerProperty: <T>(key: string, defaultValue: T, options?: ManagedPropertyRegistrationOptions<T>) => BehaviorSubject<T>;
5
+ getManagedProperty: <T>(key: string) => T | undefined;
6
+ setManagedProperty: <T>(key: string, value: T) => void;
7
+ getManagedPropertyObservable: <T>(key: string) => BehaviorSubject<T> | undefined;
8
+ hasManagedProperty: (key: string) => boolean;
9
+ unregisterProperty: (key: string) => boolean;
10
+ validateManagedProperties: (data: unknown) => ManagedPropertyLoadValidationResult;
11
+ /** Apply only an owner-issued validation result; this does not rerun validators. */
12
+ applyValidatedManagedProperties: (result: ManagedPropertyLoadValidationResult) => void;
13
+ loadManagedProperties: (data: unknown) => ManagedPropertyLoadDiagnostic[];
14
+ saveManagedProperties: () => Record<string, unknown>;
15
+ }