From 4c76f7a54f3ceec028ca473b024fdc88ae34292f Mon Sep 17 00:00:00 2001 From: Josh Perez Date: Fri, 6 Feb 2015 17:43:42 -0800 Subject: [PATCH] Replace lifecycle method API --- dist/alt-with-runtime.js | 46 +++++++++++++++++----------------------- dist/alt.js | 46 +++++++++++++++++----------------------- src/alt.js | 46 +++++++++++++++++----------------------- test/index.js | 35 ++++++++++++++---------------- 4 files changed, 73 insertions(+), 100 deletions(-) diff --git a/dist/alt-with-runtime.js b/dist/alt-with-runtime.js index fd579b04..7c7de677 100644 --- a/dist/alt-with-runtime.js +++ b/dist/alt-with-runtime.js @@ -18,12 +18,9 @@ var BOOTSTRAP_FLAG = VariableSymbol("has bootstrap"); var EE = Symbol("event emitter instance"); var INIT_SNAPSHOT = Symbol("init snapshot storage"); var LAST_SNAPSHOT = Symbol("last snapshot storage"); +var LIFECYCLE = Symbol("store lifecycle listeners"); var LISTENERS = Symbol("stores action listeners storage"); var STATE_CONTAINER = VariableSymbol("the state container"); -var STORE_BOOTSTRAP = Symbol("onBootstrapped"); -var STORE_INIT = Symbol("onInitialized"); -var STORE_SNAPSHOT = Symbol("onTakeSnapshot"); -var STORE_ROLLBACK = Symbol("onRolledback"); var formatAsConstant = function (name) { return name.replace(/[a-z]([A-Z])/g, function (i) { @@ -53,21 +50,11 @@ var AltStore = (function () { var _this = this; to5Runtime.classCallCheck(this, AltStore); - this[STATE_CONTAINER] = state; this[EE] = new EventEmitter(); + this[LIFECYCLE] = {}; + this[STATE_CONTAINER] = state; - if (state.onBootstrapped) { - this[STORE_BOOTSTRAP] = state.onBootstrapped.bind(state); - } - if (state.onInitialized) { - this[STORE_INIT] = state.onInitialized.bind(state); - } - if (state.onRolledback) { - this[STORE_ROLLBACK] = state.onRolledback.bind(state); - } - if (state.onTakeSnapshot) { - this[STORE_SNAPSHOT] = state.onTakeSnapshot.bind(state); - } + assign(this[LIFECYCLE], state[LIFECYCLE]); // Register dispatcher this.dispatchToken = dispatcher.register(function (payload) { @@ -77,8 +64,8 @@ var AltStore = (function () { } }); - if (this[STORE_INIT]) { - this[STORE_INIT](); + if (this[LIFECYCLE].init) { + this[LIFECYCLE].init(); } } @@ -146,6 +133,10 @@ var ActionCreator = (function () { })(); var StoreMixin = { + on: function on(lifecycleEvent, handler) { + this[LIFECYCLE][lifecycleEvent] = handler.bind(this); + }, + bindAction: function bindAction(symbol, handler) { if (!symbol) { throw new ReferenceError("Invalid action reference passed in"); @@ -212,8 +203,8 @@ var setAppState = function (instance, data, onStore) { var snapshot = function (instance) { return JSON.stringify(Object.keys(instance.stores).reduce(function (obj, key) { - if (instance.stores[key][STORE_SNAPSHOT]) { - instance.stores[key][STORE_SNAPSHOT](); + if (instance.stores[key][LIFECYCLE].snapshot) { + instance.stores[key][LIFECYCLE].snapshot(); } obj[key] = instance.stores[key].getState(); return obj; @@ -259,6 +250,7 @@ var Alt = (function () { // prototype with the mixin behaviour and I'm extending from StoreModel // so we can inherit any extensions from the provided store. function Store() { + this[LIFECYCLE] = {}; this[LISTENERS] = {}; StoreModel.call(this); } @@ -351,8 +343,8 @@ var Alt = (function () { rollback: { value: function rollback() { setAppState(this, this[LAST_SNAPSHOT], function (store) { - if (store[STORE_ROLLBACK]) { - store[STORE_ROLLBACK](); + if (store[LIFECYCLE].rollback) { + store[LIFECYCLE].rollback(); } }); }, @@ -368,8 +360,8 @@ var Alt = (function () { var snapshot = storeNames.length ? filterSnapshotOfStores(this[INIT_SNAPSHOT], storeNames) : this[INIT_SNAPSHOT]; setAppState(this, snapshot, function (store) { - if (store[STORE_INIT]) { - store[STORE_INIT](); + if (store[LIFECYCLE].init) { + store[LIFECYCLE].init(); } }); }, @@ -388,8 +380,8 @@ var Alt = (function () { bootstrap: { value: function bootstrap(data) { setAppState(this, data, function (store) { - if (store[STORE_BOOTSTRAP]) { - store[STORE_BOOTSTRAP](); + if (store[LIFECYCLE].bootstrap) { + store[LIFECYCLE].bootstrap(); } }); diff --git a/dist/alt.js b/dist/alt.js index d3b25983..b868eaf0 100644 --- a/dist/alt.js +++ b/dist/alt.js @@ -22,12 +22,9 @@ var BOOTSTRAP_FLAG = VariableSymbol("has bootstrap"); var EE = Symbol("event emitter instance"); var INIT_SNAPSHOT = Symbol("init snapshot storage"); var LAST_SNAPSHOT = Symbol("last snapshot storage"); +var LIFECYCLE = Symbol("store lifecycle listeners"); var LISTENERS = Symbol("stores action listeners storage"); var STATE_CONTAINER = VariableSymbol("the state container"); -var STORE_BOOTSTRAP = Symbol("onBootstrapped"); -var STORE_INIT = Symbol("onInitialized"); -var STORE_SNAPSHOT = Symbol("onTakeSnapshot"); -var STORE_ROLLBACK = Symbol("onRolledback"); var formatAsConstant = function (name) { return name.replace(/[a-z]([A-Z])/g, function (i) { @@ -57,21 +54,11 @@ var AltStore = (function () { var _this = this; _classCallCheck(this, AltStore); - this[STATE_CONTAINER] = state; this[EE] = new EventEmitter(); + this[LIFECYCLE] = {}; + this[STATE_CONTAINER] = state; - if (state.onBootstrapped) { - this[STORE_BOOTSTRAP] = state.onBootstrapped.bind(state); - } - if (state.onInitialized) { - this[STORE_INIT] = state.onInitialized.bind(state); - } - if (state.onRolledback) { - this[STORE_ROLLBACK] = state.onRolledback.bind(state); - } - if (state.onTakeSnapshot) { - this[STORE_SNAPSHOT] = state.onTakeSnapshot.bind(state); - } + assign(this[LIFECYCLE], state[LIFECYCLE]); // Register dispatcher this.dispatchToken = dispatcher.register(function (payload) { @@ -81,8 +68,8 @@ var AltStore = (function () { } }); - if (this[STORE_INIT]) { - this[STORE_INIT](); + if (this[LIFECYCLE].init) { + this[LIFECYCLE].init(); } } @@ -150,6 +137,10 @@ var ActionCreator = (function () { })(); var StoreMixin = { + on: function on(lifecycleEvent, handler) { + this[LIFECYCLE][lifecycleEvent] = handler.bind(this); + }, + bindAction: function bindAction(symbol, handler) { if (!symbol) { throw new ReferenceError("Invalid action reference passed in"); @@ -216,8 +207,8 @@ var setAppState = function (instance, data, onStore) { var snapshot = function (instance) { return JSON.stringify(Object.keys(instance.stores).reduce(function (obj, key) { - if (instance.stores[key][STORE_SNAPSHOT]) { - instance.stores[key][STORE_SNAPSHOT](); + if (instance.stores[key][LIFECYCLE].snapshot) { + instance.stores[key][LIFECYCLE].snapshot(); } obj[key] = instance.stores[key].getState(); return obj; @@ -263,6 +254,7 @@ var Alt = (function () { // prototype with the mixin behaviour and I'm extending from StoreModel // so we can inherit any extensions from the provided store. function Store() { + this[LIFECYCLE] = {}; this[LISTENERS] = {}; StoreModel.call(this); } @@ -355,8 +347,8 @@ var Alt = (function () { rollback: { value: function rollback() { setAppState(this, this[LAST_SNAPSHOT], function (store) { - if (store[STORE_ROLLBACK]) { - store[STORE_ROLLBACK](); + if (store[LIFECYCLE].rollback) { + store[LIFECYCLE].rollback(); } }); }, @@ -372,8 +364,8 @@ var Alt = (function () { var snapshot = storeNames.length ? filterSnapshotOfStores(this[INIT_SNAPSHOT], storeNames) : this[INIT_SNAPSHOT]; setAppState(this, snapshot, function (store) { - if (store[STORE_INIT]) { - store[STORE_INIT](); + if (store[LIFECYCLE].init) { + store[LIFECYCLE].init(); } }); }, @@ -392,8 +384,8 @@ var Alt = (function () { bootstrap: { value: function bootstrap(data) { setAppState(this, data, function (store) { - if (store[STORE_BOOTSTRAP]) { - store[STORE_BOOTSTRAP](); + if (store[LIFECYCLE].bootstrap) { + store[LIFECYCLE].bootstrap(); } }); diff --git a/src/alt.js b/src/alt.js index 06a080ca..108eeb56 100644 --- a/src/alt.js +++ b/src/alt.js @@ -16,12 +16,9 @@ let BOOTSTRAP_FLAG = VariableSymbol('has bootstrap') let EE = Symbol('event emitter instance') let INIT_SNAPSHOT = Symbol('init snapshot storage') let LAST_SNAPSHOT = Symbol('last snapshot storage') +let LIFECYCLE = Symbol('store lifecycle listeners') let LISTENERS = Symbol('stores action listeners storage') let STATE_CONTAINER = VariableSymbol('the state container') -let STORE_BOOTSTRAP = Symbol('onBootstrapped') -let STORE_INIT = Symbol('onInitialized') -let STORE_SNAPSHOT = Symbol('onTakeSnapshot') -let STORE_ROLLBACK = Symbol('onRolledback') let formatAsConstant = (name) => { return name.replace(/[a-z]([A-Z])/g, (i) => { @@ -48,21 +45,11 @@ let getInternalMethods = (obj, excluded) => { class AltStore { constructor(dispatcher, state) { - this[STATE_CONTAINER] = state this[EE] = new EventEmitter() + this[LIFECYCLE] = {} + this[STATE_CONTAINER] = state - if (state.onBootstrapped) { - this[STORE_BOOTSTRAP] = state.onBootstrapped.bind(state) - } - if (state.onInitialized) { - this[STORE_INIT] = state.onInitialized.bind(state) - } - if (state.onRolledback) { - this[STORE_ROLLBACK] = state.onRolledback.bind(state) - } - if (state.onTakeSnapshot) { - this[STORE_SNAPSHOT] = state.onTakeSnapshot.bind(state) - } + assign(this[LIFECYCLE], state[LIFECYCLE]) // Register dispatcher this.dispatchToken = dispatcher.register((payload) => { @@ -72,8 +59,8 @@ class AltStore { } }) - if (this[STORE_INIT]) { - this[STORE_INIT]() + if (this[LIFECYCLE].init) { + this[LIFECYCLE].init() } } @@ -114,6 +101,10 @@ class ActionCreator { } let StoreMixin = { + on(lifecycleEvent, handler) { + this[LIFECYCLE][lifecycleEvent] = handler.bind(this) + }, + bindAction(symbol, handler) { if (!symbol) { throw new ReferenceError('Invalid action reference passed in') @@ -189,8 +180,8 @@ let setAppState = (instance, data, onStore) => { let snapshot = (instance) => { return JSON.stringify( Object.keys(instance.stores).reduce((obj, key) => { - if (instance.stores[key][STORE_SNAPSHOT]) { - instance.stores[key][STORE_SNAPSHOT]() + if (instance.stores[key][LIFECYCLE].snapshot) { + instance.stores[key][LIFECYCLE].snapshot() } obj[key] = instance.stores[key].getState() return obj @@ -232,6 +223,7 @@ class Alt { // prototype with the mixin behaviour and I'm extending from StoreModel // so we can inherit any extensions from the provided store. function Store() { + this[LIFECYCLE] = {} this[LISTENERS] = {} StoreModel.call(this) } @@ -311,8 +303,8 @@ your own custom identifier for each store` rollback() { setAppState(this, this[LAST_SNAPSHOT], (store) => { - if (store[STORE_ROLLBACK]) { - store[STORE_ROLLBACK]() + if (store[LIFECYCLE].rollback) { + store[LIFECYCLE].rollback() } }) } @@ -323,8 +315,8 @@ your own custom identifier for each store` : this[INIT_SNAPSHOT] setAppState(this, snapshot, (store) => { - if (store[STORE_INIT]) { - store[STORE_INIT]() + if (store[LIFECYCLE].init) { + store[LIFECYCLE].init() } }) } @@ -337,8 +329,8 @@ your own custom identifier for each store` bootstrap(data) { setAppState(this, data, (store) => { - if (store[STORE_BOOTSTRAP]) { - store[STORE_BOOTSTRAP]() + if (store[LIFECYCLE].bootstrap) { + store[LIFECYCLE].bootstrap() } }) diff --git a/test/index.js b/test/index.js index c87e1282..f79aa1ec 100644 --- a/test/index.js +++ b/test/index.js @@ -95,10 +95,10 @@ class SecondStore { this.recycled = false this.bindActions(myActions) - } - onInitialized() { - this.recycled = true + this.on('init', () => { + this.recycled = true + }) } onResetRecycled() { @@ -157,22 +157,19 @@ class LifeCycleStore { this.init = false this.rollback = false this.snapshotted = false - } - - onInitialized() { - this.init = true - } - - onBootstrapped() { - this.bootstrapped = true - } - onTakeSnapshot() { - this.snapshotted = true - } - - onRolledback() { - this.rollback = true + this.on('init', () => { + this.init = true + }) + this.on('bootstrap', () => { + this.bootstrapped = true + }) + this.on('snapshot', () => { + this.snapshotted = true + }) + this.on('rollback', () => { + this.rollback = true + }) } } @@ -598,7 +595,7 @@ module.exports = { myActions.resetRecycled() assert.equal(secondStore.getState().recycled, false, 'recycle var was reset due to action') alt.recycle() - assert.equal(secondStore.getState().recycled, true, 'onInitialized was called by recycling') + assert.equal(secondStore.getState().recycled, true, 'init lifecycle method was called by recycling') }, 'flushing'() {