Skip to content

Commit 3c8ffb7

Browse files
committed
cleanup init logic
1 parent ab21d2c commit 3c8ffb7

4 files changed

Lines changed: 15 additions & 5 deletions

File tree

integration/todo/src/elements/todo-card.element.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ import type { TodoStatus } from "../services/todo.service.js";
5757
],
5858
})
5959
export class TodoCardElement extends HTMLElement {
60-
@attr()
61-
@observe()
62-
accessor status: TodoStatus = "active";
63-
6460
@bind(({ status }) => ({
6561
value: status === "active" ? "complete" : "active",
6662
showStar: status === "complete",
@@ -70,6 +66,10 @@ export class TodoCardElement extends HTMLElement {
7066
showStar: false,
7167
};
7268

69+
@attr()
70+
@observe()
71+
accessor status: TodoStatus = "active";
72+
7373
@listen("click", "#complete")
7474
onClick() {
7575
this.dispatchEvent(new Event("complete", { bubbles: true }));

packages/observable/src/lib/metadata.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ it("should return default metadata", () => {
1010
changes: new Changes(),
1111
scheduler: null,
1212
bindings: new Set<() => void>(),
13+
initialized: new Set<string | symbol>(),
1314
});
1415
});
1516

packages/observable/src/lib/metadata.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class ObservableInstanceMetadata<T> {
1313
scheduler: Promise<void> | null = null;
1414
changes: Changes<T> = new Changes();
1515
bindings: Set<(changes: Changes<T>) => void> = new Set();
16+
initialized: Set<string | symbol> = new Set();
1617
}
1718

1819
export class ObservableInstanceMetaDataStore extends WeakMap<

packages/observable/src/lib/observe.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function observe<This extends object, Value>(mapper?: (instance: This) =>
1111

1212
observableMeta.effects.add(function mapperFn(this: This) {
1313
if (mapper) {
14-
ctx.access.set(this, mapper(this));
14+
base.set.call(this, mapper(this));
1515
}
1616
});
1717

@@ -35,6 +35,14 @@ export function observe<This extends object, Value>(mapper?: (instance: This) =>
3535
},
3636
get() {
3737
if (mapper) {
38+
const instanceMeta = instanceMetadataStore.read<This>(this);
39+
40+
if (!instanceMeta.initialized.has(ctx.name)) {
41+
instanceMeta.initialized.add(ctx.name);
42+
43+
return mapper(this);
44+
}
45+
3846
return mapper(this);
3947
}
4048

0 commit comments

Comments
 (0)