Skip to content

Commit

Permalink
Use mobservable.createTransformer to create (immutable, shared) histo…
Browse files Browse the repository at this point in the history
…ry of the state.

Reduces memory usage significantly, improves performance a lot when having 10.000 items or more.
  • Loading branch information
mweststrate committed Dec 24, 2015
1 parent 96916ca commit dea834a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"webpack-dev-server": "^1.8.2"
},
"dependencies": {
"mobservable": "^1.0.2",
"mobservable": "^1.2.0",
"mobservable-react": "^2.0.0",
"mobservable-react-devtools": "^2.0.1",
"node-uuid": "^1.4.3",
Expand Down
40 changes: 14 additions & 26 deletions src/stores/domain-state.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {observable, transaction} from 'mobservable';
import {observable, transaction, createTransformer} from 'mobservable';
import Box from './box';

import {randomUuid} from '../utils';
Expand Down Expand Up @@ -43,30 +43,22 @@ export default store;

window.store = store; // for demo











/**
Serialize this store to json
*/
export function serializeState(store) {
return {
boxes: store.boxes.map(box => ({...box})),
arrows: store.arrows.map(arrow => ({
id: arrow.id,
to: arrow.to.id,
from: arrow.from.id
})),
selection: store.selection ? store.selection.id : null
}
}
const serializeBox = createTransformer(box => ({...box}));

const serializeArrow = createTransformer(arrow => ({
id: arrow.id,
to: arrow.to.id,
from: arrow.from.id
}));

export const serializeState = createTransformer(store => ({
boxes: store.boxes.map(serializeBox),
arrows: store.arrows.map(serializeArrow),
selection: store.selection ? store.selection.id : null
}));

/**
Update the store from the given json
Expand All @@ -82,10 +74,6 @@ export function deserializeState(store, data) {
store.selection = findBox(data.selection);
}





/**
Generate 'amount' new random arrows and boxes
*/
Expand Down

0 comments on commit dea834a

Please sign in to comment.