@etsoo/shared 1.1.11 → 1.1.14

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/README.md CHANGED
@@ -122,6 +122,7 @@ Extend current class/object functioning
122
122
  |Name|Description|
123
123
  |---:|---|
124
124
  |applyMixins|Apply mixins to current class|
125
+ |delayedExecutor|Create delayed executor|
125
126
  |promiseHandler|Promise handler to catch error|
126
127
  |sleep|Delay promise|
127
128
 
@@ -23,6 +23,26 @@ test('Tests for applyMixins', () => {
23
23
  expect(item.m2()).toBe('hello');
24
24
  });
25
25
 
26
+ test('Tests for delayedExecutor', () => {
27
+ // Arrange
28
+ const f = jest.fn();
29
+
30
+ jest.useFakeTimers();
31
+ jest.spyOn(global, 'setTimeout');
32
+
33
+ const e = ExtendUtils.delayedExecutor(f, 50);
34
+
35
+ e.call(1, false, 'a');
36
+ expect(e.isRunning()).toBeTruthy();
37
+
38
+ e.call(2, true, 'b');
39
+ expect(setTimeout).toHaveBeenCalledTimes(2);
40
+
41
+ jest.runOnlyPendingTimers();
42
+ expect(f).toBeCalledTimes(1);
43
+ expect(e.isRunning()).toBeFalsy();
44
+ });
45
+
26
46
  test('Tests for promiseHandler', async () => {
27
47
  // Arrange
28
48
  const p = (id: number) => {
@@ -1,3 +1,4 @@
1
+ import { DelayedExecutorType } from './types/DelayedExecutorType';
1
2
  /**
2
3
  * Extend utilities
3
4
  */
@@ -9,6 +10,13 @@ export declare namespace ExtendUtils {
9
10
  * @param baseCtors Mixin base classes
10
11
  */
11
12
  function applyMixins(derivedCtor: any, baseCtors: any[]): void;
13
+ /**
14
+ * Create delayed executor
15
+ * @param func Function
16
+ * @param delayMiliseconds Delay miliseconds
17
+ * @returns Result
18
+ */
19
+ function delayedExecutor<P extends any[]>(func: (...args: P) => void, delayMiliseconds: number): DelayedExecutorType<P>;
12
20
  /**
13
21
  * Promise handler to catch error
14
22
  * @param promise Promise
@@ -23,6 +23,46 @@ var ExtendUtils;
23
23
  });
24
24
  }
25
25
  ExtendUtils.applyMixins = applyMixins;
26
+ /**
27
+ * Create delayed executor
28
+ * @param func Function
29
+ * @param delayMiliseconds Delay miliseconds
30
+ * @returns Result
31
+ */
32
+ function delayedExecutor(func, delayMiliseconds) {
33
+ let seed = 0;
34
+ return {
35
+ /**
36
+ * Call the function
37
+ * @param miliseconds Delayed miliseconds for this call
38
+ * @param args Args
39
+ */
40
+ call(miliseconds, ...args) {
41
+ this.clear();
42
+ seed = window.setTimeout((...args) => {
43
+ func(...args);
44
+ seed = 0;
45
+ }, miliseconds !== null && miliseconds !== void 0 ? miliseconds : delayMiliseconds, ...args);
46
+ },
47
+ /**
48
+ * Clear
49
+ */
50
+ clear() {
51
+ if (this.isRunning()) {
52
+ window.clearTimeout(seed);
53
+ seed = 0;
54
+ }
55
+ },
56
+ /**
57
+ * Is running or not
58
+ * @returns Result
59
+ */
60
+ isRunning() {
61
+ return seed > 0;
62
+ }
63
+ };
64
+ }
65
+ ExtendUtils.delayedExecutor = delayedExecutor;
26
66
  /**
27
67
  * Promise handler to catch error
28
68
  * @param promise Promise
@@ -1,4 +1,5 @@
1
1
  export * from './types/FormData';
2
+ export * from './types/DelayedExecutorType';
2
3
  export * from './storage/IStorage';
3
4
  export * from './storage/WindowStorage';
4
5
  export * from './DataTypes';
package/lib/cjs/index.js CHANGED
@@ -11,6 +11,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./types/FormData"), exports);
14
+ __exportStar(require("./types/DelayedExecutorType"), exports);
14
15
  __exportStar(require("./storage/IStorage"), exports);
15
16
  __exportStar(require("./storage/WindowStorage"), exports);
16
17
  __exportStar(require("./DataTypes"), exports);
@@ -0,0 +1,17 @@
1
+ export declare type DelayedExecutorType<P extends any[]> = {
2
+ /**
3
+ * Call the function
4
+ * @param miliseconds Delayed miliseconds for this call
5
+ * @param args Args
6
+ */
7
+ call(miliseconds?: number, ...args: P): void;
8
+ /**
9
+ * Clear
10
+ */
11
+ clear(): void;
12
+ /**
13
+ * Is running or not
14
+ * @returns Result
15
+ */
16
+ isRunning(): boolean;
17
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,4 @@
1
+ import { DelayedExecutorType } from './types/DelayedExecutorType';
1
2
  /**
2
3
  * Extend utilities
3
4
  */
@@ -9,6 +10,13 @@ export declare namespace ExtendUtils {
9
10
  * @param baseCtors Mixin base classes
10
11
  */
11
12
  function applyMixins(derivedCtor: any, baseCtors: any[]): void;
13
+ /**
14
+ * Create delayed executor
15
+ * @param func Function
16
+ * @param delayMiliseconds Delay miliseconds
17
+ * @returns Result
18
+ */
19
+ function delayedExecutor<P extends any[]>(func: (...args: P) => void, delayMiliseconds: number): DelayedExecutorType<P>;
12
20
  /**
13
21
  * Promise handler to catch error
14
22
  * @param promise Promise
@@ -20,6 +20,46 @@ export var ExtendUtils;
20
20
  });
21
21
  }
22
22
  ExtendUtils.applyMixins = applyMixins;
23
+ /**
24
+ * Create delayed executor
25
+ * @param func Function
26
+ * @param delayMiliseconds Delay miliseconds
27
+ * @returns Result
28
+ */
29
+ function delayedExecutor(func, delayMiliseconds) {
30
+ let seed = 0;
31
+ return {
32
+ /**
33
+ * Call the function
34
+ * @param miliseconds Delayed miliseconds for this call
35
+ * @param args Args
36
+ */
37
+ call(miliseconds, ...args) {
38
+ this.clear();
39
+ seed = window.setTimeout((...args) => {
40
+ func(...args);
41
+ seed = 0;
42
+ }, miliseconds !== null && miliseconds !== void 0 ? miliseconds : delayMiliseconds, ...args);
43
+ },
44
+ /**
45
+ * Clear
46
+ */
47
+ clear() {
48
+ if (this.isRunning()) {
49
+ window.clearTimeout(seed);
50
+ seed = 0;
51
+ }
52
+ },
53
+ /**
54
+ * Is running or not
55
+ * @returns Result
56
+ */
57
+ isRunning() {
58
+ return seed > 0;
59
+ }
60
+ };
61
+ }
62
+ ExtendUtils.delayedExecutor = delayedExecutor;
23
63
  /**
24
64
  * Promise handler to catch error
25
65
  * @param promise Promise
@@ -1,4 +1,5 @@
1
1
  export * from './types/FormData';
2
+ export * from './types/DelayedExecutorType';
2
3
  export * from './storage/IStorage';
3
4
  export * from './storage/WindowStorage';
4
5
  export * from './DataTypes';
package/lib/mjs/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './types/FormData';
2
+ export * from './types/DelayedExecutorType';
2
3
  export * from './storage/IStorage';
3
4
  export * from './storage/WindowStorage';
4
5
  export * from './DataTypes';
@@ -0,0 +1,17 @@
1
+ export declare type DelayedExecutorType<P extends any[]> = {
2
+ /**
3
+ * Call the function
4
+ * @param miliseconds Delayed miliseconds for this call
5
+ * @param args Args
6
+ */
7
+ call(miliseconds?: number, ...args: P): void;
8
+ /**
9
+ * Clear
10
+ */
11
+ clear(): void;
12
+ /**
13
+ * Is running or not
14
+ * @returns Result
15
+ */
16
+ isRunning(): boolean;
17
+ };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.11",
3
+ "version": "1.1.14",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -54,13 +54,13 @@
54
54
  "homepage": "https://github.com/ETSOO/Shared#readme",
55
55
  "dependencies": {},
56
56
  "devDependencies": {
57
- "@types/jest": "^27.4.0",
58
- "@typescript-eslint/eslint-plugin": "^5.10.1",
59
- "@typescript-eslint/parser": "^5.10.1",
60
- "eslint": "^8.8.0",
57
+ "@types/jest": "^27.4.1",
58
+ "@typescript-eslint/eslint-plugin": "^5.12.1",
59
+ "@typescript-eslint/parser": "^5.12.1",
60
+ "eslint": "^8.10.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.25.4",
63
- "jest": "^27.4.7",
63
+ "jest": "^27.5.1",
64
64
  "ts-jest": "^27.1.3",
65
65
  "typescript": "^4.5.5"
66
66
  }
@@ -1,3 +1,5 @@
1
+ import { DelayedExecutorType } from './types/DelayedExecutorType';
2
+
1
3
  /**
2
4
  * Extend utilities
3
5
  */
@@ -19,6 +21,55 @@ export namespace ExtendUtils {
19
21
  });
20
22
  }
21
23
 
24
+ /**
25
+ * Create delayed executor
26
+ * @param func Function
27
+ * @param delayMiliseconds Delay miliseconds
28
+ * @returns Result
29
+ */
30
+ export function delayedExecutor<P extends any[]>(
31
+ func: (...args: P) => void,
32
+ delayMiliseconds: number
33
+ ): DelayedExecutorType<P> {
34
+ let seed: number = 0;
35
+ return {
36
+ /**
37
+ * Call the function
38
+ * @param miliseconds Delayed miliseconds for this call
39
+ * @param args Args
40
+ */
41
+ call(miliseconds?: number, ...args: P) {
42
+ this.clear();
43
+ seed = window.setTimeout(
44
+ (...args: P) => {
45
+ func(...args);
46
+ seed = 0;
47
+ },
48
+ miliseconds ?? delayMiliseconds,
49
+ ...args
50
+ );
51
+ },
52
+
53
+ /**
54
+ * Clear
55
+ */
56
+ clear() {
57
+ if (this.isRunning()) {
58
+ window.clearTimeout(seed);
59
+ seed = 0;
60
+ }
61
+ },
62
+
63
+ /**
64
+ * Is running or not
65
+ * @returns Result
66
+ */
67
+ isRunning() {
68
+ return seed > 0;
69
+ }
70
+ };
71
+ }
72
+
22
73
  /**
23
74
  * Promise handler to catch error
24
75
  * @param promise Promise
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './types/FormData';
2
+ export * from './types/DelayedExecutorType';
2
3
 
3
4
  export * from './storage/IStorage';
4
5
  export * from './storage/WindowStorage';
@@ -0,0 +1,19 @@
1
+ export type DelayedExecutorType<P extends any[]> = {
2
+ /**
3
+ * Call the function
4
+ * @param miliseconds Delayed miliseconds for this call
5
+ * @param args Args
6
+ */
7
+ call(miliseconds?: number, ...args: P): void;
8
+
9
+ /**
10
+ * Clear
11
+ */
12
+ clear(): void;
13
+
14
+ /**
15
+ * Is running or not
16
+ * @returns Result
17
+ */
18
+ isRunning(): boolean;
19
+ };