Skip to content

Commit

Permalink
Use WeakRefs for eventObservation container references
Browse files Browse the repository at this point in the history
Improve tests: see iterators.ts#IterableProperties
  • Loading branch information
MatAtBread committed Sep 21, 2024
1 parent 3817d2f commit 2571765
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 56 deletions.
13 changes: 7 additions & 6 deletions guide/examples/ts/iterable-array-map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { tag } from '../../../module/esm/ai-ui.js';
import { AsyncExtraIterable } from '../../../module/esm/iterators.js';

/* Specify what base tags you reference in your UI */
const { div } = tag();
Expand All @@ -9,30 +8,32 @@ function sum(a:number, b:number) {
}

const App = div.extended(({
styles:`.App > div { margin-bottom: 1em } .App > div > div { margin-left: 1em }`,
override: {
className: 'App',
onclick() {
this.data.unshift(this.data[0] + this.data[1])
// @ts-expect-error: see iterators.ts#IterableProperties
this.data = [1,2]
}
},
iterable: {
data: [3,2,1] as number[]
},
constructed() {
const t = this.data;
const w = t[0]
const golden = () => this.data[0] / this.data[1];
return [
div('Array access',
div('join: ',t.join(', ')),
div('reduce: ',t.reduce(sum,0)),
div('map: ', Array.prototype.map.call(t, n => String(n)+' ') as string[])
div('map: ', Array.prototype.map.call(t, n => String(n)+' ') as string[]),
div("golden: ", golden())
),

div('AsyncIterator array access',
div('join: ', t.map!(d => d.join(', '))),
div('reduce: ', t.map!(d => d.reduce(sum,0))),
div('map: ', t.map!(d => d.map( n => String(n)+' ')))
div('map: ', t.map!(d => d.map( n => String(n)+' '))),
div("golden: ", this.data.map!(golden))
),

div('AsyncIterator array item access',
Expand Down
11 changes: 6 additions & 5 deletions module/dist/ai-ui.cjs

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions module/dist/ai-ui.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions module/dist/ai-ui.min.cjs

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions module/dist/ai-ui.min.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions module/dist/ai-ui.min.mjs

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions module/dist/ai-ui.mjs

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions module/esm/when.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ function docEventHandler(ev) {
if (observations) {
for (const o of observations) {
try {
const { push, terminate, container, selector, includeChildren } = o;
if (!container.isConnected) {
const msg = "Container `#" + container.id + ">" + (selector || '') + "` removed from DOM. Removing subscription";
const { push, terminate, containerRef, selector, includeChildren } = o;
const container = containerRef.deref();
if (!container || !container.isConnected) {
const msg = "Container `#" + container?.id + ">" + (selector || '') + "` removed from DOM. Removing subscription";
observations.delete(o);
terminate(new Error(msg));
}
Expand Down Expand Up @@ -75,7 +76,7 @@ function whenEvent(container, what) {
const details = {
push: queue.push,
terminate(ex) { queue.return?.(ex); },
container,
containerRef: new WeakRef(container),
includeChildren,
selector
};
Expand Down
11 changes: 6 additions & 5 deletions module/src/when.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type ExtractEvents<S> = WhenEvents[ExtractEventNames<S>];
type EventObservation<EventName extends keyof GlobalEventHandlersEventMap> = {
push: (ev: GlobalEventHandlersEventMap[EventName])=>void;
terminate: (ex: Error)=>void;
container: Element
containerRef: WeakRef<Element>
selector: string | null;
includeChildren: boolean;
};
Expand All @@ -102,9 +102,10 @@ function docEventHandler<EventName extends keyof GlobalEventHandlersEventMap>(th
if (observations) {
for (const o of observations) {
try {
const { push, terminate, container, selector, includeChildren } = o;
if (!container.isConnected) {
const msg = "Container `#" + container.id + ">" + (selector || '') + "` removed from DOM. Removing subscription";
const { push, terminate, containerRef, selector, includeChildren } = o;
const container = containerRef.deref();
if (!container || !container.isConnected) {
const msg = "Container `#" + container?.id + ">" + (selector || '') + "` removed from DOM. Removing subscription";
observations.delete(o);
terminate(new Error(msg));
} else {
Expand Down Expand Up @@ -174,7 +175,7 @@ function whenEvent<EventName extends string>(container: Element, what: IsValidWh
const details: EventObservation<keyof GlobalEventHandlersEventMap> = {
push: queue.push,
terminate(ex: Error) { queue.return?.(ex)},
container,
containerRef: new WeakRef(container),
includeChildren,
selector
};
Expand Down
16 changes: 11 additions & 5 deletions type_tests/iterable-array-map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { tag } from '../module/src/ai-ui';
import { tag } from '../module/src/ai-ui.js';

/* Specify what base tags you reference in your UI */
const { div } = tag();
Expand All @@ -8,29 +8,34 @@ function sum(a:number, b:number) {
}

const App = div.extended(({
styles:`.App > div { margin-bottom: 1em } .App > div > div { margin-left: 1em }`,
override: {
className: 'App',
onclick() {
this.data.unshift(this.data[0] + this.data[1]);
// @ts-expect-error: see iterators.ts#IterableProperties
this.data = [1,2];
this.data = [200,400];
this.data.unshift(this.data[0] + this.data[1]);
}
},
iterable: {
data: [3,2,1] as number[]
},
constructed() {
const t = this.data;
const golden = () => this.data[0] / this.data[1];
return [
div('Array access',
div('join: ',t.join(', ')),
div('reduce: ',t.reduce(sum,0)),
div('map: ', Array.prototype.map.call(t, n => String(n)+' ') as string[])
div('map: ', Array.prototype.map.call(t, n => String(n)+' ') as string[]),
div("golden: ", golden())
),

div('AsyncIterator array access',
div('join: ', t.map!(d => d.join(', '))),
div('reduce: ', t.map!(d => d.reduce(sum,0))),
div('map: ', t.map!(d => d.map( n => String(n)+' ')))
div('map: ', t.map!(d => d.map( n => String(n)+' '))),
div("golden: ", this.data.map!(golden))
),

div('AsyncIterator array item access',
Expand All @@ -43,3 +48,4 @@ const App = div.extended(({

/* Add add it to the document so the user can see it! */
document.body.append(App());

0 comments on commit 2571765

Please sign in to comment.