mount-observer 0.0.53 → 0.0.55

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/Newish.js CHANGED
@@ -1,15 +1,25 @@
1
1
  export { waitForEvent } from './waitForEvent.js';
2
+ import { ObsAttr } from './ObsAttr.js';
3
+ import { splitRefs } from './itemRefUtils/splitRefs.js';
2
4
  export const attached = Symbol.for('xyyspnstnU+CDrNVa0VnxA');
3
5
  export class Newish {
4
6
  queue = [];
5
7
  isResolved = false;
6
8
  #ce;
9
+ #ref;
7
10
  //#assigner: undefined | Assigner = undefined;
8
11
  #options;
9
12
  constructor(enhancedElement, itemscope, options) {
10
13
  this.#options = options || { assigner: Object.assign };
14
+ this.#ref = new WeakRef(enhancedElement);
11
15
  this.#do(enhancedElement, itemscope);
12
16
  }
17
+ handleEvent(event) {
18
+ const enhancedElement = this.#ref.deref();
19
+ if (!enhancedElement)
20
+ return;
21
+ this.#attachItemrefs(enhancedElement);
22
+ }
13
23
  async #do(enhancedElement, itemscope) {
14
24
  if (enhancedElement[attached] === true)
15
25
  return;
@@ -43,26 +53,32 @@ export class Newish {
43
53
  });
44
54
  this.#assignGingerly();
45
55
  //attach any itemref references
56
+ this.#attachItemrefs(enhancedElement);
57
+ const et = ObsAttr(enhancedElement, 'itemref');
58
+ et.addEventListener('attr-changed', this);
59
+ this.isResolved = true;
60
+ enhancedElement.dispatchEvent(new Event('ishAttached'));
61
+ }
62
+ #alreadyAttached = new Set();
63
+ #attachItemrefs(enhancedElement) {
64
+ //TODO: watch for already attached itemrefs to be removed and remove them from the set
65
+ // and call outOfScopeCallback on them
46
66
  if (enhancedElement.hasAttribute('itemref')) {
47
67
  const itemref = enhancedElement.getAttribute('itemref');
48
- const itemrefList = itemref.split(' ');
49
- let nextSibling = enhancedElement.nextElementSibling;
50
- while (nextSibling) {
51
- if (itemrefList.includes(nextSibling.id)) {
52
- this.#ce.inScopeCallback(nextSibling);
53
- itemrefList.splice(itemrefList.indexOf(nextSibling.id), 1);
68
+ const itemrefList = splitRefs(itemref); // itemref.split(' ').map((id) => id.trim()).filter((id) => id.length > 0);
69
+ if (itemrefList.length === 0)
70
+ return;
71
+ const rn = enhancedElement.getRootNode();
72
+ for (const id of itemrefList) {
73
+ if (this.#alreadyAttached.has(id))
74
+ continue;
75
+ const itemrefElement = rn.getElementById(id);
76
+ if (itemrefElement) {
77
+ this.#ce.inScopeCallback(itemrefElement);
78
+ this.#alreadyAttached.add(id);
54
79
  }
55
- if (itemrefList.length === 0)
56
- break;
57
- nextSibling = nextSibling.nextElementSibling;
58
- }
59
- if (itemrefList.length > 0) {
60
- //TODO add an observer queue for the id found elsewhere
61
- throw 'NI';
62
80
  }
63
81
  }
64
- this.isResolved = true;
65
- enhancedElement.dispatchEvent(new Event('ishAttached'));
66
82
  }
67
83
  async #assignGingerly() {
68
84
  let ce = this.#ce;
package/Newish.ts CHANGED
@@ -1,19 +1,28 @@
1
1
  import { Assigner, BindishOptions } from './ts-refs/mount-observer/types.js';
2
2
 
3
3
  export {waitForEvent} from './waitForEvent.js';
4
+ import {ObsAttr} from './ObsAttr.js';
5
+ import {splitRefs} from './itemRefUtils/splitRefs.js';
4
6
  export const attached = Symbol.for('xyyspnstnU+CDrNVa0VnxA');
5
- export class Newish{
7
+ export class Newish implements EventListenerObject {
6
8
  queue: Array<any> = [];
7
9
  isResolved = false;
8
10
  #ce: HTMLElement | undefined;
11
+ #ref: WeakRef<Element>;
9
12
 
10
13
  //#assigner: undefined | Assigner = undefined;
11
14
  #options: BindishOptions;
12
15
 
13
16
  constructor(enhancedElement: Element, itemscope: string, options?: BindishOptions){
14
17
  this.#options = options || {assigner: Object.assign};
18
+ this.#ref = new WeakRef(enhancedElement);
15
19
  this.#do(enhancedElement, itemscope);
16
20
  }
21
+ handleEvent(event: Event): void {
22
+ const enhancedElement = this.#ref.deref();
23
+ if(!enhancedElement) return;
24
+ this.#attachItemrefs(enhancedElement);
25
+ }
17
26
 
18
27
  async #do(enhancedElement: Element, itemscope: string){
19
28
  if((<any>enhancedElement)[attached] === true) return;
@@ -46,25 +55,35 @@ export class Newish{
46
55
  });
47
56
  this.#assignGingerly();
48
57
  //attach any itemref references
58
+ this.#attachItemrefs(enhancedElement);
59
+ const et = ObsAttr(enhancedElement, 'itemref');
60
+ et.addEventListener('attr-changed', this);
61
+ this.isResolved = true;
62
+ enhancedElement.dispatchEvent(new Event('ishAttached'));
63
+ }
64
+
65
+
66
+
67
+ #alreadyAttached = new Set<string>();
68
+
69
+ #attachItemrefs(enhancedElement: Element){
70
+ //TODO: watch for already attached itemrefs to be removed and remove them from the set
71
+ // and call outOfScopeCallback on them
49
72
  if(enhancedElement.hasAttribute('itemref')){
50
73
  const itemref = enhancedElement.getAttribute('itemref')!;
51
- const itemrefList = itemref.split(' ');
52
- let nextSibling = enhancedElement.nextElementSibling;
53
- while(nextSibling){
54
- if(itemrefList.includes(nextSibling.id)){
55
- (<any>this.#ce).inScopeCallback(nextSibling);
56
- itemrefList.splice(itemrefList.indexOf(nextSibling.id), 1);
74
+ const itemrefList = splitRefs(itemref);// itemref.split(' ').map((id) => id.trim()).filter((id) => id.length > 0);
75
+ if(itemrefList.length === 0) return;
76
+ const rn = enhancedElement.getRootNode() as Document | ShadowRoot;
77
+ for(const id of itemrefList){
78
+ if(this.#alreadyAttached.has(id)) continue;
79
+ const itemrefElement = rn.getElementById(id);
80
+ if(itemrefElement){
81
+ (<any>this.#ce).inScopeCallback(itemrefElement);
82
+ this.#alreadyAttached.add(id);
57
83
  }
58
- if(itemrefList.length === 0) break;
59
- nextSibling = nextSibling.nextElementSibling;
60
- }
61
- if(itemrefList.length > 0){
62
- //TODO add an observer queue for the id found elsewhere
63
- throw 'NI';
64
84
  }
65
85
  }
66
- this.isResolved = true;
67
- enhancedElement.dispatchEvent(new Event('ishAttached'));
86
+
68
87
  }
69
88
 
70
89
  async #assignGingerly(){
package/ObsAttr.js ADDED
@@ -0,0 +1,18 @@
1
+ export function ObsAttr(element, attr) {
2
+ const eventTarget = new EventTarget();
3
+ const obs = new MutationObserver((mutations) => {
4
+ eventTarget.dispatchEvent(new Event('attr-changed'));
5
+ // for(const mutation of mutations){
6
+ // if(mutation.type === 'attributes' && mutation.attributeName === attr){
7
+ // obs.disconnect();
8
+ // eventTarget.dispatchEvent(new Event('obsAttr'));
9
+ // break;
10
+ // }
11
+ // }
12
+ });
13
+ obs.observe(element, {
14
+ attributes: true,
15
+ attributeFilter: [attr],
16
+ });
17
+ return eventTarget;
18
+ }
package/ObsAttr.ts ADDED
@@ -0,0 +1,18 @@
1
+ export function ObsAttr(element: Element, attr: string): EventTarget{
2
+ const eventTarget = new EventTarget();
3
+ const obs = new MutationObserver((mutations) => {
4
+ eventTarget.dispatchEvent(new Event('attr-changed'));
5
+ // for(const mutation of mutations){
6
+ // if(mutation.type === 'attributes' && mutation.attributeName === attr){
7
+ // obs.disconnect();
8
+ // eventTarget.dispatchEvent(new Event('obsAttr'));
9
+ // break;
10
+ // }
11
+ // }
12
+ });
13
+ obs.observe(element, {
14
+ attributes: true,
15
+ attributeFilter: [attr],
16
+ });
17
+ return eventTarget;
18
+ }
@@ -0,0 +1,6 @@
1
+ export function splitRefs(refs) {
2
+ return refs
3
+ .split(' ')
4
+ .map(s => s.trim())
5
+ .filter(s => !!s);
6
+ }
@@ -0,0 +1,6 @@
1
+ export function splitRefs(refs: string){
2
+ return refs
3
+ .split(' ')
4
+ .map(s => s.trim())
5
+ .filter(s => !!s);
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mount-observer",
3
- "version": "0.0.53",
3
+ "version": "0.0.55",
4
4
  "description": "Observe and act on css matches.",
5
5
  "main": "MountObserver.js",
6
6
  "module": "MountObserver.js",
@@ -21,6 +21,10 @@
21
21
  "default": "./Newish.js",
22
22
  "types": "./Newish.ts"
23
23
  },
24
+ "./ObsAttr.js": {
25
+ "default": "./ObsAttr.js",
26
+ "types": "./ObsAttr.ts"
27
+ },
24
28
  "./Synthesizer.js": {
25
29
  "default": "./Synthesizer.js",
26
30
  "types": "./Synthesizer.ts"
@@ -40,12 +44,17 @@
40
44
  "./waitForIsh.js": {
41
45
  "default": "./waitForIsh.js",
42
46
  "types": "./waitForIsh.ts"
47
+ },
48
+ "./itemRefUtils/splitRefs.js": {
49
+ "default": "./itemRefUtils/splitRefs.js",
50
+ "types": "./itemRefUtils/splitRefs.ts"
43
51
  }
44
52
  },
45
53
  "files": [
46
54
  "*.js",
47
55
  "*.ts",
48
- "./ts-refs/*"
56
+ "./ts-refs/*",
57
+ "./itemRefUtils/*"
49
58
  ],
50
59
  "types": "./ts-refs/mount-observer/types.d.ts",
51
60
  "scripts": {
@@ -0,0 +1,23 @@
1
+ import {IEnhancement, BEAllProps} from '../trans-render/be/types';
2
+
3
+ export interface EndUserProps extends IEnhancement{
4
+ names: string;
5
+ }
6
+
7
+ export interface AllProps extends EndUserProps{
8
+ parsedNames: string[];
9
+ }
10
+
11
+ export type AP = AllProps;
12
+
13
+ export type PAP = Partial<AP>;
14
+
15
+ export type ProPAP = Promise<PAP>;
16
+
17
+ export type BAP = AP & BEAllProps;
18
+
19
+ export interface Actions{
20
+ parse(self: BAP): PAP;
21
+ hydrate(self: BAP): ProPAP;
22
+ retire(self: BAP): void;
23
+ }
@@ -116,6 +116,8 @@ export interface EnhancementMountConfig<TBranches = any, TProps = any>{
116
116
 
117
117
  mapEnhKeyTo?: keyof TProps,
118
118
 
119
+ mapEmcTo?: keyof TProps,
120
+
119
121
  allowedMutations?: {[key: CSSQuery]: []}
120
122
 
121
123
  top?: EnhancementMountConfig<TBranches, TProps>,
@@ -1,7 +1,7 @@
1
1
  import { IMountObserver } from '../../mount-observer/types';
2
2
  import { Scope} from '../lib/types';
3
3
  import { WrapperConfig } from '../XV/types';
4
- import {XForm} from '../types';
4
+ import {CSSQuery, XForm} from '../types';
5
5
 
6
6
  export interface IEventConfig<MCProps = any, MCActions = MCProps, TAction = Action>{
7
7
  on?: string,
@@ -189,7 +189,7 @@ export interface IshConfig<TProps = any, TActions = TProps, ETProps = TProps>{
189
189
 
190
190
  isSleepless?: boolean;
191
191
  xform?: XForm<TProps, TActions>;
192
- inScopeXForms?: Array<XForm<TProps, TActions>>;
192
+ inScopeXForms?: {[key: CSSQuery]: XForm<TProps, TActions>};
193
193
  }
194
194
  export interface OConfig<TProps = any, TActions = TProps, ETProps = TProps> extends IshConfig<TProps, TActions, ETProps>{
195
195
  mainTemplate?: string | HTMLTemplateElement;