@etsoo/shared 1.1.11 → 1.1.12

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.
@@ -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) => {
@@ -9,6 +9,28 @@ export declare namespace ExtendUtils {
9
9
  * @param baseCtors Mixin base classes
10
10
  */
11
11
  function applyMixins(derivedCtor: any, baseCtors: any[]): void;
12
+ /**
13
+ * Create delayed executor
14
+ * @param func Function
15
+ * @param delayMiliseconds Delay miliseconds
16
+ * @returns Result
17
+ */
18
+ function delayedExecutor<P extends any[]>(func: (...args: P) => void, delayMiliseconds: number): {
19
+ /**
20
+ * Call the function
21
+ * @param args Args
22
+ */
23
+ call(...args: P): void;
24
+ /**
25
+ * Clear
26
+ */
27
+ clear(): void;
28
+ /**
29
+ * Is running or not
30
+ * @returns Result
31
+ */
32
+ isRunning(): boolean;
33
+ };
12
34
  /**
13
35
  * Promise handler to catch error
14
36
  * @param promise Promise
@@ -23,6 +23,45 @@ 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 args Args
38
+ */
39
+ call(...args) {
40
+ this.clear();
41
+ seed = window.setTimeout((...args) => {
42
+ func(...args);
43
+ seed = 0;
44
+ }, delayMiliseconds, ...args);
45
+ },
46
+ /**
47
+ * Clear
48
+ */
49
+ clear() {
50
+ if (this.isRunning()) {
51
+ window.clearTimeout(seed);
52
+ seed = 0;
53
+ }
54
+ },
55
+ /**
56
+ * Is running or not
57
+ * @returns Result
58
+ */
59
+ isRunning() {
60
+ return seed > 0;
61
+ }
62
+ };
63
+ }
64
+ ExtendUtils.delayedExecutor = delayedExecutor;
26
65
  /**
27
66
  * Promise handler to catch error
28
67
  * @param promise Promise
@@ -9,6 +9,28 @@ export declare namespace ExtendUtils {
9
9
  * @param baseCtors Mixin base classes
10
10
  */
11
11
  function applyMixins(derivedCtor: any, baseCtors: any[]): void;
12
+ /**
13
+ * Create delayed executor
14
+ * @param func Function
15
+ * @param delayMiliseconds Delay miliseconds
16
+ * @returns Result
17
+ */
18
+ function delayedExecutor<P extends any[]>(func: (...args: P) => void, delayMiliseconds: number): {
19
+ /**
20
+ * Call the function
21
+ * @param args Args
22
+ */
23
+ call(...args: P): void;
24
+ /**
25
+ * Clear
26
+ */
27
+ clear(): void;
28
+ /**
29
+ * Is running or not
30
+ * @returns Result
31
+ */
32
+ isRunning(): boolean;
33
+ };
12
34
  /**
13
35
  * Promise handler to catch error
14
36
  * @param promise Promise
@@ -20,6 +20,45 @@ 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 args Args
35
+ */
36
+ call(...args) {
37
+ this.clear();
38
+ seed = window.setTimeout((...args) => {
39
+ func(...args);
40
+ seed = 0;
41
+ }, delayMiliseconds, ...args);
42
+ },
43
+ /**
44
+ * Clear
45
+ */
46
+ clear() {
47
+ if (this.isRunning()) {
48
+ window.clearTimeout(seed);
49
+ seed = 0;
50
+ }
51
+ },
52
+ /**
53
+ * Is running or not
54
+ * @returns Result
55
+ */
56
+ isRunning() {
57
+ return seed > 0;
58
+ }
59
+ };
60
+ }
61
+ ExtendUtils.delayedExecutor = delayedExecutor;
23
62
  /**
24
63
  * Promise handler to catch error
25
64
  * @param promise Promise
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.11",
3
+ "version": "1.1.12",
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
  }
@@ -19,6 +19,54 @@ export namespace ExtendUtils {
19
19
  });
20
20
  }
21
21
 
22
+ /**
23
+ * Create delayed executor
24
+ * @param func Function
25
+ * @param delayMiliseconds Delay miliseconds
26
+ * @returns Result
27
+ */
28
+ export function delayedExecutor<P extends any[]>(
29
+ func: (...args: P) => void,
30
+ delayMiliseconds: number
31
+ ) {
32
+ let seed: number = 0;
33
+ return {
34
+ /**
35
+ * Call the function
36
+ * @param args Args
37
+ */
38
+ call(...args: P) {
39
+ this.clear();
40
+ seed = window.setTimeout(
41
+ (...args: P) => {
42
+ func(...args);
43
+ seed = 0;
44
+ },
45
+ delayMiliseconds,
46
+ ...args
47
+ );
48
+ },
49
+
50
+ /**
51
+ * Clear
52
+ */
53
+ clear() {
54
+ if (this.isRunning()) {
55
+ window.clearTimeout(seed);
56
+ seed = 0;
57
+ }
58
+ },
59
+
60
+ /**
61
+ * Is running or not
62
+ * @returns Result
63
+ */
64
+ isRunning() {
65
+ return seed > 0;
66
+ }
67
+ };
68
+ }
69
+
22
70
  /**
23
71
  * Promise handler to catch error
24
72
  * @param promise Promise