alm
Version:
The best IDE for TypeScript
102 lines (101 loc) • 3.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var redux_1 = require("redux");
/**
* Instead of creating a "reducer" (with a switch statement!) + "actionCreator"
* You just call `add` to provide a typeSafe "reducer" and we give you a typeSafe "actionCreator"
*/
var SimpleRedux = /** @class */ (function () {
function SimpleRedux(initialState) {
var _this = this;
this.initialState = initialState;
this._listeners = {};
this.getState = function () {
return _this.store.getState();
};
this.subscribe = function (changed) {
return { dispose: _this.store.subscribe(function () { return changed(_this.getState()); }) };
};
this.subscribeSub = function (select, onChange) {
var currentState = select(_this.getState());
var handleChange = function () {
var nextState = select(_this.getState());
if (nextState !== currentState) {
currentState = nextState;
onChange(currentState);
}
};
return _this.subscribe(handleChange);
};
this._reducer = function (state, action) {
if (state === void 0) { state = _this.initialState; }
if (_this._listeners[action.type])
return _this._extendState(state, _this._listeners[action.type](state, action.payload));
else
return state;
};
/**
* Take every field of fields and put them override them in the complete object
* NOTE: this API is a bit reverse of extend because of the way generic constraints work in TypeScript
*/
this.updateFields = function (fields) { return function (complete) {
var result = {};
for (var id in complete) {
result[id] = complete[id];
}
for (var id in fields) {
result[id] = fields[id];
}
return result;
}; };
this.store = redux_1.createStore(this._reducer);
}
SimpleRedux.prototype.add = function (usefulNameForDebugging, reducer) {
var _this = this;
if (this._listeners[usefulNameForDebugging])
throw new Error("REDUX: already have reducer \"" + usefulNameForDebugging + "\"");
var dispatcher = function (payload) { return _this.store.dispatch({
type: usefulNameForDebugging,
payload: payload
}); };
this._listeners[usefulNameForDebugging] = reducer;
return dispatcher;
};
/**
* WARNING: this only supports 1 level of nesting
*/
SimpleRedux.prototype.addSub = function (usefulNameForDebugging, select, reducer) {
var _this = this;
var dispatcher = function (payload) { return _this.store.dispatch({
type: usefulNameForDebugging,
payload: payload
}); };
this._listeners[usefulNameForDebugging] = function (state, payload) {
var sub = select(state);
for (var key in state) {
if (state[key] == sub)
break;
}
var newSub = reducer(sub, payload);
state[key] = newSub;
return state;
};
return dispatcher;
};
SimpleRedux.prototype._extendState = function (state, newState) {
var result = {};
for (var key in state) {
result[key] = state[key];
}
for (var key in newState) {
result[key] = newState[key];
}
return result;
};
// Update the item at index
SimpleRedux.prototype.updateArrayItem = function (array, index, item) {
return array.map(function (x, i) { return i == index ? item : x; });
};
return SimpleRedux;
}());
exports.SimpleRedux = SimpleRedux;