@studiometa/js-toolkit 3.2.0 → 3.3.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/decorators/withGroup.d.ts +1 -1
- package/decorators/withGroup.js +9 -3
- package/decorators/withGroup.js.map +2 -2
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -13,4 +13,4 @@ export interface WithGroupInterface extends BaseInterface {
|
|
|
13
13
|
*
|
|
14
14
|
* @link https://js-toolkit.studiometa.dev/api/decorators/withGroup.html
|
|
15
15
|
*/
|
|
16
|
-
export declare function withGroup<S extends Base = Base>(BaseClass: typeof Base): BaseDecorator<WithGroupInterface, S, WithGroupProps>;
|
|
16
|
+
export declare function withGroup<S extends Base = Base>(BaseClass: typeof Base, namespace?: string): BaseDecorator<WithGroupInterface, S, WithGroupProps>;
|
package/decorators/withGroup.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
function groups() {
|
|
2
2
|
return globalThis.__JS_TOOLKIT_GROUPS__ ??= /* @__PURE__ */ new Map();
|
|
3
3
|
}
|
|
4
|
-
function withGroup(BaseClass) {
|
|
4
|
+
function withGroup(BaseClass, namespace = "") {
|
|
5
5
|
return class WithGroup extends BaseClass {
|
|
6
6
|
static config = {
|
|
7
7
|
...BaseClass.config,
|
|
@@ -13,8 +13,14 @@ function withGroup(BaseClass) {
|
|
|
13
13
|
* Get the group set.
|
|
14
14
|
*/
|
|
15
15
|
get $group() {
|
|
16
|
-
const
|
|
17
|
-
|
|
16
|
+
const group = `${namespace}${this.$options.group}`;
|
|
17
|
+
const instances = groups().get(group) ?? groups().set(group, /* @__PURE__ */ new Set()).get(group);
|
|
18
|
+
for (const instance of instances) {
|
|
19
|
+
if (!instance.$el.isConnected) {
|
|
20
|
+
instances.delete(instance);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return instances;
|
|
18
24
|
}
|
|
19
25
|
constructor(element) {
|
|
20
26
|
super(element);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/decorators/withGroup.ts"],
|
|
4
|
-
"sourcesContent": ["import type { BaseDecorator, BaseInterface } from '../Base/types.js';\nimport type { Base, BaseProps, BaseConfig } from '../Base/index.js';\n\nexport interface WithGroupProps extends BaseProps {\n $options: {\n group: string;\n };\n}\n\nexport interface WithGroupInterface extends BaseInterface {\n get $group(): Set<Base>;\n}\n\n/**\n * Get global groups map.\n */\nfunction groups(): Map<string, Set<
|
|
5
|
-
"mappings": "AAgBA,SAAS,
|
|
4
|
+
"sourcesContent": ["import type { BaseDecorator, BaseInterface } from '../Base/types.js';\nimport type { Base, BaseProps, BaseConfig } from '../Base/index.js';\n\nexport interface WithGroupProps extends BaseProps {\n $options: {\n group: string;\n };\n}\n\nexport interface WithGroupInterface extends BaseInterface {\n get $group(): Set<Base>;\n}\n\n/**\n * Get global groups map.\n */\nfunction groups<T extends Base = Base>(): Map<string, Set<T>> {\n return (globalThis.__JS_TOOLKIT_GROUPS__ ??= new Map<string, Set<T>>());\n}\n\n/**\n * Add group features to easily bind multiple components together.\n *\n * @link https://js-toolkit.studiometa.dev/api/decorators/withGroup.html\n */\nexport function withGroup<S extends Base = Base>(\n BaseClass: typeof Base,\n namespace = '',\n): BaseDecorator<WithGroupInterface, S, WithGroupProps> {\n // @ts-expect-error Decorators can not be typed.\n return class WithGroup<T extends BaseProps = BaseProps> extends BaseClass<T & WithGroupProps> {\n static config: BaseConfig = {\n ...BaseClass.config,\n options: {\n group: String,\n },\n };\n\n /**\n * Get the group set.\n */\n get $group() {\n const group = `${namespace}${this.$options.group}`;\n const instances = groups<this>().get(group) ?? groups<this>().set(group, new Set()).get(group);\n\n for (const instance of instances) {\n if (!instance.$el.isConnected) {\n instances.delete(instance);\n }\n }\n\n return instances;\n }\n\n constructor(element: HTMLElement) {\n super(element);\n\n this.$on('mounted', () => {\n this.$group.add(this);\n });\n this.$on('destroyed', () => {\n this.$group.delete(this);\n });\n }\n };\n}\n"],
|
|
5
|
+
"mappings": "AAgBA,SAAS,SAAqD;AAC5D,SAAQ,WAAW,0BAA0B,oBAAI,IAAoB;AACvE;AAOO,SAAS,UACd,WACA,YAAY,IAC0C;AAEtD,SAAO,MAAM,kBAAmD,UAA8B;AAAA,IAC5F,OAAO,SAAqB;AAAA,MAC1B,GAAG,UAAU;AAAA,MACb,SAAS;AAAA,QACP,OAAO;AAAA,MACT;AAAA,IACF;AAAA;AAAA;AAAA;AAAA,IAKA,IAAI,SAAS;AACX,YAAM,QAAQ,GAAG,SAAS,GAAG,KAAK,SAAS,KAAK;AAChD,YAAM,YAAY,OAAa,EAAE,IAAI,KAAK,KAAK,OAAa,EAAE,IAAI,OAAO,oBAAI,IAAI,CAAC,EAAE,IAAI,KAAK;AAE7F,iBAAW,YAAY,WAAW;AAChC,YAAI,CAAC,SAAS,IAAI,aAAa;AAC7B,oBAAU,OAAO,QAAQ;AAAA,QAC3B;AAAA,MACF;AAEA,aAAO;AAAA,IACT;AAAA,IAEA,YAAY,SAAsB;AAChC,YAAM,OAAO;AAEb,WAAK,IAAI,WAAW,MAAM;AACxB,aAAK,OAAO,IAAI,IAAI;AAAA,MACtB,CAAC;AACD,WAAK,IAAI,aAAa,MAAM;AAC1B,aAAK,OAAO,OAAO,IAAI;AAAA,MACzB,CAAC;AAAA,IACH;AAAA,EACF;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../packages/js-toolkit/index.ts"],
|
|
4
|
-
"sourcesContent": ["export const version = '3.
|
|
4
|
+
"sourcesContent": ["export const version = '3.3.0' as const;\n\nexport * from './Base/index.js';\nexport * from './Base/types.js';\nexport * from './decorators/index.js';\nexport * from './helpers/index.js';\nexport * from './services/index.js';\n"],
|
|
5
5
|
"mappings": "AAAO,MAAM,UAAU;AAEvB,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|