overvue-rx
Version:
A library providing Vue applications with 'asynchronous-first' state management
111 lines (82 loc) • 4.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Store = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*import fs from 'fs';
import webpack from 'webpack';*/
exports.default = install;
var _Rx = require('rxjs/Rx');
var _Rx2 = _interopRequireDefault(_Rx);
var _Observable = require('rxjs/Observable');
var _mixin = require('./mixin');
var _mixin2 = _interopRequireDefault(_mixin);
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 isObservable = function isObservable(obs) {
return obs instanceof _Observable.Observable;
};
var Store = exports.Store = function () {
function Store() {
var initialState = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, Store);
// initial state is overridden if we have session data in order to allow for page refreshes
this.state = this.hasSessionData() ? this.getSessionData() : initialState.state;
// set up the motherStream, which will host all new action events
this.motherStream$ = new _Rx2.default.BehaviorSubject();
}
// create the motherStream, passing in mutators and state
_createClass(Store, [{
key: 'createStateStream',
value: function createStateStream(mutate) {
var _this = this;
var state = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.state;
return this.motherStream$.flatMap(function (action) {
return isObservable(action) ? action : _Observable.Observable.from([action]);
}).startWith(state).scan(function (state, action) {
if (action) {
mutate(_this.state, action);
_this.setSessionData(_this.state);
}
});
}
// each action will be dispatched as a new event on the motherStream
}, {
key: 'dispatchAction',
value: function dispatchAction(func) {
return function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var action = func.call.apply(func, [null].concat(args));
this.motherStream$.next(action);
if (isObservable(action.payload)) {
this.motherStream$.next(action.payload);
}
return action;
}.bind(this);
}
// session storage methods
}, {
key: 'setSessionData',
value: function setSessionData(state) {
sessionStorage.setItem('overVue', JSON.stringify(state));
}
}, {
key: 'getSessionData',
value: function getSessionData() {
return JSON.parse(sessionStorage.getItem('overVue'));
}
}, {
key: 'hasSessionData',
value: function hasSessionData() {
return sessionStorage.getItem('overVue');
}
}]);
return Store;
}();
// install is necessary to integrate with Vue
// This will be called from within Vue.use
function install(_Vue) {
(0, _mixin2.default)(_Vue);
}