nexustate
Version:
State management with listeners
64 lines (56 loc) • 1.9 kB
JavaScript
import { assurePathExists, set } from 'objer';
export function findIndex(array, callback, context) {
for (let keydex = 0; keydex < array.length; keydex += 1) {
if (callback(array[keydex])) return keydex;
}
return -1;
}
// ** COPIED FROM UNDERSCORE JS **
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time. Normally, the throttled function will run
// as much as it can, without ever going more than once per `wait` duration;
// but if you'd like to disable the execution on the leading edge, pass
// `{leading: false}`. To disable execution on the trailing edge, ditto.
export function throttle(func, wait, options) {
var timeout, context, args, result;
var previous = 0;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : new Date().getTime();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
var throttled = function() {
var now = new Date().getTime();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
throttled.cancel = function() {
clearTimeout(timeout);
previous = 0;
timeout = context = args = null;
};
return throttled;
}
export function getKeyFilledObject(key, value) {
if (key === null) return value;
const result = {};
assurePathExists(result, key);
set(result, key, value);
return result;
}