@companion-module/base 1.4.2 → 1.4.3-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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@companion-module/base",
3
- "version": "1.4.2",
3
+ "version": "v1.4.3-0",
4
4
  "type": "commonjs",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -24,13 +24,14 @@
24
24
  "CHANGELOG.md",
25
25
  "dist",
26
26
  "generated",
27
- "assets"
27
+ "assets",
28
+ "vendor"
28
29
  ],
29
30
  "dependencies": {
30
31
  "@sentry/node": "^7.63.0",
31
32
  "@sentry/tracing": "^7.63.0",
32
33
  "ajv": "^8.12.0",
33
- "debounce-fn": "github:julusian/debounce-fn#4.0.0-maxWaithack.0",
34
+ "debounce-fn": "link:./vendor/debounce-fn",
34
35
  "ejson": "^2.2.3",
35
36
  "eventemitter3": "^4.0.7",
36
37
  "nanoid": "^3.3.4",
@@ -0,0 +1,83 @@
1
+ declare namespace debounceFn {
2
+ interface Options {
3
+ /**
4
+ Time to wait until the `input` function is called.
5
+
6
+ @default 0
7
+ */
8
+ readonly wait?: number;
9
+
10
+ /**
11
+ Maximum time to wait until the `input` function is called.
12
+ Only applies when after is true.
13
+ Disabled when 0
14
+
15
+ @default 0
16
+ */
17
+ readonly maxWait?: number;
18
+
19
+ /**
20
+ Trigger the function on the leading edge of the `wait` interval.
21
+
22
+ For example, this can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
23
+
24
+ @default false
25
+ */
26
+ readonly before?: boolean;
27
+
28
+ /**
29
+ Trigger the function on the trailing edge of the `wait` interval.
30
+
31
+ @default true
32
+ */
33
+ readonly after?: boolean;
34
+ }
35
+
36
+ interface BeforeOptions extends Options {
37
+ readonly before: true;
38
+ }
39
+
40
+ interface NoBeforeNoAfterOptions extends Options {
41
+ readonly after: false;
42
+ readonly before?: false;
43
+ }
44
+
45
+ interface DebouncedFunction<ArgumentsType extends unknown[], ReturnType> {
46
+ (...arguments: ArgumentsType): ReturnType;
47
+ cancel(): void;
48
+ }
49
+ }
50
+
51
+ /**
52
+ [Debounce](https://davidwalsh.name/javascript-debounce-function) a function.
53
+
54
+ @param input - Function to debounce.
55
+ @returns A debounced function that delays calling the `input` function until after `wait` milliseconds have elapsed since the last time the debounced function was called.
56
+
57
+ It comes with a `.cancel()` method to cancel any scheduled `input` function calls.
58
+
59
+ @example
60
+ ```
61
+ import debounceFn = require('debounce-fn');
62
+
63
+ window.onresize = debounceFn(() => {
64
+ // Do something on window resize
65
+ }, {wait: 100});
66
+ ```
67
+ */
68
+ declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
69
+ input: (...arguments: ArgumentsType) => ReturnType,
70
+ options: debounceFn.BeforeOptions
71
+ ): debounceFn.DebouncedFunction<ArgumentsType, ReturnType>;
72
+
73
+ declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
74
+ input: (...arguments: ArgumentsType) => ReturnType,
75
+ options: debounceFn.NoBeforeNoAfterOptions
76
+ ): debounceFn.DebouncedFunction<ArgumentsType, undefined>;
77
+
78
+ declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
79
+ input: (...arguments: ArgumentsType) => ReturnType,
80
+ options?: debounceFn.Options
81
+ ): debounceFn.DebouncedFunction<ArgumentsType, ReturnType | undefined>;
82
+
83
+ export = debounceFn;
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+ const mimicFn = require('mimic-fn');
3
+
4
+ module.exports = (inputFunction, options = {}) => {
5
+ if (typeof inputFunction !== 'function') {
6
+ throw new TypeError(`Expected the first argument to be a function, got \`${typeof inputFunction}\``);
7
+ }
8
+
9
+ const {
10
+ wait = 0,
11
+ maxWait = 0,
12
+ before = false,
13
+ after = true
14
+ } = options;
15
+
16
+ if (!before && !after) {
17
+ throw new Error('Both `before` and `after` are false, function wouldn\'t be called.');
18
+ }
19
+
20
+ let timeout;
21
+ let maxTimeout;
22
+ let result;
23
+
24
+ const debouncedFunction = function (...arguments_) {
25
+ const context = this;
26
+
27
+ const later = () => {
28
+ timeout = undefined;
29
+
30
+ if (maxTimeout) {
31
+ clearTimeout(maxTimeout);
32
+ maxTimeout = undefined;
33
+ }
34
+
35
+ if (after) {
36
+ result = inputFunction.apply(context, arguments_);
37
+ }
38
+ };
39
+
40
+ const maxLater = () => {
41
+ maxTimeout = undefined;
42
+
43
+ if (timeout) {
44
+ clearTimeout(timeout);
45
+ timeout = undefined;
46
+ }
47
+
48
+ result = inputFunction.apply(context, arguments_);
49
+ };
50
+
51
+ const shouldCallNow = before && !timeout;
52
+ clearTimeout(timeout);
53
+ timeout = setTimeout(later, wait);
54
+
55
+ if (maxWait > 0 && !maxTimeout && after) {
56
+ maxTimeout = setTimeout(maxLater, maxWait);
57
+ }
58
+
59
+ if (shouldCallNow) {
60
+ result = inputFunction.apply(context, arguments_);
61
+ }
62
+
63
+ return result;
64
+ };
65
+
66
+ mimicFn(debouncedFunction, inputFunction);
67
+
68
+ debouncedFunction.cancel = () => {
69
+ if (timeout) {
70
+ clearTimeout(timeout);
71
+ timeout = undefined;
72
+ }
73
+
74
+ if (maxTimeout) {
75
+ clearTimeout(maxTimeout);
76
+ maxTimeout = undefined;
77
+ }
78
+ };
79
+
80
+ return debouncedFunction;
81
+ };
@@ -0,0 +1,24 @@
1
+ import {expectType, expectError} from 'tsd';
2
+ import debounceFn = require('.');
3
+
4
+ const stringToBoolean = (string: string) => true;
5
+
6
+ const options: debounceFn.Options = {};
7
+ const debounced = debounceFn(stringToBoolean);
8
+ expectType<debounceFn.DebouncedFunction<[string], boolean | undefined>>(debounced);
9
+ expectType<boolean | undefined>(debounced('foo'));
10
+ debounced.cancel();
11
+
12
+ expectType<boolean | undefined>(debounceFn(stringToBoolean)('foo'));
13
+ expectError<boolean>(debounceFn(stringToBoolean)('foo'));
14
+
15
+ expectType<boolean | undefined>(debounceFn(stringToBoolean, {wait: 20})('foo'));
16
+ expectError<boolean>(debounceFn(stringToBoolean, {wait: 20})('foo'));
17
+ expectType<boolean | undefined>(debounceFn(stringToBoolean, {after: true})('foo'));
18
+ expectError<boolean>(debounceFn(stringToBoolean, {after: true})('foo'));
19
+
20
+ expectType<boolean>(debounceFn(stringToBoolean, {before: true})('foo'));
21
+ expectType<boolean>(debounceFn(stringToBoolean, {before: true, after: true})('foo'));
22
+
23
+ expectType<undefined>(debounceFn(stringToBoolean, {after: false})('foo'));
24
+ expectError<boolean>(debounceFn(stringToBoolean, {after: false})('foo'));
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "debounce-fn",
3
+ "version": "4.0.0-maxWaithack.0",
4
+ "description": "Debounce a function",
5
+ "license": "MIT",
6
+ "repository": "sindresorhus/debounce-fn",
7
+ "funding": "https://github.com/sponsors/sindresorhus",
8
+ "author": {
9
+ "name": "Sindre Sorhus",
10
+ "email": "sindresorhus@gmail.com",
11
+ "url": "https://sindresorhus.com"
12
+ },
13
+ "engines": {
14
+ "node": ">=10"
15
+ },
16
+ "scripts": {
17
+ "test": "xo && ava && tsd"
18
+ },
19
+ "files": [
20
+ "index.js",
21
+ "index.d.ts"
22
+ ],
23
+ "keywords": [
24
+ "debounce",
25
+ "function",
26
+ "debouncer",
27
+ "fn",
28
+ "func",
29
+ "throttle",
30
+ "delay",
31
+ "invoked"
32
+ ],
33
+ "dependencies": {
34
+ "mimic-fn": "^3.0.0"
35
+ },
36
+ "devDependencies": {
37
+ "ava": "^1.4.1",
38
+ "delay": "^4.2.0",
39
+ "tsd": "^0.11.0",
40
+ "xo": "^0.26.1"
41
+ }
42
+ }
@@ -0,0 +1,64 @@
1
+ # debounce-fn
2
+
3
+ > [Debounce](https://davidwalsh.name/javascript-debounce-function) a function
4
+
5
+ ## Install
6
+
7
+ ```
8
+ $ npm install debounce-fn
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ const debounceFn = require('debounce-fn');
15
+
16
+ window.onresize = debounceFn(() => {
17
+ // Do something on window resize
18
+ }, {wait: 100});
19
+ ```
20
+
21
+ ## API
22
+
23
+ ### debounceFn(input, options?)
24
+
25
+ Returns a debounced function that delays calling the `input` function until after `wait` milliseconds have elapsed since the last time the debounced function was called.
26
+
27
+ It comes with a `.cancel()` method to cancel any scheduled `input` function calls.
28
+
29
+ #### input
30
+
31
+ Type: `Function`
32
+
33
+ Function to debounce.
34
+
35
+ #### options
36
+
37
+ Type: `object`
38
+
39
+ ##### wait
40
+
41
+ Type: `number`\
42
+ Default: `0`
43
+
44
+ Time to wait until the `input` function is called.
45
+
46
+ ##### before
47
+
48
+ Type: `boolean`\
49
+ Default: `false`
50
+
51
+ Trigger the function on the leading edge of the `wait` interval.
52
+
53
+ For example, can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
54
+
55
+ ##### after
56
+
57
+ Type: `boolean`\
58
+ Default: `true`
59
+
60
+ Trigger the function on the trailing edge of the `wait` interval.
61
+
62
+ ## Related
63
+
64
+ - [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
@@ -0,0 +1,169 @@
1
+ import test from 'ava';
2
+ import delay from 'delay';
3
+ import debounceFn from '.';
4
+
5
+ test('debounces a function', async t => {
6
+ let count = 0;
7
+
8
+ const debounced = debounceFn(value => {
9
+ count++;
10
+ return value;
11
+ }, {
12
+ wait: 20
13
+ });
14
+
15
+ t.is(debounced(1), undefined);
16
+ t.is(debounced(2), undefined);
17
+ t.is(debounced(3), undefined);
18
+
19
+ await delay(100);
20
+ t.is(debounced(4), 3);
21
+ t.is(debounced(5), 3);
22
+ t.is(debounced(6), 3);
23
+ t.is(count, 1);
24
+
25
+ await delay(200);
26
+ t.is(count, 2);
27
+ });
28
+
29
+ test('before:false after:false options', t => {
30
+ t.throws(() => {
31
+ debounceFn(() => null, {
32
+ wait: 20,
33
+ before: false,
34
+ after: false
35
+ });
36
+ }, {
37
+ message: 'Both `before` and `after` are false, function wouldn\'t be called.'
38
+ });
39
+ });
40
+
41
+ test('before:true after:false options', async t => {
42
+ let count = 0;
43
+
44
+ const debounced = debounceFn(value => {
45
+ count++;
46
+ return value;
47
+ }, {
48
+ wait: 20,
49
+ before: true,
50
+ after: false
51
+ });
52
+
53
+ t.is(debounced(1), 1);
54
+ t.is(debounced(2), 1);
55
+ t.is(debounced(3), 1);
56
+ t.is(count, 1);
57
+
58
+ await delay(100);
59
+ t.is(debounced(4), 4);
60
+ t.is(debounced(5), 4);
61
+ t.is(debounced(6), 4);
62
+ t.is(count, 2);
63
+
64
+ await delay(200);
65
+ t.is(count, 2);
66
+ });
67
+
68
+ test('before:false after:true options', async t => {
69
+ let count = 0;
70
+
71
+ const debounced = debounceFn(value => {
72
+ count++;
73
+ return value;
74
+ }, {
75
+ wait: 20,
76
+ before: false,
77
+ after: true
78
+ });
79
+
80
+ t.is(debounced(1), undefined);
81
+ t.is(debounced(2), undefined);
82
+ t.is(debounced(3), undefined);
83
+ t.is(count, 0);
84
+
85
+ await delay(100);
86
+ t.is(debounced(4), 3);
87
+ t.is(debounced(5), 3);
88
+ t.is(debounced(6), 3);
89
+ t.is(count, 1);
90
+
91
+ await delay(200);
92
+ t.is(count, 2);
93
+ });
94
+
95
+ test('before:true after:true options', async t => {
96
+ let count = 0;
97
+
98
+ const debounced = debounceFn(value => {
99
+ count++;
100
+ return value;
101
+ }, {
102
+ wait: 20,
103
+ before: true,
104
+ after: true
105
+ });
106
+
107
+ t.is(debounced(1), 1);
108
+ t.is(debounced(2), 1);
109
+ t.is(debounced(3), 1);
110
+ t.is(count, 1);
111
+
112
+ await delay(100);
113
+ t.is(count, 2);
114
+ t.is(debounced(4), 4);
115
+ t.is(debounced(5), 4);
116
+ t.is(debounced(6), 4);
117
+ t.is(count, 3);
118
+
119
+ await delay(200);
120
+ t.is(count, 4);
121
+ });
122
+
123
+ test('.cancel() method', async t => {
124
+ let count = 0;
125
+
126
+ const debounced = debounceFn(value => {
127
+ count++;
128
+ return value;
129
+ }, {
130
+ wait: 20
131
+ });
132
+
133
+ t.is(debounced(1), undefined);
134
+ t.is(debounced(2), undefined);
135
+ t.is(debounced(3), undefined);
136
+
137
+ debounced.cancel();
138
+
139
+ await delay(100);
140
+ t.is(debounced(1), undefined);
141
+ t.is(debounced(2), undefined);
142
+ t.is(debounced(3), undefined);
143
+ t.is(count, 0);
144
+ });
145
+
146
+ test('debounces a function with maxWait', async t => {
147
+ let count = 0;
148
+
149
+ const debounced = debounceFn(value => {
150
+ count++;
151
+ return value;
152
+ }, {
153
+ wait: 40,
154
+ maxWait: 50
155
+ });
156
+
157
+ t.is(debounced(1), undefined);
158
+ await delay(30);
159
+ t.is(count, 0);
160
+
161
+ t.is(debounced(2), undefined);
162
+ await delay(30);
163
+ t.is(count, 1);
164
+
165
+ t.is(debounced(3), 1);
166
+
167
+ await delay(200);
168
+ t.is(count, 2);
169
+ });