fluorine-orchestra
Version:
A data orchestration layer for Fluorine
234 lines (175 loc) • 6.4 kB
JavaScript
import _Object$keys from 'babel-runtime/core-js/object/keys';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _Symbol from 'babel-runtime/core-js/symbol';
import invariant from 'invariant';
import { BehaviorSubject } from 'rxjs';
import toMap from './util/toMap';
import createReducerForStore from './util/createReducerForStore';
import { Collection } from './Collection';
import { Iterable, OrderedMap, Set } from 'immutable';
import { STORE_INSERT, STORE_REMOVE, STORE_FILTER, STORE_UPDATE } from './constants/StoreConstants';
var missing = _Symbol('missingSubject');
function _ref(transformer) {
invariant(typeof transformer === 'function', 'Store: `transformer` is expected to be a function.');
this.hooks.pre = transformer;
return this;
}
function _ref2(identifier) {
var dependencies = this.dependencies;
invariant(typeof identifier === 'string', 'Store: `identifier` is expected to be a string.');
invariant(!dependencies[identifier], 'Store: There is already an existing dependency to the store `' + identifier + '`.');
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
invariant(args.length <= 2 && args.length >= 1, 'Store: `dependsOn` is expected to receive `setter` or `getter` and `setter`.');
var dependency = void 0;
if (args.length === 1) {
var setter = args[0];
invariant(typeof setter === 'function', 'Store: `setter` is expected to be a function.');
dependency = { identifier: identifier, setter: setter };
} else {
var getter = args[0];
var _setter = args[1];
invariant(typeof getter === 'function', 'Store: `getter` is expected to be a function.');
invariant(typeof _setter === 'function', 'Store: `setter` is expected to be a function.');
dependency = { identifier: identifier, getter: getter, setter: _setter };
}
dependencies[identifier] = dependency;
return this;
}
function _ref3(transformer) {
invariant(typeof transformer === 'function', 'Store: `transformer` is expected to be a function.');
this.hooks.post = transformer;
return this;
}
function _ref4() {
return this[missing].subject.asObservable();
}
function _ref5(col) {
invariant(col && col.prototype instanceof Collection, 'Store: `col` is expected to be a Collection.');
this.collection = col;
return this;
}
function _ref6() {
return this.identifier;
}
function _ref17(x) {
return toMap(x);
}
function _ref7() {
return this.hooks.pre || _ref17;
}
function _ref8() {
return this.dependencies;
}
function _ref9() {
return this.hooks.post;
}
function _ref10() {
if (this.reducer && typeof this.reducer === 'function') {
return this.reducer;
}
this.reducer = createReducerForStore(this);
return this.reducer;
}
function _ref11() {
var CollectionClass = this.collection || Collection;
var res = new CollectionClass();
res.dependencies = _Object$keys(this.dependencies);
return res;
}
function _ref18(x) {
return typeof x === 'string';
}
function _ref12(ids) {
var identifier = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var _missing2 = this[missing];
var cache = _missing2.cache;
var subject = _missing2.subject;
invariant(Set.isSet(ids) && ids.every(_ref18), 'Store: `ids` is expected to be an Immutable.Set of ids (string).');
cache[identifier] = ids;
var missingIds = _Object$keys(cache).reduce(function (acc, key) {
var _set = cache[key];
return _set ? acc.union(_set) : acc;
}, new Set());
subject.next(missingIds);
}
function _ref13(payload, groupId) {
invariant(payload && (Iterable.isKeyed(payload) || Iterable.isIterable(payload) && payload.every && payload.every(Iterable.isKeyed)), 'Store: `payload` is expected to be a keyed iterable or an iterable containing keyed iterables.');
var identifier = this.identifier;
return {
type: STORE_INSERT,
identifier: identifier,
payload: payload,
groupId: groupId
};
}
function _ref14(payload) {
invariant(payload && (typeof payload === 'string' || Iterable.isKeyed(payload) && typeof payload.get('id') === 'string'), 'Store: `payload` is expected to be an id or a keyed iterable containing an id.');
var identifier = this.identifier;
return {
type: STORE_REMOVE,
identifier: identifier,
payload: payload
};
}
function _ref15(selector) {
invariant(typeof selector === 'function', 'Store: `selector` is expected to be a function.');
var identifier = this.identifier;
return {
type: STORE_FILTER,
identifier: identifier,
selector: selector
};
}
function _ref16(selector) {
invariant(typeof selector === 'function', 'Store: `selector` is expected to be a function.');
var identifier = this.identifier;
return {
type: STORE_UPDATE,
identifier: identifier,
selector: selector
};
}
export var Store = function () {
Store.isStore = function isStore(obj) {
return typeof obj === 'object' && obj instanceof Store;
};
function Store(identifier) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, Store);
invariant(typeof identifier === 'string', 'Store: `identifier` is expected to be a string.');
this[missing] = {
subject: new BehaviorSubject(),
cache: {}
};
this.identifier = identifier;
this.opts = opts;
this.dependencies = {};
this.hooks = {};
this.insert = this.insert.bind(this);
this.remove = this.remove.bind(this);
this.filter = this.filter.bind(this);
this.update = this.update.bind(this);
}
Store.prototype.pre = _ref;
Store.prototype.dependsOn = _ref2;
Store.prototype.post = _ref3;
Store.prototype.observeMissing = _ref4;
Store.prototype.useCollection = _ref5;
Store.prototype.getIdentifier = _ref6;
Store.prototype.getPre = _ref7;
Store.prototype.getDependencies = _ref8;
Store.prototype.getPost = _ref9;
Store.prototype.getReducer = _ref10;
Store.prototype.createCollection = _ref11;
Store.prototype._missing = _ref12;
Store.prototype.insert = _ref13;
Store.prototype.remove = _ref14;
Store.prototype.filter = _ref15;
Store.prototype.update = _ref16;
return Store;
}();
export default function createStore(identifier, opts) {
return new Store(identifier, opts);
}