@kubric/redux-knob
Version:
154 lines (130 loc) • 3.76 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var litedash = require('@kubric/litedash');
const BATCH_TYPE = "BATCHED_ACTION";
const ENABLE_ACTION_QUEUE = "ENABLE_ACTION_QUEUE";
const FLUSH_ACTION_QUEUE = "FLUSH_ACTION_QUEUE";
const defaultOptions = {
controlByActions: true,
enableType: ENABLE_ACTION_QUEUE,
flushType: FLUSH_ACTION_QUEUE,
enabled: false,
filterTypes: [],
filter: "exclude",
batchType: BATCH_TYPE
};
class ActionQueue {
constructor(options = {}) {
const { enabled, size, batchType, filterTypes, filter, enableType, flushType, controlByActions, timeInterval } = {
...defaultOptions,
...options
};
this.queue = [];
this.enabled = enabled;
this.size = size;
this.batchType = batchType;
this.filterTypes = filterTypes;
this.filter = filter;
this.enableType = enableType;
this.flushType = flushType;
this.controlByActions = controlByActions;
this.timeInterval = timeInterval;
}
enable() {
this.enabled = true;
}
flush(store) {
this.enabled = false;
this.dispatch(store);
}
dispatch(store) {
if (this.queue && this.queue.length > 0) {
const payload = [...this.queue];
this.queue = [];
store.dispatch({
type: this.batchType,
payload
});
}
}
checkForSize(store) {
if (!litedash.isUndefined(this.size) && this.queue.length === this.size) {
this.dispatch(store);
}
}
getWare() {
return store => next => action => {
if (!litedash.isUndefined(this.timeInterval)) {
setInterval(() => {
this.dispatch(store);
}, this.timeInterval);
}
const { type } = action;
if (this.controlByActions && (type === this.enableType || type === this.flushType)) {
const actionMethod = this.enableType === type ? "enable" : "flush";
this[actionMethod](store);
return next(action);
}
const actionNeedsToBeIncluded =
(this.filter === "include" && this.filterTypes.includes(type)) || (this.filter === "exclude" && !this.filterTypes.includes(type));
const actionNeedsToBePushToQueue = this.enabled && actionNeedsToBeIncluded;
if (actionNeedsToBePushToQueue) {
this.queue.push(action);
this.checkForSize(store);
} else {
return next(action);
}
};
}
}
const defaultOptions$1 = {
types: [],
delay: 0
};
class ThrottleQueue {
constructor(options) {
const { types, delay } = { ...defaultOptions$1, ...options };
this.delay = delay;
this.types = types;
this.actionQueue = new ActionQueue({
filterTypes: this.types,
filter: 'include',
enabled: true,
controlByActions: false,
timeInterval: delay
});
}
enable() {
this.actionQueue.enable();
}
disable() {
this.actionQueue.flush();
}
getWare() {
return this.actionQueue.getWare();
}
}
const defaultOptions$2 = { batchType: BATCH_TYPE };
const composeReducers = (reducers = [], defaultState = {}) => (state = defaultState, action) =>
reducers.reduce((state, reducer) => reducer(state, action), state);
const compose = (functions = [], defaultState = {}) => functions.reduce((state, fn) => fn(state), defaultState);
const enableBatching = (reducer, options = {}) => (state, action) => {
const { batchType } = { ...defaultOptions$2, ...options };
if (action.type === batchType) {
let { payload = [] } = action;
if (!Array.isArray(payload)) {
payload = [payload];
}
return payload.reduce(reducer, state);
} else {
return reducer(state, action);
}
};
exports.ActionQueue = ActionQueue;
exports.BATCH_TYPE = BATCH_TYPE;
exports.ENABLE_ACTION_QUEUE = ENABLE_ACTION_QUEUE;
exports.FLUSH_ACTION_QUEUE = FLUSH_ACTION_QUEUE;
exports.ThrottleQueue = ThrottleQueue;
exports.compose = compose;
exports.composeReducers = composeReducers;
exports.enableBatching = enableBatching;