alt
Version:
A flux implementation
162 lines (124 loc) • 4.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _functions = require('../functions');
var fn = _interopRequireWildcard(_functions);
var _transmitter = require('transmitter');
var _transmitter2 = _interopRequireDefault(_transmitter);
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"); } }
var AltStore = function () {
function AltStore(alt, model, state, StoreModel) {
var _this = this;
_classCallCheck(this, AltStore);
var lifecycleEvents = model.lifecycleEvents;
this.transmitter = (0, _transmitter2['default'])();
this.lifecycle = function (event, x) {
if (lifecycleEvents[event]) lifecycleEvents[event].publish(x);
};
this.state = state;
this.alt = alt;
this.preventDefault = false;
this.displayName = model.displayName;
this.boundListeners = model.boundListeners;
this.StoreModel = StoreModel;
this.reduce = model.reduce || function (x) {
return x;
};
this.subscriptions = [];
var output = model.output || function (x) {
return x;
};
this.emitChange = function () {
return _this.transmitter.publish(output(_this.state));
};
var handleDispatch = function handleDispatch(f, payload) {
try {
return f();
} catch (e) {
if (model.handlesOwnErrors) {
_this.lifecycle('error', {
error: e,
payload: payload,
state: _this.state
});
return false;
}
throw e;
}
};
fn.assign(this, model.publicMethods);
// Register dispatcher
this.dispatchToken = alt.dispatcher.register(function (payload) {
_this.preventDefault = false;
_this.lifecycle('beforeEach', {
payload: payload,
state: _this.state
});
var actionHandlers = model.actionListeners[payload.action];
if (actionHandlers || model.otherwise) {
var result = void 0;
if (actionHandlers) {
result = handleDispatch(function () {
return actionHandlers.filter(Boolean).every(function (handler) {
return handler.call(model, payload.data, payload.action) !== false;
});
}, payload);
} else {
result = handleDispatch(function () {
return model.otherwise(payload.data, payload.action);
}, payload);
}
if (result !== false && !_this.preventDefault) _this.emitChange();
}
if (model.reduce) {
handleDispatch(function () {
var value = model.reduce(_this.state, payload);
if (value !== undefined) _this.state = value;
}, payload);
if (!_this.preventDefault) _this.emitChange();
}
_this.lifecycle('afterEach', {
payload: payload,
state: _this.state
});
});
this.lifecycle('init');
}
AltStore.prototype.listen = function () {
function listen(cb) {
var _this2 = this;
if (!fn.isFunction(cb)) throw new TypeError('listen expects a function');
var _transmitter$subscrib = this.transmitter.subscribe(cb);
var dispose = _transmitter$subscrib.dispose;
this.subscriptions.push({ cb: cb, dispose: dispose });
return function () {
_this2.lifecycle('unlisten');
dispose();
};
}
return listen;
}();
AltStore.prototype.unlisten = function () {
function unlisten(cb) {
this.lifecycle('unlisten');
this.subscriptions.filter(function (subscription) {
return subscription.cb === cb;
}).forEach(function (subscription) {
return subscription.dispose();
});
}
return unlisten;
}();
AltStore.prototype.getState = function () {
function getState() {
return this.StoreModel.config.getState.call(this, this.state);
}
return getState;
}();
return AltStore;
}();
exports['default'] = AltStore;
module.exports = exports['default'];