Comparing version 0.16.3 to 0.16.4
# Changelog | ||
## 0.16.4 | ||
### Added | ||
* @bind and @expose decorators for binding actions and exporting public methods. [commit](https://github.com/goatslacker/alt/commit/9bf17f0) | ||
* Made the lifecycles eventemitters so you can bind multiple. [commit](https://github.com/goatslacker/alt/commit/cf226f5) | ||
#### Fixes | ||
* Bug with react-native. Stop using the Object.assign polyfill since react-native overrides it with a non-spec compliant one. [commit](https://github.com/goatslacker/alt/commit/5ccab76) | ||
## 0.16.3 | ||
@@ -4,0 +15,0 @@ |
@@ -59,3 +59,3 @@ /** | ||
var mixinContainer = require('./mixinContainer') | ||
var assign = require('object-assign') | ||
var assign = require('../utils/functions').assign | ||
@@ -62,0 +62,0 @@ var AltContainer = React.createClass(assign({ |
@@ -8,3 +8,3 @@ /** | ||
var mixinContainer = require('./mixinContainer') | ||
var assign = require('object-assign') | ||
var assign = require('../utils/functions').assign | ||
@@ -11,0 +11,0 @@ var AltNativeContainer = React.createClass(assign({ |
var Subscribe = require('../mixins/Subscribe') | ||
var assign = require('object-assign') | ||
var assign = require('../utils/functions').assign | ||
@@ -4,0 +4,0 @@ function id(it) { |
859
dist/alt.js
@@ -706,30 +706,2 @@ (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){ | ||
function ToObject(val) { | ||
if (val == null) { | ||
throw new TypeError('Object.assign cannot be called with null or undefined'); | ||
} | ||
return Object(val); | ||
} | ||
module.exports = Object.assign || function (target, source) { | ||
var from; | ||
var keys; | ||
var to = ToObject(target); | ||
for (var s = 1; s < arguments.length; s++) { | ||
from = arguments[s]; | ||
keys = Object.keys(Object(from)); | ||
for (var i = 0; i < keys.length; i++) { | ||
to[keys[i]] = from[keys[i]]; | ||
} | ||
} | ||
return to; | ||
}; | ||
},{}],7:[function(require,module,exports){ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
@@ -741,13 +713,22 @@ value: true | ||
exports['default'] = makeAction; | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
var _symbolsSymbols = require('./symbols/symbols'); | ||
var _esSymbol = require('es-symbol'); | ||
var _esSymbol2 = _interopRequireDefault(_esSymbol); | ||
var _symbolsSymbols = require('../symbols/symbols'); | ||
var Sym = _interopRequireWildcard(_symbolsSymbols); | ||
var ACTION_HANDLER = Sym.ACTION_HANDLER; | ||
var ACTION_UID = Sym.ACTION_UID; | ||
var _utilsAltUtils = require('../utils/AltUtils'); | ||
var utils = _interopRequireWildcard(_utilsAltUtils); | ||
var AltAction = (function () { | ||
@@ -757,6 +738,6 @@ function AltAction(alt, name, action, actions, actionDetails) { | ||
this[ACTION_UID] = name; | ||
this[ACTION_HANDLER] = action.bind(this); | ||
this[Sym.ACTION_UID] = name; | ||
this[Sym.ACTION_HANDLER] = action.bind(this); | ||
this.actions = actions; | ||
this.actionDetails = actionDetails; | ||
this.actions = actions; | ||
this.alt = alt; | ||
@@ -768,3 +749,3 @@ } | ||
value: function dispatch(data) { | ||
this.alt.dispatch(this[ACTION_UID], data, this.actionDetails); | ||
this.alt.dispatch(this[Sym.ACTION_UID], data, this.actionDetails); | ||
} | ||
@@ -776,6 +757,43 @@ }]); | ||
exports['default'] = AltAction; | ||
function makeAction(alt, namespace, name, implementation, obj) { | ||
// make sure each Symbol is unique | ||
var actionId = utils.uid(alt[Sym.ACTIONS_REGISTRY], '' + namespace + '.' + name); | ||
alt[Sym.ACTIONS_REGISTRY][actionId] = 1; | ||
var actionSymbol = _esSymbol2['default']['for']('alt/' + actionId); | ||
var data = { | ||
namespace: namespace, | ||
name: name, | ||
id: actionId, | ||
symbol: actionSymbol | ||
}; | ||
// Wrap the action so we can provide a dispatch method | ||
var newAction = new AltAction(alt, actionSymbol, implementation, obj, data); | ||
// the action itself | ||
var action = newAction[Sym.ACTION_HANDLER]; | ||
action.defer = function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
setTimeout(function () { | ||
newAction[Sym.ACTION_HANDLER].apply(null, args); | ||
}); | ||
}; | ||
action[Sym.ACTION_KEY] = actionSymbol; | ||
action.data = data; | ||
// ensure each reference is unique in the namespace | ||
var container = alt.actions[namespace]; | ||
var id = utils.uid(container, name); | ||
container[id] = action; | ||
return action; | ||
} | ||
module.exports = exports['default']; | ||
},{"./symbols/symbols":9}],8:[function(require,module,exports){ | ||
},{"../symbols/symbols":10,"../utils/AltUtils":11,"es-symbol":1}],7:[function(require,module,exports){ | ||
'use strict'; | ||
@@ -799,6 +817,2 @@ | ||
var _objectAssign = require('object-assign'); | ||
var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
var _esSymbol = require('es-symbol'); | ||
@@ -808,12 +822,10 @@ | ||
var _symbolsSymbols = require('./symbols/symbols'); | ||
var _symbolsSymbols = require('../symbols/symbols'); | ||
var Sym = _interopRequireWildcard(_symbolsSymbols); | ||
var ALL_LISTENERS = Sym.ALL_LISTENERS; | ||
var LIFECYCLE = Sym.LIFECYCLE; | ||
var LISTENERS = Sym.LISTENERS; | ||
var PUBLIC_METHODS = Sym.PUBLIC_METHODS; | ||
var STATE_CONTAINER = Sym.STATE_CONTAINER; | ||
var _utilsFunctions = require('../../utils/functions'); | ||
var fn = _interopRequireWildcard(_utilsFunctions); | ||
// event emitter instance | ||
@@ -829,29 +841,23 @@ var EE = _esSymbol2['default'](); | ||
this[EE] = new _eventemitter32['default'](); | ||
this[LIFECYCLE] = {}; | ||
this[STATE_CONTAINER] = state || model; | ||
this[Sym.LIFECYCLE] = model[Sym.LIFECYCLE]; | ||
this[Sym.STATE_CONTAINER] = state || model; | ||
this._storeName = model._storeName; | ||
this.boundListeners = model[ALL_LISTENERS]; | ||
this.boundListeners = model[Sym.ALL_LISTENERS]; | ||
this.StoreModel = StoreModel; | ||
if (typeof this.StoreModel === 'object') { | ||
this.StoreModel.state = _objectAssign2['default']({}, StoreModel.state); | ||
} | ||
_objectAssign2['default'](this[LIFECYCLE], model[LIFECYCLE]); | ||
_objectAssign2['default'](this, model[PUBLIC_METHODS]); | ||
fn.assign(this, model[Sym.PUBLIC_METHODS]); | ||
// Register dispatcher | ||
this.dispatchToken = alt.dispatcher.register(function (payload) { | ||
if (model[LIFECYCLE].beforeEach) { | ||
model[LIFECYCLE].beforeEach(payload, _this[STATE_CONTAINER]); | ||
} | ||
_this[Sym.LIFECYCLE].emit('beforeEach', payload, _this[Sym.STATE_CONTAINER]); | ||
if (model[LISTENERS][payload.action]) { | ||
if (model[Sym.LISTENERS][payload.action]) { | ||
var result = false; | ||
try { | ||
result = model[LISTENERS][payload.action](payload.data); | ||
result = model[Sym.LISTENERS][payload.action](payload.data); | ||
} catch (e) { | ||
if (_this[LIFECYCLE].error) { | ||
_this[LIFECYCLE].error(e, payload, _this[STATE_CONTAINER]); | ||
if (model[Sym.HANDLING_ERRORS]) { | ||
_this[Sym.LIFECYCLE].emit('error', e, payload, _this[Sym.STATE_CONTAINER]); | ||
} else { | ||
@@ -867,10 +873,6 @@ throw e; | ||
if (model[LIFECYCLE].afterEach) { | ||
model[LIFECYCLE].afterEach(payload, _this[STATE_CONTAINER]); | ||
} | ||
_this[Sym.LIFECYCLE].emit('afterEach', payload, _this[Sym.STATE_CONTAINER]); | ||
}); | ||
if (this[LIFECYCLE].init) { | ||
this[LIFECYCLE].init(); | ||
} | ||
this[Sym.LIFECYCLE].emit('init'); | ||
} | ||
@@ -886,3 +888,3 @@ | ||
value: function emitChange() { | ||
this[EE].emit('change', this[STATE_CONTAINER]); | ||
this[EE].emit('change', this[Sym.STATE_CONTAINER]); | ||
} | ||
@@ -902,5 +904,3 @@ }, { | ||
value: function unlisten(cb) { | ||
if (this[LIFECYCLE].unlisten) { | ||
this[LIFECYCLE].unlisten(); | ||
} | ||
this[Sym.LIFECYCLE].emit('unlisten'); | ||
this[EE].removeListener('change', cb); | ||
@@ -911,3 +911,3 @@ } | ||
value: function getState() { | ||
return this.StoreModel.config.getState.call(this, this[STATE_CONTAINER]); | ||
return this.StoreModel.config.getState.call(this, this[Sym.STATE_CONTAINER]); | ||
} | ||
@@ -922,3 +922,3 @@ }]); | ||
},{"./symbols/symbols":9,"es-symbol":1,"eventemitter3":2,"object-assign":6}],9:[function(require,module,exports){ | ||
},{"../../utils/functions":14,"../symbols/symbols":10,"es-symbol":1,"eventemitter3":2}],8:[function(require,module,exports){ | ||
'use strict'; | ||
@@ -930,2 +930,4 @@ | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
@@ -937,124 +939,2 @@ | ||
// action creator handler | ||
var ACTION_HANDLER = _esSymbol2['default'](); | ||
exports.ACTION_HANDLER = ACTION_HANDLER; | ||
// the action's uid symbol for listening | ||
var ACTION_KEY = _esSymbol2['default'](); | ||
exports.ACTION_KEY = ACTION_KEY; | ||
// per instance registry of actions | ||
var ACTIONS_REGISTRY = _esSymbol2['default'](); | ||
exports.ACTIONS_REGISTRY = ACTIONS_REGISTRY; | ||
// the action's name | ||
var ACTION_UID = _esSymbol2['default'](); | ||
exports.ACTION_UID = ACTION_UID; | ||
// store all of a store's listeners | ||
var ALL_LISTENERS = _esSymbol2['default'](); | ||
exports.ALL_LISTENERS = ALL_LISTENERS; | ||
// initial snapshot | ||
var INIT_SNAPSHOT = _esSymbol2['default'](); | ||
exports.INIT_SNAPSHOT = INIT_SNAPSHOT; | ||
// last snapshot | ||
var LAST_SNAPSHOT = _esSymbol2['default'](); | ||
exports.LAST_SNAPSHOT = LAST_SNAPSHOT; | ||
// all lifecycle listeners | ||
var LIFECYCLE = _esSymbol2['default'](); | ||
exports.LIFECYCLE = LIFECYCLE; | ||
// store action listeners | ||
var LISTENERS = _esSymbol2['default'](); | ||
exports.LISTENERS = LISTENERS; | ||
// public methods | ||
var PUBLIC_METHODS = _esSymbol2['default'](); | ||
exports.PUBLIC_METHODS = PUBLIC_METHODS; | ||
// contains all state | ||
var STATE_CONTAINER = _esSymbol2['default'](); | ||
exports.STATE_CONTAINER = STATE_CONTAINER; | ||
},{"es-symbol":1}],10:[function(require,module,exports){ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.getInternalMethods = getInternalMethods; | ||
exports.warn = warn; | ||
exports.uid = uid; | ||
exports.formatAsConstant = formatAsConstant; | ||
exports.dispatchIdentity = dispatchIdentity; | ||
/* istanbul ignore next */ | ||
function NoopClass() {} | ||
var builtIns = Object.getOwnPropertyNames(NoopClass); | ||
var builtInProto = Object.getOwnPropertyNames(NoopClass.prototype); | ||
function getInternalMethods(obj, isProto) { | ||
var excluded = isProto ? builtInProto : builtIns; | ||
return Object.getOwnPropertyNames(obj).reduce(function (value, m) { | ||
if (excluded.indexOf(m) !== -1) { | ||
return value; | ||
} | ||
value[m] = obj[m]; | ||
return value; | ||
}, {}); | ||
} | ||
function warn(msg) { | ||
/* istanbul ignore else */ | ||
if (typeof console !== 'undefined') { | ||
console.warn(new ReferenceError(msg)); | ||
} | ||
} | ||
function uid(container, name) { | ||
var count = 0; | ||
var key = name; | ||
while (Object.hasOwnProperty.call(container, key)) { | ||
key = name + String(++count); | ||
} | ||
return key; | ||
} | ||
function formatAsConstant(name) { | ||
return name.replace(/[a-z]([A-Z])/g, function (i) { | ||
return '' + i[0] + '_' + i[1].toLowerCase(); | ||
}).toUpperCase(); | ||
} | ||
function dispatchIdentity(x) { | ||
for (var _len = arguments.length, a = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
a[_key - 1] = arguments[_key]; | ||
} | ||
this.dispatch(a.length ? [x].concat(a) : x); | ||
} | ||
},{}],11:[function(require,module,exports){ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.setAppState = setAppState; | ||
exports.snapshot = snapshot; | ||
exports.saveInitialSnapshot = saveInitialSnapshot; | ||
exports.filterSnapshots = filterSnapshots; | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _objectAssign = require('object-assign'); | ||
var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
var _symbolsSymbols = require('../symbols/symbols'); | ||
@@ -1064,76 +944,13 @@ | ||
var INIT_SNAPSHOT = Sym.INIT_SNAPSHOT; | ||
var LAST_SNAPSHOT = Sym.LAST_SNAPSHOT; | ||
var LIFECYCLE = Sym.LIFECYCLE; | ||
var STATE_CONTAINER = Sym.STATE_CONTAINER; | ||
var _utilsFunctions = require('../../utils/functions'); | ||
function setAppState(instance, data, onStore) { | ||
var obj = instance.deserialize(data); | ||
Object.keys(obj).forEach(function (key) { | ||
var store = instance.stores[key]; | ||
if (store) { | ||
var config = store.StoreModel.config; | ||
var fn = _interopRequireWildcard(_utilsFunctions); | ||
if (config.onDeserialize) { | ||
obj[key] = config.onDeserialize(obj[key]) || obj[key]; | ||
} | ||
_objectAssign2['default'](store[STATE_CONTAINER], obj[key]); | ||
onStore(store); | ||
var StoreMixin = { | ||
waitFor: function waitFor() { | ||
for (var _len = arguments.length, sources = Array(_len), _key = 0; _key < _len; _key++) { | ||
sources[_key] = arguments[_key]; | ||
} | ||
}); | ||
} | ||
function snapshot(instance) { | ||
var storeNames = arguments[1] === undefined ? [] : arguments[1]; | ||
var stores = storeNames.length ? storeNames : Object.keys(instance.stores); | ||
return stores.reduce(function (obj, storeHandle) { | ||
var storeName = storeHandle.displayName || storeHandle; | ||
var store = instance.stores[storeName]; | ||
var config = store.StoreModel.config; | ||
if (store[LIFECYCLE].snapshot) { | ||
store[LIFECYCLE].snapshot(); | ||
} | ||
var customSnapshot = config.onSerialize && config.onSerialize(store[STATE_CONTAINER]); | ||
obj[storeName] = customSnapshot ? customSnapshot : store.getState(); | ||
return obj; | ||
}, {}); | ||
} | ||
function saveInitialSnapshot(instance, key) { | ||
var state = instance.deserialize(instance.serialize(instance.stores[key][STATE_CONTAINER])); | ||
instance[INIT_SNAPSHOT][key] = state; | ||
instance[LAST_SNAPSHOT][key] = state; | ||
} | ||
function filterSnapshots(instance, state, stores) { | ||
return stores.reduce(function (obj, store) { | ||
var storeName = store.displayName || store; | ||
if (!state[storeName]) { | ||
throw new ReferenceError('' + storeName + ' is not a valid store'); | ||
} | ||
obj[storeName] = state[storeName]; | ||
return obj; | ||
}, {}); | ||
} | ||
},{"../symbols/symbols":9,"object-assign":6}],12:[function(require,module,exports){ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _esSymbol = require('es-symbol'); | ||
var _esSymbol2 = _interopRequireDefault(_esSymbol); | ||
var _symbolsSymbols = require('../symbols/symbols'); | ||
var StoreMixinEssentials = { | ||
waitFor: function waitFor(sources) { | ||
if (!sources) { | ||
if (!sources.length) { | ||
throw new ReferenceError('Dispatch tokens not provided'); | ||
@@ -1143,6 +960,4 @@ } | ||
var sourcesArray = sources; | ||
if (arguments.length === 1) { | ||
sourcesArray = Array.isArray(sourcesArray) ? sourcesArray : [sourcesArray]; | ||
} else { | ||
sourcesArray = Array.prototype.slice.call(arguments); | ||
if (sources.length === 1) { | ||
sourcesArray = Array.isArray(sources[0]) ? sources[0] : sources; | ||
} | ||
@@ -1160,9 +975,9 @@ | ||
Object.keys(methods).forEach(function (methodName) { | ||
if (typeof methods[methodName] !== 'function') { | ||
fn.eachObject(function (methodName, value) { | ||
if (!fn.isFunction(value)) { | ||
throw new TypeError('exportPublicMethods expects a function'); | ||
} | ||
_this[_symbolsSymbols.PUBLIC_METHODS][methodName] = methods[methodName]; | ||
}); | ||
_this[Sym.PUBLIC_METHODS][methodName] = value; | ||
}, [methods]); | ||
}, | ||
@@ -1172,9 +987,9 @@ | ||
this.getInstance().emitChange(); | ||
} | ||
}; | ||
}, | ||
exports.StoreMixinEssentials = StoreMixinEssentials; | ||
var StoreMixinListeners = { | ||
on: function on(lifecycleEvent, handler) { | ||
this[_symbolsSymbols.LIFECYCLE][lifecycleEvent] = handler.bind(this); | ||
if (lifecycleEvent === 'error') { | ||
this[Sym.HANDLING_ERRORS] = true; | ||
} | ||
this[Sym.LIFECYCLE].on(lifecycleEvent, handler.bind(this)); | ||
}, | ||
@@ -1186,3 +1001,3 @@ | ||
} | ||
if (typeof handler !== 'function') { | ||
if (!fn.isFunction(handler)) { | ||
throw new TypeError('bindAction expects a function'); | ||
@@ -1192,9 +1007,9 @@ } | ||
if (handler.length > 1) { | ||
throw new TypeError('Action handler in store ' + this._storeName + ' for ' + ('' + (symbol[_symbolsSymbols.ACTION_KEY] || symbol).toString() + ' was defined with 2 ') + 'parameters. Only a single parameter is passed through the ' + 'dispatcher, did you mean to pass in an Object instead?'); | ||
throw new TypeError('Action handler in store ' + this._storeName + ' for ' + ('' + (symbol[Sym.ACTION_KEY] || symbol).toString() + ' was defined with ') + 'two parameters. Only a single parameter is passed through the ' + 'dispatcher, did you mean to pass in an Object instead?'); | ||
} | ||
// You can pass in the constant or the function itself | ||
var key = symbol[_symbolsSymbols.ACTION_KEY] ? symbol[_symbolsSymbols.ACTION_KEY] : symbol; | ||
this[_symbolsSymbols.LISTENERS][key] = handler.bind(this); | ||
this[_symbolsSymbols.ALL_LISTENERS].push(_esSymbol2['default'].keyFor(key)); | ||
var key = symbol[Sym.ACTION_KEY] ? symbol[Sym.ACTION_KEY] : symbol; | ||
this[Sym.LISTENERS][key] = handler.bind(this); | ||
this[Sym.ALL_LISTENERS].push(_esSymbol2['default'].keyFor(key)); | ||
}, | ||
@@ -1205,4 +1020,3 @@ | ||
Object.keys(actions).forEach(function (action) { | ||
var symbol = actions[action]; | ||
fn.eachObject(function (action, symbol) { | ||
var matchFirstCharacter = /./; | ||
@@ -1228,3 +1042,3 @@ var assumedEventHandler = action.replace(matchFirstCharacter, function (x) { | ||
} | ||
}); | ||
}, [actions]); | ||
}, | ||
@@ -1235,4 +1049,3 @@ | ||
Object.keys(obj).forEach(function (methodName) { | ||
var symbol = obj[methodName]; | ||
fn.eachObject(function (methodName, symbol) { | ||
var listener = _this3[methodName]; | ||
@@ -1251,9 +1064,10 @@ | ||
} | ||
}); | ||
}, [obj]); | ||
} | ||
}; | ||
exports.StoreMixinListeners = StoreMixinListeners; | ||
},{"../symbols/symbols":9,"es-symbol":1}],13:[function(require,module,exports){ | ||
exports['default'] = StoreMixin; | ||
module.exports = exports['default']; | ||
},{"../../utils/functions":14,"../symbols/symbols":10,"es-symbol":1}],9:[function(require,module,exports){ | ||
'use strict'; | ||
@@ -1275,2 +1089,4 @@ | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
@@ -1282,16 +1098,26 @@ | ||
var _objectAssign = require('object-assign'); | ||
var _eventemitter3 = require('eventemitter3'); | ||
var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
var _eventemitter32 = _interopRequireDefault(_eventemitter3); | ||
var _AltStore = require('../AltStore'); | ||
var _symbolsSymbols = require('../symbols/symbols'); | ||
var Sym = _interopRequireWildcard(_symbolsSymbols); | ||
var _utilsAltUtils = require('../utils/AltUtils'); | ||
var utils = _interopRequireWildcard(_utilsAltUtils); | ||
var _utilsFunctions = require('../../utils/functions'); | ||
var fn = _interopRequireWildcard(_utilsFunctions); | ||
var _AltStore = require('./AltStore'); | ||
var _AltStore2 = _interopRequireDefault(_AltStore); | ||
var _AltUtils = require('./AltUtils'); | ||
var _StoreMixin = require('./StoreMixin'); | ||
var _StoreMixins = require('./StoreMixins'); | ||
var _StoreMixin2 = _interopRequireDefault(_StoreMixin); | ||
var _symbolsSymbols = require('../symbols/symbols'); | ||
function doSetState(store, storeInstance, state) { | ||
@@ -1304,5 +1130,5 @@ if (!state) { | ||
var nextState = typeof state === 'function' ? state(storeInstance[_symbolsSymbols.STATE_CONTAINER]) : state; | ||
var nextState = fn.isFunction(state) ? state(storeInstance[Sym.STATE_CONTAINER]) : state; | ||
storeInstance[_symbolsSymbols.STATE_CONTAINER] = config.setState.call(store, storeInstance[_symbolsSymbols.STATE_CONTAINER], nextState); | ||
storeInstance[Sym.STATE_CONTAINER] = config.setState.call(store, storeInstance[Sym.STATE_CONTAINER], nextState); | ||
@@ -1314,11 +1140,21 @@ if (!store.alt.dispatcher.isDispatching()) { | ||
function createPrototype(proto, alt, key, extras) { | ||
proto[Sym.ALL_LISTENERS] = []; | ||
proto[Sym.LIFECYCLE] = new _eventemitter32['default'](); | ||
proto[Sym.LISTENERS] = {}; | ||
proto[Sym.PUBLIC_METHODS] = {}; | ||
return fn.assign(proto, _StoreMixin2['default'], { | ||
_storeName: key, | ||
alt: alt, | ||
dispatcher: alt.dispatcher | ||
}, extras); | ||
} | ||
function createStoreConfig(globalConfig, StoreModel) { | ||
StoreModel.config = _objectAssign2['default']({ | ||
StoreModel.config = fn.assign({ | ||
getState: function getState(state) { | ||
return Object.keys(state).reduce(function (obj, key) { | ||
obj[key] = state[key]; | ||
return obj; | ||
}, {}); | ||
return fn.assign({}, state); | ||
}, | ||
setState: _objectAssign2['default'] | ||
setState: fn.assign | ||
}, globalConfig, StoreModel.config); | ||
@@ -1336,11 +1172,3 @@ } | ||
var StoreProto = {}; | ||
StoreProto[_symbolsSymbols.ALL_LISTENERS] = []; | ||
StoreProto[_symbolsSymbols.LIFECYCLE] = {}; | ||
StoreProto[_symbolsSymbols.LISTENERS] = {}; | ||
_objectAssign2['default'](StoreProto, { | ||
_storeName: key, | ||
alt: alt, | ||
dispatcher: alt.dispatcher, | ||
var StoreProto = createPrototype({}, alt, key, fn.assign({ | ||
getInstance: function getInstance() { | ||
@@ -1352,3 +1180,3 @@ return storeInstance; | ||
} | ||
}, _StoreMixins.StoreMixinListeners, _StoreMixins.StoreMixinEssentials, StoreModel); | ||
}, StoreModel)); | ||
@@ -1358,3 +1186,3 @@ // bind the store listeners | ||
if (StoreProto.bindListeners) { | ||
_StoreMixins.StoreMixinListeners.bindListeners.call(StoreProto, StoreProto.bindListeners); | ||
_StoreMixin2['default'].bindListeners.call(StoreProto, StoreProto.bindListeners); | ||
} | ||
@@ -1365,9 +1193,9 @@ | ||
if (StoreProto.lifecycle) { | ||
Object.keys(StoreProto.lifecycle).forEach(function (event) { | ||
_StoreMixins.StoreMixinListeners.on.call(StoreProto, event, StoreProto.lifecycle[event]); | ||
}); | ||
fn.eachObject(function (eventName, event) { | ||
_StoreMixin2['default'].on.call(StoreProto, eventName, event); | ||
}, [StoreProto.lifecycle]); | ||
} | ||
// create the instance and assign the public methods to the instance | ||
storeInstance = _objectAssign2['default'](new _AltStore2['default'](alt, StoreProto, StoreProto.state, StoreModel), StoreProto.publicMethods, { displayName: key }); | ||
// create the instance and fn.assign the public methods to the instance | ||
storeInstance = fn.assign(new _AltStore2['default'](alt, StoreProto, StoreProto.state, StoreModel), StoreProto.publicMethods, { displayName: key }); | ||
@@ -1405,6 +1233,3 @@ return storeInstance; | ||
_objectAssign2['default'](Store.prototype, _StoreMixins.StoreMixinListeners, _StoreMixins.StoreMixinEssentials, { | ||
_storeName: key, | ||
alt: alt, | ||
dispatcher: alt.dispatcher, | ||
createPrototype(Store.prototype, alt, key, { | ||
getInstance: function getInstance() { | ||
@@ -1418,15 +1243,14 @@ return storeInstance; | ||
Store.prototype[_symbolsSymbols.ALL_LISTENERS] = []; | ||
Store.prototype[_symbolsSymbols.LIFECYCLE] = {}; | ||
Store.prototype[_symbolsSymbols.LISTENERS] = {}; | ||
Store.prototype[_symbolsSymbols.PUBLIC_METHODS] = {}; | ||
var store = new (_bind.apply(Store, [null].concat(argsForClass)))(); | ||
storeInstance = _objectAssign2['default'](new _AltStore2['default'](alt, store, store[alt.config.stateKey] || store[config.stateKey] || null, StoreModel), _AltUtils.getInternalMethods(StoreModel), { displayName: key }); | ||
if (config.bindListeners) { | ||
store.bindListeners(config.bindListeners); | ||
} | ||
storeInstance = fn.assign(new _AltStore2['default'](alt, store, store[alt.config.stateKey] || store[config.stateKey] || null, StoreModel), utils.getInternalMethods(StoreModel), config.publicMethods, { displayName: key }); | ||
return storeInstance; | ||
} | ||
},{"../AltStore":8,"../symbols/symbols":9,"./AltUtils":10,"./StoreMixins":12,"object-assign":6}],14:[function(require,module,exports){ | ||
},{"../../utils/functions":14,"../symbols/symbols":10,"../utils/AltUtils":11,"./AltStore":7,"./StoreMixin":8,"eventemitter3":2}],10:[function(require,module,exports){ | ||
'use strict'; | ||
@@ -1437,67 +1261,118 @@ | ||
}); | ||
exports['default'] = makeAction; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
var _esSymbol = require('es-symbol'); | ||
var _symbolsSymbols = require('../symbols/symbols'); | ||
var _esSymbol2 = _interopRequireDefault(_esSymbol); | ||
var Sym = _interopRequireWildcard(_symbolsSymbols); | ||
// action creator handler | ||
var ACTION_HANDLER = _esSymbol2['default'](); | ||
var _esSymbol = require('es-symbol'); | ||
exports.ACTION_HANDLER = ACTION_HANDLER; | ||
// the action's uid symbol for listening | ||
var ACTION_KEY = _esSymbol2['default'](); | ||
var _esSymbol2 = _interopRequireDefault(_esSymbol); | ||
exports.ACTION_KEY = ACTION_KEY; | ||
// per instance registry of actions | ||
var ACTIONS_REGISTRY = _esSymbol2['default'](); | ||
var _AltAction = require('../AltAction'); | ||
exports.ACTIONS_REGISTRY = ACTIONS_REGISTRY; | ||
// the action's name | ||
var ACTION_UID = _esSymbol2['default'](); | ||
var _AltAction2 = _interopRequireDefault(_AltAction); | ||
exports.ACTION_UID = ACTION_UID; | ||
// store all of a store's listeners | ||
var ALL_LISTENERS = _esSymbol2['default'](); | ||
var _AltUtils = require('./AltUtils'); | ||
exports.ALL_LISTENERS = ALL_LISTENERS; | ||
// are we handling our own errors | ||
var HANDLING_ERRORS = _esSymbol2['default'](); | ||
var ACTION_KEY = Sym.ACTION_KEY; | ||
var ACTION_HANDLER = Sym.ACTION_HANDLER; | ||
var ACTIONS_REGISTRY = Sym.ACTIONS_REGISTRY; | ||
exports.HANDLING_ERRORS = HANDLING_ERRORS; | ||
// initial snapshot | ||
var INIT_SNAPSHOT = _esSymbol2['default'](); | ||
function makeAction(alt, namespace, name, implementation, obj) { | ||
// make sure each Symbol is unique | ||
var actionId = _AltUtils.uid(alt[ACTIONS_REGISTRY], '' + namespace + '.' + name); | ||
alt[ACTIONS_REGISTRY][actionId] = 1; | ||
var actionSymbol = _esSymbol2['default']['for']('alt/' + actionId); | ||
exports.INIT_SNAPSHOT = INIT_SNAPSHOT; | ||
// last snapshot | ||
var LAST_SNAPSHOT = _esSymbol2['default'](); | ||
var data = { | ||
namespace: namespace, | ||
name: name, | ||
id: actionId, | ||
symbol: actionSymbol | ||
}; | ||
exports.LAST_SNAPSHOT = LAST_SNAPSHOT; | ||
// all lifecycle listeners | ||
var LIFECYCLE = _esSymbol2['default'](); | ||
// Wrap the action so we can provide a dispatch method | ||
var newAction = new _AltAction2['default'](alt, actionSymbol, implementation, obj, data); | ||
exports.LIFECYCLE = LIFECYCLE; | ||
// store action listeners | ||
var LISTENERS = _esSymbol2['default'](); | ||
// the action itself | ||
var action = newAction[ACTION_HANDLER]; | ||
action.defer = function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
exports.LISTENERS = LISTENERS; | ||
// public methods | ||
var PUBLIC_METHODS = _esSymbol2['default'](); | ||
exports.PUBLIC_METHODS = PUBLIC_METHODS; | ||
// contains all state | ||
var STATE_CONTAINER = _esSymbol2['default'](); | ||
exports.STATE_CONTAINER = STATE_CONTAINER; | ||
},{"es-symbol":1}],11:[function(require,module,exports){ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.getInternalMethods = getInternalMethods; | ||
exports.warn = warn; | ||
exports.uid = uid; | ||
exports.formatAsConstant = formatAsConstant; | ||
exports.dispatchIdentity = dispatchIdentity; | ||
/* istanbul ignore next */ | ||
function NoopClass() {} | ||
var builtIns = Object.getOwnPropertyNames(NoopClass); | ||
var builtInProto = Object.getOwnPropertyNames(NoopClass.prototype); | ||
function getInternalMethods(Obj, isProto) { | ||
var excluded = isProto ? builtInProto : builtIns; | ||
var obj = isProto ? Obj.prototype : Obj; | ||
return Object.getOwnPropertyNames(obj).reduce(function (value, m) { | ||
if (excluded.indexOf(m) !== -1) { | ||
return value; | ||
} | ||
setTimeout(function () { | ||
newAction[ACTION_HANDLER].apply(null, args); | ||
}); | ||
}; | ||
action[ACTION_KEY] = actionSymbol; | ||
action.data = data; | ||
value[m] = obj[m]; | ||
return value; | ||
}, {}); | ||
} | ||
// ensure each reference is unique in the namespace | ||
var container = alt.actions[namespace]; | ||
var id = _AltUtils.uid(container, name); | ||
container[id] = action; | ||
function warn(msg) { | ||
/* istanbul ignore else */ | ||
if (typeof console !== 'undefined') { | ||
console.warn(new ReferenceError(msg)); | ||
} | ||
} | ||
return action; | ||
function uid(container, name) { | ||
var count = 0; | ||
var key = name; | ||
while (Object.hasOwnProperty.call(container, key)) { | ||
key = name + String(++count); | ||
} | ||
return key; | ||
} | ||
module.exports = exports['default']; | ||
function formatAsConstant(name) { | ||
return name.replace(/[a-z]([A-Z])/g, function (i) { | ||
return '' + i[0] + '_' + i[1].toLowerCase(); | ||
}).toUpperCase(); | ||
} | ||
},{"../AltAction":7,"../symbols/symbols":9,"./AltUtils":10,"es-symbol":1}],15:[function(require,module,exports){ | ||
function dispatchIdentity(x) { | ||
for (var _len = arguments.length, a = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
a[_key - 1] = arguments[_key]; | ||
} | ||
this.dispatch(a.length ? [x].concat(a) : x); | ||
} | ||
},{}],12:[function(require,module,exports){ | ||
'use strict'; | ||
@@ -1508,2 +1383,72 @@ | ||
}); | ||
exports.setAppState = setAppState; | ||
exports.snapshot = snapshot; | ||
exports.saveInitialSnapshot = saveInitialSnapshot; | ||
exports.filterSnapshots = filterSnapshots; | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
var _symbolsSymbols = require('../symbols/symbols'); | ||
var Sym = _interopRequireWildcard(_symbolsSymbols); | ||
var _utilsFunctions = require('../../utils/functions'); | ||
var fn = _interopRequireWildcard(_utilsFunctions); | ||
function setAppState(instance, data, onStore) { | ||
var obj = instance.deserialize(data); | ||
fn.eachObject(function (key, value) { | ||
var store = instance.stores[key]; | ||
if (store) { | ||
var config = store.StoreModel.config; | ||
if (config.onDeserialize) { | ||
obj[key] = config.onDeserialize(value) || value; | ||
} | ||
fn.assign(store[Sym.STATE_CONTAINER], obj[key]); | ||
onStore(store); | ||
} | ||
}, [obj]); | ||
} | ||
function snapshot(instance) { | ||
var storeNames = arguments[1] === undefined ? [] : arguments[1]; | ||
var stores = storeNames.length ? storeNames : Object.keys(instance.stores); | ||
return stores.reduce(function (obj, storeHandle) { | ||
var storeName = storeHandle.displayName || storeHandle; | ||
var store = instance.stores[storeName]; | ||
var config = store.StoreModel.config; | ||
store[Sym.LIFECYCLE].emit('snapshot'); | ||
var customSnapshot = config.onSerialize && config.onSerialize(store[Sym.STATE_CONTAINER]); | ||
obj[storeName] = customSnapshot ? customSnapshot : store.getState(); | ||
return obj; | ||
}, {}); | ||
} | ||
function saveInitialSnapshot(instance, key) { | ||
var state = instance.deserialize(instance.serialize(instance.stores[key][Sym.STATE_CONTAINER])); | ||
instance[Sym.INIT_SNAPSHOT][key] = state; | ||
instance[Sym.LAST_SNAPSHOT][key] = state; | ||
} | ||
function filterSnapshots(instance, state, stores) { | ||
return stores.reduce(function (obj, store) { | ||
var storeName = store.displayName || store; | ||
if (!state[storeName]) { | ||
throw new ReferenceError('' + storeName + ' is not a valid store'); | ||
} | ||
obj[storeName] = state[storeName]; | ||
return obj; | ||
}, {}); | ||
} | ||
},{"../../utils/functions":14,"../symbols/symbols":10}],13:[function(require,module,exports){ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var _bind = Function.prototype.bind; | ||
@@ -1519,2 +1464,4 @@ | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } | ||
@@ -1524,20 +1471,28 @@ | ||
var _objectAssign = require('object-assign'); | ||
var _flux = require('flux'); | ||
var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
var _utilsStateFunctions = require('./utils/StateFunctions'); | ||
var _flux = require('flux'); | ||
var StateFunctions = _interopRequireWildcard(_utilsStateFunctions); | ||
var _utilsMakeAction = require('./utils/makeAction'); | ||
var _symbolsSymbols = require('./symbols/symbols'); | ||
var _utilsMakeAction2 = _interopRequireDefault(_utilsMakeAction); | ||
var Sym = _interopRequireWildcard(_symbolsSymbols); | ||
var _utilsStateFunctions = require('./utils/StateFunctions'); | ||
var _utilsFunctions = require('../utils/functions'); | ||
var _utilsStoreUtils = require('./utils/StoreUtils'); | ||
var fn = _interopRequireWildcard(_utilsFunctions); | ||
var _symbolsSymbols = require('./symbols/symbols'); | ||
var _store = require('./store'); | ||
var store = _interopRequireWildcard(_store); | ||
var _utilsAltUtils = require('./utils/AltUtils'); | ||
var utils = _interopRequireWildcard(_utilsAltUtils); | ||
var _actions = require('./actions'); | ||
var _actions2 = _interopRequireDefault(_actions); | ||
var Alt = (function () { | ||
@@ -1556,5 +1511,5 @@ function Alt() { | ||
this.storeTransforms = config.storeTransforms || []; | ||
this[_symbolsSymbols.ACTIONS_REGISTRY] = {}; | ||
this[_symbolsSymbols.INIT_SNAPSHOT] = {}; | ||
this[_symbolsSymbols.LAST_SNAPSHOT] = {}; | ||
this[Sym.ACTIONS_REGISTRY] = {}; | ||
this[Sym.INIT_SNAPSHOT] = {}; | ||
this[Sym.LAST_SNAPSHOT] = {}; | ||
} | ||
@@ -1575,6 +1530,6 @@ | ||
var key = StoreModel.displayName || ''; | ||
_utilsStoreUtils.createStoreConfig(this.config, StoreModel); | ||
var Store = _utilsStoreUtils.transformStore(this.storeTransforms, StoreModel); | ||
store.createStoreConfig(this.config, StoreModel); | ||
var Store = store.transformStore(this.storeTransforms, StoreModel); | ||
return typeof Store === 'object' ? _utilsStoreUtils.createStoreFromObject(this, Store, key) : _utilsStoreUtils.createStoreFromClass.apply(undefined, [this, Store, key].concat(args)); | ||
return fn.isFunction(Store) ? store.createStoreFromClass.apply(store, [this, Store, key].concat(args)) : store.createStoreFromObject(this, Store, key); | ||
} | ||
@@ -1589,19 +1544,19 @@ }, { | ||
var key = iden || StoreModel.displayName || StoreModel.name || ''; | ||
_utilsStoreUtils.createStoreConfig(this.config, StoreModel); | ||
var Store = _utilsStoreUtils.transformStore(this.storeTransforms, StoreModel); | ||
store.createStoreConfig(this.config, StoreModel); | ||
var Store = store.transformStore(this.storeTransforms, StoreModel); | ||
if (this.stores[key] || !key) { | ||
if (this.stores[key]) { | ||
_utilsAltUtils.warn('A store named ' + key + ' already exists, double check your store ' + 'names or pass in your own custom identifier for each store'); | ||
utils.warn('A store named ' + key + ' already exists, double check your store ' + 'names or pass in your own custom identifier for each store'); | ||
} else { | ||
_utilsAltUtils.warn('Store name was not specified'); | ||
utils.warn('Store name was not specified'); | ||
} | ||
key = _utilsAltUtils.uid(this.stores, key); | ||
key = utils.uid(this.stores, key); | ||
} | ||
var storeInstance = typeof Store === 'object' ? _utilsStoreUtils.createStoreFromObject(this, Store, key) : _utilsStoreUtils.createStoreFromClass.apply(undefined, [this, Store, key].concat(args)); | ||
var storeInstance = fn.isFunction(Store) ? store.createStoreFromClass.apply(store, [this, Store, key].concat(args)) : store.createStoreFromObject(this, Store, key); | ||
this.stores[key] = storeInstance; | ||
_utilsStateFunctions.saveInitialSnapshot(this, key); | ||
StateFunctions.saveInitialSnapshot(this, key); | ||
@@ -1619,3 +1574,3 @@ return storeInstance; | ||
return this.createActions(actionNames.reduce(function (obj, action) { | ||
obj[action] = _utilsAltUtils.dispatchIdentity; | ||
obj[action] = utils.dispatchIdentity; | ||
return obj; | ||
@@ -1627,3 +1582,3 @@ }, actions)); | ||
value: function createAction(name, implementation, obj) { | ||
return _utilsMakeAction2['default'](this, 'global', name, implementation, obj); | ||
return _actions2['default'](this, 'global', name, implementation, obj); | ||
} | ||
@@ -1642,7 +1597,7 @@ }, { | ||
var actions = {}; | ||
var key = _utilsAltUtils.uid(this[_symbolsSymbols.ACTIONS_REGISTRY], ActionsClass.displayName || ActionsClass.name || 'Unknown'); | ||
var key = utils.uid(this[Sym.ACTIONS_REGISTRY], ActionsClass.displayName || ActionsClass.name || 'Unknown'); | ||
if (typeof ActionsClass === 'function') { | ||
if (fn.isFunction(ActionsClass)) { | ||
(function () { | ||
_objectAssign2['default'](actions, _utilsAltUtils.getInternalMethods(ActionsClass.prototype, true)); | ||
fn.assign(actions, utils.getInternalMethods(ActionsClass, true)); | ||
@@ -1670,3 +1625,3 @@ var ActionsGenerator = (function (_ActionsClass) { | ||
actionNames.forEach(function (actionName) { | ||
actions[actionName] = _utilsAltUtils.dispatchIdentity; | ||
actions[actionName] = utils.dispatchIdentity; | ||
}); | ||
@@ -1679,6 +1634,6 @@ } | ||
_objectAssign2['default'](actions, new (_bind.apply(ActionsGenerator, [null].concat(argsForConstructor)))()); | ||
fn.assign(actions, new (_bind.apply(ActionsGenerator, [null].concat(argsForConstructor)))()); | ||
})(); | ||
} else { | ||
_objectAssign2['default'](actions, ActionsClass); | ||
fn.assign(actions, ActionsClass); | ||
} | ||
@@ -1688,16 +1643,15 @@ | ||
return Object.keys(actions).reduce(function (obj, action) { | ||
if (typeof actions[action] !== 'function') { | ||
return obj; | ||
fn.eachObject(function (actionName, action) { | ||
if (!fn.isFunction(action)) { | ||
return; | ||
} | ||
// create the action | ||
obj[action] = _utilsMakeAction2['default'](_this2, key, action, actions[action], obj); | ||
exportObj[actionName] = _actions2['default'](_this2, key, actionName, action, exportObj); | ||
// generate a constant | ||
var constant = _utilsAltUtils.formatAsConstant(action); | ||
obj[constant] = obj[action][_symbolsSymbols.ACTION_KEY]; | ||
return obj; | ||
}, exportObj); | ||
var constant = utils.formatAsConstant(actionName); | ||
exportObj[constant] = exportObj[actionName][Sym.ACTION_KEY]; | ||
}, [actions]); | ||
return exportObj; | ||
} | ||
@@ -1711,4 +1665,4 @@ }, { | ||
var state = _utilsStateFunctions.snapshot(this, storeNames); | ||
_objectAssign2['default'](this[_symbolsSymbols.LAST_SNAPSHOT], state); | ||
var state = StateFunctions.snapshot(this, storeNames); | ||
fn.assign(this[Sym.LAST_SNAPSHOT], state); | ||
return this.serialize(state); | ||
@@ -1719,7 +1673,5 @@ } | ||
value: function rollback() { | ||
_utilsStateFunctions.setAppState(this, this.serialize(this[_symbolsSymbols.LAST_SNAPSHOT]), function (store) { | ||
if (store[_symbolsSymbols.LIFECYCLE].rollback) { | ||
store[_symbolsSymbols.LIFECYCLE].rollback(); | ||
} | ||
store.emitChange(); | ||
StateFunctions.setAppState(this, this.serialize(this[Sym.LAST_SNAPSHOT]), function (storeInst) { | ||
storeInst[Sym.LIFECYCLE].emit('rollback'); | ||
storeInst.emitChange(); | ||
}); | ||
@@ -1734,9 +1686,7 @@ } | ||
var initialSnapshot = storeNames.length ? _utilsStateFunctions.filterSnapshots(this, this[_symbolsSymbols.INIT_SNAPSHOT], storeNames) : this[_symbolsSymbols.INIT_SNAPSHOT]; | ||
var initialSnapshot = storeNames.length ? StateFunctions.filterSnapshots(this, this[Sym.INIT_SNAPSHOT], storeNames) : this[Sym.INIT_SNAPSHOT]; | ||
_utilsStateFunctions.setAppState(this, this.serialize(initialSnapshot), function (store) { | ||
if (store[_symbolsSymbols.LIFECYCLE].init) { | ||
store[_symbolsSymbols.LIFECYCLE].init(); | ||
} | ||
store.emitChange(); | ||
StateFunctions.setAppState(this, this.serialize(initialSnapshot), function (storeInst) { | ||
storeInst[Sym.LIFECYCLE].emit('init'); | ||
storeInst.emitChange(); | ||
}); | ||
@@ -1747,3 +1697,3 @@ } | ||
value: function flush() { | ||
var state = this.serialize(_utilsStateFunctions.snapshot(this)); | ||
var state = this.serialize(StateFunctions.snapshot(this)); | ||
this.recycle(); | ||
@@ -1755,7 +1705,5 @@ return state; | ||
value: function bootstrap(data) { | ||
_utilsStateFunctions.setAppState(this, data, function (store) { | ||
if (store[_symbolsSymbols.LIFECYCLE].bootstrap) { | ||
store[_symbolsSymbols.LIFECYCLE].bootstrap(); | ||
} | ||
store.emitChange(); | ||
StateFunctions.setAppState(this, data, function (storeInst) { | ||
storeInst[Sym.LIFECYCLE].emit('bootstrap'); | ||
storeInst.emitChange(); | ||
}); | ||
@@ -1765,8 +1713,8 @@ } | ||
key: 'prepare', | ||
value: function prepare(store, payload) { | ||
value: function prepare(storeInst, payload) { | ||
var data = {}; | ||
if (!store._storeName) { | ||
if (!storeInst.displayName) { | ||
throw new ReferenceError('Store provided does not have a name'); | ||
} | ||
data[store._storeName] = payload; | ||
data[storeInst.displayName] = payload; | ||
return this.serialize(data); | ||
@@ -1813,3 +1761,36 @@ } | ||
},{"./symbols/symbols":9,"./utils/AltUtils":10,"./utils/StateFunctions":11,"./utils/StoreUtils":13,"./utils/makeAction":14,"flux":3,"object-assign":6}]},{},[15])(15) | ||
},{"../utils/functions":14,"./actions":6,"./store":9,"./symbols/symbols":10,"./utils/AltUtils":11,"./utils/StateFunctions":12,"flux":3}],14:[function(require,module,exports){ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.eachObject = eachObject; | ||
exports.assign = assign; | ||
var isFunction = function isFunction(x) { | ||
return typeof x === 'function'; | ||
}; | ||
exports.isFunction = isFunction; | ||
function eachObject(f, o) { | ||
o.forEach(function (from) { | ||
Object.keys(Object(from)).forEach(function (key) { | ||
f(key, from[key]); | ||
}); | ||
}); | ||
} | ||
function assign(target) { | ||
for (var _len = arguments.length, source = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
source[_key - 1] = arguments[_key]; | ||
} | ||
eachObject(function (key, value) { | ||
return target[key] = value; | ||
}, source); | ||
return target; | ||
} | ||
},{}]},{},[13])(13) | ||
}); |
123
lib/index.js
@@ -16,2 +16,4 @@ 'use strict'; | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } | ||
@@ -21,20 +23,28 @@ | ||
var _objectAssign = require('object-assign'); | ||
var _flux = require('flux'); | ||
var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
var _utilsStateFunctions = require('./utils/StateFunctions'); | ||
var _flux = require('flux'); | ||
var StateFunctions = _interopRequireWildcard(_utilsStateFunctions); | ||
var _utilsMakeAction = require('./utils/makeAction'); | ||
var _symbolsSymbols = require('./symbols/symbols'); | ||
var _utilsMakeAction2 = _interopRequireDefault(_utilsMakeAction); | ||
var Sym = _interopRequireWildcard(_symbolsSymbols); | ||
var _utilsStateFunctions = require('./utils/StateFunctions'); | ||
var _utilsFunctions = require('../utils/functions'); | ||
var _utilsStoreUtils = require('./utils/StoreUtils'); | ||
var fn = _interopRequireWildcard(_utilsFunctions); | ||
var _symbolsSymbols = require('./symbols/symbols'); | ||
var _store = require('./store'); | ||
var store = _interopRequireWildcard(_store); | ||
var _utilsAltUtils = require('./utils/AltUtils'); | ||
var utils = _interopRequireWildcard(_utilsAltUtils); | ||
var _actions = require('./actions'); | ||
var _actions2 = _interopRequireDefault(_actions); | ||
var Alt = (function () { | ||
@@ -53,5 +63,5 @@ function Alt() { | ||
this.storeTransforms = config.storeTransforms || []; | ||
this[_symbolsSymbols.ACTIONS_REGISTRY] = {}; | ||
this[_symbolsSymbols.INIT_SNAPSHOT] = {}; | ||
this[_symbolsSymbols.LAST_SNAPSHOT] = {}; | ||
this[Sym.ACTIONS_REGISTRY] = {}; | ||
this[Sym.INIT_SNAPSHOT] = {}; | ||
this[Sym.LAST_SNAPSHOT] = {}; | ||
} | ||
@@ -72,6 +82,6 @@ | ||
var key = StoreModel.displayName || ''; | ||
_utilsStoreUtils.createStoreConfig(this.config, StoreModel); | ||
var Store = _utilsStoreUtils.transformStore(this.storeTransforms, StoreModel); | ||
store.createStoreConfig(this.config, StoreModel); | ||
var Store = store.transformStore(this.storeTransforms, StoreModel); | ||
return typeof Store === 'object' ? _utilsStoreUtils.createStoreFromObject(this, Store, key) : _utilsStoreUtils.createStoreFromClass.apply(undefined, [this, Store, key].concat(args)); | ||
return fn.isFunction(Store) ? store.createStoreFromClass.apply(store, [this, Store, key].concat(args)) : store.createStoreFromObject(this, Store, key); | ||
} | ||
@@ -86,19 +96,19 @@ }, { | ||
var key = iden || StoreModel.displayName || StoreModel.name || ''; | ||
_utilsStoreUtils.createStoreConfig(this.config, StoreModel); | ||
var Store = _utilsStoreUtils.transformStore(this.storeTransforms, StoreModel); | ||
store.createStoreConfig(this.config, StoreModel); | ||
var Store = store.transformStore(this.storeTransforms, StoreModel); | ||
if (this.stores[key] || !key) { | ||
if (this.stores[key]) { | ||
_utilsAltUtils.warn('A store named ' + key + ' already exists, double check your store ' + 'names or pass in your own custom identifier for each store'); | ||
utils.warn('A store named ' + key + ' already exists, double check your store ' + 'names or pass in your own custom identifier for each store'); | ||
} else { | ||
_utilsAltUtils.warn('Store name was not specified'); | ||
utils.warn('Store name was not specified'); | ||
} | ||
key = _utilsAltUtils.uid(this.stores, key); | ||
key = utils.uid(this.stores, key); | ||
} | ||
var storeInstance = typeof Store === 'object' ? _utilsStoreUtils.createStoreFromObject(this, Store, key) : _utilsStoreUtils.createStoreFromClass.apply(undefined, [this, Store, key].concat(args)); | ||
var storeInstance = fn.isFunction(Store) ? store.createStoreFromClass.apply(store, [this, Store, key].concat(args)) : store.createStoreFromObject(this, Store, key); | ||
this.stores[key] = storeInstance; | ||
_utilsStateFunctions.saveInitialSnapshot(this, key); | ||
StateFunctions.saveInitialSnapshot(this, key); | ||
@@ -116,3 +126,3 @@ return storeInstance; | ||
return this.createActions(actionNames.reduce(function (obj, action) { | ||
obj[action] = _utilsAltUtils.dispatchIdentity; | ||
obj[action] = utils.dispatchIdentity; | ||
return obj; | ||
@@ -124,3 +134,3 @@ }, actions)); | ||
value: function createAction(name, implementation, obj) { | ||
return _utilsMakeAction2['default'](this, 'global', name, implementation, obj); | ||
return _actions2['default'](this, 'global', name, implementation, obj); | ||
} | ||
@@ -139,7 +149,7 @@ }, { | ||
var actions = {}; | ||
var key = _utilsAltUtils.uid(this[_symbolsSymbols.ACTIONS_REGISTRY], ActionsClass.displayName || ActionsClass.name || 'Unknown'); | ||
var key = utils.uid(this[Sym.ACTIONS_REGISTRY], ActionsClass.displayName || ActionsClass.name || 'Unknown'); | ||
if (typeof ActionsClass === 'function') { | ||
if (fn.isFunction(ActionsClass)) { | ||
(function () { | ||
_objectAssign2['default'](actions, _utilsAltUtils.getInternalMethods(ActionsClass.prototype, true)); | ||
fn.assign(actions, utils.getInternalMethods(ActionsClass, true)); | ||
@@ -167,3 +177,3 @@ var ActionsGenerator = (function (_ActionsClass) { | ||
actionNames.forEach(function (actionName) { | ||
actions[actionName] = _utilsAltUtils.dispatchIdentity; | ||
actions[actionName] = utils.dispatchIdentity; | ||
}); | ||
@@ -176,6 +186,6 @@ } | ||
_objectAssign2['default'](actions, new (_bind.apply(ActionsGenerator, [null].concat(argsForConstructor)))()); | ||
fn.assign(actions, new (_bind.apply(ActionsGenerator, [null].concat(argsForConstructor)))()); | ||
})(); | ||
} else { | ||
_objectAssign2['default'](actions, ActionsClass); | ||
fn.assign(actions, ActionsClass); | ||
} | ||
@@ -185,16 +195,15 @@ | ||
return Object.keys(actions).reduce(function (obj, action) { | ||
if (typeof actions[action] !== 'function') { | ||
return obj; | ||
fn.eachObject(function (actionName, action) { | ||
if (!fn.isFunction(action)) { | ||
return; | ||
} | ||
// create the action | ||
obj[action] = _utilsMakeAction2['default'](_this2, key, action, actions[action], obj); | ||
exportObj[actionName] = _actions2['default'](_this2, key, actionName, action, exportObj); | ||
// generate a constant | ||
var constant = _utilsAltUtils.formatAsConstant(action); | ||
obj[constant] = obj[action][_symbolsSymbols.ACTION_KEY]; | ||
return obj; | ||
}, exportObj); | ||
var constant = utils.formatAsConstant(actionName); | ||
exportObj[constant] = exportObj[actionName][Sym.ACTION_KEY]; | ||
}, [actions]); | ||
return exportObj; | ||
} | ||
@@ -208,4 +217,4 @@ }, { | ||
var state = _utilsStateFunctions.snapshot(this, storeNames); | ||
_objectAssign2['default'](this[_symbolsSymbols.LAST_SNAPSHOT], state); | ||
var state = StateFunctions.snapshot(this, storeNames); | ||
fn.assign(this[Sym.LAST_SNAPSHOT], state); | ||
return this.serialize(state); | ||
@@ -216,7 +225,5 @@ } | ||
value: function rollback() { | ||
_utilsStateFunctions.setAppState(this, this.serialize(this[_symbolsSymbols.LAST_SNAPSHOT]), function (store) { | ||
if (store[_symbolsSymbols.LIFECYCLE].rollback) { | ||
store[_symbolsSymbols.LIFECYCLE].rollback(); | ||
} | ||
store.emitChange(); | ||
StateFunctions.setAppState(this, this.serialize(this[Sym.LAST_SNAPSHOT]), function (storeInst) { | ||
storeInst[Sym.LIFECYCLE].emit('rollback'); | ||
storeInst.emitChange(); | ||
}); | ||
@@ -231,9 +238,7 @@ } | ||
var initialSnapshot = storeNames.length ? _utilsStateFunctions.filterSnapshots(this, this[_symbolsSymbols.INIT_SNAPSHOT], storeNames) : this[_symbolsSymbols.INIT_SNAPSHOT]; | ||
var initialSnapshot = storeNames.length ? StateFunctions.filterSnapshots(this, this[Sym.INIT_SNAPSHOT], storeNames) : this[Sym.INIT_SNAPSHOT]; | ||
_utilsStateFunctions.setAppState(this, this.serialize(initialSnapshot), function (store) { | ||
if (store[_symbolsSymbols.LIFECYCLE].init) { | ||
store[_symbolsSymbols.LIFECYCLE].init(); | ||
} | ||
store.emitChange(); | ||
StateFunctions.setAppState(this, this.serialize(initialSnapshot), function (storeInst) { | ||
storeInst[Sym.LIFECYCLE].emit('init'); | ||
storeInst.emitChange(); | ||
}); | ||
@@ -244,3 +249,3 @@ } | ||
value: function flush() { | ||
var state = this.serialize(_utilsStateFunctions.snapshot(this)); | ||
var state = this.serialize(StateFunctions.snapshot(this)); | ||
this.recycle(); | ||
@@ -252,7 +257,5 @@ return state; | ||
value: function bootstrap(data) { | ||
_utilsStateFunctions.setAppState(this, data, function (store) { | ||
if (store[_symbolsSymbols.LIFECYCLE].bootstrap) { | ||
store[_symbolsSymbols.LIFECYCLE].bootstrap(); | ||
} | ||
store.emitChange(); | ||
StateFunctions.setAppState(this, data, function (storeInst) { | ||
storeInst[Sym.LIFECYCLE].emit('bootstrap'); | ||
storeInst.emitChange(); | ||
}); | ||
@@ -262,8 +265,8 @@ } | ||
key: 'prepare', | ||
value: function prepare(store, payload) { | ||
value: function prepare(storeInst, payload) { | ||
var data = {}; | ||
if (!store._storeName) { | ||
if (!storeInst.displayName) { | ||
throw new ReferenceError('Store provided does not have a name'); | ||
} | ||
data[store._storeName] = payload; | ||
data[storeInst.displayName] = payload; | ||
return this.serialize(data); | ||
@@ -270,0 +273,0 @@ } |
@@ -33,2 +33,6 @@ 'use strict'; | ||
exports.ALL_LISTENERS = ALL_LISTENERS; | ||
// are we handling our own errors | ||
var HANDLING_ERRORS = _esSymbol2['default'](); | ||
exports.HANDLING_ERRORS = HANDLING_ERRORS; | ||
// initial snapshot | ||
@@ -35,0 +39,0 @@ var INIT_SNAPSHOT = _esSymbol2['default'](); |
@@ -17,4 +17,5 @@ 'use strict'; | ||
function getInternalMethods(obj, isProto) { | ||
function getInternalMethods(Obj, isProto) { | ||
var excluded = isProto ? builtInProto : builtIns; | ||
var obj = isProto ? Obj.prototype : Obj; | ||
return Object.getOwnPropertyNames(obj).reduce(function (value, m) { | ||
@@ -21,0 +22,0 @@ if (excluded.indexOf(m) !== -1) { |
@@ -13,8 +13,2 @@ 'use strict'; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _objectAssign = require('object-assign'); | ||
var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
var _symbolsSymbols = require('../symbols/symbols'); | ||
@@ -24,10 +18,9 @@ | ||
var INIT_SNAPSHOT = Sym.INIT_SNAPSHOT; | ||
var LAST_SNAPSHOT = Sym.LAST_SNAPSHOT; | ||
var LIFECYCLE = Sym.LIFECYCLE; | ||
var STATE_CONTAINER = Sym.STATE_CONTAINER; | ||
var _utilsFunctions = require('../../utils/functions'); | ||
var fn = _interopRequireWildcard(_utilsFunctions); | ||
function setAppState(instance, data, onStore) { | ||
var obj = instance.deserialize(data); | ||
Object.keys(obj).forEach(function (key) { | ||
fn.eachObject(function (key, value) { | ||
var store = instance.stores[key]; | ||
@@ -38,8 +31,8 @@ if (store) { | ||
if (config.onDeserialize) { | ||
obj[key] = config.onDeserialize(obj[key]) || obj[key]; | ||
obj[key] = config.onDeserialize(value) || value; | ||
} | ||
_objectAssign2['default'](store[STATE_CONTAINER], obj[key]); | ||
fn.assign(store[Sym.STATE_CONTAINER], obj[key]); | ||
onStore(store); | ||
} | ||
}); | ||
}, [obj]); | ||
} | ||
@@ -56,6 +49,4 @@ | ||
if (store[LIFECYCLE].snapshot) { | ||
store[LIFECYCLE].snapshot(); | ||
} | ||
var customSnapshot = config.onSerialize && config.onSerialize(store[STATE_CONTAINER]); | ||
store[Sym.LIFECYCLE].emit('snapshot'); | ||
var customSnapshot = config.onSerialize && config.onSerialize(store[Sym.STATE_CONTAINER]); | ||
obj[storeName] = customSnapshot ? customSnapshot : store.getState(); | ||
@@ -67,5 +58,5 @@ return obj; | ||
function saveInitialSnapshot(instance, key) { | ||
var state = instance.deserialize(instance.serialize(instance.stores[key][STATE_CONTAINER])); | ||
instance[INIT_SNAPSHOT][key] = state; | ||
instance[LAST_SNAPSHOT][key] = state; | ||
var state = instance.deserialize(instance.serialize(instance.stores[key][Sym.STATE_CONTAINER])); | ||
instance[Sym.INIT_SNAPSHOT][key] = state; | ||
instance[Sym.LAST_SNAPSHOT][key] = state; | ||
} | ||
@@ -72,0 +63,0 @@ |
{ | ||
"name": "alt", | ||
"version": "0.16.3", | ||
"version": "0.16.4", | ||
"description": "A flux implementation", | ||
@@ -8,9 +8,8 @@ "main": "lib", | ||
"es-symbol": "1.1.2", | ||
"eventemitter3": "^0.1.6", | ||
"flux": "^2.0.1", | ||
"object-assign": "^2.0.0" | ||
"eventemitter3": "0.1.6", | ||
"flux": "2.0.3" | ||
}, | ||
"devDependencies": { | ||
"babel": "^5.1.11", | ||
"babel-core": "^5.1.11", | ||
"babel": "^5.2.13", | ||
"babel-core": "^5.2.13", | ||
"babelify": "^6.0.2", | ||
@@ -49,3 +48,3 @@ "browserify": "^9.0.3", | ||
"build-test": "babel src/alt --out-dir lib -r --stage 0 && babel src/utils --out-dir utils -r --stage 0", | ||
"coverage": "npm run build-test && istanbul cover node_modules/mocha/bin/_mocha -- -u exports -R spec --require ./test/babel --require babel-core/external-helpers test", | ||
"coverage": "npm run build-test && istanbul cover node_modules/mocha/bin/_mocha -- -u exports -R spec --require ./test/babel test", | ||
"clean": "rimraf lib && rimraf utils", | ||
@@ -56,5 +55,6 @@ "lint": "eslint src components", | ||
"pretest": "npm run clean && npm run build-test", | ||
"size": "npm run build-alt; browserify flux.js > flux-build.js; uglifyjs -m -c 'comparisons=false,keep_fargs=true,unsafe=true,unsafe_comps=true,warnings=false' flux-build.js > flux-build.min.js", | ||
"test": "npm run tests-node", | ||
"test-browser": "browserify test/browser/index.js -t babelify --outfile test/browser/tests.js", | ||
"tests-node": "mocha -u exports -R spec --require ./test/babel --require babel-core/external-helpers test" | ||
"tests-node": "mocha -u exports -R spec --require ./test/babel test" | ||
}, | ||
@@ -61,0 +61,0 @@ "browserify-shim": { |
@@ -20,3 +20,3 @@ # alt | ||
* Extremely [flexible](#flexibility) and unopinionated in how you use flux. Create traditional singletons or use dependency injection. | ||
* It is [terse](https://github.com/goatslacker/alt#flux-minus-the-boilerplate). No boilerplate. | ||
* It is terse. No boilerplate. | ||
@@ -74,4 +74,26 @@ ### What does it look like? | ||
## Principles of Flux | ||
## In the Wild | ||
### Examples | ||
* [Airbnb Airpal](https://github.com/airbnb/airpal/tree/master/src/main/resources/assets) | ||
* [Alt Notify](https://github.com/sourcescript/alt-notify) | ||
* [Chrome Devtool](https://github.com/goatslacker/alt-devtool) | ||
* [Example Tests](https://github.com/jdlehman/alt-example-tests) | ||
* [Github Example](https://github.com/RookieOne/react-alt-github-example) | ||
* [Isomorphic Alt](https://github.com/patrickkim/iso-alt) | ||
* [React Router Example](https://github.com/lostpebble/alt-react-router-example) | ||
* [React Router Loopback](https://github.com/bkniffler/react-router-alt-loopback) | ||
* [React Weather](https://github.com/sapegin/react-weather) | ||
* [Shopping Cart](https://github.com/voronianski/flux-comparison/tree/master/alt) | ||
* [Todo](https://github.com/benstokoe/alt-todo) | ||
* [Typeahead](https://github.com/timtyrrell/alt-typeahead) | ||
### Boilerplates | ||
* [Isomorphic Flux Boilerplate](https://github.com/iam4x/isomorphic-flux-boilerplate) | ||
* [React + Webpack + Node](https://github.com/choonkending/react-webpack-node) | ||
## Pure Flux + More | ||
* Unidirectional data flow | ||
@@ -83,21 +105,4 @@ * Stores have no setters | ||
Read more about the [Principles of Flux](https://medium.com/@goatslacker/principles-of-flux-ea872bc20772). | ||
Read about the [Principles of Flux](https://medium.com/@goatslacker/principles-of-flux-ea872bc20772). | ||
## Flux minus the boilerplate | ||
* No [JS "constants"](https://github.com/facebook/flux/blob/master/examples/flux-chat/js/constants/ChatConstants.js). | ||
* No [static string tossing](https://github.com/facebook/flux/blob/master/examples/flux-chat/js/dispatcher/ChatAppDispatcher.js#L39). | ||
* No [massive switch statements](https://github.com/facebook/flux/blob/master/examples/flux-chat/js/stores/MessageStore.js#L111). | ||
There is no giant switch statement you have to write in your store and this is because alt removes the burden of constants from the developer. | ||
This has the wonderful side effect of making the custom dispatcher logic unnecessary, thus removing the dispatcher from the equation. | ||
Make no mistake, there is still a single dispatcher through which actions flow on their merry way to the store, in fact, you still get the benefit of being able to hook into the dispatcher to listen to all the global events for debugging, fun, or misery. | ||
The dispatcher is just a part of alt and something you don't necessarily have to write custom code for. | ||
These removals make the code terse and easy to follow, there is less indirection and the learning curve to grok is much lower. | ||
Think I'm lying? [Check out an example](#differences-example). | ||
## Flux enhanced | ||
One really cool aspect of alt is that you can save snapshots of the entire application's state at any given point in time. | ||
@@ -113,9 +118,13 @@ This has many different use cases like: | ||
* [ActionListener](/src/utils/ActionListeners.js) lets you listen to individual actions without having to create a store. | ||
* [AltContainer](/components/AltContainer.js) a higher-order container component that is your swiss army knife for React. | ||
* [atomic](/src/utils/atomic.js) enables your stores for atomic transactions. | ||
* [connectToStores](/src/utils/connectToStores.js) a higher-order function that wraps your React components for store listening. | ||
* [decorators](/src/utils/decorators.js) a collection of useful ES7 decorators for working with alt. | ||
* [DispatchRecorder](/src/utils/DispatcherRecorder.js) lets you record all your dispatches and replay them back at a later time. | ||
* [ImmutableUtil](/src/utils/ImmutableUtil.js) makes working with immutable-js easy. | ||
* [IsomorphicRenderer](/src/utils/IsomorphicRenderer.js) a function that wraps your component to be isomorphic ready. | ||
* [TimeTravel](/src/utils/TimeTravel.js) enhances your stores so they are able to travel through different states in time. | ||
* [FinalStore](/src/utils/makeFinalStore.js) is a Store that you can listen to that only emits when all your other stores have received all their data. | ||
* [IsomorphicRenderer](/src/utils/IsomorphicRenderer.js) a function that wraps your component to be isomorphic ready. | ||
* [ActionListener](/src/utils/ActionListeners.js) lets you listen to individual actions without having to create a store. | ||
Last but not least, alt is meant to work with ES6. That is we're betting you'll be writing your stores and actions as classes. This part isn't necessary but you really should write some ES6 anyways because it's nice. | ||
## Topical Guide | ||
@@ -122,0 +131,0 @@ |
@@ -1,3 +0,4 @@ | ||
require('babel/register')({ | ||
require('babel-core/external-helpers') | ||
require('babel/register-without-polyfill')({ | ||
stage: 0 | ||
}) |
import { assert } from 'chai' | ||
import Alt from '../' | ||
import { createActions, createStore } from '../utils/decorators' | ||
import { createActions, createStore, bind, expose } from '../utils/decorators' | ||
@@ -54,3 +54,37 @@ const alt = new Alt() | ||
}, | ||
'decorating action listening and public methods'() { | ||
const TodoActions = alt.generateActions('addTodo') | ||
@createStore(alt) | ||
class TodoStore { | ||
static displayName = 'TodoStore' | ||
constructor() { | ||
this.todos = {} | ||
this.id = 0 | ||
} | ||
uid() { | ||
return this.id++ | ||
} | ||
@bind(TodoActions.addTodo) | ||
addTodo(todo) { | ||
this.todos[this.uid()] = todo | ||
} | ||
@expose | ||
getTodo(id) { | ||
return this.getState().todos[id] | ||
} | ||
} | ||
TodoActions.addTodo('hello') | ||
assert(TodoStore.getState().id === 1) | ||
assert.isFunction(TodoStore.getTodo) | ||
assert(TodoStore.getTodo(0) === 'hello') | ||
}, | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
import { assign } from '../../utils/functions' | ||
class ReactComponent { | ||
@@ -55,3 +57,3 @@ setState(state) { | ||
: {} | ||
component.props = Object.assign({}, defaultProps, props) | ||
component.props = assign({}, defaultProps, props) | ||
@@ -58,0 +60,0 @@ component.state = component.getInitialState |
@@ -504,7 +504,7 @@ import Alt from '../' | ||
assert.deepEqual(Object.keys(JSON.parse(snapshot)), ['MyStore', 'AltSecondStore'], 'the snapshot includes specified stores') | ||
assert.isFalse(Object.keys(JSON.parse(snapshot)).includes('LifeCycleStore'), 'the snapshot does not include unspecified stores') | ||
assert(Object.keys(JSON.parse(snapshot)).indexOf('LifeCycleStore') === -1, 'the snapshot does not include unspecified stores') | ||
const snapshot2 = alt.takeSnapshot(myStore, secondStore) | ||
assert.deepEqual(Object.keys(JSON.parse(snapshot2)), ['MyStore', 'AltSecondStore'], 'the snapshot includes specified stores') | ||
assert.isFalse(Object.keys(JSON.parse(snapshot2)).includes('LifeCycleStore'), 'the snapshot does not include unspecified stores') | ||
assert(Object.keys(JSON.parse(snapshot2)).indexOf('LifeCycleStore') === -1, 'the snapshot does not include unspecified stores') | ||
}, | ||
@@ -711,3 +711,3 @@ | ||
this.foo = 'bar' | ||
this.exportPublicMethods('foo') | ||
this.exportPublicMethods({ foo: 'foo' }) | ||
} | ||
@@ -749,3 +749,3 @@ } | ||
assert.throw(() => alt.createStore(MethodsAreUnary1), TypeError, /Action handler in store .* was defined with 2 parameters/) | ||
assert.throw(() => alt.createStore(MethodsAreUnary1), TypeError, /Action handler in store .* was defined with two parameters/) | ||
@@ -760,3 +760,3 @@ class MethodsAreUnary2 { | ||
assert.throw(() => alt.createStore(MethodsAreUnary2), TypeError, /Action handler in store .* was defined with 2 parameters/) | ||
assert.throw(() => alt.createStore(MethodsAreUnary2), TypeError, /Action handler in store .* was defined with two parameters/) | ||
}, | ||
@@ -763,0 +763,0 @@ |
@@ -42,8 +42,2 @@ import Alt from '../dist/alt-with-runtime' | ||
MyStore.StoreModel.state.test = 4 | ||
assert(MyStore.StoreModel.state.test === 4, 'I have changed the store model state') | ||
assert(MyStore.getState().test === 2, 'but that does not affect our store state') | ||
assert(MyStore.StoreModel === MyStoreModelObj, 'the store model is the same as the original object') | ||
@@ -50,0 +44,0 @@ |
@@ -7,4 +7,2 @@ 'use strict'; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
@@ -14,6 +12,4 @@ | ||
var _objectAssign = require('object-assign'); | ||
var _functions = require('./functions'); | ||
var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
var noop = function noop() {}; | ||
@@ -54,3 +50,3 @@ | ||
_objectAssign2['default'](DerivedStore.prototype, StorePrototype); | ||
_functions.assign(DerivedStore.prototype, StorePrototype); | ||
return new DerivedStore(); | ||
@@ -57,0 +53,0 @@ }, |
@@ -23,2 +23,4 @@ 'use strict'; | ||
var _functions = require('./functions'); | ||
function makeAtomicClass(alt, StoreModel) { | ||
@@ -60,3 +62,3 @@ var AtomicClass = (function (_StoreModel) { | ||
return function (StoreModel) { | ||
return typeof StoreModel === 'function' ? makeAtomicClass(alt, StoreModel) : makeAtomicObject(alt, StoreModel); | ||
return _functions.isFunction(StoreModel) ? makeAtomicClass(alt, StoreModel) : makeAtomicObject(alt, StoreModel); | ||
}; | ||
@@ -63,0 +65,0 @@ } |
@@ -58,12 +58,10 @@ 'use strict'; | ||
var _objectAssign = require('object-assign'); | ||
var _functions = require('./functions'); | ||
var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
function connectToStores(Component) { | ||
// Check for required static methods. | ||
if (typeof Component.getStores !== 'function') { | ||
if (!_functions.isFunction(Component.getStores)) { | ||
throw new Error('connectToStores() expects the wrapped component to have a static getStores() method'); | ||
} | ||
if (typeof Component.getPropsFromStores !== 'function') { | ||
if (!_functions.isFunction(Component.getPropsFromStores)) { | ||
throw new Error('connectToStores() expects the wrapped component to have a static getPropsFromStores() method'); | ||
@@ -104,3 +102,3 @@ } | ||
render: function render() { | ||
return _react2['default'].createElement(Component, _objectAssign2['default']({}, this.props, this.state)); | ||
return _react2['default'].createElement(Component, _functions.assign({}, this.props, this.state)); | ||
} | ||
@@ -107,0 +105,0 @@ }); |
@@ -1,4 +0,4 @@ | ||
"use strict"; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
@@ -8,3 +8,17 @@ }); | ||
exports.createStore = createStore; | ||
exports.bind = bind; | ||
exports.expose = expose; | ||
var _functions = require('./functions'); | ||
/* istanbul ignore next */ | ||
function NoopClass() {} | ||
var builtInProto = Object.getOwnPropertyNames(NoopClass.prototype); | ||
function addMeta(description, decoration) { | ||
description.value.alt = description.value.alt || {}; | ||
_functions.assign(description.value.alt, decoration); | ||
return description; | ||
} | ||
function createActions(alt) { | ||
@@ -26,4 +40,43 @@ for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
return function (Store) { | ||
var proto = Store.prototype; | ||
var publicMethods = {}; | ||
var bindListeners = {}; | ||
Object.getOwnPropertyNames(proto).forEach(function (name) { | ||
if (builtInProto.indexOf(name) !== -1) return; | ||
var meta = proto[name].alt; | ||
if (!meta) { | ||
return; | ||
} | ||
/* istanbul ignore else */ | ||
if (meta.action) { | ||
bindListeners[name] = meta.action; | ||
} else if (meta.publicMethod) { | ||
publicMethods[name] = proto[name]; | ||
} | ||
}); | ||
Store.config = _functions.assign({ | ||
bindListeners: bindListeners, | ||
publicMethods: publicMethods | ||
}, Store.config); | ||
return alt.createStore.apply(alt, [Store, undefined].concat(args)); | ||
}; | ||
} | ||
function bind() { | ||
for (var _len3 = arguments.length, actionIds = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
actionIds[_key3] = arguments[_key3]; | ||
} | ||
return function (obj, name, description) { | ||
return addMeta(description, { action: actionIds }); | ||
}; | ||
} | ||
function expose(obj, name, description) { | ||
return addMeta(description, { publicMethod: true }); | ||
} |
@@ -21,6 +21,4 @@ 'use strict'; | ||
var _objectAssign = require('object-assign'); | ||
var _functions = require('./functions'); | ||
var _objectAssign2 = _interopRequireDefault(_objectAssign); | ||
var _makeFinalStore = require('./makeFinalStore'); | ||
@@ -33,3 +31,3 @@ | ||
var history = _objectAssign2['default']({ | ||
var history = _functions.assign({ | ||
max: 300 | ||
@@ -36,0 +34,0 @@ }, options); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
465517
3
9170
837
+ Addedflux@2.0.3(transitive)
- Removedobject-assign@^2.0.0
- Removedasap@2.0.6(transitive)
- Removedcore-js@1.2.7(transitive)
- Removedencoding@0.1.13(transitive)
- Removedfbemitter@2.1.1(transitive)
- Removedfbjs@0.1.0-alpha.70.8.18(transitive)
- Removedflux@2.1.1(transitive)
- Removediconv-lite@0.6.3(transitive)
- Removedimmutable@3.8.2(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedisomorphic-fetch@2.2.1(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedloose-envify@1.4.0(transitive)
- Removednode-fetch@1.7.3(transitive)
- Removedobject-assign@2.1.14.1.1(transitive)
- Removedpromise@7.3.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsetimmediate@1.0.5(transitive)
- Removedua-parser-js@0.7.40(transitive)
- Removedwhatwg-fetch@0.9.03.6.20(transitive)
Updatedeventemitter3@0.1.6
Updatedflux@2.0.3