Skip to content

Commit

Permalink
Simplify a couple of things
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-ramirez-arredondo committed Oct 25, 2024
1 parent 9f56979 commit cff9bd1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
21 changes: 10 additions & 11 deletions RouteReactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ export class RouteReactor {
constructor(host) {
this.host = host;

this._updateState = this._updateState.bind(this);
this._contextReactor = new ContextReactor(host, this._updateState, this._updateState);
const updateState = (ctx) => {
const reduced = _createReducedContext(ctx);

Object.keys(reduced).forEach(ctxKey => {
this[ctxKey] = reduced[ctxKey];
});

this._view = ctx.view;
};
this._contextReactor = new ContextReactor(host, updateState, updateState);
}

renderView(opts) {
return this._view?.(this.host, opts);
}

_updateState(ctx) {
const reduced = _createReducedContext(ctx);
Object.keys(reduced).forEach(ctxKey => {
this[ctxKey] = reduced[ctxKey];
});

this._view = ctx.view;
}
}
4 changes: 2 additions & 2 deletions observeable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export default class Observable {
this._previousData = data;
}

subscribe(observer) {
subscribe(observer, initialize) {
this._observers.set(observer, observer);
if (this._hasTriggered) {
observer(this._previousData);
initialize?.(this._previousData);
}
}

Expand Down
12 changes: 3 additions & 9 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,16 @@ export class ContextReactor {
}

hostConnected() {
this._initializing = true;
_routeChangeObservable.subscribe(this._onRouteChange);
this._initializing = false;
_routeChangeObservable.subscribe(this._onRouteChange, this._initialize);
}

hostDisconnected() {
_routeChangeObservable.unsubscribe(this._onRouteChange);
}

_onRouteChange(context) {
if (this._initializing) {
this._initialize?.(context);
} else {
this._callback?.(context);
this.host.requestUpdate();
}
this._callback?.(context);
this.host.requestUpdate();
}
}

Expand Down

0 comments on commit cff9bd1

Please sign in to comment.