assign-gingerly 0.0.48 → 0.0.50
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 +42 -3
- package/defineWithFeatures.js +118 -0
- package/defineWithFeatures.ts +160 -0
- package/index.js +1 -0
- package/index.ts +1 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -4792,11 +4792,25 @@ customElements.define('my-element', MyElement);
|
|
|
4792
4792
|
**How it works:**
|
|
4793
4793
|
|
|
4794
4794
|
- `assignFeatures` checks if the spawn class defines `static onAssigned` (via `Object.hasOwn`).
|
|
4795
|
-
- If found, calls `SpawnClass.onAssigned(ctr, featureConfig)` after installing the getter.
|
|
4796
|
-
- If `onAssigned` returns a Promise,
|
|
4797
|
-
-
|
|
4795
|
+
- If found, calls `SpawnClass.onAssigned(ctr, featureConfig, key)` after installing the getter.
|
|
4796
|
+
- If `onAssigned` returns a Promise, it is **awaited sequentially** before processing the next feature. This guarantees that features declared earlier complete their setup before later features run.
|
|
4797
|
+
- This sequential ordering enables inter-feature communication: Feature A can post configuration (via `suggestFeatureInfo`) that Feature B reads in its own `onAssigned`.
|
|
4798
|
+
- If no features have `onAssigned`, `assignFeatures` runs synchronously and returns `undefined` (backward compatible).
|
|
4798
4799
|
- Only applies to synchronous spawners (the class must be available at registration time). Async spawners can't define `onAssigned` since the class isn't loaded yet.
|
|
4799
4800
|
|
|
4801
|
+
**Sequential ordering guarantee:**
|
|
4802
|
+
|
|
4803
|
+
```JavaScript
|
|
4804
|
+
await customElements.assignFeatures(MyElement, {
|
|
4805
|
+
featureA: { spawn: FeatureA }, // FeatureA.onAssigned runs first, completes
|
|
4806
|
+
featureB: { spawn: FeatureB } // FeatureB.onAssigned runs second, can read A's output
|
|
4807
|
+
});
|
|
4808
|
+
```
|
|
4809
|
+
|
|
4810
|
+
Features are processed in declaration order. If Feature A's `onAssigned` is async, it fully completes before Feature B's `onAssigned` starts. This makes it safe for features to communicate via `suggestFeatureInfo` / `getFeatureInfoSuggestions`.
|
|
4811
|
+
|
|
4812
|
+
For full documentation on inter-feature communication, see [docs/inter-feature-communication.md](docs/inter-feature-communication.md).
|
|
4813
|
+
|
|
4800
4814
|
**`await` is always safe:**
|
|
4801
4815
|
|
|
4802
4816
|
```JavaScript
|
|
@@ -4806,6 +4820,31 @@ await customElements.assignFeatures(MyElement, { feature: { spawn: SyncFeature }
|
|
|
4806
4820
|
// Both work — await on undefined is a no-op
|
|
4807
4821
|
```
|
|
4808
4822
|
|
|
4823
|
+
### Declarative element definition with `defineWithFeatures`
|
|
4824
|
+
|
|
4825
|
+
`defineWithFeatures` enables defining custom elements from JSON-serializable configuration — no class authoring needed for derived elements:
|
|
4826
|
+
|
|
4827
|
+
```JavaScript
|
|
4828
|
+
import { defineWithFeatures } from 'assign-gingerly/defineWithFeatures.js';
|
|
4829
|
+
|
|
4830
|
+
await defineWithFeatures('time-ticker', 'el-maker', {
|
|
4831
|
+
assignFeatures: {
|
|
4832
|
+
roundabout: {
|
|
4833
|
+
customData: { template: myTemplate },
|
|
4834
|
+
withAttrs: { base: 'ra', mode: '${base}-mode' },
|
|
4835
|
+
callbackForwarding: ['connectedCallback']
|
|
4836
|
+
},
|
|
4837
|
+
truthSourcer: {
|
|
4838
|
+
callbackForwarding: ['connectedCallback', 'attributeChangedCallback']
|
|
4839
|
+
}
|
|
4840
|
+
}
|
|
4841
|
+
});
|
|
4842
|
+
```
|
|
4843
|
+
|
|
4844
|
+
It resolves async `fallbackSpawn` implementations from the base class, creates a subclass, wires up features, and defines the element. Designed for use with [mount-observer cede scripts](https://github.com/bahrus/mount-observer#custom-element-definition-cede-scripts) but works standalone.
|
|
4845
|
+
|
|
4846
|
+
For full documentation, see [docs/defineWithFeatures.md](docs/defineWithFeatures.md).
|
|
4847
|
+
|
|
4809
4848
|
<details>
|
|
4810
4849
|
<summary>Catalog of Published Custom Element Features</summary>
|
|
4811
4850
|
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* defineWithFeatures - Declaratively define a custom element with features from JSON config.
|
|
3
|
+
*
|
|
4
|
+
* Resolves async fallback spawns from the base class's `static supportedFeatures`,
|
|
5
|
+
* creates a subclass, registers features with resolved spawns + JSON config,
|
|
6
|
+
* and defines the custom element.
|
|
7
|
+
*
|
|
8
|
+
* Designed to support cede scripts and other declarative custom element definition patterns.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* await defineWithFeatures('time-ticker', 'el-maker', {
|
|
12
|
+
* assignFeatures: {
|
|
13
|
+
* timeTicker: {},
|
|
14
|
+
* roundabout: {
|
|
15
|
+
* customData: {...},
|
|
16
|
+
* withAttrs: {...},
|
|
17
|
+
* callbackForwarding: ['connectedCallback']
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
* });
|
|
21
|
+
*/
|
|
22
|
+
import { assignFeatures } from './assignFeatures.js';
|
|
23
|
+
/**
|
|
24
|
+
* Determines if a function is an async spawner (same heuristic as assignFeatures).
|
|
25
|
+
*/
|
|
26
|
+
function isAsyncSpawn(fn) {
|
|
27
|
+
if (typeof fn !== 'function')
|
|
28
|
+
return false;
|
|
29
|
+
if (fn.constructor.name === 'AsyncFunction')
|
|
30
|
+
return true;
|
|
31
|
+
if (fn.prototype === undefined)
|
|
32
|
+
return true;
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Cache for resolved fallback spawns.
|
|
37
|
+
* Key: BaseClass, Value: Map<featureKey, resolvedConstructor>
|
|
38
|
+
*/
|
|
39
|
+
const resolvedSpawnCache = new WeakMap();
|
|
40
|
+
/**
|
|
41
|
+
* Declaratively define a custom element with features.
|
|
42
|
+
*
|
|
43
|
+
* 1. Waits for the base class to be defined (if not already).
|
|
44
|
+
* 2. Resolves all async fallback spawns from `static supportedFeatures`.
|
|
45
|
+
* 3. Creates a subclass extending the base class.
|
|
46
|
+
* 4. Calls `assignFeatures` with resolved spawns + the JSON config.
|
|
47
|
+
* 5. Defines the new custom element in the registry.
|
|
48
|
+
*
|
|
49
|
+
* @param tagName - The custom element tag name to define (e.g., 'time-ticker')
|
|
50
|
+
* @param baseTagName - The tag name of the base class to extend (e.g., 'el-maker')
|
|
51
|
+
* @param config - JSON-serializable configuration specifying which features to activate
|
|
52
|
+
* @param registry - Optional custom element registry (defaults to global `customElements`)
|
|
53
|
+
* @returns The newly created and defined custom element class
|
|
54
|
+
*/
|
|
55
|
+
export async function defineWithFeatures(tagName, baseTagName, config, registry, options) {
|
|
56
|
+
const reg = registry || customElements;
|
|
57
|
+
// 1. Resolve base class — wait for it if not yet defined
|
|
58
|
+
let BaseClass = reg.get(baseTagName);
|
|
59
|
+
if (!BaseClass) {
|
|
60
|
+
await reg.whenDefined(baseTagName);
|
|
61
|
+
BaseClass = reg.get(baseTagName);
|
|
62
|
+
}
|
|
63
|
+
if (!BaseClass) {
|
|
64
|
+
throw new Error(`defineWithFeatures: base class "${baseTagName}" could not be resolved`);
|
|
65
|
+
}
|
|
66
|
+
const supportedFeatures = BaseClass.supportedFeatures;
|
|
67
|
+
if (!supportedFeatures) {
|
|
68
|
+
throw new Error(`defineWithFeatures: "${baseTagName}" does not define static supportedFeatures`);
|
|
69
|
+
}
|
|
70
|
+
// 2. Resolve all async fallback spawns (with caching)
|
|
71
|
+
let classCache = resolvedSpawnCache.get(BaseClass);
|
|
72
|
+
if (!classCache) {
|
|
73
|
+
classCache = new Map();
|
|
74
|
+
resolvedSpawnCache.set(BaseClass, classCache);
|
|
75
|
+
}
|
|
76
|
+
const featureKeys = Object.keys(config.assignFeatures);
|
|
77
|
+
const resolvedSpawns = new Map();
|
|
78
|
+
await Promise.all(featureKeys.map(async (key) => {
|
|
79
|
+
const optIn = supportedFeatures[key];
|
|
80
|
+
if (!optIn) {
|
|
81
|
+
throw new Error(`defineWithFeatures: feature "${key}" not found in ${baseTagName}.supportedFeatures`);
|
|
82
|
+
}
|
|
83
|
+
// Check cache first
|
|
84
|
+
if (classCache.has(key)) {
|
|
85
|
+
resolvedSpawns.set(key, classCache.get(key));
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
let spawn = optIn.fallbackSpawn;
|
|
89
|
+
if (spawn && isAsyncSpawn(spawn)) {
|
|
90
|
+
// Resolve the async spawner
|
|
91
|
+
spawn = await spawn();
|
|
92
|
+
}
|
|
93
|
+
// Cache the resolved spawn
|
|
94
|
+
if (spawn) {
|
|
95
|
+
classCache.set(key, spawn);
|
|
96
|
+
}
|
|
97
|
+
resolvedSpawns.set(key, spawn);
|
|
98
|
+
}));
|
|
99
|
+
// 3. Create subclass
|
|
100
|
+
const NewClass = class extends BaseClass {
|
|
101
|
+
};
|
|
102
|
+
// 3b. Call onSubclassCreated callback (before define, before features if needed)
|
|
103
|
+
if (options?.onSubclassCreated) {
|
|
104
|
+
options.onSubclassCreated(NewClass);
|
|
105
|
+
}
|
|
106
|
+
// 4. Build FeatureConfigsMap: resolved spawns + JSON config
|
|
107
|
+
const featuresMap = {};
|
|
108
|
+
for (const [key, jsonConfig] of Object.entries(config.assignFeatures)) {
|
|
109
|
+
featuresMap[key] = {
|
|
110
|
+
spawn: resolvedSpawns.get(key),
|
|
111
|
+
...jsonConfig
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
// 5. assignFeatures (sequential onAssigned) + define
|
|
115
|
+
await assignFeatures(NewClass, featuresMap, reg.featuresRegistry);
|
|
116
|
+
reg.define(tagName, NewClass);
|
|
117
|
+
return NewClass;
|
|
118
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* defineWithFeatures - Declaratively define a custom element with features from JSON config.
|
|
3
|
+
*
|
|
4
|
+
* Resolves async fallback spawns from the base class's `static supportedFeatures`,
|
|
5
|
+
* creates a subclass, registers features with resolved spawns + JSON config,
|
|
6
|
+
* and defines the custom element.
|
|
7
|
+
*
|
|
8
|
+
* Designed to support cede scripts and other declarative custom element definition patterns.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* await defineWithFeatures('time-ticker', 'el-maker', {
|
|
12
|
+
* assignFeatures: {
|
|
13
|
+
* timeTicker: {},
|
|
14
|
+
* roundabout: {
|
|
15
|
+
* customData: {...},
|
|
16
|
+
* withAttrs: {...},
|
|
17
|
+
* callbackForwarding: ['connectedCallback']
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
* });
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { assignFeatures, FeatureConfigsMap, SupportedFeaturesMap } from './assignFeatures.js';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Configuration passed to defineWithFeatures (JSON-serializable).
|
|
27
|
+
*/
|
|
28
|
+
export interface DefineWithFeaturesConfig {
|
|
29
|
+
assignFeatures: Record<string, {
|
|
30
|
+
customData?: any;
|
|
31
|
+
withAttrs?: any;
|
|
32
|
+
callbackForwarding?: string[];
|
|
33
|
+
}>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Options for defineWithFeatures.
|
|
38
|
+
*/
|
|
39
|
+
export interface DefineWithFeaturesOptions {
|
|
40
|
+
/** Called after the subclass is created but before registry.define(). */
|
|
41
|
+
onSubclassCreated?: (NewCtr: Function) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Determines if a function is an async spawner (same heuristic as assignFeatures).
|
|
46
|
+
*/
|
|
47
|
+
function isAsyncSpawn(fn: any): boolean {
|
|
48
|
+
if (typeof fn !== 'function') return false;
|
|
49
|
+
if (fn.constructor.name === 'AsyncFunction') return true;
|
|
50
|
+
if (fn.prototype === undefined) return true;
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Cache for resolved fallback spawns.
|
|
56
|
+
* Key: BaseClass, Value: Map<featureKey, resolvedConstructor>
|
|
57
|
+
*/
|
|
58
|
+
const resolvedSpawnCache = new WeakMap<Function, Map<string, any>>();
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Declaratively define a custom element with features.
|
|
62
|
+
*
|
|
63
|
+
* 1. Waits for the base class to be defined (if not already).
|
|
64
|
+
* 2. Resolves all async fallback spawns from `static supportedFeatures`.
|
|
65
|
+
* 3. Creates a subclass extending the base class.
|
|
66
|
+
* 4. Calls `assignFeatures` with resolved spawns + the JSON config.
|
|
67
|
+
* 5. Defines the new custom element in the registry.
|
|
68
|
+
*
|
|
69
|
+
* @param tagName - The custom element tag name to define (e.g., 'time-ticker')
|
|
70
|
+
* @param baseTagName - The tag name of the base class to extend (e.g., 'el-maker')
|
|
71
|
+
* @param config - JSON-serializable configuration specifying which features to activate
|
|
72
|
+
* @param registry - Optional custom element registry (defaults to global `customElements`)
|
|
73
|
+
* @returns The newly created and defined custom element class
|
|
74
|
+
*/
|
|
75
|
+
export async function defineWithFeatures(
|
|
76
|
+
tagName: string,
|
|
77
|
+
baseTagName: string,
|
|
78
|
+
config: DefineWithFeaturesConfig,
|
|
79
|
+
registry?: CustomElementRegistry,
|
|
80
|
+
options?: DefineWithFeaturesOptions
|
|
81
|
+
): Promise<Function> {
|
|
82
|
+
const reg = registry || customElements;
|
|
83
|
+
|
|
84
|
+
// 1. Resolve base class — wait for it if not yet defined
|
|
85
|
+
let BaseClass = (reg as any).get(baseTagName);
|
|
86
|
+
if (!BaseClass) {
|
|
87
|
+
await (reg as any).whenDefined(baseTagName);
|
|
88
|
+
BaseClass = (reg as any).get(baseTagName);
|
|
89
|
+
}
|
|
90
|
+
if (!BaseClass) {
|
|
91
|
+
throw new Error(`defineWithFeatures: base class "${baseTagName}" could not be resolved`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const supportedFeatures: SupportedFeaturesMap | undefined = BaseClass.supportedFeatures;
|
|
95
|
+
if (!supportedFeatures) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
`defineWithFeatures: "${baseTagName}" does not define static supportedFeatures`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 2. Resolve all async fallback spawns (with caching)
|
|
102
|
+
let classCache = resolvedSpawnCache.get(BaseClass);
|
|
103
|
+
if (!classCache) {
|
|
104
|
+
classCache = new Map();
|
|
105
|
+
resolvedSpawnCache.set(BaseClass, classCache);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const featureKeys = Object.keys(config.assignFeatures);
|
|
109
|
+
const resolvedSpawns = new Map<string, any>();
|
|
110
|
+
|
|
111
|
+
await Promise.all(featureKeys.map(async (key) => {
|
|
112
|
+
const optIn = supportedFeatures[key];
|
|
113
|
+
if (!optIn) {
|
|
114
|
+
throw new Error(
|
|
115
|
+
`defineWithFeatures: feature "${key}" not found in ${baseTagName}.supportedFeatures`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Check cache first
|
|
120
|
+
if (classCache!.has(key)) {
|
|
121
|
+
resolvedSpawns.set(key, classCache!.get(key));
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let spawn = optIn.fallbackSpawn;
|
|
126
|
+
if (spawn && isAsyncSpawn(spawn)) {
|
|
127
|
+
// Resolve the async spawner
|
|
128
|
+
spawn = await (spawn as () => Promise<any>)();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Cache the resolved spawn
|
|
132
|
+
if (spawn) {
|
|
133
|
+
classCache!.set(key, spawn);
|
|
134
|
+
}
|
|
135
|
+
resolvedSpawns.set(key, spawn);
|
|
136
|
+
}));
|
|
137
|
+
|
|
138
|
+
// 3. Create subclass
|
|
139
|
+
const NewClass = class extends (BaseClass as any) {};
|
|
140
|
+
|
|
141
|
+
// 3b. Call onSubclassCreated callback (before define, before features if needed)
|
|
142
|
+
if (options?.onSubclassCreated) {
|
|
143
|
+
options.onSubclassCreated(NewClass);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 4. Build FeatureConfigsMap: resolved spawns + JSON config
|
|
147
|
+
const featuresMap: FeatureConfigsMap = {};
|
|
148
|
+
for (const [key, jsonConfig] of Object.entries(config.assignFeatures)) {
|
|
149
|
+
featuresMap[key] = {
|
|
150
|
+
spawn: resolvedSpawns.get(key),
|
|
151
|
+
...jsonConfig
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 5. assignFeatures (sequential onAssigned) + define
|
|
156
|
+
await assignFeatures(NewClass, featuresMap, (reg as any).featuresRegistry);
|
|
157
|
+
(reg as any).define(tagName, NewClass);
|
|
158
|
+
|
|
159
|
+
return NewClass;
|
|
160
|
+
}
|
package/index.js
CHANGED
|
@@ -11,4 +11,5 @@ export { resolveValues, resolveValue } from './resolveValues.js';
|
|
|
11
11
|
export { assignFrom } from './assignFrom.js';
|
|
12
12
|
export { assignFeatures, FeaturesRegistry, captureFeatureInitVals, PropertyBag, suggestFeatureInfo, getFeatureInfoSuggestions } from './assignFeatures.js';
|
|
13
13
|
export { installForwarding } from './installForwarding.js';
|
|
14
|
+
export { defineWithFeatures } from './defineWithFeatures.js';
|
|
14
15
|
import './object-extension.js';
|
package/index.ts
CHANGED
|
@@ -11,4 +11,5 @@ export {resolveValues, resolveValue} from './resolveValues.js';
|
|
|
11
11
|
export {assignFrom} from './assignFrom.js';
|
|
12
12
|
export {assignFeatures, FeaturesRegistry, captureFeatureInitVals, PropertyBag, suggestFeatureInfo, getFeatureInfoSuggestions} from './assignFeatures.js';
|
|
13
13
|
export {installForwarding} from './installForwarding.js';
|
|
14
|
+
export {defineWithFeatures} from './defineWithFeatures.js';
|
|
14
15
|
import './object-extension.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assign-gingerly",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.50",
|
|
4
4
|
"description": "This package provides a utility function for carefully merging one object into another.",
|
|
5
5
|
"homepage": "https://github.com/bahrus/assign-gingerly#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -64,6 +64,10 @@
|
|
|
64
64
|
"default": "./installForwarding.js",
|
|
65
65
|
"types": "./installForwarding.ts"
|
|
66
66
|
},
|
|
67
|
+
"./defineWithFeatures.js": {
|
|
68
|
+
"default": "./defineWithFeatures.js",
|
|
69
|
+
"types": "./defineWithFeatures.ts"
|
|
70
|
+
},
|
|
67
71
|
"./assignFrom.js": {
|
|
68
72
|
"default": "./assignFrom.js",
|
|
69
73
|
"types": "./assignFrom.ts"
|