wunderbaum 0.0.1-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +39 -0
- package/dist/wunderbaum.css +5 -0
- package/dist/wunderbaum.d.ts +1348 -0
- package/dist/wunderbaum.esm.js +5266 -0
- package/dist/wunderbaum.esm.min.js +69 -0
- package/dist/wunderbaum.esm.min.js.map +1 -0
- package/dist/wunderbaum.umd.js +5276 -0
- package/dist/wunderbaum.umd.min.js +71 -0
- package/dist/wunderbaum.umd.min.js.map +1 -0
- package/package.json +123 -0
- package/src/common.ts +198 -0
- package/src/debounce.ts +365 -0
- package/src/deferred.ts +50 -0
- package/src/util.ts +656 -0
- package/src/wb_ext_dnd.ts +336 -0
- package/src/wb_ext_edit.ts +340 -0
- package/src/wb_ext_filter.ts +370 -0
- package/src/wb_ext_keynav.ts +210 -0
- package/src/wb_ext_logger.ts +54 -0
- package/src/wb_extension_base.ts +76 -0
- package/src/wb_node.ts +1780 -0
- package/src/wb_options.ts +117 -0
- package/src/wunderbaum.scss +509 -0
- package/src/wunderbaum.ts +1819 -0
package/src/common.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Wunderbaum - common
|
|
3
|
+
* Copyright (c) 2021-2022, Martin Wendt. Released under the MIT license.
|
|
4
|
+
* @VERSION, @DATE (https://github.com/mar10/wunderbaum)
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { escapeRegex } from "./util";
|
|
8
|
+
import { WunderbaumNode } from "./wb_node";
|
|
9
|
+
import { Wunderbaum } from "./wunderbaum";
|
|
10
|
+
|
|
11
|
+
// export type WunderbaumOptions = any;
|
|
12
|
+
export type MatcherType = (node: WunderbaumNode) => boolean;
|
|
13
|
+
export type BoolOptionResolver = (node: WunderbaumNode) => boolean;
|
|
14
|
+
|
|
15
|
+
export const DEFAULT_DEBUGLEVEL = 4; // Replaced by rollup script
|
|
16
|
+
export const ROW_HEIGHT = 22;
|
|
17
|
+
export const ICON_WIDTH = 20;
|
|
18
|
+
export const ROW_EXTRA_PAD = 7; // 2x $col-padding-x + 3px rounding errors
|
|
19
|
+
export const RENDER_MIN_PREFETCH = 5;
|
|
20
|
+
export const RENDER_MAX_PREFETCH = 5;
|
|
21
|
+
export const TEST_IMG = new RegExp(/\.|\//); // strings are considered image urls if they contain '.' or '/'
|
|
22
|
+
// export const RECURSIVE_REQUEST_ERROR = "$recursive_request";
|
|
23
|
+
// export const INVALID_REQUEST_TARGET_ERROR = "$request_target_invalid";
|
|
24
|
+
|
|
25
|
+
export type NodeAnyCallback = (node: WunderbaumNode) => any;
|
|
26
|
+
|
|
27
|
+
export type NodeVisitResponse = "skip" | boolean | void;
|
|
28
|
+
export type NodeVisitCallback = (node: WunderbaumNode) => NodeVisitResponse;
|
|
29
|
+
|
|
30
|
+
// type WithWildcards<T> = T & { [key: string]: unknown };
|
|
31
|
+
export type WbEventType = {
|
|
32
|
+
name: string;
|
|
33
|
+
event: Event;
|
|
34
|
+
tree: Wunderbaum;
|
|
35
|
+
// node?: WunderbaumNode;
|
|
36
|
+
[key: string]: unknown;
|
|
37
|
+
};
|
|
38
|
+
export type WbNodeEventType = WbEventType & {
|
|
39
|
+
node: WunderbaumNode;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type WbCallbackType = (e: WbEventType) => any;
|
|
43
|
+
export type WbNodeCallbackType = (e: WbNodeEventType) => any;
|
|
44
|
+
|
|
45
|
+
export type FilterModeType = null | "dim" | "hide";
|
|
46
|
+
export type ApplyCommandType =
|
|
47
|
+
| "moveUp"
|
|
48
|
+
| "moveDown"
|
|
49
|
+
| "indent"
|
|
50
|
+
| "outdent"
|
|
51
|
+
| "remove"
|
|
52
|
+
| "rename"
|
|
53
|
+
| "addChild"
|
|
54
|
+
| "addSibling"
|
|
55
|
+
| "cut"
|
|
56
|
+
| "copy"
|
|
57
|
+
| "paste"
|
|
58
|
+
| "down"
|
|
59
|
+
| "first"
|
|
60
|
+
| "last"
|
|
61
|
+
| "left"
|
|
62
|
+
| "pageDown"
|
|
63
|
+
| "pageUp"
|
|
64
|
+
| "parent"
|
|
65
|
+
| "right"
|
|
66
|
+
| "up";
|
|
67
|
+
export type NodeFilterResponse = "skip" | "branch" | boolean | void;
|
|
68
|
+
export type NodeFilterCallback = (node: WunderbaumNode) => NodeFilterResponse;
|
|
69
|
+
|
|
70
|
+
export type AddNodeType = "before" | "after" | "prependChild" | "appendChild";
|
|
71
|
+
|
|
72
|
+
export type DndModeType = "before" | "after" | "over";
|
|
73
|
+
|
|
74
|
+
export enum ChangeType {
|
|
75
|
+
any = "any",
|
|
76
|
+
row = "row",
|
|
77
|
+
structure = "structure",
|
|
78
|
+
status = "status",
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export enum NodeStatusType {
|
|
82
|
+
ok = "ok",
|
|
83
|
+
loading = "loading",
|
|
84
|
+
error = "error",
|
|
85
|
+
noData = "noData",
|
|
86
|
+
// paging = "paging",
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**Define the subregion of a node, where an event occurred. */
|
|
90
|
+
export enum TargetType {
|
|
91
|
+
unknown = "",
|
|
92
|
+
checkbox = "checkbox",
|
|
93
|
+
column = "column",
|
|
94
|
+
expander = "expander",
|
|
95
|
+
icon = "icon",
|
|
96
|
+
prefix = "prefix",
|
|
97
|
+
title = "title",
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export let iconMap = {
|
|
101
|
+
error: "bi bi-exclamation-triangle",
|
|
102
|
+
// loading: "bi bi-hourglass-split",
|
|
103
|
+
loading: "bi bi-arrow-repeat wb-spin",
|
|
104
|
+
// loading: '<div class="spinner-border spinner-border-sm" role="status"> <span class="visually-hidden">Loading...</span> </div>',
|
|
105
|
+
// noData: "bi bi-search",
|
|
106
|
+
noData: "bi bi-question-circle",
|
|
107
|
+
expanderExpanded: "bi bi-chevron-down",
|
|
108
|
+
// expanderExpanded: "bi bi-dash-square",
|
|
109
|
+
expanderCollapsed: "bi bi-chevron-right",
|
|
110
|
+
// expanderCollapsed: "bi bi-plus-square",
|
|
111
|
+
expanderLazy: "bi bi-chevron-right wb-helper-lazy-expander",
|
|
112
|
+
// expanderLazy: "bi bi-chevron-bar-right",
|
|
113
|
+
checkChecked: "bi bi-check-square",
|
|
114
|
+
checkUnchecked: "bi bi-square",
|
|
115
|
+
checkUnknown: "bi dash-square-dotted",
|
|
116
|
+
radioChecked: "bi bi-circle-fill",
|
|
117
|
+
radioUnchecked: "bi bi-circle",
|
|
118
|
+
radioUnknown: "bi bi-circle-dotted",
|
|
119
|
+
folder: "bi bi-folder2",
|
|
120
|
+
folderOpen: "bi bi-folder2-open",
|
|
121
|
+
doc: "bi bi-file-earmark",
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const KEY_NODATA = "__not_found__";
|
|
125
|
+
|
|
126
|
+
export enum NavigationModeOption {
|
|
127
|
+
startRow = "startRow", // Start with row mode, but allow cell-nav mode
|
|
128
|
+
cell = "cell", // Cell-nav mode only
|
|
129
|
+
startCell = "startCell", // Start in cell-nav mode, but allow row mode
|
|
130
|
+
row = "row", // Row mode only
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export enum NavigationMode {
|
|
134
|
+
row = "row",
|
|
135
|
+
cellNav = "cellNav",
|
|
136
|
+
cellEdit = "cellEdit",
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Define which keys are handled by embedded <input> control, and should
|
|
140
|
+
* *not* be passed to tree navigation handler in cell-edit mode: */
|
|
141
|
+
export const INPUT_KEYS = {
|
|
142
|
+
text: ["left", "right", "home", "end", "backspace"],
|
|
143
|
+
number: ["up", "down", "left", "right", "home", "end", "backspace"],
|
|
144
|
+
checkbox: [],
|
|
145
|
+
link: [],
|
|
146
|
+
radiobutton: ["up", "down"],
|
|
147
|
+
"select-one": ["up", "down"],
|
|
148
|
+
"select-multiple": ["up", "down"],
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/** Key codes that trigger grid navigation, even when inside an input element. */
|
|
152
|
+
export const NAVIGATE_IN_INPUT_KEYS: Set<string> = new Set([
|
|
153
|
+
"ArrowDown",
|
|
154
|
+
"ArrowUp",
|
|
155
|
+
"Enter",
|
|
156
|
+
"Escape",
|
|
157
|
+
]);
|
|
158
|
+
|
|
159
|
+
/** Map `KeyEvent.key` to navigation action. */
|
|
160
|
+
export const KEY_TO_ACTION_DICT: { [key: string]: string } = {
|
|
161
|
+
" ": "toggleSelect",
|
|
162
|
+
"+": "expand",
|
|
163
|
+
Add: "expand",
|
|
164
|
+
ArrowDown: "down",
|
|
165
|
+
ArrowLeft: "left",
|
|
166
|
+
ArrowRight: "right",
|
|
167
|
+
ArrowUp: "up",
|
|
168
|
+
Backspace: "parent",
|
|
169
|
+
"/": "collapseAll",
|
|
170
|
+
Divide: "collapseAll",
|
|
171
|
+
End: "lastCol",
|
|
172
|
+
Home: "firstCol",
|
|
173
|
+
"Control+End": "last",
|
|
174
|
+
"Control+Home": "first",
|
|
175
|
+
"*": "expandAll",
|
|
176
|
+
Multiply: "expandAll",
|
|
177
|
+
PageDown: "pageDown",
|
|
178
|
+
PageUp: "pageUp",
|
|
179
|
+
"-": "collapse",
|
|
180
|
+
Subtract: "collapse",
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/** */
|
|
184
|
+
export function makeNodeTitleMatcher(s: string): MatcherType {
|
|
185
|
+
s = escapeRegex(s.toLowerCase());
|
|
186
|
+
return function (node: WunderbaumNode) {
|
|
187
|
+
return node.title.toLowerCase().indexOf(s) >= 0;
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** */
|
|
192
|
+
export function makeNodeTitleStartMatcher(s: string): MatcherType {
|
|
193
|
+
s = escapeRegex(s);
|
|
194
|
+
const reMatch = new RegExp("^" + s, "i");
|
|
195
|
+
return function (node: WunderbaumNode) {
|
|
196
|
+
return reMatch.test(node.title);
|
|
197
|
+
};
|
|
198
|
+
}
|
package/src/debounce.ts
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* debounce & throttle, taken from https://github.com/lodash/lodash v4.17.21
|
|
3
|
+
* MIT License: https://raw.githubusercontent.com/lodash/lodash/4.17.21-npm/LICENSE
|
|
4
|
+
* Modified for TypeScript type annotations.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/* --- Custom defiitions for TypeScript --- */
|
|
8
|
+
type Procedure = (...args: any[]) => any;
|
|
9
|
+
|
|
10
|
+
type DebounceOptions = {
|
|
11
|
+
leading?: boolean;
|
|
12
|
+
maxWait?: number;
|
|
13
|
+
trailing?: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type ThrottleOptions = {
|
|
17
|
+
leading?: boolean;
|
|
18
|
+
trailing?: boolean;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export interface DebouncedFunction<F extends Procedure> {
|
|
22
|
+
(this: ThisParameterType<F>, ...args: Parameters<F>): ReturnType<F>;
|
|
23
|
+
cancel: () => void;
|
|
24
|
+
flush: () => any;
|
|
25
|
+
pending: () => boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/* --- */
|
|
29
|
+
/** Detect free variable `global` from Node.js. */
|
|
30
|
+
const freeGlobal =
|
|
31
|
+
typeof global === "object" &&
|
|
32
|
+
global !== null &&
|
|
33
|
+
global.Object === Object &&
|
|
34
|
+
global;
|
|
35
|
+
|
|
36
|
+
/** Detect free variable `globalThis` */
|
|
37
|
+
const freeGlobalThis =
|
|
38
|
+
typeof globalThis === "object" &&
|
|
39
|
+
globalThis !== null &&
|
|
40
|
+
globalThis.Object == Object &&
|
|
41
|
+
globalThis;
|
|
42
|
+
|
|
43
|
+
/** Detect free variable `self`. */
|
|
44
|
+
const freeSelf =
|
|
45
|
+
typeof self === "object" && self !== null && self.Object === Object && self;
|
|
46
|
+
|
|
47
|
+
/** Used as a reference to the global object. */
|
|
48
|
+
const root =
|
|
49
|
+
freeGlobalThis || freeGlobal || freeSelf || Function("return this")();
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Checks if `value` is the
|
|
53
|
+
* [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
|
|
54
|
+
* of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
|
|
55
|
+
*
|
|
56
|
+
* @since 0.1.0
|
|
57
|
+
* @category Lang
|
|
58
|
+
* @param {*} value The value to check.
|
|
59
|
+
* @returns {boolean} Returns `true` if `value` is an object, else `false`.
|
|
60
|
+
* @example
|
|
61
|
+
*
|
|
62
|
+
* isObject({})
|
|
63
|
+
* // => true
|
|
64
|
+
*
|
|
65
|
+
* isObject([1, 2, 3])
|
|
66
|
+
* // => true
|
|
67
|
+
*
|
|
68
|
+
* isObject(Function)
|
|
69
|
+
* // => true
|
|
70
|
+
*
|
|
71
|
+
* isObject(null)
|
|
72
|
+
* // => false
|
|
73
|
+
*/
|
|
74
|
+
function isObject(value: any) {
|
|
75
|
+
const type = typeof value;
|
|
76
|
+
return value != null && (type === "object" || type === "function");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Creates a debounced function that delays invoking `func` until after `wait`
|
|
81
|
+
* milliseconds have elapsed since the last time the debounced function was
|
|
82
|
+
* invoked, or until the next browser frame is drawn. The debounced function
|
|
83
|
+
* comes with a `cancel` method to cancel delayed `func` invocations and a
|
|
84
|
+
* `flush` method to immediately invoke them. Provide `options` to indicate
|
|
85
|
+
* whether `func` should be invoked on the leading and/or trailing edge of the
|
|
86
|
+
* `wait` timeout. The `func` is invoked with the last arguments provided to the
|
|
87
|
+
* debounced function. Subsequent calls to the debounced function return the
|
|
88
|
+
* result of the last `func` invocation.
|
|
89
|
+
*
|
|
90
|
+
* **Note:** If `leading` and `trailing` options are `true`, `func` is
|
|
91
|
+
* invoked on the trailing edge of the timeout only if the debounced function
|
|
92
|
+
* is invoked more than once during the `wait` timeout.
|
|
93
|
+
*
|
|
94
|
+
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
|
|
95
|
+
* until the next tick, similar to `setTimeout` with a timeout of `0`.
|
|
96
|
+
*
|
|
97
|
+
* If `wait` is omitted in an environment with `requestAnimationFrame`, `func`
|
|
98
|
+
* invocation will be deferred until the next frame is drawn (typically about
|
|
99
|
+
* 16ms).
|
|
100
|
+
*
|
|
101
|
+
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/)
|
|
102
|
+
* for details over the differences between `debounce` and `throttle`.
|
|
103
|
+
*
|
|
104
|
+
* @since 0.1.0
|
|
105
|
+
* @category Function
|
|
106
|
+
* @param {Function} func The function to debounce.
|
|
107
|
+
* @param {number} [wait=0]
|
|
108
|
+
* The number of milliseconds to delay; if omitted, `requestAnimationFrame` is
|
|
109
|
+
* used (if available).
|
|
110
|
+
* @param {Object} [options={}] The options object.
|
|
111
|
+
* @param {boolean} [options.leading=false]
|
|
112
|
+
* Specify invoking on the leading edge of the timeout.
|
|
113
|
+
* @param {number} [options.maxWait]
|
|
114
|
+
* The maximum time `func` is allowed to be delayed before it's invoked.
|
|
115
|
+
* @param {boolean} [options.trailing=true]
|
|
116
|
+
* Specify invoking on the trailing edge of the timeout.
|
|
117
|
+
* @returns {Function} Returns the new debounced function.
|
|
118
|
+
* @example
|
|
119
|
+
*
|
|
120
|
+
* // Avoid costly calculations while the window size is in flux.
|
|
121
|
+
* jQuery(window).on('resize', debounce(calculateLayout, 150))
|
|
122
|
+
*
|
|
123
|
+
* // Invoke `sendMail` when clicked, debouncing subsequent calls.
|
|
124
|
+
* jQuery(element).on('click', debounce(sendMail, 300, {
|
|
125
|
+
* 'leading': true,
|
|
126
|
+
* 'trailing': false
|
|
127
|
+
* }))
|
|
128
|
+
*
|
|
129
|
+
* // Ensure `batchLog` is invoked once after 1 second of debounced calls.
|
|
130
|
+
* const debounced = debounce(batchLog, 250, { 'maxWait': 1000 })
|
|
131
|
+
* const source = new EventSource('/stream')
|
|
132
|
+
* jQuery(source).on('message', debounced)
|
|
133
|
+
*
|
|
134
|
+
* // Cancel the trailing debounced invocation.
|
|
135
|
+
* jQuery(window).on('popstate', debounced.cancel)
|
|
136
|
+
*
|
|
137
|
+
* // Check for pending invocations.
|
|
138
|
+
* const status = debounced.pending() ? "Pending..." : "Ready"
|
|
139
|
+
*/
|
|
140
|
+
export function debounce<F extends Procedure>(
|
|
141
|
+
func: F,
|
|
142
|
+
wait = 0,
|
|
143
|
+
options: DebounceOptions = {}
|
|
144
|
+
): DebouncedFunction<F> {
|
|
145
|
+
let lastArgs: any,
|
|
146
|
+
lastThis: any,
|
|
147
|
+
maxWait: number | undefined,
|
|
148
|
+
result: any,
|
|
149
|
+
timerId: number | undefined,
|
|
150
|
+
lastCallTime: number | undefined;
|
|
151
|
+
|
|
152
|
+
let lastInvokeTime = 0;
|
|
153
|
+
let leading = false;
|
|
154
|
+
let maxing = false;
|
|
155
|
+
let trailing = true;
|
|
156
|
+
|
|
157
|
+
// Bypass `requestAnimationFrame` by explicitly setting `wait=0`.
|
|
158
|
+
const useRAF =
|
|
159
|
+
!wait && wait !== 0 && typeof root.requestAnimationFrame === "function";
|
|
160
|
+
|
|
161
|
+
if (typeof func !== "function") {
|
|
162
|
+
throw new TypeError("Expected a function");
|
|
163
|
+
}
|
|
164
|
+
wait = +wait || 0;
|
|
165
|
+
if (isObject(options)) {
|
|
166
|
+
leading = !!options.leading;
|
|
167
|
+
maxing = "maxWait" in options;
|
|
168
|
+
maxWait = maxing ? Math.max(+options.maxWait! || 0, wait) : maxWait;
|
|
169
|
+
trailing = "trailing" in options ? !!options.trailing : trailing;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function invokeFunc(time: number) {
|
|
173
|
+
const args = lastArgs;
|
|
174
|
+
const thisArg = lastThis;
|
|
175
|
+
|
|
176
|
+
lastArgs = lastThis = undefined;
|
|
177
|
+
lastInvokeTime = time;
|
|
178
|
+
result = func.apply(thisArg, args);
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function startTimer(pendingFunc: any, wait: number) {
|
|
183
|
+
if (useRAF) {
|
|
184
|
+
root.cancelAnimationFrame(timerId);
|
|
185
|
+
return root.requestAnimationFrame(pendingFunc);
|
|
186
|
+
}
|
|
187
|
+
return setTimeout(pendingFunc, wait);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function cancelTimer(id: number) {
|
|
191
|
+
if (useRAF) {
|
|
192
|
+
return root.cancelAnimationFrame(id);
|
|
193
|
+
}
|
|
194
|
+
clearTimeout(id);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function leadingEdge(time: number) {
|
|
198
|
+
// Reset any `maxWait` timer.
|
|
199
|
+
lastInvokeTime = time;
|
|
200
|
+
// Start the timer for the trailing edge.
|
|
201
|
+
timerId = startTimer(timerExpired, wait);
|
|
202
|
+
// Invoke the leading edge.
|
|
203
|
+
return leading ? invokeFunc(time) : result;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function remainingWait(time: number) {
|
|
207
|
+
const timeSinceLastCall = time - lastCallTime!;
|
|
208
|
+
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
209
|
+
const timeWaiting = wait - timeSinceLastCall;
|
|
210
|
+
|
|
211
|
+
return maxing
|
|
212
|
+
? Math.min(timeWaiting, maxWait! - timeSinceLastInvoke)
|
|
213
|
+
: timeWaiting;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function shouldInvoke(time: number) {
|
|
217
|
+
const timeSinceLastCall = time - lastCallTime!;
|
|
218
|
+
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
219
|
+
|
|
220
|
+
// Either this is the first call, activity has stopped and we're at the
|
|
221
|
+
// trailing edge, the system time has gone backwards and we're treating
|
|
222
|
+
// it as the trailing edge, or we've hit the `maxWait` limit.
|
|
223
|
+
return (
|
|
224
|
+
lastCallTime === undefined ||
|
|
225
|
+
timeSinceLastCall >= wait ||
|
|
226
|
+
timeSinceLastCall < 0 ||
|
|
227
|
+
(maxing && timeSinceLastInvoke >= maxWait!)
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function timerExpired() {
|
|
232
|
+
const time = Date.now();
|
|
233
|
+
if (shouldInvoke(time)) {
|
|
234
|
+
return trailingEdge(time);
|
|
235
|
+
}
|
|
236
|
+
// Restart the timer.
|
|
237
|
+
timerId = startTimer(timerExpired, remainingWait(time));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function trailingEdge(time: number) {
|
|
241
|
+
timerId = undefined;
|
|
242
|
+
|
|
243
|
+
// Only invoke if we have `lastArgs` which means `func` has been
|
|
244
|
+
// debounced at least once.
|
|
245
|
+
if (trailing && lastArgs) {
|
|
246
|
+
return invokeFunc(time);
|
|
247
|
+
}
|
|
248
|
+
lastArgs = lastThis = undefined;
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function cancel() {
|
|
253
|
+
if (timerId !== undefined) {
|
|
254
|
+
cancelTimer(timerId);
|
|
255
|
+
}
|
|
256
|
+
lastInvokeTime = 0;
|
|
257
|
+
lastArgs = lastCallTime = lastThis = timerId = undefined;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function flush() {
|
|
261
|
+
return timerId === undefined ? result : trailingEdge(Date.now());
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function pending() {
|
|
265
|
+
return timerId !== undefined;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function debounced(this: any, ...args: any[]) {
|
|
269
|
+
const time = Date.now();
|
|
270
|
+
const isInvoking = shouldInvoke(time);
|
|
271
|
+
|
|
272
|
+
lastArgs = args;
|
|
273
|
+
lastThis = this;
|
|
274
|
+
lastCallTime = time;
|
|
275
|
+
|
|
276
|
+
if (isInvoking) {
|
|
277
|
+
if (timerId === undefined) {
|
|
278
|
+
return leadingEdge(lastCallTime);
|
|
279
|
+
}
|
|
280
|
+
if (maxing) {
|
|
281
|
+
// Handle invocations in a tight loop.
|
|
282
|
+
timerId = startTimer(timerExpired, wait);
|
|
283
|
+
return invokeFunc(lastCallTime);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (timerId === undefined) {
|
|
287
|
+
timerId = startTimer(timerExpired, wait);
|
|
288
|
+
}
|
|
289
|
+
return result;
|
|
290
|
+
}
|
|
291
|
+
debounced.cancel = cancel;
|
|
292
|
+
debounced.flush = flush;
|
|
293
|
+
debounced.pending = pending;
|
|
294
|
+
return debounced;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Creates a throttled function that only invokes `func` at most once per
|
|
299
|
+
* every `wait` milliseconds (or once per browser frame). The throttled function
|
|
300
|
+
* comes with a `cancel` method to cancel delayed `func` invocations and a
|
|
301
|
+
* `flush` method to immediately invoke them. Provide `options` to indicate
|
|
302
|
+
* whether `func` should be invoked on the leading and/or trailing edge of the
|
|
303
|
+
* `wait` timeout. The `func` is invoked with the last arguments provided to the
|
|
304
|
+
* throttled function. Subsequent calls to the throttled function return the
|
|
305
|
+
* result of the last `func` invocation.
|
|
306
|
+
*
|
|
307
|
+
* **Note:** If `leading` and `trailing` options are `true`, `func` is
|
|
308
|
+
* invoked on the trailing edge of the timeout only if the throttled function
|
|
309
|
+
* is invoked more than once during the `wait` timeout.
|
|
310
|
+
*
|
|
311
|
+
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
|
|
312
|
+
* until the next tick, similar to `setTimeout` with a timeout of `0`.
|
|
313
|
+
*
|
|
314
|
+
* If `wait` is omitted in an environment with `requestAnimationFrame`, `func`
|
|
315
|
+
* invocation will be deferred until the next frame is drawn (typically about
|
|
316
|
+
* 16ms).
|
|
317
|
+
*
|
|
318
|
+
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/)
|
|
319
|
+
* for details over the differences between `throttle` and `debounce`.
|
|
320
|
+
*
|
|
321
|
+
* @since 0.1.0
|
|
322
|
+
* @category Function
|
|
323
|
+
* @param {Function} func The function to throttle.
|
|
324
|
+
* @param {number} [wait=0]
|
|
325
|
+
* The number of milliseconds to throttle invocations to; if omitted,
|
|
326
|
+
* `requestAnimationFrame` is used (if available).
|
|
327
|
+
* @param {Object} [options={}] The options object.
|
|
328
|
+
* @param {boolean} [options.leading=true]
|
|
329
|
+
* Specify invoking on the leading edge of the timeout.
|
|
330
|
+
* @param {boolean} [options.trailing=true]
|
|
331
|
+
* Specify invoking on the trailing edge of the timeout.
|
|
332
|
+
* @returns {Function} Returns the new throttled function.
|
|
333
|
+
* @example
|
|
334
|
+
*
|
|
335
|
+
* // Avoid excessively updating the position while scrolling.
|
|
336
|
+
* jQuery(window).on('scroll', throttle(updatePosition, 100))
|
|
337
|
+
*
|
|
338
|
+
* // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes.
|
|
339
|
+
* const throttled = throttle(renewToken, 300000, { 'trailing': false })
|
|
340
|
+
* jQuery(element).on('click', throttled)
|
|
341
|
+
*
|
|
342
|
+
* // Cancel the trailing throttled invocation.
|
|
343
|
+
* jQuery(window).on('popstate', throttled.cancel)
|
|
344
|
+
*/
|
|
345
|
+
export function throttle<F extends Procedure>(
|
|
346
|
+
func: F,
|
|
347
|
+
wait = 0,
|
|
348
|
+
options: ThrottleOptions = {}
|
|
349
|
+
) {
|
|
350
|
+
let leading = true;
|
|
351
|
+
let trailing = true;
|
|
352
|
+
|
|
353
|
+
if (typeof func !== "function") {
|
|
354
|
+
throw new TypeError("Expected a function");
|
|
355
|
+
}
|
|
356
|
+
if (isObject(options)) {
|
|
357
|
+
leading = "leading" in options ? !!options.leading : leading;
|
|
358
|
+
trailing = "trailing" in options ? !!options.trailing : trailing;
|
|
359
|
+
}
|
|
360
|
+
return debounce(func, wait, {
|
|
361
|
+
leading,
|
|
362
|
+
trailing,
|
|
363
|
+
maxWait: wait,
|
|
364
|
+
});
|
|
365
|
+
}
|
package/src/deferred.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Wunderbaum - deferred
|
|
3
|
+
* Copyright (c) 2021-2022, Martin Wendt. Released under the MIT license.
|
|
4
|
+
* @VERSION, @DATE (https://github.com/mar10/wunderbaum)
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
type PromiseCallbackType = (val: any) => void;
|
|
8
|
+
type finallyCallbackType = () => void;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Deferred is a ES6 Promise, that exposes the resolve() and reject()` method.
|
|
12
|
+
*
|
|
13
|
+
* Loosely mimics [`jQuery.Deferred`](https://api.jquery.com/category/deferred-object/).
|
|
14
|
+
*/
|
|
15
|
+
export class Deferred {
|
|
16
|
+
private _promise: Promise<any>;
|
|
17
|
+
protected _resolve: any;
|
|
18
|
+
protected _reject: any;
|
|
19
|
+
|
|
20
|
+
constructor() {
|
|
21
|
+
this._promise = new Promise((resolve, reject) => {
|
|
22
|
+
this._resolve = resolve;
|
|
23
|
+
this._reject = reject;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** Resolve the [[Promise]]. */
|
|
27
|
+
resolve(value?: any) {
|
|
28
|
+
this._resolve(value);
|
|
29
|
+
}
|
|
30
|
+
/** Reject the [[Promise]]. */
|
|
31
|
+
reject(reason?: any) {
|
|
32
|
+
this._reject(reason);
|
|
33
|
+
}
|
|
34
|
+
/** Return the native [[Promise]] instance.*/
|
|
35
|
+
promise() {
|
|
36
|
+
return this._promise;
|
|
37
|
+
}
|
|
38
|
+
/** Call [[Promise.then]] on the embedded promise instance.*/
|
|
39
|
+
then(cb: PromiseCallbackType) {
|
|
40
|
+
return this._promise.then(cb);
|
|
41
|
+
}
|
|
42
|
+
/** Call [[Promise.catch]] on the embedded promise instance.*/
|
|
43
|
+
catch(cb: PromiseCallbackType) {
|
|
44
|
+
return this._promise.catch(cb);
|
|
45
|
+
}
|
|
46
|
+
/** Call [[Promise.finally]] on the embedded promise instance.*/
|
|
47
|
+
finally(cb: finallyCallbackType) {
|
|
48
|
+
return this._promise.finally(cb);
|
|
49
|
+
}
|
|
50
|
+
}
|