mount-observer 0.0.19 → 0.0.20

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
@@ -15,9 +15,10 @@ export class MountObserver extends EventTarget {
15
15
  super();
16
16
  const { on, whereElementIntersectsWith, whereMediaMatches } = init;
17
17
  let isComplex = false;
18
+ //TODO: further this problem further. Starting to think this is basically not polyfillable
18
19
  if (on !== undefined) {
19
20
  const reducedMatch = on.replaceAll(':not(', '');
20
- isComplex = reducedMatch.includes(' ') || reducedMatch.includes(':');
21
+ isComplex = reducedMatch.includes(' ') || (reducedMatch.includes(':') && reducedMatch.includes('('));
21
22
  }
22
23
  this.#isComplex = isComplex;
23
24
  if (whereElementIntersectsWith || whereMediaMatches)
package/README.md CHANGED
@@ -7,11 +7,11 @@ Note that much of what is described below has not yet been polyfilled.
7
7
 
8
8
  # The MountObserver api.
9
9
 
10
- Author: Bruce B. Anderson (with valuable feedback from [doeixd](https://github.com/doeixd) )
10
+ Author: Bruce B. Anderson (with valuable feedback from @doeixd )
11
11
 
12
12
  Issues / pr's / polyfill: [mount-observer](https://github.com/bahrus/mount-observer)
13
13
 
14
- Last Update: 2024-5-4
14
+ Last Update: 2024-5-5
15
15
 
16
16
  ## Benefits of this API
17
17
 
@@ -72,6 +72,8 @@ observer.observe(document);
72
72
 
73
73
  Invoking "disconnect" as shown above causes the observer to emit event "disconnectedCallback".
74
74
 
75
+ The argument can also be an array of objects that fit the pattern shown above.
76
+
75
77
  If no imports are specified, it would go straight to do.* (if any such callbacks are specified), and it will also dispatch events as discussed below.
76
78
 
77
79
  This only searches for elements matching 'my-element' outside any shadow DOM.
@@ -235,7 +237,41 @@ If an element that is in "mounted" state according to a MountObserver instance i
235
237
  3) If the mounted element is added outside the rootNode being observed, the mountObserver instance will dispatch event "exit", and the MountObserver instance will relinquish any further responsibility for this element.
236
238
  4) Ideally event "forget" would be dispatched just before the platform garbage collects an element the MountObserver instance is still monitoring, after all hard references are relinquished (or is that self-contradictory?).
237
239
  5) If the new place it was added remains within the original rootNode and remains mounted, the MountObserver instance dispatches event "reconfirmed".
238
- 6) If the element no longer satisfies the criteria of the MountObserver instance, the MountObserver instance will dispatch event "dismount".
240
+ 6) If the element no longer satisfies the criteria of the MountObserver instance, the MountObserver instance will dispatch event "dismount".
241
+
242
+ ## Dismounting
243
+
244
+ In many cases, it will be critical to inform the developer **why** the element no longer satisfies all the criteria. For example, we may be using an intersection observer, and when we've scrolled away from view, we can "shut down" until the element is (nearly) scrolled back into view. We may also be displaying things differently depending on the network speed. How we should respond when one of the original conditions, but not the other, no longer applies, is of paramount importance.
245
+
246
+ So the dismount event should provide a "checklist" of all the conditions, and their current value:
247
+
248
+ ```JavaScript
249
+ mediaMatches: true,
250
+ containerMatches: true,
251
+ satisifiesCustomCondition: true,
252
+ whereLangIn: ['en-GB'],
253
+ whereConnection:{
254
+ effectiveTypeMatches: true
255
+ },
256
+ isIntersecting: false,
257
+ changedConditions: ['isIntersecting']
258
+ ```
259
+
260
+ ## Get play-by-play updates?
261
+
262
+ An issue raised by @doeixd, I think, is what if we want to be informed of the status of all the conditions that are applicable to an element being mounted / dismounted? I can see scenarios where this would be useful, for reasons similar to wanting to know why the element dismounted.
263
+
264
+ Since this could have a negative impact on performance, I think it should be something we opt-in to:
265
+
266
+ ```JavaScript
267
+ getPlayByPlay: true
268
+ ```
269
+
270
+ Now the question is when should this progress reporting start? It could either start the moment the element becomes mounted the first time. Or it could happen the moment any of the conditions are satisfied. But some of the conditions could be trivially satisfied for the vast majority of elements (e.g. network speed is 4g or greater).
271
+
272
+ So I believe the prudent thing to do is wait for all the conditions to be satisfied, before engaging in this kind of commentary, i.e. after the first mount.
273
+
274
+ The alternative to providing this feature, which I'm leaning towards, is to just ask the developer to create "specialized" mountObserver construction arguments, that turn on and off precisely when the developer needs to know.
239
275
 
240
276
  ## A tribute to attributes
241
277
 
@@ -497,7 +533,7 @@ This proposal "sneaks in" one more feature, that perhaps should stand separately
497
533
 
498
534
  Also, this proposal is partly focused on better management of importing resources "from a distance", in particular via imports carried out via http. Is it such a stretch to look closely at scenarios where that distance happens to be shorter, i.e. found somewhere [in the document tree structure](https://github.com/tc39/proposal-module-expressions)?
499
535
 
500
- The mount-observer is always on the lookout for template tags with an href attribute starting with #:
536
+ The mount-observer is always on the lookout for template tags with a src attribute starting with #:
501
537
 
502
538
  ```html
503
539
  <template src=#id-of-source-template></template>
@@ -521,7 +557,7 @@ Let's say the source template looks as follows:
521
557
 
522
558
  ```html
523
559
  <template id=id-of-source-template>
524
- I don't know why you say <slot name=slot2></slot> I say <slot name=slot1></slot>
560
+ <div>I don't know why you say <slot name=slot2></slot> I say <slot name=slot1></slot></div>
525
561
  </template>
526
562
  ```
527
563
 
@@ -583,3 +619,13 @@ This proposal (and polyfill) also supports the option to utilize ShadowDOM / slo
583
619
  </div>
584
620
  ```
585
621
 
622
+ > [!NOTE]
623
+ > An intriguing sounding alternative to using the template tag that disappears, as shown above, is to use a new tag for this purpose. I think something along the lines of what is [proposed here](https://github.com/WICG/webcomponents/issues/1059) has a much better semantic ring to it:
624
+
625
+ ```html
626
+ <compose src="#sharedHeader"></compose>
627
+ <compose src="#productCard"></compose>
628
+ ```
629
+
630
+ The discussion there leads to an open question whether a processing instruction would be better. I think the compose tag would make much more sense, vs a processing instruction, as it could then support slotted children (behaving similar to the Beatles' example above). Or maybe another tag should be introduced that is the equivalent of the slot, to avoid confusion. or some equivalent. But I strongly suspect that could significantly reduce the payload size of some documents, if we can reuse blocks of HTML, inserting sections of customized content for each instance.
631
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mount-observer",
3
- "version": "0.0.19",
3
+ "version": "0.0.20",
4
4
  "description": "Observe and act on css matches.",
5
5
  "main": "MountObserver.js",
6
6
  "module": "MountObserver.js",
package/types.d.ts CHANGED
@@ -66,7 +66,7 @@ export interface MountContext{
66
66
  }
67
67
 
68
68
  type PipelineStage = 'Inspecting' | 'PreImport' | 'PostImport' | 'Import'
69
- export type PipelineProcessor<ReturnType = void> = (matchingElement: Element, observer: IMountObserver, ctx: MountContext) => Promise<ReturnType>;
69
+ export type PipelineProcessor<ReturnType = void> = (matchingElement: Element, observer: IMountObserver, ctx: MountContext) => Promise<ReturnType> | ReturnType;
70
70
 
71
71
  //#region mutation event
72
72
  export type mutationEventName = 'mutation-event';