@augment-vir/common 22.0.1 → 22.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.
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDebounce = exports.DebounceStyle = void 0;
4
+ var DebounceStyle;
5
+ (function (DebounceStyle) {
6
+ /**
7
+ * Fires on the first call, then waits the given amount of milliseconds until a subsequent call
8
+ * can be made.
9
+ */
10
+ DebounceStyle["FirstThenWait"] = "first-then-wait";
11
+ /**
12
+ * Waits the given amount of milliseconds after the first call and then fires the latest
13
+ * assigned callback.
14
+ */
15
+ DebounceStyle["AfterWait"] = "after-wait";
16
+ })(DebounceStyle || (exports.DebounceStyle = DebounceStyle = {}));
17
+ function createDebounce(debounceStyle, debounceDuration) {
18
+ let nextCallTimestamp = 0;
19
+ let latestCallback = undefined;
20
+ return (callback) => {
21
+ latestCallback = callback;
22
+ const now = Date.now();
23
+ if (nextCallTimestamp > now) {
24
+ return;
25
+ }
26
+ if (debounceStyle === DebounceStyle.FirstThenWait) {
27
+ latestCallback();
28
+ }
29
+ else if (debounceStyle === DebounceStyle.AfterWait) {
30
+ setTimeout(() => {
31
+ /** Use whatever the latest latestCallback is. */
32
+ latestCallback?.();
33
+ }, debounceDuration.milliseconds);
34
+ }
35
+ nextCallTimestamp = now + debounceDuration.milliseconds;
36
+ };
37
+ }
38
+ exports.createDebounce = createDebounce;
package/dist/cjs/index.js CHANGED
@@ -20,6 +20,7 @@ __exportStar(require("./augments/async"), exports);
20
20
  __exportStar(require("./augments/boolean"), exports);
21
21
  __exportStar(require("./augments/common-number"), exports);
22
22
  __exportStar(require("./augments/common-string"), exports);
23
+ __exportStar(require("./augments/debounce"), exports);
23
24
  __exportStar(require("./augments/environment"), exports);
24
25
  __exportStar(require("./augments/error"), exports);
25
26
  __exportStar(require("./augments/function"), exports);
@@ -0,0 +1,34 @@
1
+ export var DebounceStyle;
2
+ (function (DebounceStyle) {
3
+ /**
4
+ * Fires on the first call, then waits the given amount of milliseconds until a subsequent call
5
+ * can be made.
6
+ */
7
+ DebounceStyle["FirstThenWait"] = "first-then-wait";
8
+ /**
9
+ * Waits the given amount of milliseconds after the first call and then fires the latest
10
+ * assigned callback.
11
+ */
12
+ DebounceStyle["AfterWait"] = "after-wait";
13
+ })(DebounceStyle || (DebounceStyle = {}));
14
+ export function createDebounce(debounceStyle, debounceDuration) {
15
+ let nextCallTimestamp = 0;
16
+ let latestCallback = undefined;
17
+ return (callback) => {
18
+ latestCallback = callback;
19
+ const now = Date.now();
20
+ if (nextCallTimestamp > now) {
21
+ return;
22
+ }
23
+ if (debounceStyle === DebounceStyle.FirstThenWait) {
24
+ latestCallback();
25
+ }
26
+ else if (debounceStyle === DebounceStyle.AfterWait) {
27
+ setTimeout(() => {
28
+ /** Use whatever the latest latestCallback is. */
29
+ latestCallback?.();
30
+ }, debounceDuration.milliseconds);
31
+ }
32
+ nextCallTimestamp = now + debounceDuration.milliseconds;
33
+ };
34
+ }
package/dist/esm/index.js CHANGED
@@ -4,6 +4,7 @@ export * from './augments/async';
4
4
  export * from './augments/boolean';
5
5
  export * from './augments/common-number';
6
6
  export * from './augments/common-string';
7
+ export * from './augments/debounce';
7
8
  export * from './augments/environment';
8
9
  export * from './augments/error';
9
10
  export * from './augments/function';
@@ -0,0 +1,15 @@
1
+ export declare enum DebounceStyle {
2
+ /**
3
+ * Fires on the first call, then waits the given amount of milliseconds until a subsequent call
4
+ * can be made.
5
+ */
6
+ FirstThenWait = "first-then-wait",
7
+ /**
8
+ * Waits the given amount of milliseconds after the first call and then fires the latest
9
+ * assigned callback.
10
+ */
11
+ AfterWait = "after-wait"
12
+ }
13
+ export declare function createDebounce(debounceStyle: DebounceStyle, debounceDuration: {
14
+ milliseconds: number;
15
+ }): (callback: () => void) => void;
@@ -4,6 +4,7 @@ export * from './augments/async';
4
4
  export * from './augments/boolean';
5
5
  export * from './augments/common-number';
6
6
  export * from './augments/common-string';
7
+ export * from './augments/debounce';
7
8
  export * from './augments/environment';
8
9
  export * from './augments/error';
9
10
  export * from './augments/function';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "22.0.1",
3
+ "version": "22.1.0",
4
4
  "homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
5
5
  "bugs": {
6
6
  "url": "https://github.com/electrovir/augment-vir/issues"