Skip to content

Commit

Permalink
Customize setState and getState
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 18, 2015
1 parent a309bef commit 60c11b6
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 61 deletions.
48 changes: 25 additions & 23 deletions dist/alt-with-addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,8 @@ var EventEmitter = _interopRequire(require("eventemitter3"));

var assign = _interopRequire(require("object-assign"));

var Symbol = _interopRequire(require("es-symbol"));

var _utilsWarnings = require("./utils/warnings");

var warn = _utilsWarnings.warn;
Expand All @@ -1033,19 +1035,24 @@ var deprecatedBeforeAfterEachWarning = _utilsWarnings.deprecatedBeforeAfterEachW
var _symbolsSymbols = require("./symbols/symbols");

var ALL_LISTENERS = _symbolsSymbols.ALL_LISTENERS;
var EE = _symbolsSymbols.EE;
var LIFECYCLE = _symbolsSymbols.LIFECYCLE;
var LISTENERS = _symbolsSymbols.LISTENERS;
var PUBLIC_METHODS = _symbolsSymbols.PUBLIC_METHODS;
var STATE_CHANGED = _symbolsSymbols.STATE_CHANGED;
var STATE_CONTAINER = _symbolsSymbols.STATE_CONTAINER;

// alt container
var ALT = Symbol();
// event emitter instance
var EE = Symbol();

var AltStore = (function () {
function AltStore(dispatcher, model, state, StoreModel) {
function AltStore(alt, model, state, StoreModel) {
var _this = this;

_classCallCheck(this, AltStore);

this[ALT] = alt;
this[EE] = new EventEmitter();
this[LIFECYCLE] = {};
this[STATE_CHANGED] = false;
Expand All @@ -1061,7 +1068,7 @@ var AltStore = (function () {
assign(this, model[PUBLIC_METHODS]);

// Register dispatcher
this.dispatchToken = dispatcher.register(function (payload) {
this.dispatchToken = alt.dispatcher.register(function (payload) {
if (model[LIFECYCLE].beforeEach) {
model[LIFECYCLE].beforeEach(payload.action.toString(), payload.data, _this[STATE_CONTAINER]);
} else if (typeof model.beforeEach === "function") {
Expand Down Expand Up @@ -1133,12 +1140,7 @@ var AltStore = (function () {
},
getState: {
value: function getState() {
// Copy over state so it's RO.
var state = this[STATE_CONTAINER];
return Object.keys(state).reduce(function (obj, key) {
obj[key] = state[key];
return obj;
}, {});
return this[ALT].getState(this[STATE_CONTAINER]);
}
}
});
Expand All @@ -1148,7 +1150,7 @@ var AltStore = (function () {

module.exports = AltStore;

},{"./symbols/symbols":15,"./utils/warnings":22,"eventemitter3":6,"object-assign":10}],13:[function(require,module,exports){
},{"./symbols/symbols":15,"./utils/warnings":22,"es-symbol":5,"eventemitter3":6,"object-assign":10}],13:[function(require,module,exports){
"use strict";

var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
Expand Down Expand Up @@ -1244,6 +1246,13 @@ var Alt = (function () {

this.serialize = config.serialize || JSON.stringify;
this.deserialize = config.deserialize || JSON.parse;
this.setState = config.setState || assign;
this.getState = config.getState || function (state) {
return Object.keys(state).reduce(function (obj, key) {
obj[key] = state[key];
return obj;
}, {});
};
this.dispatcher = config.dispatcher || new Dispatcher();
this.actions = {};
this.stores = {};
Expand Down Expand Up @@ -1514,10 +1523,6 @@ exports.ACTION_UID = ACTION_UID;
var ALL_LISTENERS = Symbol();

exports.ALL_LISTENERS = ALL_LISTENERS;
// event emitter instance
var EE = Symbol();

exports.EE = EE;
// initial snapshot
var INIT_SNAPSHOT = Symbol();

Expand Down Expand Up @@ -1711,21 +1716,18 @@ var PUBLIC_METHODS = _symbolsSymbols.PUBLIC_METHODS;
var STATE_CHANGED = _symbolsSymbols.STATE_CHANGED;
var STATE_CONTAINER = _symbolsSymbols.STATE_CONTAINER;

function doSetState(store, storeInstance, nextState) {
if (!nextState) {
function doSetState(store, storeInstance, state) {
if (!state) {
return;
}

if (!store.alt.dispatcher.isDispatching()) {
throw new Error("You can only use setState while dispatching");
}

if (typeof nextState === "function") {
assign(storeInstance[STATE_CONTAINER], nextState(storeInstance[STATE_CONTAINER]));
} else {
assign(storeInstance[STATE_CONTAINER], nextState);
}
var nextState = typeof state === "function" ? state(storeInstance[STATE_CONTAINER]) : state;

storeInstance[STATE_CONTAINER] = store.alt.setState(storeInstance[STATE_CONTAINER], nextState);
storeInstance[STATE_CHANGED] = true;
}

Expand Down Expand Up @@ -1764,7 +1766,7 @@ function createStoreFromObject(alt, StoreModel, key) {
}

// create the instance and assign the public methods to the instance
storeInstance = assign(new AltStore(alt.dispatcher, StoreProto, StoreProto.state, StoreModel), StoreProto.publicMethods);
storeInstance = assign(new AltStore(alt, StoreProto, StoreProto.state, StoreModel), StoreProto.publicMethods);

return storeInstance;
}
Expand Down Expand Up @@ -1815,7 +1817,7 @@ function createStoreFromClass(alt, StoreModel, key) {

var store = _applyConstructor(Store, argsForClass);

storeInstance = assign(new AltStore(alt.dispatcher, store, typeof alt._stateKey === "string" ? store[alt._stateKey] : null, StoreModel), getInternalMethods(StoreModel));
storeInstance = assign(new AltStore(alt, store, typeof alt._stateKey === "string" ? store[alt._stateKey] : null, StoreModel), getInternalMethods(StoreModel));

return storeInstance;
}
Expand Down
48 changes: 25 additions & 23 deletions dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,8 @@ var EventEmitter = _interopRequire(require("eventemitter3"));

var assign = _interopRequire(require("object-assign"));

var Symbol = _interopRequire(require("es-symbol"));

var _utilsWarnings = require("./utils/warnings");

var warn = _utilsWarnings.warn;
Expand All @@ -784,19 +786,24 @@ var deprecatedBeforeAfterEachWarning = _utilsWarnings.deprecatedBeforeAfterEachW
var _symbolsSymbols = require("./symbols/symbols");

var ALL_LISTENERS = _symbolsSymbols.ALL_LISTENERS;
var EE = _symbolsSymbols.EE;
var LIFECYCLE = _symbolsSymbols.LIFECYCLE;
var LISTENERS = _symbolsSymbols.LISTENERS;
var PUBLIC_METHODS = _symbolsSymbols.PUBLIC_METHODS;
var STATE_CHANGED = _symbolsSymbols.STATE_CHANGED;
var STATE_CONTAINER = _symbolsSymbols.STATE_CONTAINER;

// alt container
var ALT = Symbol();
// event emitter instance
var EE = Symbol();

var AltStore = (function () {
function AltStore(dispatcher, model, state, StoreModel) {
function AltStore(alt, model, state, StoreModel) {
var _this = this;

_classCallCheck(this, AltStore);

this[ALT] = alt;
this[EE] = new EventEmitter();
this[LIFECYCLE] = {};
this[STATE_CHANGED] = false;
Expand All @@ -812,7 +819,7 @@ var AltStore = (function () {
assign(this, model[PUBLIC_METHODS]);

// Register dispatcher
this.dispatchToken = dispatcher.register(function (payload) {
this.dispatchToken = alt.dispatcher.register(function (payload) {
if (model[LIFECYCLE].beforeEach) {
model[LIFECYCLE].beforeEach(payload.action.toString(), payload.data, _this[STATE_CONTAINER]);
} else if (typeof model.beforeEach === "function") {
Expand Down Expand Up @@ -884,12 +891,7 @@ var AltStore = (function () {
},
getState: {
value: function getState() {
// Copy over state so it's RO.
var state = this[STATE_CONTAINER];
return Object.keys(state).reduce(function (obj, key) {
obj[key] = state[key];
return obj;
}, {});
return this[ALT].getState(this[STATE_CONTAINER]);
}
}
});
Expand All @@ -899,7 +901,7 @@ var AltStore = (function () {

module.exports = AltStore;

},{"./symbols/symbols":9,"./utils/warnings":16,"eventemitter3":2,"object-assign":6}],9:[function(require,module,exports){
},{"./symbols/symbols":9,"./utils/warnings":16,"es-symbol":1,"eventemitter3":2,"object-assign":6}],9:[function(require,module,exports){
"use strict";

var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
Expand All @@ -926,10 +928,6 @@ exports.ACTION_UID = ACTION_UID;
var ALL_LISTENERS = Symbol();

exports.ALL_LISTENERS = ALL_LISTENERS;
// event emitter instance
var EE = Symbol();

exports.EE = EE;
// initial snapshot
var INIT_SNAPSHOT = Symbol();

Expand Down Expand Up @@ -1123,21 +1121,18 @@ var PUBLIC_METHODS = _symbolsSymbols.PUBLIC_METHODS;
var STATE_CHANGED = _symbolsSymbols.STATE_CHANGED;
var STATE_CONTAINER = _symbolsSymbols.STATE_CONTAINER;

function doSetState(store, storeInstance, nextState) {
if (!nextState) {
function doSetState(store, storeInstance, state) {
if (!state) {
return;
}

if (!store.alt.dispatcher.isDispatching()) {
throw new Error("You can only use setState while dispatching");
}

if (typeof nextState === "function") {
assign(storeInstance[STATE_CONTAINER], nextState(storeInstance[STATE_CONTAINER]));
} else {
assign(storeInstance[STATE_CONTAINER], nextState);
}
var nextState = typeof state === "function" ? state(storeInstance[STATE_CONTAINER]) : state;

storeInstance[STATE_CONTAINER] = store.alt.setState(storeInstance[STATE_CONTAINER], nextState);
storeInstance[STATE_CHANGED] = true;
}

Expand Down Expand Up @@ -1176,7 +1171,7 @@ function createStoreFromObject(alt, StoreModel, key) {
}

// create the instance and assign the public methods to the instance
storeInstance = assign(new AltStore(alt.dispatcher, StoreProto, StoreProto.state, StoreModel), StoreProto.publicMethods);
storeInstance = assign(new AltStore(alt, StoreProto, StoreProto.state, StoreModel), StoreProto.publicMethods);

return storeInstance;
}
Expand Down Expand Up @@ -1227,7 +1222,7 @@ function createStoreFromClass(alt, StoreModel, key) {

var store = _applyConstructor(Store, argsForClass);

storeInstance = assign(new AltStore(alt.dispatcher, store, typeof alt._stateKey === "string" ? store[alt._stateKey] : null, StoreModel), getInternalMethods(StoreModel));
storeInstance = assign(new AltStore(alt, store, typeof alt._stateKey === "string" ? store[alt._stateKey] : null, StoreModel), getInternalMethods(StoreModel));

return storeInstance;
}
Expand Down Expand Up @@ -1433,6 +1428,13 @@ var Alt = (function () {

this.serialize = config.serialize || JSON.stringify;
this.deserialize = config.deserialize || JSON.parse;
this.setState = config.setState || assign;
this.getState = config.getState || function (state) {
return Object.keys(state).reduce(function (obj, key) {
obj[key] = state[key];
return obj;
}, {});
};
this.dispatcher = config.dispatcher || new Dispatcher();
this.actions = {};
this.stores = {};
Expand Down
37 changes: 37 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,43 @@ This controls how store data is serialized in snapshots. By default alt uses `JS

This controls how store data is deserialized from snapshot/bootstrap data. By default alt uses `JSON.parse`, but you can provide your own function to deserialize data.

#### stateKey

`stateKey` is a string that controls where state should be defined at the store level. For example:

```js
var alt = new Alt({
stateKey: 'cherry'
});

class MyStore {
constructor() {
// state now goes in this.cherry
this.cherry = {
a: 1,
b: 2,
c: 3
};

// instance properties declared below do not go into state
this.isPrivate = 'yes'
this.yay = true
}
}
```

#### setState

> setState(currentState: object, nextState: object): object
`setState` is used internally by Alt to set the state. You can override this to provide your own setState implementation. Internally, setState is an alias for `Object.assign`. `setState` must return an object.

#### getState

> getState(currentState: object): mixed
`getState` receives the current state and returns a copy of it. You can override this function to provide your own implementation.

## Instances

Alternatively, you can [create instances](/docs/altInstances/) of the entire alt universe including stores.
Expand Down
19 changes: 10 additions & 9 deletions src/alt/AltStore.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import EventEmitter from 'eventemitter3'
import assign from 'object-assign'
import Symbol from 'es-symbol'
import { warn, deprecatedBeforeAfterEachWarning } from './utils/warnings'
import {
ALL_LISTENERS,
EE,
LIFECYCLE,
LISTENERS,
PUBLIC_METHODS,
STATE_CHANGED,
STATE_CONTAINER
} from './symbols/symbols'

// alt container
const ALT = Symbol()
// event emitter instance
const EE = Symbol()

export default class AltStore {
constructor(dispatcher, model, state, StoreModel) {
constructor(alt, model, state, StoreModel) {
this[ALT] = alt
this[EE] = new EventEmitter()
this[LIFECYCLE] = {}
this[STATE_CHANGED] = false
Expand All @@ -28,7 +34,7 @@ export default class AltStore {
assign(this, model[PUBLIC_METHODS])

// Register dispatcher
this.dispatchToken = dispatcher.register((payload) => {
this.dispatchToken = alt.dispatcher.register((payload) => {
if (model[LIFECYCLE].beforeEach) {
model[LIFECYCLE].beforeEach(
payload.action.toString(),
Expand Down Expand Up @@ -111,11 +117,6 @@ export default class AltStore {
}

getState() {
// Copy over state so it's RO.
const state = this[STATE_CONTAINER]
return Object.keys(state).reduce((obj, key) => {
obj[key] = state[key]
return obj
}, {})
return this[ALT].getState(this[STATE_CONTAINER])
}
}
7 changes: 7 additions & 0 deletions src/alt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class Alt {
constructor(config = {}) {
this.serialize = config.serialize || JSON.stringify
this.deserialize = config.deserialize || JSON.parse
this.setState = config.setState || assign
this.getState = config.getState || function (state) {
return Object.keys(state).reduce((obj, key) => {
obj[key] = state[key]
return obj
}, {})
}
this.dispatcher = config.dispatcher || new Dispatcher()
this.actions = {}
this.stores = {}
Expand Down
3 changes: 0 additions & 3 deletions src/alt/symbols/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ export const ACTION_UID = Symbol()
// store all of a store's listeners
export const ALL_LISTENERS = Symbol()

// event emitter instance
export const EE = Symbol()

// initial snapshot
export const INIT_SNAPSHOT = Symbol()

Expand Down
Loading

0 comments on commit 60c11b6

Please sign in to comment.