@ng-primitives/state 0.29.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 +19 -0
- package/fesm2022/ng-primitives-state.mjs +61 -0
- package/fesm2022/ng-primitives-state.mjs.map +1 -0
- package/index.d.ts +41 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Angular Primitives State
|
|
2
|
+
|
|
3
|
+
Angular Primitives State is a low-level state library with a focus on developer experience.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @ng-primitives/state
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
This package is auto-generated from `ng-primitives/state` and the source should be modified there.
|
|
12
|
+
|
|
13
|
+
## Contributing
|
|
14
|
+
|
|
15
|
+
We welcome contributions from the community. Please refer to our [contributing guidelines](CONTRIBUTING.md) for more information.
|
|
16
|
+
|
|
17
|
+
## License
|
|
18
|
+
|
|
19
|
+
Angular Primitives is licensed under the [Apache License 2.0](LICENSE).
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { InjectionToken, inject, signal, computed, isSignal, linkedSignal } from '@angular/core';
|
|
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
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Generated bundle index. Do not edit.
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
export { createState, createStateInjector, createStateProvider, createStateToken };
|
|
61
|
+
//# sourceMappingURL=ng-primitives-state.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ng-primitives-state.mjs","sources":["../../../../packages/state/src/index.ts","../../../../packages/state/src/ng-primitives-state.ts"],"sourcesContent":["import {\n computed,\n FactoryProvider,\n inject,\n InjectionToken,\n InputSignal,\n InputSignalWithTransform,\n isSignal,\n linkedSignal,\n ProviderToken,\n signal,\n Signal,\n WritableSignal,\n} from '@angular/core';\n\n/**\n * This converts the state object to a writable state object.\n * This means that inputs become signals which are writable.\n */\nexport type State<T> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [K in keyof T]: T[K] extends InputSignalWithTransform<infer U, any>\n ? WritableSignal<U>\n : T[K] extends InputSignal<infer R>\n ? WritableSignal<R>\n : T[K];\n};\n\nexport type InjectedState<T> = Signal<State<T>>;\n\n/**\n * Create a new injection token for the state.\n * @param description The description of the token\n */\nexport function createStateToken<T>(description: string): InjectionToken<T> {\n return new InjectionToken<Signal<State<T>>>(`Ngp${description}StateToken`);\n}\n\n/**\n * Create a new provider for the state. It first tries to inject the state from the parent injector,\n * as this allows for the state to be hoisted to a higher level in the component tree. This can\n * be useful to avoid issues where the injector can't be shared in some cases when ng-content is used.\n * @param token The token for the state\n */\nexport function createStateProvider<T>(token: ProviderToken<T>): () => FactoryProvider {\n return () => ({\n provide: token,\n useFactory: () => inject(token, { optional: true, skipSelf: true }) ?? signal({}),\n });\n}\n\ntype CreateStateInjectorOptions = {\n /**\n * Whether the state may not be immediately available. This can happen when the child is instantiated before the parent.\n */\n deferred?: boolean;\n};\n\n/**\n * Create a new state injector for the state.\n * @param token The token for the state\n */\nexport function createStateInjector<T>(\n token: ProviderToken<State<T>>,\n options: { deferred: true },\n): <U = T>() => Signal<State<U> | undefined>;\nexport function createStateInjector<T>(\n token: ProviderToken<State<T>>,\n options?: CreateStateInjectorOptions,\n): <U = T>() => Signal<State<U>>;\nexport function createStateInjector<T>(\n token: ProviderToken<State<T>>,\n options: CreateStateInjectorOptions = {},\n): <U = T>() => Signal<State<U> | undefined> {\n return <U = T>() => {\n const value = inject(token) as Signal<State<U> | undefined>;\n\n if (options.deferred) {\n return computed(() =>\n Object.keys(value() ?? {}).length === 0 ? undefined : value(),\n ) as Signal<State<U> | undefined>;\n }\n\n return value as Signal<State<U>>;\n };\n}\n\n/**\n * Convert the original state object into a writable state object.\n * @param token The token for the state\n */\nexport function createState(token: ProviderToken<WritableSignal<State<unknown>>>) {\n return <U>(state: U): State<U> => {\n const internalState = inject(token);\n\n internalState.update(obj => {\n // Iterating over properties\n for (const key in state) {\n const value = state[key as keyof U];\n\n // @ts-ignore\n obj[key] = isSignal(value) ? linkedSignal(() => value()) : value;\n }\n\n // Iterating over prototype methods\n const prototype = Object.getPrototypeOf(state);\n\n for (const key of Object.getOwnPropertyNames(prototype)) {\n (obj as Record<string, unknown>)[key] = prototype[key as keyof U].bind(state);\n }\n\n return { ...obj };\n });\n\n return internalState() as unknown as State<U>;\n };\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;AA8BA;;;AAGG;AACG,SAAU,gBAAgB,CAAI,WAAmB,EAAA;AACrD,IAAA,OAAO,IAAI,cAAc,CAAmB,MAAM,WAAW,CAAA,UAAA,CAAY,CAAC;AAC5E;AAEA;;;;;AAKG;AACG,SAAU,mBAAmB,CAAI,KAAuB,EAAA;IAC5D,OAAO,OAAO;AACZ,QAAA,OAAO,EAAE,KAAK;QACd,UAAU,EAAE,MAAM,MAAM,CAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,IAAI,MAAM,CAAC,EAAE,CAAC;AAClF,KAAA,CAAC;AACJ;SAqBgB,mBAAmB,CACjC,KAA8B,EAC9B,UAAsC,EAAE,EAAA;AAExC,IAAA,OAAO,MAAY;AACjB,QAAA,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAiC;AAE3D,QAAA,IAAI,OAAO,CAAC,QAAQ,EAAE;AACpB,YAAA,OAAO,QAAQ,CAAC,MACd,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,MAAM,KAAK,CAAC,GAAG,SAAS,GAAG,KAAK,EAAE,CAC9B;;AAGnC,QAAA,OAAO,KAAyB;AAClC,KAAC;AACH;AAEA;;;AAGG;AACG,SAAU,WAAW,CAAC,KAAoD,EAAA;IAC9E,OAAO,CAAI,KAAQ,KAAc;AAC/B,QAAA,MAAM,aAAa,GAAG,MAAM,CAAC,KAAK,CAAC;AAEnC,QAAA,aAAa,CAAC,MAAM,CAAC,GAAG,IAAG;;AAEzB,YAAA,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE;AACvB,gBAAA,MAAM,KAAK,GAAG,KAAK,CAAC,GAAc,CAAC;;gBAGnC,GAAG,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,YAAY,CAAC,MAAM,KAAK,EAAE,CAAC,GAAG,KAAK;;;YAIlE,MAAM,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC;YAE9C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,mBAAmB,CAAC,SAAS,CAAC,EAAE;AACtD,gBAAA,GAA+B,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,GAAc,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC;;AAG/E,YAAA,OAAO,EAAE,GAAG,GAAG,EAAE;AACnB,SAAC,CAAC;QAEF,OAAO,aAAa,EAAyB;AAC/C,KAAC;AACH;;ACpHA;;AAEG;;;;"}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { FactoryProvider, InjectionToken, InputSignal, InputSignalWithTransform, ProviderToken, Signal, WritableSignal } from '@angular/core';
|
|
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 {};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ng-primitives/state",
|
|
3
|
+
"description": "Angular Primitives State is a low-level state library with a focus on developer experience.",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"version": "0.29.0",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"angular",
|
|
8
|
+
"primitives",
|
|
9
|
+
"state",
|
|
10
|
+
"cdk"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://angularprimitives.com",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/ng-primitives/ng-primitives.git"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/ng-primitives/ng-primitives/issues"
|
|
19
|
+
},
|
|
20
|
+
"author": {
|
|
21
|
+
"name": "Ashley Hunter",
|
|
22
|
+
"url": "https://github.com/ashley-hunter"
|
|
23
|
+
},
|
|
24
|
+
"funding": {
|
|
25
|
+
"type": "github",
|
|
26
|
+
"url": "https://github.com/sponsors/ng-primitives"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@angular/core": ">=19.0.0"
|
|
30
|
+
},
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"module": "fesm2022/ng-primitives-state.mjs",
|
|
33
|
+
"typings": "index.d.ts",
|
|
34
|
+
"exports": {
|
|
35
|
+
"./package.json": {
|
|
36
|
+
"default": "./package.json"
|
|
37
|
+
},
|
|
38
|
+
".": {
|
|
39
|
+
"types": "./index.d.ts",
|
|
40
|
+
"default": "./fesm2022/ng-primitives-state.mjs"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"tslib": "^2.3.0"
|
|
45
|
+
}
|
|
46
|
+
}
|