diff --git a/RouteReactor.js b/RouteReactor.js index 24eeeb4..80f0a67 100644 --- a/RouteReactor.js +++ b/RouteReactor.js @@ -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; - } } diff --git a/observeable.js b/observeable.js index 53f3a55..337be57 100644 --- a/observeable.js +++ b/observeable.js @@ -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); } } diff --git a/router.js b/router.js index 0dbd598..2ce1faa 100644 --- a/router.js +++ b/router.js @@ -119,9 +119,7 @@ export class ContextReactor { } hostConnected() { - this._initializing = true; - _routeChangeObservable.subscribe(this._onRouteChange); - this._initializing = false; + _routeChangeObservable.subscribe(this._onRouteChange, this._initialize); } hostDisconnected() { @@ -129,12 +127,8 @@ export class ContextReactor { } _onRouteChange(context) { - if (this._initializing) { - this._initialize?.(context); - } else { - this._callback?.(context); - this.host.requestUpdate(); - } + this._callback?.(context); + this.host.requestUpdate(); } }