mobx-create-stores
Version:
creates an object with MobX stores (+ links stores to routes)
117 lines (86 loc) • 3.37 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react-router'), require('mobx-react-router')) :
typeof define === 'function' && define.amd ? define(['react-router', 'mobx-react-router'], factory) :
(global.mobxCreateStores = factory(global.reactRouter,global.mobxReactRouter));
}(this, (function (reactRouter,mobxReactRouter) { 'use strict';
var linkHandlers = (function (_ref) {
var _ref$stores = _ref.stores,
stores = _ref$stores === undefined ? [] : _ref$stores,
location = _ref.location,
path = _ref.path,
_ref$exact = _ref.exact,
exact = _ref$exact === undefined ? false : _ref$exact,
_ref$strict = _ref.strict,
strict = _ref$strict === undefined ? false : _ref$strict,
onMatch = _ref.onMatch;
var match = reactRouter.matchPath(location.pathname, { path: path, exact: exact, strict: strict });
if (match && onMatch) onMatch({ stores: stores, match: match, history: history });
});
var createRouterStore = (function (h) {
var routerStore = new mobxReactRouter.RouterStore();
var history = mobxReactRouter.syncHistoryWithStore(h, routerStore);
return { history: history, routerStore: routerStore };
});
var createStore = (function () {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
Store = _ref.Store,
_ref$history = _ref.history,
history = _ref$history === undefined ? false : _ref$history;
var store = new Store();
if (history && store.handleLocationChange) {
history.subscribe(store.handleLocationChange);
}
return store;
});
var getStoreName = (function (store) {
var name = store.constructor.name;
return "" + name.charAt(0).toLowerCase() + name.substring(1);
});
var createStoresObj = (function () {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
st = _ref.stores,
_ref$history = _ref.history,
h = _ref$history === undefined ? false : _ref$history;
var routerStore = void 0,
history = void 0;
var stores = {};
if (h) {
var _createRouterStore = createRouterStore(h);
history = _createRouterStore.history;
routerStore = _createRouterStore.routerStore;
}
st = st.map(function (Store) {
return createStore({ Store: Store, history: history });
});
if (h) st.push(routerStore);
st.forEach(function (s) {
var name = getStoreName(s);
stores[name] = s;
if (process.env.NODE_ENV !== 'production') window[name] = s;
});
return stores;
});
var index = (function () {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$stores = _ref.stores,
stores = _ref$stores === undefined ? [] : _ref$stores,
h = _ref.history,
_ref$routes = _ref.routes,
routes = _ref$routes === undefined ? [] : _ref$routes;
var history = void 0;
stores = createStoresObj({
history: h,
stores: stores
});
if (stores.routerStore && routes.length > 0) {
history = stores.routerStore.history;
history.subscribe(function (location) {
routes.forEach(function (r) {
return linkHandlers(Object.assign({}, r, { stores: stores, location: location }));
});
});
}
return { history: history, stores: stores };
});
return index;
})));