@asyra/system-context 0.2.5 → 0.5.0
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/LICENSE +21 -0
- package/README.md +13 -1
- package/dist/apis/index.d.ts +11 -10
- package/dist/apis/index.js +3 -11
- package/dist/apis/managed-property-state.d.ts +39 -0
- package/dist/apis/managed-property-state.js +53 -0
- package/dist/apis/root.d.ts +1 -1
- package/dist/apis/root.js +18 -11
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/states/index.d.ts +2 -6
- package/dist/states/index.js +2 -6
- package/dist/states/managed-property-state.d.ts +46 -0
- package/dist/states/managed-property-state.js +192 -0
- package/dist/system-context.d.ts +11 -11
- package/dist/system-context.js +13 -20
- package/dist/types/deps.d.ts +2 -10
- package/dist/types/index.d.ts +5 -9
- package/dist/types/index.js +1 -1
- package/dist/types/managed-property-state.d.ts +15 -0
- package/package.json +22 -4
- package/.turbo/turbo-test$colon$ci.log +0 -77
- package/.turbo/turbo-test$colon$local.log +0 -23
- package/.turbo/turbo-test.log +0 -359
- package/CHANGELOG.md +0 -46
- package/coverage/base.css +0 -224
- package/coverage/block-navigation.js +0 -87
- package/coverage/clover.xml +0 -345
- package/coverage/coverage-final.json +0 -5831
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +0 -176
- package/coverage/prettify.css +0 -1
- package/coverage/prettify.js +0 -2
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +0 -196
- package/coverage/src/apis/index.html +0 -206
- package/coverage/src/apis/index.ts.html +0 -133
- package/coverage/src/apis/key-state.ts.html +0 -124
- package/coverage/src/apis/mouse-state.ts.html +0 -124
- package/coverage/src/apis/primary-tool-state.ts.html +0 -124
- package/coverage/src/apis/root.ts.html +0 -139
- package/coverage/src/apis/system-state.ts.html +0 -112
- package/coverage/src/apis/target-state.ts.html +0 -121
- package/coverage/src/index.html +0 -131
- package/coverage/src/index.ts.html +0 -97
- package/coverage/src/states/index.html +0 -191
- package/coverage/src/states/index.ts.html +0 -139
- package/coverage/src/states/key-state.ts.html +0 -148
- package/coverage/src/states/mouse-state.ts.html +0 -184
- package/coverage/src/states/primary-tool-state.ts.html +0 -130
- package/coverage/src/states/system-state.ts.html +0 -130
- package/coverage/src/states/target-state.ts.html +0 -154
- package/coverage/src/subscribe/index.html +0 -191
- package/coverage/src/subscribe/index.ts.html +0 -187
- package/coverage/src/subscribe/key-state.ts.html +0 -109
- package/coverage/src/subscribe/mouse-state.ts.html +0 -109
- package/coverage/src/subscribe/primary-tool-state.ts.html +0 -136
- package/coverage/src/subscribe/system-state.ts.html +0 -94
- package/coverage/src/subscribe/target-state.ts.html +0 -109
- package/coverage/src/system-context.ts.html +0 -247
- package/coverage/src/types/deps.ts.html +0 -145
- package/coverage/src/types/index.html +0 -221
- package/coverage/src/types/index.ts.html +0 -157
- package/coverage/src/types/key-state.ts.html +0 -118
- package/coverage/src/types/mouse-state.ts.html +0 -118
- package/coverage/src/types/primary-tool-state.ts.html +0 -121
- package/coverage/src/types/root.ts.html +0 -106
- package/coverage/src/types/system-state.ts.html +0 -106
- package/coverage/src/types/target-state.ts.html +0 -118
- package/dist/__tests__/system-context.test.js +0 -63
- package/dist/apis/key-state.d.ts +0 -2
- package/dist/apis/key-state.js +0 -8
- package/dist/apis/mouse-state copy.d.ts +0 -2
- package/dist/apis/mouse-state copy.js +0 -8
- package/dist/apis/mouse-state.d.ts +0 -2
- package/dist/apis/mouse-state.js +0 -8
- package/dist/apis/primary-tool-state.d.ts +0 -2
- package/dist/apis/primary-tool-state.js +0 -8
- package/dist/apis/system-state.d.ts +0 -2
- package/dist/apis/system-state.js +0 -5
- package/dist/apis/target-state.d.ts +0 -2
- package/dist/apis/target-state.js +0 -8
- package/dist/states/key-state.d.ts +0 -9
- package/dist/states/key-state.js +0 -16
- package/dist/states/mouse-state copy.d.ts +0 -9
- package/dist/states/mouse-state copy.js +0 -28
- package/dist/states/mouse-state.d.ts +0 -9
- package/dist/states/mouse-state.js +0 -28
- package/dist/states/primary-tool-state.d.ts +0 -8
- package/dist/states/primary-tool-state.js +0 -11
- package/dist/states/system-state.d.ts +0 -8
- package/dist/states/system-state.js +0 -11
- package/dist/states/target-state.d.ts +0 -9
- package/dist/states/target-state.js +0 -17
- package/dist/subscribe/index.d.ts +0 -2
- package/dist/subscribe/index.js +0 -24
- package/dist/subscribe/key-state.d.ts +0 -2
- package/dist/subscribe/key-state.js +0 -6
- package/dist/subscribe/mouse-state copy.d.ts +0 -2
- package/dist/subscribe/mouse-state copy.js +0 -6
- package/dist/subscribe/mouse-state.d.ts +0 -2
- package/dist/subscribe/mouse-state.js +0 -6
- package/dist/subscribe/primary-tool-state.d.ts +0 -2
- package/dist/subscribe/primary-tool-state.js +0 -10
- package/dist/subscribe/root.d.ts +0 -2
- package/dist/subscribe/root.js +0 -7
- package/dist/subscribe/system-state.d.ts +0 -1
- package/dist/subscribe/system-state.js +0 -3
- package/dist/subscribe/target-state.d.ts +0 -2
- package/dist/subscribe/target-state.js +0 -6
- package/dist/types/key-state.d.ts +0 -8
- package/dist/types/key-state.js +0 -1
- package/dist/types/mouse-state copy.d.ts +0 -8
- package/dist/types/mouse-state copy.js +0 -1
- package/dist/types/mouse-state.d.ts +0 -8
- package/dist/types/mouse-state.js +0 -1
- package/dist/types/primary-tool-state.d.ts +0 -8
- package/dist/types/primary-tool-state.js +0 -1
- package/dist/types/system-state.d.ts +0 -5
- package/dist/types/system-state.js +0 -1
- package/dist/types/target-state.d.ts +0 -8
- package/dist/types/target-state.js +0 -1
- package/src/__tests__/system-context.test.ts +0 -79
- package/src/apis/index.ts +0 -16
- package/src/apis/key-state.ts +0 -13
- package/src/apis/mouse-state.ts +0 -13
- package/src/apis/primary-tool-state.ts +0 -13
- package/src/apis/root.ts +0 -18
- package/src/apis/system-state.ts +0 -9
- package/src/apis/target-state.ts +0 -12
- package/src/index.ts +0 -4
- package/src/states/index.ts +0 -18
- package/src/states/key-state.ts +0 -21
- package/src/states/mouse-state.ts +0 -33
- package/src/states/primary-tool-state.ts +0 -15
- package/src/states/system-state.ts +0 -15
- package/src/states/target-state.ts +0 -23
- package/src/subscribe/index.ts +0 -34
- package/src/subscribe/key-state.ts +0 -8
- package/src/subscribe/mouse-state.ts +0 -8
- package/src/subscribe/primary-tool-state.ts +0 -17
- package/src/subscribe/system-state.ts +0 -3
- package/src/subscribe/target-state.ts +0 -8
- package/src/system-context.ts +0 -54
- package/src/types/deps.ts +0 -20
- package/src/types/index.ts +0 -24
- package/src/types/key-state.ts +0 -11
- package/src/types/mouse-state.ts +0 -11
- package/src/types/primary-tool-state.ts +0 -12
- package/src/types/root.ts +0 -7
- package/src/types/system-state.ts +0 -7
- package/src/types/target-state.ts +0 -11
- package/tsconfig.json +0 -17
- /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,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@asyra/system-context`
|
|
2
|
+
|
|
3
|
+
Managed system properties and runtime context shared through explicit
|
|
4
|
+
framework registration.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import systemContext from '@asyra/system-context'
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Release support
|
|
11
|
+
|
|
12
|
+
The `0.2.5` ESM artifact supports Node.js 24.x. Use only package-root exports.
|
|
13
|
+
See the [Framework release support contract](../../docs/ai/framework/RELEASE_SUPPORT.md).
|
package/dist/apis/index.d.ts
CHANGED
|
@@ -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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
};
|
package/dist/apis/index.js
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
import { createRootAPIs } from './root';
|
|
2
|
-
import {
|
|
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
|
-
...
|
|
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
|
+
});
|
package/dist/apis/root.d.ts
CHANGED
|
@@ -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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
package/dist/index.js
CHANGED
package/dist/states/index.d.ts
CHANGED
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
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 };
|
package/dist/states/index.js
CHANGED
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
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();
|
package/dist/system-context.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { HandlerDeps,
|
|
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;
|
package/dist/system-context.js
CHANGED
|
@@ -1,30 +1,23 @@
|
|
|
1
|
-
import { createAllAPIs } from './apis';
|
|
2
|
-
import {
|
|
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
|
-
|
|
25
|
-
primaryToolState,
|
|
26
|
-
mouseState,
|
|
27
|
-
keyState,
|
|
28
|
-
targetState
|
|
21
|
+
managedPropertyState
|
|
29
22
|
});
|
|
30
23
|
export default systemContext;
|
package/dist/types/deps.d.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ManagedPropertyState } from '../states/index.js';
|
|
2
2
|
export interface HandlerDeps {
|
|
3
|
-
|
|
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
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import { RootAPIs } from './root';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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';
|
package/dist/types/index.js
CHANGED
|
@@ -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
|
+
}
|