Skip to content

Commit

Permalink
fix in observer disposer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yesil committed Nov 19, 2024
1 parent efd82a8 commit e1bbd8c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 26 deletions.
4 changes: 2 additions & 2 deletions app/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LitElement, html, css } from 'https://jspm.dev/npm:lit@2';
import { StoreObservable } from './model.js';
import { makeObserver, reaction } from '../dist/picosm.js';
import { litObserver, reaction } from '../dist/picosm.js';

/**
* also see ../index.html
Expand Down Expand Up @@ -178,4 +178,4 @@ export class App extends LitElement {
}
}

customElements.define('pico-demo', makeObserver(App, ['store']));
customElements.define('pico-demo', litObserver(App, ['store']));
23 changes: 12 additions & 11 deletions dist/picosm.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,23 @@ function untrack(target, source) {
}

// src/LitObserver.js
function makeObserver(constructor, properties) {
function litObserver(constructor, properties) {
return class LitObserver extends constructor {
#observables = /* @__PURE__ */ new Set();
#disposers = /* @__PURE__ */ new Set();
constructor(...args) {
super(...args);
this.observables = /* @__PURE__ */ new Set();
this.disposers = /* @__PURE__ */ new Set();
}
trackProperties() {
properties.forEach((property) => {
const observable = this[property];
if (!observable?.__observers)
return;
if (this.observables.has(observable)) {
if (this.#observables.has(observable)) {
return;
}
this.observables.add(observable);
observe(observable, this.requestUpdate.bind(this));
this.#observables.add(observable);
this.#disposers.add(observe(observable, this.requestUpdate.bind(this)));
});
}
update(changedProperties) {
Expand All @@ -144,22 +144,23 @@ function makeObserver(constructor, properties) {
}
connectedCallback() {
super.connectedCallback();
this.observables.forEach((o) => {
observe(o, this.requestUpdate.bind(this));
this.#observables.forEach((o) => {
this.#disposers.add(observe(o, this.requestUpdate.bind(this)));
});
}
disconnectedCallback() {
super.disconnectedCallback();
this.disposers.forEach((disposer) => {
this.#disposers.forEach((disposer) => {
disposer();
});
this.disposers.clear();
this.#disposers.clear();
console.log(this.#disposers.size);
}
};
}
export {
litObserver,
makeObservable,
makeObserver,
observe,
reaction,
track,
Expand Down
Loading

0 comments on commit e1bbd8c

Please sign in to comment.