@studiometa/js-toolkit 3.7.0 → 3.8.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.
@@ -10,4 +10,5 @@ export * from './importWhenPrefersMotion.js';
10
10
  export * from './importWhenVisible.js';
11
11
  export * from './logTree.js';
12
12
  export * from './registerComponent.js';
13
+ export * from './registerComponents.js';
13
14
  export { type QueryOptions, queryComponent, queryComponentAll, closestComponent, } from './queryComponent.js';
package/helpers/index.js CHANGED
@@ -10,6 +10,7 @@ export * from "./importWhenPrefersMotion.js";
10
10
  export * from "./importWhenVisible.js";
11
11
  export * from "./logTree.js";
12
12
  export * from "./registerComponent.js";
13
+ export * from "./registerComponents.js";
13
14
  import {
14
15
  queryComponent,
15
16
  queryComponentAll,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/helpers/index.ts"],
4
- "sourcesContent": ["export * from './createApp.js';\nexport * from './defineFeatures.js';\nexport * from './getClosestParent.js';\nexport * from './getDirectChildren.js';\nexport * from './getInstanceFromElement.js';\nexport * from './importOnInteraction.js';\nexport * from './importOnMediaQuery.js';\nexport * from './importWhenIdle.js';\nexport * from './importWhenPrefersMotion.js';\nexport * from './importWhenVisible.js';\nexport * from './logTree.js';\nexport * from './registerComponent.js';\nexport {\n type QueryOptions,\n queryComponent,\n queryComponentAll,\n closestComponent,\n} from './queryComponent.js';\n"],
5
- "mappings": "AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,OACK;",
4
+ "sourcesContent": ["export * from './createApp.js';\nexport * from './defineFeatures.js';\nexport * from './getClosestParent.js';\nexport * from './getDirectChildren.js';\nexport * from './getInstanceFromElement.js';\nexport * from './importOnInteraction.js';\nexport * from './importOnMediaQuery.js';\nexport * from './importWhenIdle.js';\nexport * from './importWhenPrefersMotion.js';\nexport * from './importWhenVisible.js';\nexport * from './logTree.js';\nexport * from './registerComponent.js';\nexport * from './registerComponents.js';\nexport {\n type QueryOptions,\n queryComponent,\n queryComponentAll,\n closestComponent,\n} from './queryComponent.js';\n"],
5
+ "mappings": "AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,OACK;",
6
6
  "names": []
7
7
  }
@@ -1,10 +1,24 @@
1
1
  import { BaseConstructor } from '../Base/Base.js';
2
2
  /**
3
- * Register globally and mount a given component, or a promise resolving to a component.
3
+ * Register globally and mount a given component.
4
+ *
5
+ * The component can be provided in any of the forms also accepted for child
6
+ * components in `config.components`:
7
+ * - a `Base` constructor;
8
+ * - a promise resolving to a constructor or a module namespace (`import(...)`);
9
+ * - a factory function returning such a promise (`() => import(...)`).
10
+ *
11
+ * Instances are mounted independently: an element that fails to mount is
12
+ * skipped (and logged in development) instead of failing the whole call, so
13
+ * the resolved array contains every instance that mounted successfully.
4
14
  *
5
15
  * @link https://js-toolkit.studiometa.dev/api/helpers/registerComponent.html
6
- * @param ctor The component constructor or a promise resolving to it.
16
+ * @param ctor The component constructor, or a way to resolve it.
7
17
  * @param nameOrSelector The name or selector to used for this component.
8
18
  * @return A promise resolving to an array of mounted component instances.
9
19
  */
10
- export declare function registerComponent<T extends BaseConstructor = BaseConstructor>(ctor: T | Promise<T>, nameOrSelector?: string): Promise<Awaited<InstanceType<T>>[]>;
20
+ export declare function registerComponent<T extends BaseConstructor = BaseConstructor>(ctor: T | Promise<T | {
21
+ default: T;
22
+ }> | (() => Promise<T | {
23
+ default: T;
24
+ }>), nameOrSelector?: string): Promise<Awaited<InstanceType<T>>[]>;
@@ -1,9 +1,28 @@
1
+ import { isDev, isFunction } from "../utils/index.js";
1
2
  async function registerComponent(ctor, nameOrSelector) {
3
+ if (isFunction(ctor) && !("$isBase" in ctor)) {
4
+ ctor = ctor(void 0);
5
+ }
6
+ let Ctor;
2
7
  if (ctor instanceof Promise) {
3
- ctor = await ctor;
8
+ const module = await ctor;
9
+ Ctor = "$isBase" in module ? module : module.default;
10
+ } else {
11
+ Ctor = ctor;
4
12
  }
5
- const instances = await Promise.all(ctor.$register(nameOrSelector));
6
- return instances;
13
+ const results = await Promise.allSettled(Ctor.$register(nameOrSelector));
14
+ return results.flatMap((result) => {
15
+ if (result.status === "fulfilled") {
16
+ return [result.value];
17
+ }
18
+ if (isDev) {
19
+ console.error(
20
+ "[registerComponent] An instance failed to mount and was skipped.",
21
+ result.reason
22
+ );
23
+ }
24
+ return [];
25
+ });
7
26
  }
8
27
  export {
9
28
  registerComponent
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../packages/js-toolkit/helpers/registerComponent.ts"],
4
- "sourcesContent": ["import { BaseConstructor } from '../Base/Base.js';\n\n/**\n * Register globally and mount a given component, or a promise resolving to a component.\n *\n * @link https://js-toolkit.studiometa.dev/api/helpers/registerComponent.html\n * @param ctor The component constructor or a promise resolving to it.\n * @param nameOrSelector The name or selector to used for this component.\n * @return A promise resolving to an array of mounted component instances.\n */\nexport async function registerComponent<T extends BaseConstructor = BaseConstructor>(\n ctor: T | Promise<T>,\n nameOrSelector?: string,\n) {\n if (ctor instanceof Promise) {\n ctor = await ctor;\n }\n\n const instances = await Promise.all(ctor.$register(nameOrSelector));\n return instances;\n}\n"],
5
- "mappings": "AAUA,eAAsB,kBACpB,MACA,gBACA;AACA,MAAI,gBAAgB,SAAS;AAC3B,WAAO,MAAM;AAAA,EACf;AAEA,QAAM,YAAY,MAAM,QAAQ,IAAI,KAAK,UAAU,cAAc,CAAC;AAClE,SAAO;AACT;",
4
+ "sourcesContent": ["import { BaseConstructor, BaseAsyncConstructor } from '../Base/Base.js';\nimport { isDev, isFunction } from '../utils/index.js';\n\n/**\n * Register globally and mount a given component.\n *\n * The component can be provided in any of the forms also accepted for child\n * components in `config.components`:\n * - a `Base` constructor;\n * - a promise resolving to a constructor or a module namespace (`import(...)`);\n * - a factory function returning such a promise (`() => import(...)`).\n *\n * Instances are mounted independently: an element that fails to mount is\n * skipped (and logged in development) instead of failing the whole call, so\n * the resolved array contains every instance that mounted successfully.\n *\n * @link https://js-toolkit.studiometa.dev/api/helpers/registerComponent.html\n * @param ctor The component constructor, or a way to resolve it.\n * @param nameOrSelector The name or selector to used for this component.\n * @return A promise resolving to an array of mounted component instances.\n */\nexport async function registerComponent<T extends BaseConstructor = BaseConstructor>(\n ctor: T | Promise<T | { default: T }> | (() => Promise<T | { default: T }>),\n nameOrSelector?: string,\n) {\n // Resolve factory functions such as `() => import('./Component.js')`, mirroring\n // how lazy child components are declared. A `Base` constructor is a function too,\n // so it is distinguished by its `$isBase` marker.\n if (isFunction(ctor) && !('$isBase' in ctor)) {\n ctor = (ctor as BaseAsyncConstructor)(undefined as never) as Promise<T | { default: T }>;\n }\n\n let Ctor: T;\n if (ctor instanceof Promise) {\n const module = await ctor;\n // A resolved `Base` constructor is used as-is; only a module namespace is\n // unwrapped. Keying on `$isBase` (like the async child resolution) avoids\n // mistaking a user-defined static `default` member for a module namespace.\n Ctor = ('$isBase' in module ? module : (module as { default: T }).default) as T;\n } else {\n Ctor = ctor;\n }\n\n const results = await Promise.allSettled(Ctor.$register(nameOrSelector));\n\n return results.flatMap((result) => {\n if (result.status === 'fulfilled') {\n return [result.value];\n }\n\n if (isDev) {\n console.error(\n '[registerComponent] An instance failed to mount and was skipped.',\n result.reason,\n );\n }\n\n return [];\n });\n}\n"],
5
+ "mappings": "AACA,SAAS,OAAO,kBAAkB;AAoBlC,eAAsB,kBACpB,MACA,gBACA;AAIA,MAAI,WAAW,IAAI,KAAK,EAAE,aAAa,OAAO;AAC5C,WAAQ,KAA8B,MAAkB;AAAA,EAC1D;AAEA,MAAI;AACJ,MAAI,gBAAgB,SAAS;AAC3B,UAAM,SAAS,MAAM;AAIrB,WAAQ,aAAa,SAAS,SAAU,OAA0B;AAAA,EACpE,OAAO;AACL,WAAO;AAAA,EACT;AAEA,QAAM,UAAU,MAAM,QAAQ,WAAW,KAAK,UAAU,cAAc,CAAC;AAEvE,SAAO,QAAQ,QAAQ,CAAC,WAAW;AACjC,QAAI,OAAO,WAAW,aAAa;AACjC,aAAO,CAAC,OAAO,KAAK;AAAA,IACtB;AAEA,QAAI,OAAO;AACT,cAAQ;AAAA,QACN;AAAA,QACA,OAAO;AAAA,MACT;AAAA,IACF;AAEA,WAAO,CAAC;AAAA,EACV,CAAC;AACH;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,22 @@
1
+ import { BaseConstructor } from '../Base/Base.js';
2
+ /**
3
+ * Register globally and mount multiple components at once.
4
+ *
5
+ * Each component accepts the same forms as {@link registerComponent}: a `Base`
6
+ * constructor, a promise resolving to a constructor or module namespace, or a
7
+ * factory function returning such a promise.
8
+ *
9
+ * Components are registered independently: a single failure (e.g. a rejected
10
+ * dynamic import) does not prevent the others from being registered, and the
11
+ * returned array contains every instance that mounted successfully. Failures
12
+ * are logged in development.
13
+ *
14
+ * @link https://js-toolkit.studiometa.dev/api/helpers/registerComponents.html
15
+ * @param ctors The components, or ways to resolve them.
16
+ * @return A promise resolving to a flat array of mounted component instances.
17
+ */
18
+ export declare function registerComponents<T extends BaseConstructor = BaseConstructor>(...ctors: (T | Promise<T | {
19
+ default: T;
20
+ }> | (() => Promise<T | {
21
+ default: T;
22
+ }>))[]): Promise<Awaited<InstanceType<T>>[]>;
@@ -0,0 +1,21 @@
1
+ import { isDev } from "../utils/index.js";
2
+ import { registerComponent } from "./registerComponent.js";
3
+ async function registerComponents(...ctors) {
4
+ const results = await Promise.allSettled(ctors.map((ctor) => registerComponent(ctor)));
5
+ return results.flatMap((result) => {
6
+ if (result.status === "fulfilled") {
7
+ return result.value;
8
+ }
9
+ if (isDev) {
10
+ console.error(
11
+ "[registerComponents] A component failed to register and was skipped.",
12
+ result.reason
13
+ );
14
+ }
15
+ return [];
16
+ });
17
+ }
18
+ export {
19
+ registerComponents
20
+ };
21
+ //# sourceMappingURL=registerComponents.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../packages/js-toolkit/helpers/registerComponents.ts"],
4
+ "sourcesContent": ["import { BaseConstructor } from '../Base/Base.js';\nimport { isDev } from '../utils/index.js';\nimport { registerComponent } from './registerComponent.js';\n\n/**\n * Register globally and mount multiple components at once.\n *\n * Each component accepts the same forms as {@link registerComponent}: a `Base`\n * constructor, a promise resolving to a constructor or module namespace, or a\n * factory function returning such a promise.\n *\n * Components are registered independently: a single failure (e.g. a rejected\n * dynamic import) does not prevent the others from being registered, and the\n * returned array contains every instance that mounted successfully. Failures\n * are logged in development.\n *\n * @link https://js-toolkit.studiometa.dev/api/helpers/registerComponents.html\n * @param ctors The components, or ways to resolve them.\n * @return A promise resolving to a flat array of mounted component instances.\n */\nexport async function registerComponents<T extends BaseConstructor = BaseConstructor>(\n ...ctors: (T | Promise<T | { default: T }> | (() => Promise<T | { default: T }>))[]\n) {\n const results = await Promise.allSettled(ctors.map((ctor) => registerComponent(ctor)));\n\n return results.flatMap((result) => {\n if (result.status === 'fulfilled') {\n return result.value;\n }\n\n if (isDev) {\n console.error(\n '[registerComponents] A component failed to register and was skipped.',\n result.reason,\n );\n }\n\n return [];\n });\n}\n"],
5
+ "mappings": "AACA,SAAS,aAAa;AACtB,SAAS,yBAAyB;AAkBlC,eAAsB,sBACjB,OACH;AACA,QAAM,UAAU,MAAM,QAAQ,WAAW,MAAM,IAAI,CAAC,SAAS,kBAAkB,IAAI,CAAC,CAAC;AAErF,SAAO,QAAQ,QAAQ,CAAC,WAAW;AACjC,QAAI,OAAO,WAAW,aAAa;AACjC,aAAO,OAAO;AAAA,IAChB;AAEA,QAAI,OAAO;AACT,cAAQ;AAAA,QACN;AAAA,QACA,OAAO;AAAA,MACT;AAAA,IACF;AAEA,WAAO,CAAC;AAAA,EACV,CAAC;AACH;",
6
+ "names": []
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studiometa/js-toolkit",
3
- "version": "3.7.0",
3
+ "version": "3.8.0",
4
4
  "description": "A set of useful little bits of JavaScript to boost your project! 🚀",
5
5
  "publishConfig": {
6
6
  "access": "public"