mount-observer 0.0.22 → 0.0.23

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/MountObserver.js CHANGED
@@ -181,7 +181,8 @@ export class MountObserver extends EventTarget {
181
181
  }, { signal: this.#abortController.signal });
182
182
  await this.#inspectWithin(within, true);
183
183
  }
184
- synthesize(within, customElement, mose) {
184
+ static synthesize(within, customElement, mose) {
185
+ mose.type = 'mountobserver';
185
186
  const name = customElements.getName(customElement);
186
187
  if (name === null)
187
188
  throw 400;
package/Synthesizer.js CHANGED
@@ -6,12 +6,12 @@ export class Synthesizer extends HTMLElement {
6
6
  for (const mutation of mutationList) {
7
7
  const { addedNodes } = mutation;
8
8
  for (const node of addedNodes) {
9
- if (!(node instanceof HTMLScriptElement))
9
+ if (!(node instanceof HTMLScriptElement) || node.type !== 'mountobserver')
10
10
  continue;
11
11
  const mose = node;
12
12
  this.mountObserverElements.push(mose);
13
- this.#import(mose);
14
- const e = new SyntheticEvent(mose);
13
+ this.activate(mose);
14
+ const e = new SynthetizeEvent(mose);
15
15
  this.dispatchEvent(e);
16
16
  }
17
17
  }
@@ -21,25 +21,35 @@ export class Synthesizer extends HTMLElement {
21
21
  const init = {
22
22
  childList: true
23
23
  };
24
- this.#mutationObserver = new MutationObserver(this.mutationCallback);
25
- this.#mutationObserver.observe(this.getRootNode());
26
- this.#inherit();
24
+ this.querySelectorAll('script[type="mountobserver"]').forEach(s => {
25
+ const mose = s;
26
+ this.mountObserverElements.push(mose);
27
+ this.activate(mose);
28
+ });
29
+ this.#mutationObserver = new MutationObserver(this.mutationCallback.bind(this));
30
+ this.#mutationObserver.observe(this, init);
31
+ this.inherit();
27
32
  }
28
- #import(mose) {
33
+ activate(mose) {
29
34
  const { init, do: d, id } = mose;
30
- const se = document.createElement('script');
31
- se.init = init;
32
- se.id = id;
33
- se.do = d;
34
35
  const mi = {
35
36
  do: d,
36
37
  ...init
37
38
  };
38
39
  const mo = new MountObserver(mi);
39
- se.observer = mo;
40
+ mose.observer = mo;
41
+ mo.observe(this.getRootNode());
42
+ }
43
+ import(mose) {
44
+ const { init, do: d, id, synConfig } = mose;
45
+ const se = document.createElement('script');
46
+ se.init = { ...init };
47
+ se.id = id;
48
+ se.do = { ...d };
49
+ se.synConfig = { ...synConfig };
40
50
  this.appendChild(se);
41
51
  }
42
- #inherit() {
52
+ inherit() {
43
53
  const rn = this.getRootNode();
44
54
  const host = rn.host;
45
55
  if (!host)
@@ -49,11 +59,11 @@ export class Synthesizer extends HTMLElement {
49
59
  const parentScopeSynthesizer = parentShadowRealm.querySelector(localName);
50
60
  const { mountObserverElements } = parentScopeSynthesizer;
51
61
  for (const moe of mountObserverElements) {
52
- this.#import(moe);
62
+ this.import(moe);
53
63
  }
54
64
  if (parentScopeSynthesizer !== null) {
55
- parentScopeSynthesizer.addEventListener(SyntheticEvent.eventName, e => {
56
- this.#import(e.mountObserverElement);
65
+ parentScopeSynthesizer.addEventListener(SynthetizeEvent.eventName, e => {
66
+ this.import(e.mountObserverElement);
57
67
  });
58
68
  }
59
69
  }
@@ -61,6 +71,9 @@ export class Synthesizer extends HTMLElement {
61
71
  if (this.#mutationObserver !== undefined) {
62
72
  this.#mutationObserver.disconnect();
63
73
  }
74
+ for (const mose of this.mountObserverElements) {
75
+ mose.observer.disconnect(this.getRootNode());
76
+ }
64
77
  }
65
78
  }
66
79
  // https://github.com/webcomponents-cg/community-protocols/issues/12#issuecomment-872415080
@@ -68,11 +81,11 @@ export class Synthesizer extends HTMLElement {
68
81
  * The `mutation-event` event represents something that happened.
69
82
  * We can document it here.
70
83
  */
71
- export class SyntheticEvent extends Event {
84
+ export class SynthetizeEvent extends Event {
72
85
  mountObserverElement;
73
86
  static eventName = 'synthesize';
74
87
  constructor(mountObserverElement) {
75
- super(SyntheticEvent.eventName);
88
+ super(SynthetizeEvent.eventName);
76
89
  this.mountObserverElement = mountObserverElement;
77
90
  }
78
91
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mount-observer",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "description": "Observe and act on css matches.",
5
5
  "main": "MountObserver.js",
6
6
  "module": "MountObserver.js",
package/types.d.ts CHANGED
@@ -170,11 +170,15 @@ export interface AddLoadEventListener{
170
170
  //#endregion
171
171
 
172
172
  //#region MountObserver Script Element
173
- export interface MountObserverScriptElement extends HTMLScriptElement{
173
+ export interface MountObserverScriptElementEndUserProps<TSynConfig=any>{
174
174
  init: JSONSerializableMountInit;
175
- //mountedElements: Array<WeakRef<Element>>;
176
175
  observer: IMountObserver;
177
176
  do: MountObserverCallbacks;
177
+ synConfig: TSynConfig;
178
+ }
179
+ export interface MountObserverScriptElement<TSynConfig=any>
180
+ extends HTMLScriptElement, MountObserverScriptElementEndUserProps<TSynConfig>{
181
+
178
182
  }
179
183
  //#endregion
180
184