Skip to content

Commit

Permalink
Observeable -> PubSub, support non-lit uses
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-ramirez-arredondo committed Nov 12, 2024
1 parent 047ef82 commit f69af5e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
28 changes: 0 additions & 28 deletions observeable.js

This file was deleted.

28 changes: 28 additions & 0 deletions pub-sub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default class PubSub {
constructor() {
this._listeners = new Map();
this._hasTriggered = false;
this._previousData = null;
}

clear() {
this._listeners.clear();
}

publish(data) {
this._listeners.forEach(listener => listener(data));
this._hasTriggered = true;
this._previousData = data;
}

subscribe(listener, initialize) {
this._listeners.set(listener, listener);
if (this._hasTriggered) {
initialize?.(this._previousData);
}
}

unsubscribe(listener) {
this._listeners.delete(listener);
}
}
26 changes: 18 additions & 8 deletions router.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Observable from './observeable.js';
import PubSub from './pub-sub.js';
import page from 'page';

Check failure on line 2 in router.js

View workflow job for this annotation

GitHub Actions / test

Imports should be sorted alphabetically

let activePage = page;
Expand All @@ -15,7 +15,7 @@ export const _createReducedContext = pageContext => ({
options: {},
});

const _routeChangeObservable = new Observable();
const _routeChangePubSub = new PubSub();

const _handleRouteView = (context, r) => {
if (r.view) {
Expand All @@ -24,7 +24,7 @@ const _handleRouteView = (context, r) => {
reducedContext.options = options || {};
return r.view.call(host, reducedContext);
};
_routeChangeObservable.notify(context);
_routeChangePubSub.publish(context);
}
};

Expand Down Expand Up @@ -106,25 +106,35 @@ export const redirect = path => {
export class ContextReactor {
constructor(host, callback, initialize) {
this.host = host;
this.host.addController(this);
this.host?.addController(this);

this._callback = callback;
this._initialize = initialize;

this._onRouteChange = this._onRouteChange.bind(this);

if (!this.host) this.connect(); // Support for non-LitElement use cases
}

connect() {
_routeChangePubSub.subscribe(this._onRouteChange, this._initialize);
}

disconnect() {
_routeChangePubSub.unsubscribe(this._onRouteChange);
}

hostConnected() {
_routeChangeObservable.subscribe(this._onRouteChange, this._initialize);
this.connect();
}

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

_onRouteChange(context) {
this._callback?.(context);
this.host.requestUpdate();
this.host?.requestUpdate();
}
}

Expand All @@ -133,7 +143,7 @@ export const RouterTesting = {
activePage.stop();
activePage = page.create();
hasRegistered = false;
_routeChangeObservable.clear();
_routeChangePubSub.clear();
},

restart: () => {
Expand Down

0 comments on commit f69af5e

Please sign in to comment.