Skip to content

Commit 98b3c04

Browse files
committed
Allow takeSnapshot to specify specific stores
- Resolves Issue #54 - This enables snapshotting a subset of the app data - `takeSnapshot` returns stringified JSON containing just the subset of data specified. The new snapshot is merged with the LAST_SNAPSHOT data to ensure that the last snapshot of the stores not specified in the subset is not lost. This also means that rollbacks will just work.
1 parent c386348 commit 98b3c04

File tree

8 files changed

+87
-22
lines changed

8 files changed

+87
-22
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,12 @@ Restart the loop by making your views kick off new actions.
529529

530530
### Snapshots
531531

532-
`takeSnapshot :: String`
532+
`takeSnapshot :: ?...String -> String`
533533

534534
Snapshots are a core component of alt. The idea is that at any given point in time you can `takeSnapshot` and have your entire application's state
535535
serialized for persistence, transferring, logging, or debugging.
536536

537-
Taking a snapshot is as easy as calling `alt.takeSnapshot()`.
537+
Taking a snapshot is as easy as calling `alt.takeSnapshot()`. It can also take an optional number of arguments as strings which correspond to the store names you would like to include in the snapshot. This allows you to take a snapshot of a subset of your app's data.
538538

539539
### Bootstrapping
540540

dist/alt-browser-with-addons.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,12 @@ var setAppState = function (instance, data, onStore) {
12581258
};
12591259

12601260
var snapshot = function (instance) {
1261-
return JSON.stringify(Object.keys(instance.stores).reduce(function (obj, key) {
1261+
for (var _len = arguments.length, storeNames = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1262+
storeNames[_key - 1] = arguments[_key];
1263+
}
1264+
1265+
var stores = storeNames.length ? storeNames : Object.keys(instance.stores);
1266+
return JSON.stringify(stores.reduce(function (obj, key) {
12621267
var store = instance.stores[key];
12631268
var customSnapshot = store[LIFECYCLE].serialize && store[LIFECYCLE].serialize();
12641269
obj[key] = customSnapshot ? customSnapshot : store.getState();
@@ -1509,8 +1514,16 @@ var Alt = (function () {
15091514
},
15101515
takeSnapshot: {
15111516
value: function takeSnapshot() {
1512-
var state = snapshot(this);
1513-
this[LAST_SNAPSHOT] = state;
1517+
for (var _len = arguments.length, storeNames = Array(_len), _key = 0; _key < _len; _key++) {
1518+
storeNames[_key] = arguments[_key];
1519+
}
1520+
1521+
var state = snapshot.apply(undefined, [this].concat(storeNames));
1522+
if (this[LAST_SNAPSHOT]) {
1523+
assign(this[LAST_SNAPSHOT], state);
1524+
} else {
1525+
this[LAST_SNAPSHOT] = state;
1526+
}
15141527
return state;
15151528
}
15161529
},

dist/alt-browser.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,12 @@ var setAppState = function (instance, data, onStore) {
10021002
};
10031003

10041004
var snapshot = function (instance) {
1005-
return JSON.stringify(Object.keys(instance.stores).reduce(function (obj, key) {
1005+
for (var _len = arguments.length, storeNames = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1006+
storeNames[_key - 1] = arguments[_key];
1007+
}
1008+
1009+
var stores = storeNames.length ? storeNames : Object.keys(instance.stores);
1010+
return JSON.stringify(stores.reduce(function (obj, key) {
10061011
var store = instance.stores[key];
10071012
var customSnapshot = store[LIFECYCLE].serialize && store[LIFECYCLE].serialize();
10081013
obj[key] = customSnapshot ? customSnapshot : store.getState();
@@ -1253,8 +1258,16 @@ var Alt = (function () {
12531258
},
12541259
takeSnapshot: {
12551260
value: function takeSnapshot() {
1256-
var state = snapshot(this);
1257-
this[LAST_SNAPSHOT] = state;
1261+
for (var _len = arguments.length, storeNames = Array(_len), _key = 0; _key < _len; _key++) {
1262+
storeNames[_key] = arguments[_key];
1263+
}
1264+
1265+
var state = snapshot.apply(undefined, [this].concat(storeNames));
1266+
if (this[LAST_SNAPSHOT]) {
1267+
assign(this[LAST_SNAPSHOT], state);
1268+
} else {
1269+
this[LAST_SNAPSHOT] = state;
1270+
}
12581271
return state;
12591272
}
12601273
},

dist/alt-with-runtime.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,12 @@ var setAppState = function (instance, data, onStore) {
257257
};
258258

259259
var snapshot = function (instance) {
260-
return JSON.stringify(Object.keys(instance.stores).reduce(function (obj, key) {
260+
for (var _len = arguments.length, storeNames = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
261+
storeNames[_key - 1] = arguments[_key];
262+
}
263+
264+
var stores = storeNames.length ? storeNames : Object.keys(instance.stores);
265+
return JSON.stringify(stores.reduce(function (obj, key) {
261266
var store = instance.stores[key];
262267
var customSnapshot = store[LIFECYCLE].serialize && store[LIFECYCLE].serialize();
263268
obj[key] = customSnapshot ? customSnapshot : store.getState();
@@ -503,8 +508,16 @@ var Alt = (function () {
503508
},
504509
takeSnapshot: {
505510
value: function takeSnapshot() {
506-
var state = snapshot(this);
507-
this[LAST_SNAPSHOT] = state;
511+
for (var _len = arguments.length, storeNames = Array(_len), _key = 0; _key < _len; _key++) {
512+
storeNames[_key] = arguments[_key];
513+
}
514+
515+
var state = snapshot.apply(undefined, [this].concat(storeNames));
516+
if (this[LAST_SNAPSHOT]) {
517+
assign(this[LAST_SNAPSHOT], state);
518+
} else {
519+
this[LAST_SNAPSHOT] = state;
520+
}
508521
return state;
509522
}
510523
},

dist/alt.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,12 @@ var setAppState = function (instance, data, onStore) {
271271
};
272272

273273
var snapshot = function (instance) {
274-
return JSON.stringify(Object.keys(instance.stores).reduce(function (obj, key) {
274+
for (var _len = arguments.length, storeNames = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
275+
storeNames[_key - 1] = arguments[_key];
276+
}
277+
278+
var stores = storeNames.length ? storeNames : Object.keys(instance.stores);
279+
return JSON.stringify(stores.reduce(function (obj, key) {
275280
var store = instance.stores[key];
276281
var customSnapshot = store[LIFECYCLE].serialize && store[LIFECYCLE].serialize();
277282
obj[key] = customSnapshot ? customSnapshot : store.getState();
@@ -520,8 +525,16 @@ var Alt = (function () {
520525
},
521526
takeSnapshot: {
522527
value: function takeSnapshot() {
523-
var state = snapshot(this);
524-
this[LAST_SNAPSHOT] = state;
528+
for (var _len = arguments.length, storeNames = Array(_len), _key = 0; _key < _len; _key++) {
529+
storeNames[_key] = arguments[_key];
530+
}
531+
532+
var state = snapshot.apply(undefined, [this].concat(storeNames));
533+
if (this[LAST_SNAPSHOT]) {
534+
assign(this[LAST_SNAPSHOT], state);
535+
} else {
536+
this[LAST_SNAPSHOT] = state;
537+
}
525538
return state;
526539
}
527540
},

docs/takeSnapshot.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ permalink: /docs/takeSnapshot/
77

88
# takeSnapshot
99

10-
> (): string
10+
> (...storeNames: ?string): string
1111
12-
Take snapshot provides you with the entire application's state serialized to JSON.
12+
Take snapshot provides you with the entire application's state serialized to JSON, by default, but you may also pass in store names to take a snapshot of a subset of the application's state.
1313

1414
Snapshots are a core component of alt. The idea is that at any given point in time you can `takeSnapshot` and have your entire application's state
15-
serialized for persistence, transfering, logging, or debugging.
15+
serialized for persistence, transferring, logging, or debugging.
1616

1717
```js
1818
var snapshot = alt.takeSnapshot();
19+
var partialSnapshot = alt.takeSnapshot('Store1', 'Store3');
1920
```

src/alt.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,10 @@ const setAppState = (instance, data, onStore) => {
239239
})
240240
}
241241

242-
const snapshot = (instance) => {
242+
const snapshot = (instance, ...storeNames) => {
243+
const stores = storeNames.length ? storeNames : Object.keys(instance.stores)
243244
return JSON.stringify(
244-
Object.keys(instance.stores).reduce((obj, key) => {
245+
stores.reduce((obj, key) => {
245246
const store = instance.stores[key]
246247
const customSnapshot = store[LIFECYCLE].serialize && store[LIFECYCLE].serialize()
247248
obj[key] = customSnapshot ? customSnapshot : store.getState()
@@ -456,9 +457,14 @@ class Alt {
456457
}, exportObj)
457458
}
458459

459-
takeSnapshot() {
460-
const state = snapshot(this)
461-
this[LAST_SNAPSHOT] = state
460+
takeSnapshot(...storeNames) {
461+
const state = snapshot(this, ...storeNames)
462+
if(this[LAST_SNAPSHOT]) {
463+
assign(this[LAST_SNAPSHOT], state)
464+
}
465+
else {
466+
this[LAST_SNAPSHOT] = state
467+
}
462468
return state
463469
}
464470

test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,12 @@ const tests = {
473473
assert(JSON.parse(snapshot).MyStore.name === 'bear', 'the snapshot is not affected by action')
474474
},
475475

476+
'specifying stores to snapshot'() {
477+
const snapshot = alt.takeSnapshot('MyStore', 'AltSecondStore')
478+
assert.deepEqual(Object.keys(JSON.parse(snapshot)), ['MyStore', 'AltSecondStore'], 'the snapshot includes specified stores')
479+
assert.isFalse(Object.keys(JSON.parse(snapshot)).includes('LifeCycleStore'), 'the snapshot does not include unspecified stores')
480+
},
481+
476482
'serializing/deserializing snapshot/bootstrap data'(){
477483
myActions.updateAnotherVal(11)
478484
const snapshot = alt.takeSnapshot()

0 commit comments

Comments
 (0)