Skip to content

Commit

Permalink
Adds two more lifecycle methods
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Feb 7, 2015
1 parent 395db8f commit 25dd191
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 36 deletions.
46 changes: 35 additions & 11 deletions dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ var INIT_SNAPSHOT = Symbol("init snapshot storage");
var LAST_SNAPSHOT = Symbol("last snapshot storage");
var LISTENERS = Symbol("stores action listeners storage");
var STATE_CONTAINER = VariableSymbol("the state container");
var STORE_BOOTSTRAP = Symbol("event handler onBootstrap");
var STORE_SNAPSHOT = Symbol("event handler onTakeSnapshot");
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,8 +55,15 @@ var AltStore = (function () {

this[STATE_CONTAINER] = state;
this[EE] = new EventEmitter();
if (state.onBootstrap) {
this[STORE_BOOTSTRAP] = state.onBootstrap.bind(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);
Expand All @@ -67,6 +76,10 @@ var AltStore = (function () {
result !== false && _this.emitChange();
}
});

if (this[STORE_INIT]) {
this[STORE_INIT]();
}
}

to5Runtime.prototypeProperties(AltStore, null, {
Expand Down Expand Up @@ -189,13 +202,11 @@ var StoreMixin = {
}
};

var setAppState = function (instance, data) {
var setAppState = function (instance, data, onStore) {
var obj = JSON.parse(data);
Object.keys(obj).forEach(function (key) {
assign(instance.stores[key][STATE_CONTAINER], obj[key]);
if (instance.stores[key][STORE_BOOTSTRAP]) {
instance.stores[key][STORE_BOOTSTRAP]();
}
onStore(instance.stores[key]);
});
};

Expand Down Expand Up @@ -339,7 +350,11 @@ var Alt = (function () {
},
rollback: {
value: function rollback() {
setAppState(this, this[LAST_SNAPSHOT]);
setAppState(this, this[LAST_SNAPSHOT], function (store) {
if (store[STORE_ROLLBACK]) {
store[STORE_ROLLBACK]();
}
});
},
writable: true,
configurable: true
Expand All @@ -352,7 +367,11 @@ var Alt = (function () {

var snapshot = storeNames.length ? filterSnapshotOfStores(this[INIT_SNAPSHOT], storeNames) : this[INIT_SNAPSHOT];

setAppState(this, snapshot);
setAppState(this, snapshot, function (store) {
if (store[STORE_INIT]) {
store[STORE_INIT]();
}
});
},
writable: true,
configurable: true
Expand All @@ -368,7 +387,12 @@ var Alt = (function () {
},
bootstrap: {
value: function bootstrap(data) {
setAppState(this, data);
setAppState(this, data, function (store) {
if (store[STORE_BOOTSTRAP]) {
store[STORE_BOOTSTRAP]();
}
});

if (typeof window !== "undefined") {
if (this[BOOTSTRAP_FLAG]) {
throw new ReferenceError("Stores have already been bootstrapped");
Expand Down
46 changes: 35 additions & 11 deletions dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ var INIT_SNAPSHOT = Symbol("init snapshot storage");
var LAST_SNAPSHOT = Symbol("last snapshot storage");
var LISTENERS = Symbol("stores action listeners storage");
var STATE_CONTAINER = VariableSymbol("the state container");
var STORE_BOOTSTRAP = Symbol("event handler onBootstrap");
var STORE_SNAPSHOT = Symbol("event handler onTakeSnapshot");
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,8 +59,15 @@ var AltStore = (function () {

this[STATE_CONTAINER] = state;
this[EE] = new EventEmitter();
if (state.onBootstrap) {
this[STORE_BOOTSTRAP] = state.onBootstrap.bind(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);
Expand All @@ -71,6 +80,10 @@ var AltStore = (function () {
result !== false && _this.emitChange();
}
});

if (this[STORE_INIT]) {
this[STORE_INIT]();
}
}

_prototypeProperties(AltStore, null, {
Expand Down Expand Up @@ -193,13 +206,11 @@ var StoreMixin = {
}
};

var setAppState = function (instance, data) {
var setAppState = function (instance, data, onStore) {
var obj = JSON.parse(data);
Object.keys(obj).forEach(function (key) {
assign(instance.stores[key][STATE_CONTAINER], obj[key]);
if (instance.stores[key][STORE_BOOTSTRAP]) {
instance.stores[key][STORE_BOOTSTRAP]();
}
onStore(instance.stores[key]);
});
};

Expand Down Expand Up @@ -343,7 +354,11 @@ var Alt = (function () {
},
rollback: {
value: function rollback() {
setAppState(this, this[LAST_SNAPSHOT]);
setAppState(this, this[LAST_SNAPSHOT], function (store) {
if (store[STORE_ROLLBACK]) {
store[STORE_ROLLBACK]();
}
});
},
writable: true,
configurable: true
Expand All @@ -356,7 +371,11 @@ var Alt = (function () {

var snapshot = storeNames.length ? filterSnapshotOfStores(this[INIT_SNAPSHOT], storeNames) : this[INIT_SNAPSHOT];

setAppState(this, snapshot);
setAppState(this, snapshot, function (store) {
if (store[STORE_INIT]) {
store[STORE_INIT]();
}
});
},
writable: true,
configurable: true
Expand All @@ -372,7 +391,12 @@ var Alt = (function () {
},
bootstrap: {
value: function bootstrap(data) {
setAppState(this, data);
setAppState(this, data, function (store) {
if (store[STORE_BOOTSTRAP]) {
store[STORE_BOOTSTRAP]();
}
});

if (typeof window !== "undefined") {
if (this[BOOTSTRAP_FLAG]) {
throw new ReferenceError("Stores have already been bootstrapped");
Expand Down
46 changes: 35 additions & 11 deletions src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ let INIT_SNAPSHOT = Symbol('init snapshot storage')
let LAST_SNAPSHOT = Symbol('last snapshot storage')
let LISTENERS = Symbol('stores action listeners storage')
let STATE_CONTAINER = VariableSymbol('the state container')
let STORE_BOOTSTRAP = Symbol('event handler onBootstrap')
let STORE_SNAPSHOT = Symbol('event handler onTakeSnapshot')
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 Down Expand Up @@ -48,8 +50,15 @@ class AltStore {
constructor(dispatcher, state) {
this[STATE_CONTAINER] = state
this[EE] = new EventEmitter()
if (state.onBootstrap) {
this[STORE_BOOTSTRAP] = state.onBootstrap.bind(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)
Expand All @@ -62,6 +71,10 @@ class AltStore {
result !== false && this.emitChange()
}
})

if (this[STORE_INIT]) {
this[STORE_INIT]()
}
}

emitChange() {
Expand Down Expand Up @@ -165,13 +178,11 @@ let StoreMixin = {
}
}

let setAppState = (instance, data) => {
let setAppState = (instance, data, onStore) => {
let obj = JSON.parse(data)
Object.keys(obj).forEach((key) => {
assign(instance.stores[key][STATE_CONTAINER], obj[key])
if (instance.stores[key][STORE_BOOTSTRAP]) {
instance.stores[key][STORE_BOOTSTRAP]()
}
onStore(instance.stores[key])
})
}

Expand Down Expand Up @@ -299,15 +310,23 @@ your own custom identifier for each store`
}

rollback() {
setAppState(this, this[LAST_SNAPSHOT])
setAppState(this, this[LAST_SNAPSHOT], (store) => {
if (store[STORE_ROLLBACK]) {
store[STORE_ROLLBACK]()
}
})
}

recycle(...storeNames) {
let snapshot = storeNames.length
? filterSnapshotOfStores(this[INIT_SNAPSHOT], storeNames)
: this[INIT_SNAPSHOT]

setAppState(this, snapshot)
setAppState(this, snapshot, (store) => {
if (store[STORE_INIT]) {
store[STORE_INIT]()
}
})
}

flush() {
Expand All @@ -317,7 +336,12 @@ your own custom identifier for each store`
}

bootstrap(data) {
setAppState(this, data)
setAppState(this, data, (store) => {
if (store[STORE_BOOTSTRAP]) {
store[STORE_BOOTSTRAP]()
}
})

if (typeof window !== 'undefined') {
if (this[BOOTSTRAP_FLAG]) {
throw new ReferenceError('Stores have already been bootstrapped')
Expand Down
35 changes: 32 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class MyActions {
'getInstanceInside',
'dontEmit',
'moreActions2',
'moreActions3'
'moreActions3',
'resetRecycled'
)
this.generateActions('anotherAction')
}
Expand Down Expand Up @@ -91,9 +92,19 @@ class SecondStore {

this.deferrals = 0

this.recycled = false

this.bindActions(myActions)
}

onInitialized() {
this.recycled = true
}

onResetRecycled() {
this.recycled = false
}

onUpdateTwo(x) {
this.foo = x.a + x.b
}
Expand Down Expand Up @@ -143,16 +154,26 @@ var secondStore = alt.createStore(SecondStore, 'AltSecondStore')
class LifeCycleStore {
constructor() {
this.bootstrapped = false
this.init = false
this.rollback = false
this.snapshotted = false
}

onBootstrap() {
onInitialized() {
this.init = true
}

onBootstrapped() {
this.bootstrapped = true
}

onTakeSnapshot() {
this.snapshotted = true
}

onRolledback() {
this.rollback = true
}
}

var lifecycleStore = alt.createStore(LifeCycleStore)
Expand Down Expand Up @@ -238,8 +259,10 @@ module.exports = {
'getting state'() {
assert.equal(typeof myStore.getState()._dispatcher, 'object', 'the dispatcher is exposed internally')

assert.equal(lifecycleStore.getState().bootstrapped, true, 'bootstrap has not been called yet, but recycle calls onBootstrap')
assert.equal(lifecycleStore.getState().bootstrapped, false, 'bootstrap has not been called yet')
assert.equal(lifecycleStore.getState().snapshotted, false, 'takeSnapshot has not been called yet')
assert.equal(lifecycleStore.getState().rollback, false, 'rollback has not been called')
assert.equal(lifecycleStore.getState().init, true, 'init gets called when store initializes')
},

'snapshots and bootstrapping'() {
Expand Down Expand Up @@ -323,6 +346,7 @@ module.exports = {
assert.equal(rollbackValue, undefined, 'rollback returns nothing')

assert.equal(myStore.getState().name, 'bear', 'state has been rolledback to last snapshot')
assert.equal(lifecycleStore.getState().rollback, true, 'rollback lifecycle method was called')
},

'store listening'() {
Expand Down Expand Up @@ -570,6 +594,11 @@ module.exports = {
'recycling'() {
alt.recycle()
assert.equal(myStore.getState().name, 'first', 'recycle sets the state back to its origin')

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')
},

'flushing'() {
Expand Down

0 comments on commit 25dd191

Please sign in to comment.