mount-observer 0.1.11 → 0.1.13

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.
@@ -1,95 +1,96 @@
1
- import { EvtRt } from './EvtRt.js';
2
- //import { buildCSSQuery } from 'assign-gingerly/buildCSSQuery.js';
3
- import 'assign-gingerly/object-extension.js';
4
- /**
5
- * Handler for automatically enhancing mounted elements using assign-gingerly.
6
- * Searches the first imported module for an export with a "spawn" property
7
- * and uses element.enh.get() to spawn the enhancement.
8
- */
9
- export class EnhanceMountedElementHandler extends EvtRt {
10
- async mount(mountedElement, MountConfig, context) {
11
- // Check if modules are specified
12
- if (!context.modules || context.modules.length === 0) {
13
- throw new Error('Must specify an ES Module with import property');
14
- }
15
- const module = context.modules[0];
16
- // Find registry item (object with spawn property)
17
- const registryItem = await this._findRegistryItem(module, mountedElement);
18
- if (!registryItem) {
19
- throw new Error('No registry item found in module. Expected an export with a "spawn" property.');
20
- }
21
- // Validate spawn is a constructor
22
- if (typeof registryItem.spawn !== 'function') {
23
- throw new Error('Registry item "spawn" property must be a constructor function');
24
- }
25
- // Spawn the enhancement
26
- this._spawnEnhancement(mountedElement, registryItem, context);
27
- }
28
- /**
29
- * Spawn the enhancement using element.enh.get().
30
- * Polyfills customElementRegistry if needed for browsers without scoped registry support.
31
- */
32
- _spawnEnhancement(element, registryItem, context) {
33
- // Polyfill element.customElementRegistry if it doesn't exist (for browsers without scoped registries)
34
- if (!element.customElementRegistry) {
35
- Object.defineProperty(element, 'customElementRegistry', {
36
- value: customElements,
37
- writable: true,
38
- enumerable: false,
39
- configurable: true
40
- });
41
- }
42
- // Use element.enh.get() to spawn the enhancement
43
- const enh = element.enh;
44
- if (!enh || typeof enh.get !== 'function') {
45
- throw new Error('Element does not have enh.get() method. Make sure assign-gingerly/object-extension.js is loaded.');
46
- }
47
- enh.get(registryItem, context);
48
- }
49
- /**
50
- * Find a registry item in the module exports.
51
- * A registry item is an object with a "spawn" property.
52
- * @param module - The imported module
53
- * @returns The registry item or null if not found
54
- */
55
- async _findRegistryItem(module, el) {
56
- // Check default export first
57
- if (module.default && await this._isRegistryItem(module.default, el)) {
58
- return module.default;
59
- }
60
- // Search all exports for a registry item
61
- const exports = Object.values(module);
62
- const registryItems = [];
63
- for (const e of exports) {
64
- const isRegistryItem = await this._isRegistryItem(e, el);
65
- if (isRegistryItem)
66
- registryItems.push(e);
67
- }
68
- if (registryItems.length === 0) {
69
- return null;
70
- }
71
- if (registryItems.length > 1) {
72
- throw new Error('More than one registry item found in module. Expected exactly one export with a "spawn" property.');
73
- }
74
- return registryItems[0];
75
- }
76
- /**
77
- * Check if an export is a registry item (has a spawn property).
78
- * @param exp - The export to check
79
- * @returns True if the export is a registry item
80
- */
81
- async _isRegistryItem(exp, mountedElement) {
82
- let test = exp !== null
83
- && typeof exp === 'object'
84
- && 'spawn' in exp
85
- && typeof exp.spawn === 'function';
86
- if (!test)
87
- return false;
88
- const emc = exp;
89
- if (emc.withAttrs !== undefined) {
90
- const cssQuery = (await import('assign-gingerly/buildCSSQuery.js')).buildCSSQuery(emc);
91
- return mountedElement.matches(cssQuery);
92
- }
93
- return true;
94
- }
95
- }
1
+ import { EvtRt } from './EvtRt.js';
2
+ //import { buildCSSQuery } from 'assign-gingerly/buildCSSQuery.js';
3
+ import 'assign-gingerly/object-extension.js';
4
+ /**
5
+ * Handler for automatically enhancing mounted elements using assign-gingerly.
6
+ * Searches the first imported module for an export with a "spawn" property
7
+ * and uses element.enh.get() to spawn the enhancement.
8
+ */
9
+ export class EnhanceMountedElementHandler extends EvtRt {
10
+ async mount(mountedElement, MountConfig, context) {
11
+ this.abort();
12
+ // Check if modules are specified
13
+ if (!context.modules || context.modules.length === 0) {
14
+ throw new Error('Must specify an ES Module with import property');
15
+ }
16
+ const module = context.modules[0];
17
+ // Find registry item (object with spawn property)
18
+ const registryItem = await this._findRegistryItem(module, mountedElement);
19
+ if (!registryItem) {
20
+ throw new Error('No registry item found in module. Expected an export with a "spawn" property.');
21
+ }
22
+ // Validate spawn is a constructor
23
+ if (typeof registryItem.spawn !== 'function') {
24
+ throw new Error('Registry item "spawn" property must be a constructor function');
25
+ }
26
+ // Spawn the enhancement
27
+ this.#spawnEnhancement(mountedElement, registryItem, context);
28
+ }
29
+ /**
30
+ * Spawn the enhancement using element.enh.get().
31
+ * Polyfills customElementRegistry if needed for browsers without scoped registry support.
32
+ */
33
+ #spawnEnhancement(element, registryItem, context) {
34
+ // Polyfill element.customElementRegistry if it doesn't exist (for browsers without scoped registries)
35
+ if (!element.customElementRegistry) {
36
+ Object.defineProperty(element, 'customElementRegistry', {
37
+ value: customElements,
38
+ writable: true,
39
+ enumerable: false,
40
+ configurable: true
41
+ });
42
+ }
43
+ // Use element.enh.get() to spawn the enhancement
44
+ const enh = element.enh;
45
+ if (!enh || typeof enh.get !== 'function') {
46
+ throw new Error('Element does not have enh.get() method. Make sure assign-gingerly/object-extension.js is loaded.');
47
+ }
48
+ enh.get(registryItem, context);
49
+ }
50
+ /**
51
+ * Find a registry item in the module exports.
52
+ * A registry item is an object with a "spawn" property.
53
+ * @param module - The imported module
54
+ * @returns The registry item or null if not found
55
+ */
56
+ async _findRegistryItem(module, el) {
57
+ // Check default export first
58
+ if (module.default && await this._isRegistryItem(module.default, el)) {
59
+ return module.default;
60
+ }
61
+ // Search all exports for a registry item
62
+ const exports = Object.values(module);
63
+ const registryItems = [];
64
+ for (const e of exports) {
65
+ const isRegistryItem = await this._isRegistryItem(e, el);
66
+ if (isRegistryItem)
67
+ registryItems.push(e);
68
+ }
69
+ if (registryItems.length === 0) {
70
+ return null;
71
+ }
72
+ if (registryItems.length > 1) {
73
+ throw new Error('More than one registry item found in module. Expected exactly one export with a "spawn" property.');
74
+ }
75
+ return registryItems[0];
76
+ }
77
+ /**
78
+ * Check if an export is a registry item (has a spawn property).
79
+ * @param exp - The export to check
80
+ * @returns True if the export is a registry item
81
+ */
82
+ async _isRegistryItem(exp, mountedElement) {
83
+ let test = exp !== null
84
+ && typeof exp === 'object'
85
+ && 'spawn' in exp
86
+ && typeof exp.spawn === 'function';
87
+ if (!test)
88
+ return false;
89
+ const emc = exp;
90
+ if (emc.withAttrs !== undefined) {
91
+ const cssQuery = (await import('assign-gingerly/buildCSSQuery.js')).buildCSSQuery(emc);
92
+ return mountedElement.matches(cssQuery);
93
+ }
94
+ return true;
95
+ }
96
+ }
package/Events.js CHANGED
@@ -8,62 +8,62 @@ export const mediaunmatchEventName = 'mediaunmatch';
8
8
  export class MountEvent extends Event {
9
9
  mountedElement;
10
10
  modules;
11
- MountConfig;
11
+ mountConfig;
12
12
  mountContext;
13
13
  static eventName = mountEventName;
14
- constructor(mountedElement, modules, MountConfig, mountContext) {
14
+ constructor(mountedElement, modules, mountConfig, mountContext) {
15
15
  super(MountEvent.eventName);
16
16
  this.mountedElement = mountedElement;
17
17
  this.modules = modules;
18
- this.MountConfig = MountConfig;
18
+ this.mountConfig = mountConfig;
19
19
  this.mountContext = mountContext;
20
20
  }
21
21
  }
22
22
  export class DismountEvent extends Event {
23
23
  mountedElement;
24
24
  reason;
25
- MountConfig;
25
+ mountConfig;
26
26
  static eventName = dismountEventName;
27
- constructor(mountedElement, reason, MountConfig) {
27
+ constructor(mountedElement, reason, mountConfig) {
28
28
  super(DismountEvent.eventName);
29
29
  this.mountedElement = mountedElement;
30
30
  this.reason = reason;
31
- this.MountConfig = MountConfig;
31
+ this.mountConfig = mountConfig;
32
32
  }
33
33
  }
34
34
  export class DisconnectEvent extends Event {
35
35
  mountedElement;
36
- MountConfig;
36
+ mountConfig;
37
37
  static eventName = disconnectEventName;
38
- constructor(mountedElement, MountConfig) {
38
+ constructor(mountedElement, mountConfig) {
39
39
  super(DisconnectEvent.eventName);
40
40
  this.mountedElement = mountedElement;
41
- this.MountConfig = MountConfig;
41
+ this.mountConfig = mountConfig;
42
42
  }
43
43
  }
44
44
  export class LoadEvent extends Event {
45
45
  modules;
46
- MountConfig;
46
+ mountConfig;
47
47
  static eventName = loadEventName;
48
- constructor(modules, MountConfig) {
48
+ constructor(modules, mountConfig) {
49
49
  super(LoadEvent.eventName);
50
50
  this.modules = modules;
51
- this.MountConfig = MountConfig;
51
+ this.mountConfig = mountConfig;
52
52
  }
53
53
  }
54
54
  export class MediaMatchEvent extends Event {
55
- MountConfig;
55
+ mountConfig;
56
56
  static eventName = mediamatchEventName;
57
- constructor(MountConfig) {
57
+ constructor(mountConfig) {
58
58
  super(MediaMatchEvent.eventName);
59
- this.MountConfig = MountConfig;
59
+ this.mountConfig = mountConfig;
60
60
  }
61
61
  }
62
62
  export class MediaUnmatchEvent extends Event {
63
- MountConfig;
63
+ mountConfig;
64
64
  static eventName = mediaunmatchEventName;
65
- constructor(MountConfig) {
65
+ constructor(mountConfig) {
66
66
  super(MediaUnmatchEvent.eventName);
67
- this.MountConfig = MountConfig;
67
+ this.mountConfig = mountConfig;
68
68
  }
69
69
  }
package/Events.ts CHANGED
@@ -15,7 +15,7 @@ export class MountEvent extends Event implements IMountEvent {
15
15
  constructor(
16
16
  public mountedElement: Element,
17
17
  public modules: any[],
18
- public MountConfig: MountConfig,
18
+ public mountConfig: MountConfig,
19
19
  public mountContext: MountContext
20
20
  ) {
21
21
  super(MountEvent.eventName);
@@ -25,7 +25,7 @@ export class MountEvent extends Event implements IMountEvent {
25
25
  export class DismountEvent extends Event implements IDismountEvent {
26
26
  static eventName: typeof dismountEventName = dismountEventName;
27
27
 
28
- constructor(public mountedElement: Element, public reason: DismountReason, public MountConfig: MountConfig) {
28
+ constructor(public mountedElement: Element, public reason: DismountReason, public mountConfig: MountConfig) {
29
29
  super(DismountEvent.eventName);
30
30
  }
31
31
  }
@@ -33,7 +33,7 @@ export class DismountEvent extends Event implements IDismountEvent {
33
33
  export class DisconnectEvent extends Event {
34
34
  static eventName: typeof disconnectEventName = disconnectEventName;
35
35
 
36
- constructor(public mountedElement: Element, public MountConfig: MountConfig) {
36
+ constructor(public mountedElement: Element, public mountConfig: MountConfig) {
37
37
  super(DisconnectEvent.eventName);
38
38
  }
39
39
  }
@@ -41,7 +41,7 @@ export class DisconnectEvent extends Event {
41
41
  export class LoadEvent extends Event {
42
42
  static eventName: typeof loadEventName = loadEventName;
43
43
 
44
- constructor(public modules: any[], public MountConfig: MountConfig) {
44
+ constructor(public modules: any[], public mountConfig: MountConfig) {
45
45
  super(LoadEvent.eventName);
46
46
  }
47
47
  }
@@ -49,7 +49,7 @@ export class LoadEvent extends Event {
49
49
  export class MediaMatchEvent extends Event {
50
50
  static eventName: typeof mediamatchEventName = mediamatchEventName;
51
51
 
52
- constructor(public MountConfig: MountConfig) {
52
+ constructor(public mountConfig: MountConfig) {
53
53
  super(MediaMatchEvent.eventName);
54
54
  }
55
55
  }
@@ -57,7 +57,7 @@ export class MediaMatchEvent extends Event {
57
57
  export class MediaUnmatchEvent extends Event {
58
58
  static eventName: typeof mediaunmatchEventName = mediaunmatchEventName;
59
59
 
60
- constructor(public MountConfig: MountConfig) {
60
+ constructor(public mountConfig: MountConfig) {
61
61
  super(MediaUnmatchEvent.eventName);
62
62
  }
63
63
  }
package/EvtRt.js CHANGED
@@ -1,34 +1,41 @@
1
1
  import { DismountEvent, MountEvent, DisconnectEvent, dismountEventName, disconnectEventName, mountEventName } from './Events.js';
2
+ import { MountObserver } from './MountObserver.js';
2
3
  export class EvtRt {
4
+ #ac;
3
5
  constructor(mountedElement, ctx) {
4
- const { observer, MountConfig } = ctx;
6
+ const { observer, mountConfig } = ctx;
7
+ this.#ac = new AbortController();
5
8
  const et = observer.getNotifier(mountedElement);
6
- et.addEventListener(mountEventName, this);
7
- et.addEventListener(disconnectEventName, this);
8
- et.addEventListener(dismountEventName, this);
9
- this.mount(mountedElement, MountConfig, ctx);
9
+ et.addEventListener(mountEventName, this, { signal: this.#ac.signal });
10
+ et.addEventListener(disconnectEventName, this, { signal: this.#ac.signal });
11
+ et.addEventListener(dismountEventName, this, { signal: this.#ac.signal });
12
+ this.mount(mountedElement, mountConfig, ctx);
10
13
  }
11
- mount(mountedElement, MountConfig, context) {
12
- console.log({ mountedElement, MountConfig, context });
14
+ abort() {
15
+ this.#ac.abort();
13
16
  }
14
- disconnect(mountedElement, MountConfig) {
15
- console.log({ mountedElement, MountConfig });
17
+ mount(mountedElement, mountConfig, context) {
18
+ console.log({ mountedElement, mountConfig, context });
16
19
  }
17
- dismount(mountedElement, MountConfig) {
18
- console.log({ mountedElement, MountConfig });
20
+ disconnect(mountedElement, mountConfig) {
21
+ console.log({ mountedElement, mountConfig });
22
+ }
23
+ dismount(mountedElement, mountConfig) {
24
+ console.log({ mountedElement, mountConfig });
19
25
  }
20
26
  handleEvent(evt) {
21
27
  if (evt instanceof MountEvent) {
22
- const { mountedElement, mountContext, MountConfig } = evt;
23
- this.mount(mountedElement, MountConfig, mountContext);
28
+ const { mountedElement, mountContext, mountConfig } = evt;
29
+ this.mount(mountedElement, mountConfig, mountContext);
24
30
  }
25
31
  else if (evt instanceof DismountEvent) {
26
- const { mountedElement, MountConfig } = evt;
27
- this.dismount(mountedElement, MountConfig);
32
+ const { mountedElement, mountConfig } = evt;
33
+ this.dismount(mountedElement, mountConfig);
28
34
  }
29
35
  else if (evt instanceof DisconnectEvent) {
30
- const { mountedElement, MountConfig } = evt;
31
- this.disconnect(mountedElement, MountConfig);
36
+ const { mountedElement, mountConfig } = evt;
37
+ this.disconnect(mountedElement, mountConfig);
32
38
  }
33
39
  }
34
40
  }
41
+ MountObserver.define('builtIns.logToConsole', EvtRt);
package/EvtRt.ts CHANGED
@@ -4,39 +4,51 @@ import {
4
4
  DismountEvent, MountEvent, DisconnectEvent,
5
5
  dismountEventName, disconnectEventName, mountEventName
6
6
  } from './Events.js';
7
+ import { MountObserver } from './MountObserver.js';
7
8
  export class EvtRt implements EventListenerObject{
9
+
10
+
11
+ #ac: AbortController;
12
+
8
13
  constructor(mountedElement: Element, ctx: MountContext ){
9
- const {observer, MountConfig} = ctx;
14
+ const {observer, mountConfig} = ctx;
15
+ this.#ac = new AbortController();
10
16
  const et = observer.getNotifier(mountedElement);
11
- et.addEventListener(mountEventName, this);
12
- et.addEventListener(disconnectEventName, this);
13
- et.addEventListener(dismountEventName, this);
14
- this.mount(mountedElement, MountConfig, ctx);
17
+ et.addEventListener(mountEventName, this, {signal: this.#ac.signal});
18
+ et.addEventListener(disconnectEventName, this, {signal: this.#ac.signal});
19
+ et.addEventListener(dismountEventName, this, {signal: this.#ac.signal});
20
+ this.mount(mountedElement, mountConfig, ctx);
15
21
 
16
22
  }
17
23
 
18
- mount(mountedElement: Element, MountConfig: MountConfig, context: MountContext){
19
- console.log({mountedElement, MountConfig, context});
24
+ abort(){
25
+ this.#ac.abort();
20
26
  }
21
27
 
22
- disconnect(mountedElement: Element, MountConfig: MountConfig){
23
- console.log({mountedElement, MountConfig});
28
+ mount(mountedElement: Element, mountConfig: MountConfig, context: MountContext){
29
+ console.log({mountedElement, mountConfig, context});
24
30
  }
25
31
 
26
- dismount(mountedElement: Element, MountConfig: MountConfig){
27
- console.log({mountedElement, MountConfig});
32
+ disconnect(mountedElement: Element, mountConfig: MountConfig){
33
+ console.log({mountedElement, mountConfig});
34
+ }
35
+
36
+ dismount(mountedElement: Element, mountConfig: MountConfig){
37
+ console.log({mountedElement, mountConfig});
28
38
  }
29
39
 
30
40
  handleEvent(evt: Event): void {
31
41
  if(evt instanceof MountEvent){
32
- const {mountedElement, mountContext, MountConfig} = evt;
33
- this.mount(mountedElement, MountConfig, mountContext);
42
+ const {mountedElement, mountContext, mountConfig} = evt;
43
+ this.mount(mountedElement, mountConfig, mountContext);
34
44
  }else if(evt instanceof DismountEvent){
35
- const {mountedElement, MountConfig} = evt;
36
- this.dismount(mountedElement, MountConfig);
45
+ const {mountedElement, mountConfig} = evt;
46
+ this.dismount(mountedElement, mountConfig);
37
47
  }else if(evt instanceof DisconnectEvent){
38
- const {mountedElement, MountConfig} = evt;
39
- this.disconnect(mountedElement, MountConfig);
48
+ const {mountedElement, mountConfig} = evt;
49
+ this.disconnect(mountedElement, mountConfig);
40
50
  }
41
51
  }
42
- }
52
+ }
53
+
54
+ MountObserver.define('builtIns.logToConsole', EvtRt);