@ng-primitives/state 0.29.0 → 0.30.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/README.md +1 -1
- package/fesm2022/ng-primitives-state.mjs +1 -56
- package/fesm2022/ng-primitives-state.mjs.map +1 -1
- package/index.d.ts +1 -41
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Angular Primitives State is a low-level state library with a focus on developer
|
|
|
8
8
|
npm install @ng-primitives/state
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
This package
|
|
11
|
+
This package re-exports the state utilities from 'ng-primitives/state' for standalone usage.
|
|
12
12
|
|
|
13
13
|
## Contributing
|
|
14
14
|
|
|
@@ -1,61 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Create a new injection token for the state.
|
|
5
|
-
* @param description The description of the token
|
|
6
|
-
*/
|
|
7
|
-
function createStateToken(description) {
|
|
8
|
-
return new InjectionToken(`Ngp${description}StateToken`);
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Create a new provider for the state. It first tries to inject the state from the parent injector,
|
|
12
|
-
* as this allows for the state to be hoisted to a higher level in the component tree. This can
|
|
13
|
-
* be useful to avoid issues where the injector can't be shared in some cases when ng-content is used.
|
|
14
|
-
* @param token The token for the state
|
|
15
|
-
*/
|
|
16
|
-
function createStateProvider(token) {
|
|
17
|
-
return () => ({
|
|
18
|
-
provide: token,
|
|
19
|
-
useFactory: () => inject(token, { optional: true, skipSelf: true }) ?? signal({}),
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
function createStateInjector(token, options = {}) {
|
|
23
|
-
return () => {
|
|
24
|
-
const value = inject(token);
|
|
25
|
-
if (options.deferred) {
|
|
26
|
-
return computed(() => Object.keys(value() ?? {}).length === 0 ? undefined : value());
|
|
27
|
-
}
|
|
28
|
-
return value;
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Convert the original state object into a writable state object.
|
|
33
|
-
* @param token The token for the state
|
|
34
|
-
*/
|
|
35
|
-
function createState(token) {
|
|
36
|
-
return (state) => {
|
|
37
|
-
const internalState = inject(token);
|
|
38
|
-
internalState.update(obj => {
|
|
39
|
-
// Iterating over properties
|
|
40
|
-
for (const key in state) {
|
|
41
|
-
const value = state[key];
|
|
42
|
-
// @ts-ignore
|
|
43
|
-
obj[key] = isSignal(value) ? linkedSignal(() => value()) : value;
|
|
44
|
-
}
|
|
45
|
-
// Iterating over prototype methods
|
|
46
|
-
const prototype = Object.getPrototypeOf(state);
|
|
47
|
-
for (const key of Object.getOwnPropertyNames(prototype)) {
|
|
48
|
-
obj[key] = prototype[key].bind(state);
|
|
49
|
-
}
|
|
50
|
-
return { ...obj };
|
|
51
|
-
});
|
|
52
|
-
return internalState();
|
|
53
|
-
};
|
|
54
|
-
}
|
|
1
|
+
export * from 'ng-primitives/state';
|
|
55
2
|
|
|
56
3
|
/**
|
|
57
4
|
* Generated bundle index. Do not edit.
|
|
58
5
|
*/
|
|
59
|
-
|
|
60
|
-
export { createState, createStateInjector, createStateProvider, createStateToken };
|
|
61
6
|
//# sourceMappingURL=ng-primitives-state.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ng-primitives-state.mjs","sources":["../../../../packages/state/src/
|
|
1
|
+
{"version":3,"file":"ng-primitives-state.mjs","sources":["../../../../packages/state/src/ng-primitives-state.ts"],"sourcesContent":["/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;AAAA;;AAEG"}
|
package/index.d.ts
CHANGED
|
@@ -1,41 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* This converts the state object to a writable state object.
|
|
4
|
-
* This means that inputs become signals which are writable.
|
|
5
|
-
*/
|
|
6
|
-
export type State<T> = {
|
|
7
|
-
[K in keyof T]: T[K] extends InputSignalWithTransform<infer U, any> ? WritableSignal<U> : T[K] extends InputSignal<infer R> ? WritableSignal<R> : T[K];
|
|
8
|
-
};
|
|
9
|
-
export type InjectedState<T> = Signal<State<T>>;
|
|
10
|
-
/**
|
|
11
|
-
* Create a new injection token for the state.
|
|
12
|
-
* @param description The description of the token
|
|
13
|
-
*/
|
|
14
|
-
export declare function createStateToken<T>(description: string): InjectionToken<T>;
|
|
15
|
-
/**
|
|
16
|
-
* Create a new provider for the state. It first tries to inject the state from the parent injector,
|
|
17
|
-
* as this allows for the state to be hoisted to a higher level in the component tree. This can
|
|
18
|
-
* be useful to avoid issues where the injector can't be shared in some cases when ng-content is used.
|
|
19
|
-
* @param token The token for the state
|
|
20
|
-
*/
|
|
21
|
-
export declare function createStateProvider<T>(token: ProviderToken<T>): () => FactoryProvider;
|
|
22
|
-
type CreateStateInjectorOptions = {
|
|
23
|
-
/**
|
|
24
|
-
* Whether the state may not be immediately available. This can happen when the child is instantiated before the parent.
|
|
25
|
-
*/
|
|
26
|
-
deferred?: boolean;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Create a new state injector for the state.
|
|
30
|
-
* @param token The token for the state
|
|
31
|
-
*/
|
|
32
|
-
export declare function createStateInjector<T>(token: ProviderToken<State<T>>, options: {
|
|
33
|
-
deferred: true;
|
|
34
|
-
}): <U = T>() => Signal<State<U> | undefined>;
|
|
35
|
-
export declare function createStateInjector<T>(token: ProviderToken<State<T>>, options?: CreateStateInjectorOptions): <U = T>() => Signal<State<U>>;
|
|
36
|
-
/**
|
|
37
|
-
* Convert the original state object into a writable state object.
|
|
38
|
-
* @param token The token for the state
|
|
39
|
-
*/
|
|
40
|
-
export declare function createState(token: ProviderToken<WritableSignal<State<unknown>>>): <U>(state: U) => State<U>;
|
|
41
|
-
export {};
|
|
1
|
+
export * from 'ng-primitives/state';
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@ng-primitives/state",
|
|
3
3
|
"description": "Angular Primitives State is a low-level state library with a focus on developer experience.",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.30.0",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"angular",
|
|
8
8
|
"primitives",
|
|
@@ -28,6 +28,10 @@
|
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"@angular/core": ">=19.0.0"
|
|
30
30
|
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"ng-primitives": "0.30.0",
|
|
33
|
+
"tslib": "^2.3.0"
|
|
34
|
+
},
|
|
31
35
|
"sideEffects": false,
|
|
32
36
|
"module": "fesm2022/ng-primitives-state.mjs",
|
|
33
37
|
"typings": "index.d.ts",
|
|
@@ -39,8 +43,5 @@
|
|
|
39
43
|
"types": "./index.d.ts",
|
|
40
44
|
"default": "./fesm2022/ng-primitives-state.mjs"
|
|
41
45
|
}
|
|
42
|
-
},
|
|
43
|
-
"dependencies": {
|
|
44
|
-
"tslib": "^2.3.0"
|
|
45
46
|
}
|
|
46
47
|
}
|