Skip to content

Commit

Permalink
Add snapshot back in
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Mar 26, 2015
1 parent 07745f5 commit 5767ae6
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
14 changes: 13 additions & 1 deletion dist/alt-browser-with-addons.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Alt = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";

/**
* This mixin lets you setup your listeners. It is similar to Fluxible's mixin.
*
Expand Down Expand Up @@ -79,6 +80,7 @@ module.exports = FluxyMixin;

},{"./Subscribe":4}],2:[function(require,module,exports){
"use strict";

var Subscribe = require("./Subscribe");

var ListenerMixin = {
Expand Down Expand Up @@ -109,6 +111,7 @@ module.exports = ListenerMixin;

},{"./Subscribe":4}],3:[function(require,module,exports){
"use strict";

/**
* This mixin automatically sets the state for you based on the key you provide
*
Expand Down Expand Up @@ -190,6 +193,7 @@ module.exports = ReactStateMagicMixin;

},{"./Subscribe":4}],4:[function(require,module,exports){
"use strict";

var Symbol = require("es-symbol");
var MIXIN_REGISTRY = Symbol("alt store listeners");

Expand Down Expand Up @@ -1266,6 +1270,9 @@ var snapshot = function (instance) {
var stores = storeNames.length ? storeNames : Object.keys(instance.stores);
return stores.reduce(function (obj, key) {
var store = instance.stores[key];
if (store[LIFECYCLE].snapshot) {
store[LIFECYCLE].snapshot();
}
var customSnapshot = store[LIFECYCLE].serialize && store[LIFECYCLE].serialize();
obj[key] = customSnapshot ? customSnapshot : store.getState();
return obj;
Expand Down Expand Up @@ -1438,7 +1445,9 @@ var Alt = (function () {
}

return this.createActions(function () {
this.generateActions.apply(this, actionNames);
var _ref;

(_ref = this).generateActions.apply(_ref, actionNames);
});
}
},
Expand Down Expand Up @@ -1598,6 +1607,7 @@ module.exports = Alt;

},{"es-symbol":5,"eventemitter3":6,"flux":7,"object-assign":10}],13:[function(require,module,exports){
"use strict";

/**
* ActionListeners(alt: AltInstance): ActionListenersInstance
*
Expand Down Expand Up @@ -1660,6 +1670,7 @@ ActionListeners.prototype.removeAllActionListeners = function () {

},{"es-symbol":5}],14:[function(require,module,exports){
"use strict";

/**
* DispatcherRecorder(alt: AltInstance): DispatcherInstance
*
Expand Down Expand Up @@ -1795,6 +1806,7 @@ DispatcherRecorder.prototype.loadEvents = function (events) {

},{"es-symbol":5}],15:[function(require,module,exports){
"use strict";

/**
* makeFinalStore(alt: AltInstance): AltStore
*
Expand Down
7 changes: 6 additions & 1 deletion dist/alt-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,9 @@ var snapshot = function (instance) {
var stores = storeNames.length ? storeNames : Object.keys(instance.stores);
return stores.reduce(function (obj, key) {
var store = instance.stores[key];
if (store[LIFECYCLE].snapshot) {
store[LIFECYCLE].snapshot();
}
var customSnapshot = store[LIFECYCLE].serialize && store[LIFECYCLE].serialize();
obj[key] = customSnapshot ? customSnapshot : store.getState();
return obj;
Expand Down Expand Up @@ -1186,7 +1189,9 @@ var Alt = (function () {
}

return this.createActions(function () {
this.generateActions.apply(this, actionNames);
var _ref;

(_ref = this).generateActions.apply(_ref, actionNames);
});
}
},
Expand Down
3 changes: 3 additions & 0 deletions dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ var snapshot = function (instance) {
var stores = storeNames.length ? storeNames : Object.keys(instance.stores);
return stores.reduce(function (obj, key) {
var store = instance.stores[key];
if (store[LIFECYCLE].snapshot) {
store[LIFECYCLE].snapshot();
}
var customSnapshot = store[LIFECYCLE].serialize && store[LIFECYCLE].serialize();
obj[key] = customSnapshot ? customSnapshot : store.getState();
return obj;
Expand Down
3 changes: 3 additions & 0 deletions dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ var snapshot = function (instance) {
var stores = storeNames.length ? storeNames : Object.keys(instance.stores);
return stores.reduce(function (obj, key) {
var store = instance.stores[key];
if (store[LIFECYCLE].snapshot) {
store[LIFECYCLE].snapshot();
}
var customSnapshot = store[LIFECYCLE].serialize && store[LIFECYCLE].serialize();
obj[key] = customSnapshot ? customSnapshot : store.getState();
return obj;
Expand Down
16 changes: 15 additions & 1 deletion docs/lifecycleListeners.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,23 @@ class Store {
}
```

## Snapshot

`snapshot` is called before the store's state is serialized. Here you can perform any final tasks you need to before the state is saved.

```js
class Store {
constructor() {
this.on('snapshot', () => {
// do something here
});
}
}
```

## Serialize

`serialize` is called before the store's state is serialized. Here you can perform any final tasks you need to before the state is saved. You may optionally return an object, which will be used directly as the snapshot data for the store. If you do not return anything, the default, [`MyStore#getState()`](stores.md#storegetstate) is used for the snapshot data.
`serialize` is also called before the store's state is serialized. You may optionally return an object, which will be used directly as the snapshot data for the store. If you do not return anything, the default, [`MyStore#getState()`](stores.md#storegetstate) is used for the snapshot data.

```js
class Store {
Expand Down
3 changes: 3 additions & 0 deletions src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ const snapshot = (instance, ...storeNames) => {
const stores = storeNames.length ? storeNames : Object.keys(instance.stores)
return stores.reduce((obj, key) => {
const store = instance.stores[key]
if (store[LIFECYCLE].snapshot) {
store[LIFECYCLE].snapshot()
}
const customSnapshot = store[LIFECYCLE].serialize && store[LIFECYCLE].serialize()
obj[key] = customSnapshot ? customSnapshot : store.getState()
return obj
Expand Down
8 changes: 7 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class LifeCycleStore {
this.init = false
this.rollback = false
this.snapshotted = false
this.serialized = false
this.deserialized = false

this.bindListeners({
Expand All @@ -217,9 +218,12 @@ class LifeCycleStore {
this.on('bootstrap', () => {
this.bootstrapped = true
})
this.on('serialize', () => {
this.on('snapshot', () => {
this.snapshotted = true
})
this.on('serialize', () => {
this.serialized = true
})
this.on('rollback', () => {
this.rollback = true
})
Expand Down Expand Up @@ -394,6 +398,7 @@ const tests = {

assert(lifecycleStore.getState().bootstrapped === false, 'bootstrap has not been called yet')
assert(lifecycleStore.getState().snapshotted === false, 'takeSnapshot has not been called yet')
assert(lifecycleStore.getState().serialized === false, 'takeSnapshot has not been called yet')
assert(lifecycleStore.getState().rollback === false, 'rollback has not been called')
assert(lifecycleStore.getState().init === true, 'init gets called when store initializes')
assert(lifecycleStore.getState().deserialized === true, 'deserialize has not been called yet')
Expand All @@ -407,6 +412,7 @@ const tests = {
assert(bootstrapReturnValue === undefined, 'bootstrap returns nothing')
assert(lifecycleStore.getState().bootstrapped === true, 'bootstrap was called and the life cycle event was triggered')
assert(lifecycleStore.getState().snapshotted === true, 'snapshot was called and the life cycle event was triggered')
assert(lifecycleStore.getState().serialized === true, 'takeSnapshot has not been called yet')
assert(lifecycleStore.getState().deserialized === true, 'deserialize was called and the life cycle event was triggered')
},

Expand Down

0 comments on commit 5767ae6

Please sign in to comment.