UNPKG

@rxloop/loading

Version:
168 lines (152 loc) 4.41 kB
function loading(config) { if (config === void 0) { config = { name: 'loading' }; } return function init(_ref) { var _this = this; var onModelBeforeCreate$ = _ref.onModelBeforeCreate$, onPipeStart$ = _ref.onPipeStart$, onPipeEnd$ = _ref.onPipeEnd$, onPipeCancel$ = _ref.onPipeCancel$, onPipeError$ = _ref.onPipeError$, onStart$ = _ref.onStart$; var _model = { name: config.name, state: { pipes: {} }, reducers: { init: function init(state, action) { state.pipes = action.pipes; return state; }, pipeStart: function pipeStart(state, action) { var pipeCounterKey = action.pipe + "Counter"; var pipeCounter = state.pipes[action.model][pipeCounterKey] + action.loading; state.pipes[action.model][pipeCounterKey] = pipeCounter; state.pipes[action.model][action.pipe] = pipeCounter > 0; return state; }, pipeStop: function pipeStop(state, action) { var pipeCounterKey = action.pipe + "Counter"; state.pipes[action.model][pipeCounterKey] = 0; state.pipes[action.model][action.pipe] = false; return state; } } }; this.model(_model); this.stream(config.name).subscribe(); onModelBeforeCreate$.subscribe(function (_ref2) { var model = _ref2.model; if (typeof model.state !== 'object' || !model.pipes || model.state.loading !== void 0) return; var loading = {}; Object.keys(model.pipes).forEach(function (pipe) { loading[pipe + "Counter"] = 0; loading[pipe] = false; }); model.state.loading = loading; model.reducers.loadingStart = loadingStart; model.reducers.loadingEnd = loadingEnd; function loadingStart(state, _ref3) { var pipe = _ref3.payload.pipe; var pipeCounterKey = pipe + "Counter"; var pipeCounter = state.loading[pipeCounterKey] + 1; state.loading[pipeCounterKey] = pipeCounter; state.loading[pipe] = pipeCounter > 0; return state; } function loadingEnd(state, _ref4) { var pipe = _ref4.payload.pipe; state.loading[pipe + "Counter"] = 0; state.loading[pipe] = false; return state; } }); // hooks onStart$.subscribe(function () { var pipes = {}; Object.keys(_this._stream).forEach(function (model) { if (model === 'loading') return; pipes[model] = {}; Object.keys(_this._pipes[model]).forEach(function (pipe) { pipes[model][pipe] = false; pipes[model][pipe + "Counter"] = 0; }); }); _this.dispatch({ pipes: pipes, type: config.name + "/init" }); }); onPipeStart$.subscribe(function (_ref5) { var model = _ref5.model, pipe = _ref5.pipe; _this.dispatch({ model: model, pipe: pipe, type: config.name + "/pipeStart", loading: 1 }); _this.dispatch({ type: model + "/loadingStart", payload: { pipe: pipe } }); }); onPipeEnd$.subscribe(function (_ref6) { var model = _ref6.model, pipe = _ref6.pipe; _this.dispatch({ model: model, pipe: pipe, type: config.name + "/pipeStop", loading: 0, isEnd: true }); _this.dispatch({ type: model + "/loadingEnd", payload: { pipe: pipe } }); }); onPipeError$.subscribe(function (_ref7) { var model = _ref7.model, pipe = _ref7.pipe; _this.dispatch({ model: model, pipe: pipe, type: config.name + "/pipeStop", loading: 0, isError: true }); _this.dispatch({ type: model + "/loadingEnd", payload: { pipe: pipe } }); }); onPipeCancel$.subscribe(function (_ref8) { var model = _ref8.model, pipe = _ref8.pipe; _this.dispatch({ model: model, pipe: pipe, type: config.name + "/pipeStop", loading: 0, isCancel: true }); _this.dispatch({ type: model + "/loadingEnd", payload: { pipe: pipe } }); }); }; } export default loading;