Skip to content
Snippets Groups Projects
Commit 673a7f44 authored by Greg Fodor's avatar Greg Fodor
Browse files

Feedback from PR

parent 47f21a6e
No related branches found
No related tags found
No related merge requests found
...@@ -14,7 +14,7 @@ class ProfileEntryPanel extends Component { ...@@ -14,7 +14,7 @@ class ProfileEntryPanel extends Component {
super(props); super(props);
window.store = this.props.store; window.store = this.props.store;
this.state = {name: this.props.store.state.profile.display_name}; this.state = {name: this.props.store.state.profile.display_name};
this.props.store.subscribe(this.storeUpdated); this.props.store.addEventListener("statechanged", this.storeUpdated);
} }
storeUpdated = () => { storeUpdated = () => {
...@@ -35,7 +35,7 @@ class ProfileEntryPanel extends Component { ...@@ -35,7 +35,7 @@ class ProfileEntryPanel extends Component {
} }
componentWillUnmount() { componentWillUnmount() {
this.props.store.unsubscribe(this.storeUpdated); this.props.store.removeEventListener('statechanged', this.storeUpdated);
} }
render () { render () {
......
...@@ -4,6 +4,7 @@ import { Validator } from "jsonschema"; ...@@ -4,6 +4,7 @@ import { Validator } from "jsonschema";
const LOCAL_STORE_KEY = "___mozilla_duck"; const LOCAL_STORE_KEY = "___mozilla_duck";
const STORE_STATE_CACHE_KEY = Symbol(); const STORE_STATE_CACHE_KEY = Symbol();
const validator = new Validator(); const validator = new Validator();
import { EventTarget } from "event-target-shim"
// Durable (via local-storage) schema-enforced state that is meant to be consumed via forward data flow. // Durable (via local-storage) schema-enforced state that is meant to be consumed via forward data flow.
// (Think flux but with way less incidental complexity, at least for now :)) // (Think flux but with way less incidental complexity, at least for now :))
...@@ -30,10 +31,10 @@ export const SCHEMA = { ...@@ -30,10 +31,10 @@ export const SCHEMA = {
additionalProperties: false additionalProperties: false
} }
export default class Store { export default class Store extends EventTarget {
subscribers = new Set();
constructor() { constructor() {
super();
if (localStorage.getItem(LOCAL_STORE_KEY) === null) { if (localStorage.getItem(LOCAL_STORE_KEY) === null) {
localStorage.setItem(LOCAL_STORE_KEY, JSON.stringify({ id: uuid() })); localStorage.setItem(LOCAL_STORE_KEY, JSON.stringify({ id: uuid() }));
} }
...@@ -47,34 +48,23 @@ export default class Store { ...@@ -47,34 +48,23 @@ export default class Store {
return this[STORE_STATE_CACHE_KEY]; return this[STORE_STATE_CACHE_KEY];
} }
subscribe(subscriber) {
this.subscribers.add(subscriber);
}
unsubscribe(subscriber) {
this.subscribers.delete(subscriber);
}
update(newState) { update(newState) {
if (newState.id) { if (newState.id) {
console.error("Store id is immutable."); throw "Store id is immutable.";
return;
} }
const finalState = { ...this.state, ...newState }; const finalState = { ...this.state, ...newState };
const isValid = validator.validate(finalState, SCHEMA).valid; const isValid = validator.validate(finalState, SCHEMA).valid;
if (!isValid) { if (!isValid) {
console.warn(`Write of ${JSON.stringify(finalState)} to store failed schema validation.`) throw `Write of ${JSON.stringify(finalState)} to store failed schema validation.`;
return; return;
} }
localStorage.setItem(LOCAL_STORE_KEY, JSON.stringify(finalState)); localStorage.setItem(LOCAL_STORE_KEY, JSON.stringify(finalState));
delete this[STORE_STATE_CACHE_KEY]; delete this[STORE_STATE_CACHE_KEY];
for (const subscriber of this.subscribers) { this.dispatchEvent(new CustomEvent("statechanged"));
subscriber();
}
return finalState; return finalState;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment