Skip to content

Instantly share code, notes, and snippets.

@jankuca
Last active October 10, 2015 10:32
Show Gist options
  • Save jankuca/8da9d699742d3ab06eb0 to your computer and use it in GitHub Desktop.
Save jankuca/8da9d699742d3ab06eb0 to your computer and use it in GitHub Desktop.
Classes vs. Factories
class Dispatcher {
register(listener) {
this.listener = listener;
}
dispatch(action) {
if (this.listener) {
this.listener(action);
}
}
}
class Store {
register(dispatcher) {
this.dispatchIndex = dispatcher.register(this.__onDispatch.bind(this));
}
__onDispatch(action) {
console.log('action', action);
}
}
class UserDataStore extends Store {
constructor(revision) {
super();
this.revision = revision;
}
__onDispatch(action) {
console.log('doc', action.doc);
console.log('(doc instanceof Doc) =', action.doc instanceof Doc);
super.__onDispatch(action);
}
getRevisionById(revisionId) {
return this.revision;
}
};
class Doc {
constructor(revision) {
this.revision = revision;
}
}
class DocumentLoader {
constructor(dispatcher, userDataStore) {
this.dispatcher = dispatcher;
this.userDataStore = userDataStore;
}
loadRevisionById(revisionId) {
var revision = this.userDataStore.getRevisionById(revisionId);
var doc = new Doc(revision);
this.dispatcher.dispatch({ type: 'DOCUMENT_LOAD', doc });
}
}
var revision = { id: 1, name: 'Revision' };
var userDataStore = new UserDataStore(revision);
var dispatcher = new Dispatcher();
var documentLoader = new DocumentLoader(dispatcher, userDataStore);
userDataStore.register(dispatcher);
documentLoader.loadRevisionById(1);
var Dispatcher = {
create() {
return Object.create(Dispatcher);
},
register(listener) {
this.listener = listener;
},
dispatch(action) {
if (this.listener) {
this.listener(action);
}
}
};
var Store = {
register(dispatcher) {
this.dispatchIndex = dispatcher.register(this.__onDispatch.bind(this));
},
__onDispatch(action) {
console.log('action', action);
}
};
var UserDataStore = Object.assign(Object.create(Store), {
create(revision) {
return Object.assign(Object.create(UserDataStore), {
revision: revision
});
},
__onDispatch(action) {
console.log('doc', action.doc);
console.log('Doc.isPrototypeOf(Doc) ->', Doc.isPrototypeOf(action.doc))
Object.getPrototypeOf(UserDataStore).__onDispatch.call(this, action);
},
getRevisionById(revisionId) {
return this.revision;
}
});
var Doc = {
create(revision) {
return Object.assign(Object.create(Doc), {
revision: revision
});
}
};
var DocumentLoader = {
create(dispatcher, userDataStore) {
return Object.assign(Object.create(DocumentLoader), {
dispatcher: dispatcher,
userDataStore: userDataStore
});
},
loadRevisionById(revisionId) {
var revision = this.userDataStore.getRevisionById(revisionId);
var doc = Doc.create(revision);
this.dispatcher.dispatch({ type: 'DOCUMENT_LOAD', doc });
}
};
var revision = { id: 1, name: 'Revision' };
var userDataStore = UserDataStore.create(revision);
var dispatcher = Dispatcher.create();
var documentLoader = DocumentLoader.create(dispatcher, userDataStore);
userDataStore.register(dispatcher);
documentLoader.loadRevisionById(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment