element-vir 23.4.2 → 25.0.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 +34 -31
- package/dist/declarative-element/declarative-element-init.d.ts +17 -15
- package/dist/declarative-element/declarative-element.d.ts +18 -21
- package/dist/declarative-element/declarative-element.js +3 -4
- package/dist/declarative-element/define-element-no-inputs.d.ts +1 -8
- package/dist/declarative-element/define-element-no-inputs.js +22 -17
- package/dist/declarative-element/define-element.d.ts +12 -6
- package/dist/declarative-element/define-element.js +8 -2
- package/dist/declarative-element/directives/assign.directive.d.ts +3 -3
- package/dist/declarative-element/directives/async-prop.d.ts +59 -12
- package/dist/declarative-element/directives/async-prop.js +71 -11
- package/dist/declarative-element/is-declarative-element-definition.js +3 -4
- package/dist/declarative-element/properties/element-properties.d.ts +0 -17
- package/dist/declarative-element/properties/host-classes.d.ts +2 -3
- package/dist/declarative-element/properties/property-proxy.js +1 -5
- package/dist/declarative-element/properties/styles.d.ts +3 -4
- package/dist/declarative-element/render-callback.d.ts +10 -11
- package/dist/declarative-element/wrap-define-element.d.ts +7 -8
- package/dist/declarative-element/wrap-define-element.js +2 -2
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -3
- package/dist/readme-examples/my-custom-define.d.ts +3 -3
- package/dist/readme-examples/my-with-async-prop.element.d.ts +3 -2
- package/dist/readme-examples/my-with-async-prop.element.js +9 -6
- package/dist/readme-examples/my-with-cleanup-callback.element.d.ts +1 -1
- package/dist/readme-examples/my-with-cleanup-callback.element.js +4 -10
- package/dist/readme-examples/my-with-event-listening.element.js +4 -2
- package/dist/readme-examples/my-with-host-class-definition.element.js +4 -2
- package/dist/readme-examples/my-with-update-state.element.js +9 -7
- package/package.json +11 -11
- package/dist/declarative-element/directives/is-resolved.directive.d.ts +0 -110
- package/dist/declarative-element/directives/is-resolved.directive.js +0 -127
- package/dist/declarative-element/properties/element-vir-state-setup.d.ts +0 -54
- package/dist/declarative-element/properties/element-vir-state-setup.js +0 -22
- package/dist/declarative-element/properties/per-instance.d.ts +0 -26
- package/dist/declarative-element/properties/per-instance.js +0 -32
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { AsyncProp, AsyncValue } from './async-prop.js';
|
|
2
|
-
/**
|
|
3
|
-
* Checks and type guards that the given async value is resolved, meaning that it is no longer a
|
|
4
|
-
* promise. It may be an error though. This must be passed an {@link AsyncProp}'s `.value` property,
|
|
5
|
-
* not the async prop itself.
|
|
6
|
-
*
|
|
7
|
-
* @category Async
|
|
8
|
-
* @example
|
|
9
|
-
*
|
|
10
|
-
* ```ts
|
|
11
|
-
* import {waitValue, extractErrorMessage} from '@augment-vir/common';
|
|
12
|
-
* import {isResolved, html, defineElementNoInputs, asyncProp} from 'element-vir';
|
|
13
|
-
*
|
|
14
|
-
* const MyElement = defineElementNoInputs({
|
|
15
|
-
* tagName: 'my-element',
|
|
16
|
-
* stateInitStatic: {
|
|
17
|
-
* myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
|
|
18
|
-
* },
|
|
19
|
-
* render({state}) {
|
|
20
|
-
* if (isAsyncError(state.myProp.value)) {
|
|
21
|
-
* return html`
|
|
22
|
-
* Error: ${extractErrorMessage(state.myProp.value)}
|
|
23
|
-
* `;
|
|
24
|
-
* } else if (isResolved(state.myProp.value)) {
|
|
25
|
-
* return html`
|
|
26
|
-
* Done!
|
|
27
|
-
* `;
|
|
28
|
-
* } else {
|
|
29
|
-
* return html`
|
|
30
|
-
* Still waiting...
|
|
31
|
-
* `;
|
|
32
|
-
* }
|
|
33
|
-
* },
|
|
34
|
-
* });
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
export declare function isResolved<Value extends AsyncValue<any>>(asyncValue: Value extends AsyncProp<any, any> ? 'Error: pass AsyncProp.value, not AsyncProp itself.' : Value): asyncValue is Exclude<typeof asyncValue, Promise<any>>;
|
|
38
|
-
/**
|
|
39
|
-
* Checks and type guards that the given async value is an error, meaning that the async prop's
|
|
40
|
-
* promise was rejected. This must be passed an {@link AsyncProp}'s `.value` property, not the async
|
|
41
|
-
* prop itself.
|
|
42
|
-
*
|
|
43
|
-
* @category Async
|
|
44
|
-
* @example
|
|
45
|
-
*
|
|
46
|
-
* ```ts
|
|
47
|
-
* import {waitValue, extractErrorMessage} from '@augment-vir/common';
|
|
48
|
-
* import {isResolved, html, defineElementNoInputs, asyncProp} from 'element-vir';
|
|
49
|
-
*
|
|
50
|
-
* const MyElement = defineElementNoInputs({
|
|
51
|
-
* tagName: 'my-element',
|
|
52
|
-
* stateInitStatic: {
|
|
53
|
-
* myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
|
|
54
|
-
* },
|
|
55
|
-
* render({state}) {
|
|
56
|
-
* if (isAsyncError(state.myProp.value)) {
|
|
57
|
-
* return html`
|
|
58
|
-
* Error: ${extractErrorMessage(state.myProp.value)}
|
|
59
|
-
* `;
|
|
60
|
-
* } else if (isResolved(state.myProp.value)) {
|
|
61
|
-
* return html`
|
|
62
|
-
* Done!
|
|
63
|
-
* `;
|
|
64
|
-
* } else {
|
|
65
|
-
* return html`
|
|
66
|
-
* Still waiting...
|
|
67
|
-
* `;
|
|
68
|
-
* }
|
|
69
|
-
* },
|
|
70
|
-
* });
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
|
-
export declare function isAsyncError<Value extends AsyncValue<any>>(asyncValue: Value extends AsyncProp<any, any> ? 'Error: pass AsyncProp.value, not AsyncProp itself.' : Value): asyncValue is Extract<typeof asyncValue, Error>;
|
|
74
|
-
/**
|
|
75
|
-
* Same as {@link isResolved} but instead of a returning a boolean, it returns either the resolved
|
|
76
|
-
* value or `undefined`.
|
|
77
|
-
*
|
|
78
|
-
* @category Async
|
|
79
|
-
* @example
|
|
80
|
-
*
|
|
81
|
-
* ```ts
|
|
82
|
-
* import {waitValue, extractErrorMessage} from '@augment-vir/common';
|
|
83
|
-
* import {isResolved, html, defineElementNoInputs, asyncProp} from 'element-vir';
|
|
84
|
-
*
|
|
85
|
-
* const MyElement = defineElementNoInputs({
|
|
86
|
-
* tagName: 'my-element',
|
|
87
|
-
* stateInitStatic: {
|
|
88
|
-
* myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
|
|
89
|
-
* },
|
|
90
|
-
* render({state}) {
|
|
91
|
-
* const resolvedValue = resolvedOrUndefined(state.myProp.value);
|
|
92
|
-
*
|
|
93
|
-
* if (!resolvedValue) {
|
|
94
|
-
* return html`
|
|
95
|
-
* Still waiting...
|
|
96
|
-
* `;
|
|
97
|
-
* } else if (isAsyncError(resolvedValue)) {
|
|
98
|
-
* return html`
|
|
99
|
-
* Error: ${extractErrorMessage(state.myProp.value)}
|
|
100
|
-
* `;
|
|
101
|
-
* }
|
|
102
|
-
*
|
|
103
|
-
* return html`
|
|
104
|
-
* Value: ${resolvedValue}
|
|
105
|
-
* `;
|
|
106
|
-
* },
|
|
107
|
-
* });
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
export declare function resolvedOrUndefined<Value extends AsyncValue<any>>(asyncValue: Value extends AsyncProp<any, any> ? 'Error: pass AsyncProp.value, not AsyncProp itself.' : Value): Exclude<typeof asyncValue, Promise<any>> | undefined;
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { AsyncProp } from './async-prop.js';
|
|
2
|
-
/**
|
|
3
|
-
* Checks and type guards that the given async value is resolved, meaning that it is no longer a
|
|
4
|
-
* promise. It may be an error though. This must be passed an {@link AsyncProp}'s `.value` property,
|
|
5
|
-
* not the async prop itself.
|
|
6
|
-
*
|
|
7
|
-
* @category Async
|
|
8
|
-
* @example
|
|
9
|
-
*
|
|
10
|
-
* ```ts
|
|
11
|
-
* import {waitValue, extractErrorMessage} from '@augment-vir/common';
|
|
12
|
-
* import {isResolved, html, defineElementNoInputs, asyncProp} from 'element-vir';
|
|
13
|
-
*
|
|
14
|
-
* const MyElement = defineElementNoInputs({
|
|
15
|
-
* tagName: 'my-element',
|
|
16
|
-
* stateInitStatic: {
|
|
17
|
-
* myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
|
|
18
|
-
* },
|
|
19
|
-
* render({state}) {
|
|
20
|
-
* if (isAsyncError(state.myProp.value)) {
|
|
21
|
-
* return html`
|
|
22
|
-
* Error: ${extractErrorMessage(state.myProp.value)}
|
|
23
|
-
* `;
|
|
24
|
-
* } else if (isResolved(state.myProp.value)) {
|
|
25
|
-
* return html`
|
|
26
|
-
* Done!
|
|
27
|
-
* `;
|
|
28
|
-
* } else {
|
|
29
|
-
* return html`
|
|
30
|
-
* Still waiting...
|
|
31
|
-
* `;
|
|
32
|
-
* }
|
|
33
|
-
* },
|
|
34
|
-
* });
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
export function isResolved(asyncValue) {
|
|
38
|
-
if (asyncValue instanceof AsyncProp) {
|
|
39
|
-
throw new TypeError('Pass AsyncProp.value, not AsyncProp itself.');
|
|
40
|
-
}
|
|
41
|
-
return !(asyncValue instanceof Promise);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Checks and type guards that the given async value is an error, meaning that the async prop's
|
|
45
|
-
* promise was rejected. This must be passed an {@link AsyncProp}'s `.value` property, not the async
|
|
46
|
-
* prop itself.
|
|
47
|
-
*
|
|
48
|
-
* @category Async
|
|
49
|
-
* @example
|
|
50
|
-
*
|
|
51
|
-
* ```ts
|
|
52
|
-
* import {waitValue, extractErrorMessage} from '@augment-vir/common';
|
|
53
|
-
* import {isResolved, html, defineElementNoInputs, asyncProp} from 'element-vir';
|
|
54
|
-
*
|
|
55
|
-
* const MyElement = defineElementNoInputs({
|
|
56
|
-
* tagName: 'my-element',
|
|
57
|
-
* stateInitStatic: {
|
|
58
|
-
* myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
|
|
59
|
-
* },
|
|
60
|
-
* render({state}) {
|
|
61
|
-
* if (isAsyncError(state.myProp.value)) {
|
|
62
|
-
* return html`
|
|
63
|
-
* Error: ${extractErrorMessage(state.myProp.value)}
|
|
64
|
-
* `;
|
|
65
|
-
* } else if (isResolved(state.myProp.value)) {
|
|
66
|
-
* return html`
|
|
67
|
-
* Done!
|
|
68
|
-
* `;
|
|
69
|
-
* } else {
|
|
70
|
-
* return html`
|
|
71
|
-
* Still waiting...
|
|
72
|
-
* `;
|
|
73
|
-
* }
|
|
74
|
-
* },
|
|
75
|
-
* });
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
export function isAsyncError(asyncValue) {
|
|
79
|
-
if (asyncValue instanceof AsyncProp) {
|
|
80
|
-
throw new TypeError('Pass AsyncProp.value, not AsyncProp itself.');
|
|
81
|
-
}
|
|
82
|
-
return asyncValue instanceof Error;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Same as {@link isResolved} but instead of a returning a boolean, it returns either the resolved
|
|
86
|
-
* value or `undefined`.
|
|
87
|
-
*
|
|
88
|
-
* @category Async
|
|
89
|
-
* @example
|
|
90
|
-
*
|
|
91
|
-
* ```ts
|
|
92
|
-
* import {waitValue, extractErrorMessage} from '@augment-vir/common';
|
|
93
|
-
* import {isResolved, html, defineElementNoInputs, asyncProp} from 'element-vir';
|
|
94
|
-
*
|
|
95
|
-
* const MyElement = defineElementNoInputs({
|
|
96
|
-
* tagName: 'my-element',
|
|
97
|
-
* stateInitStatic: {
|
|
98
|
-
* myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
|
|
99
|
-
* },
|
|
100
|
-
* render({state}) {
|
|
101
|
-
* const resolvedValue = resolvedOrUndefined(state.myProp.value);
|
|
102
|
-
*
|
|
103
|
-
* if (!resolvedValue) {
|
|
104
|
-
* return html`
|
|
105
|
-
* Still waiting...
|
|
106
|
-
* `;
|
|
107
|
-
* } else if (isAsyncError(resolvedValue)) {
|
|
108
|
-
* return html`
|
|
109
|
-
* Error: ${extractErrorMessage(state.myProp.value)}
|
|
110
|
-
* `;
|
|
111
|
-
* }
|
|
112
|
-
*
|
|
113
|
-
* return html`
|
|
114
|
-
* Value: ${resolvedValue}
|
|
115
|
-
* `;
|
|
116
|
-
* },
|
|
117
|
-
* });
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
export function resolvedOrUndefined(asyncValue) {
|
|
121
|
-
if (isResolved(asyncValue)) {
|
|
122
|
-
return asyncValue;
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
return undefined;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { PropertyInitMapBase } from './element-properties.js';
|
|
2
|
-
/**
|
|
3
|
-
* Use this key with a function value on any object set in `stateInitStatic` to, rather than using
|
|
4
|
-
* the object itself, call the function during each element's initialization and use the output of
|
|
5
|
-
* that function rather than the object itself.
|
|
6
|
-
*
|
|
7
|
-
* See `perInstance` for an example usage.
|
|
8
|
-
*
|
|
9
|
-
* @category Util
|
|
10
|
-
*/
|
|
11
|
-
export declare const stateSetupKey: unique symbol;
|
|
12
|
-
/**
|
|
13
|
-
* The type for an object that uses {@link stateSetupKey}.
|
|
14
|
-
*
|
|
15
|
-
* @category Internal
|
|
16
|
-
*/
|
|
17
|
-
export type ElementVirStateSetup<InnerValue> = {
|
|
18
|
-
[key in typeof stateSetupKey]: () => InnerValue;
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Check if the given value is a value {@link ElementVirStateSetup} implementation.
|
|
22
|
-
*
|
|
23
|
-
* @category Internal
|
|
24
|
-
*/
|
|
25
|
-
export declare function isElementVirStateSetup<T = unknown>(input: unknown): input is ElementVirStateSetup<T>;
|
|
26
|
-
/**
|
|
27
|
-
* If the type parameter is an instance of {@link ElementVirStateSetup}, this unwraps that instance
|
|
28
|
-
* to its type parameter. Otherwise, the original type parameter is used.
|
|
29
|
-
*
|
|
30
|
-
* @category Internal
|
|
31
|
-
*/
|
|
32
|
-
export type UnwrapElementVirStateSetup<T> = T extends ElementVirStateSetup<infer U> ? U : T;
|
|
33
|
-
/**
|
|
34
|
-
* Allow wrapped state setup objects.
|
|
35
|
-
*
|
|
36
|
-
* @category Internal
|
|
37
|
-
*/
|
|
38
|
-
export type MaybeElementVirStateSetup<T> = UnwrapElementVirStateSetup<T> | ElementVirStateSetup<UnwrapElementVirStateSetup<T>>;
|
|
39
|
-
/**
|
|
40
|
-
* Unwraps all {@link ElementVirStateSetup} properties in the given state init object type parameter.
|
|
41
|
-
*
|
|
42
|
-
* @category Internal
|
|
43
|
-
*/
|
|
44
|
-
export type FlattenElementVirStateSetup<StateInit extends PropertyInitMapBase> = {
|
|
45
|
-
[Prop in keyof StateInit]: Extract<StateInit[Prop], ElementVirStateSetup<any>> extends never ? StateInit[Prop] : Extract<StateInit[Prop], ElementVirStateSetup<any>> extends ElementVirStateSetup<infer InnerValue> ? InnerValue | Exclude<StateInit[Prop], ElementVirStateSetup<any>> : StateInit[Prop];
|
|
46
|
-
};
|
|
47
|
-
/**
|
|
48
|
-
* Allows setting stat with {@link ElementVirStateSetup} properties.
|
|
49
|
-
*
|
|
50
|
-
* @category Internal
|
|
51
|
-
*/
|
|
52
|
-
export type AllowElementVirStateSetup<OriginalObject extends PropertyInitMapBase> = {
|
|
53
|
-
[Prop in keyof OriginalObject]: MaybeElementVirStateSetup<OriginalObject[Prop]>;
|
|
54
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { check } from '@augment-vir/assert';
|
|
2
|
-
/**
|
|
3
|
-
* Use this key with a function value on any object set in `stateInitStatic` to, rather than using
|
|
4
|
-
* the object itself, call the function during each element's initialization and use the output of
|
|
5
|
-
* that function rather than the object itself.
|
|
6
|
-
*
|
|
7
|
-
* See `perInstance` for an example usage.
|
|
8
|
-
*
|
|
9
|
-
* @category Util
|
|
10
|
-
*/
|
|
11
|
-
export const stateSetupKey = Symbol('element-vir-state-setup');
|
|
12
|
-
/**
|
|
13
|
-
* Check if the given value is a value {@link ElementVirStateSetup} implementation.
|
|
14
|
-
*
|
|
15
|
-
* @category Internal
|
|
16
|
-
*/
|
|
17
|
-
export function isElementVirStateSetup(input) {
|
|
18
|
-
if (!check.isObject(input)) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
return stateSetupKey in input && check.isFunction(input[stateSetupKey]);
|
|
22
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* For use within `stateInitStatic`: use this as the value of a state prop to call the given
|
|
3
|
-
* callback on each element's creation rather than creating a static state init value.
|
|
4
|
-
*
|
|
5
|
-
* @category Directives
|
|
6
|
-
* @example
|
|
7
|
-
*
|
|
8
|
-
* ```ts
|
|
9
|
-
* import {waitValue, extractErrorMessage} from '@augment-vir/common';
|
|
10
|
-
* import {isResolved, html, defineElementNoInputs, perInstance} from 'element-vir';
|
|
11
|
-
*
|
|
12
|
-
* const MyElement = defineElementNoInputs({
|
|
13
|
-
* tagName: 'my-element',
|
|
14
|
-
* stateInitStatic: {
|
|
15
|
-
* // each instance of `MyElement` will have a different `randomValue`
|
|
16
|
-
* randomValue: perInstance(() => Math.random()),
|
|
17
|
-
* },
|
|
18
|
-
* render({state}) {
|
|
19
|
-
* return html`
|
|
20
|
-
* Value: ${state.randomValue}
|
|
21
|
-
* `;
|
|
22
|
-
* },
|
|
23
|
-
* });
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
export declare function perInstance<T>(creationCallback: () => T): T;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { stateSetupKey } from './element-vir-state-setup.js';
|
|
2
|
-
/**
|
|
3
|
-
* For use within `stateInitStatic`: use this as the value of a state prop to call the given
|
|
4
|
-
* callback on each element's creation rather than creating a static state init value.
|
|
5
|
-
*
|
|
6
|
-
* @category Directives
|
|
7
|
-
* @example
|
|
8
|
-
*
|
|
9
|
-
* ```ts
|
|
10
|
-
* import {waitValue, extractErrorMessage} from '@augment-vir/common';
|
|
11
|
-
* import {isResolved, html, defineElementNoInputs, perInstance} from 'element-vir';
|
|
12
|
-
*
|
|
13
|
-
* const MyElement = defineElementNoInputs({
|
|
14
|
-
* tagName: 'my-element',
|
|
15
|
-
* stateInitStatic: {
|
|
16
|
-
* // each instance of `MyElement` will have a different `randomValue`
|
|
17
|
-
* randomValue: perInstance(() => Math.random()),
|
|
18
|
-
* },
|
|
19
|
-
* render({state}) {
|
|
20
|
-
* return html`
|
|
21
|
-
* Value: ${state.randomValue}
|
|
22
|
-
* `;
|
|
23
|
-
* },
|
|
24
|
-
* });
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
export function perInstance(creationCallback) {
|
|
28
|
-
const stateSetup = {
|
|
29
|
-
[stateSetupKey]: creationCallback,
|
|
30
|
-
};
|
|
31
|
-
return stateSetup;
|
|
32
|
-
}
|