Skip to content

Commit

Permalink
Replace lifecycle method API
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Feb 7, 2015
1 parent 25dd191 commit 4c76f7a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 100 deletions.
46 changes: 19 additions & 27 deletions dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -77,8 +64,8 @@ var AltStore = (function () {
}
});

if (this[STORE_INIT]) {
this[STORE_INIT]();
if (this[LIFECYCLE].init) {
this[LIFECYCLE].init();
}
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
});
},
Expand All @@ -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();
}
});
},
Expand All @@ -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();
}
});

Expand Down
46 changes: 19 additions & 27 deletions dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -81,8 +68,8 @@ var AltStore = (function () {
}
});

if (this[STORE_INIT]) {
this[STORE_INIT]();
if (this[LIFECYCLE].init) {
this[LIFECYCLE].init();
}
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
});
},
Expand All @@ -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();
}
});
},
Expand All @@ -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();
}
});

Expand Down
46 changes: 19 additions & 27 deletions src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -72,8 +59,8 @@ class AltStore {
}
})

if (this[STORE_INIT]) {
this[STORE_INIT]()
if (this[LIFECYCLE].init) {
this[LIFECYCLE].init()
}
}

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
}
})
}
Expand All @@ -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()
}
})
}
Expand All @@ -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()
}
})

Expand Down
35 changes: 16 additions & 19 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ class SecondStore {
this.recycled = false

this.bindActions(myActions)
}

onInitialized() {
this.recycled = true
this.on('init', () => {
this.recycled = true
})
}

onResetRecycled() {
Expand Down Expand Up @@ -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
})
}
}

Expand Down Expand Up @@ -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'() {
Expand Down

0 comments on commit 4c76f7a

Please sign in to comment.