alt
Version:
A flux implementation
212 lines (161 loc) • 6.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createStoreConfig = createStoreConfig;
exports.transformStore = transformStore;
exports.createStoreFromObject = createStoreFromObject;
exports.createStoreFromClass = createStoreFromClass;
var _AltUtils = require('../utils/AltUtils');
var utils = _interopRequireWildcard(_AltUtils);
var _functions = require('../functions');
var fn = _interopRequireWildcard(_functions);
var _AltStore = require('./AltStore');
var _AltStore2 = _interopRequireDefault(_AltStore);
var _StoreMixin = require('./StoreMixin');
var _StoreMixin2 = _interopRequireDefault(_StoreMixin);
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; } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
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) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function doSetState(store, storeInstance, state) {
if (!state) {
return;
}
var config = storeInstance.StoreModel.config;
var nextState = fn.isFunction(state) ? state(storeInstance.state) : state;
storeInstance.state = config.setState.call(store, storeInstance.state, nextState);
if (!store.alt.dispatcher.isDispatching()) {
store.emitChange();
}
}
function createPrototype(proto, alt, key, extras) {
return fn.assign(proto, _StoreMixin2['default'], {
displayName: key,
alt: alt,
dispatcher: alt.dispatcher,
preventDefault: function () {
function preventDefault() {
this.getInstance().preventDefault = true;
}
return preventDefault;
}(),
boundListeners: [],
lifecycleEvents: {},
actionListeners: {},
publicMethods: {},
handlesOwnErrors: false
}, extras);
}
function createStoreConfig(globalConfig, StoreModel) {
StoreModel.config = fn.assign({
getState: function () {
function getState(state) {
if (Array.isArray(state)) {
return state.slice();
} else if (fn.isMutableObject(state)) {
return fn.assign({}, state);
}
return state;
}
return getState;
}(),
setState: function () {
function setState(currentState, nextState) {
if (fn.isMutableObject(nextState)) {
return fn.assign(currentState, nextState);
}
return nextState;
}
return setState;
}()
}, globalConfig, StoreModel.config);
}
function transformStore(transforms, StoreModel) {
return transforms.reduce(function (Store, transform) {
return transform(Store);
}, StoreModel);
}
function createStoreFromObject(alt, StoreModel, key) {
var storeInstance = void 0;
var StoreProto = createPrototype({}, alt, key, fn.assign({
getInstance: function () {
function getInstance() {
return storeInstance;
}
return getInstance;
}(),
setState: function () {
function setState(nextState) {
doSetState(this, storeInstance, nextState);
}
return setState;
}()
}, StoreModel));
// bind the store listeners
/* istanbul ignore else */
if (StoreProto.bindListeners) {
_StoreMixin2['default'].bindListeners.call(StoreProto, StoreProto.bindListeners);
}
/* istanbul ignore else */
if (StoreProto.observe) {
_StoreMixin2['default'].bindListeners.call(StoreProto, StoreProto.observe(alt));
}
// bind the lifecycle events
/* istanbul ignore else */
if (StoreProto.lifecycle) {
fn.eachObject(function (eventName, event) {
_StoreMixin2['default'].on.call(StoreProto, eventName, event);
}, [StoreProto.lifecycle]);
}
// create the instance and fn.assign the public methods to the instance
storeInstance = fn.assign(new _AltStore2['default'](alt, StoreProto, StoreProto.state !== undefined ? StoreProto.state : {}, StoreModel), StoreProto.publicMethods, {
displayName: key,
config: StoreModel.config
});
return storeInstance;
}
function createStoreFromClass(alt, StoreModel, key) {
var storeInstance = void 0;
var config = StoreModel.config;
// Creating a class here so we don't overload the provided store's
// prototype with the mixin behaviour and I'm extending from StoreModel
// so we can inherit any extensions from the provided store.
var Store = function (_StoreModel) {
_inherits(Store, _StoreModel);
function Store() {
_classCallCheck(this, Store);
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return _possibleConstructorReturn(this, _StoreModel.call.apply(_StoreModel, [this].concat(args)));
}
return Store;
}(StoreModel);
createPrototype(Store.prototype, alt, key, {
type: 'AltStore',
getInstance: function () {
function getInstance() {
return storeInstance;
}
return getInstance;
}(),
setState: function () {
function setState(nextState) {
doSetState(this, storeInstance, nextState);
}
return setState;
}()
});
for (var _len = arguments.length, argsForClass = Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
argsForClass[_key - 3] = arguments[_key];
}
var store = new (Function.prototype.bind.apply(Store, [null].concat(argsForClass)))();
/* istanbul ignore next */
if (config.bindListeners) store.bindListeners(config.bindListeners);
/* istanbul ignore next */
if (config.datasource) store.registerAsync(config.datasource);
storeInstance = fn.assign(new _AltStore2['default'](alt, store, store.state !== undefined ? store.state : store, StoreModel), utils.getInternalMethods(StoreModel), config.publicMethods, { displayName: key });
return storeInstance;
}