tiny-essentials 1.16.0 → 1.16.2

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,180 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * @typedef {(() => void)} FnData - Function with no arguments and no return value
5
+ */
6
+
7
+ /**
8
+ * A function that handles a scroll event.
9
+ * It receives a standard `Event` object when a scroll occurs.
10
+ *
11
+ * @typedef {(ev: Event) => void} OnScrollFunc
12
+ */
13
+
14
+ /**
15
+ * A scroll tracker that queues functions to be executed
16
+ * after the user stops scrolling a specific element or the window.
17
+ */
18
+ class TinyAfterScrollWatcher {
19
+ /** @type {Element|Window} */
20
+ #scrollTarget;
21
+
22
+ /** @type {null|NodeJS.Timeout} */
23
+ #lastScrollTime = null;
24
+
25
+ /** @type {FnData[]} */
26
+ #afterScrollQueue = [];
27
+
28
+ /** @type {number} */
29
+ #inactivityTime = 100;
30
+
31
+ /** @type {Set<OnScrollFunc>} */
32
+ #externalScrollListeners = new Set();
33
+
34
+ /** @type {Set<FnData>} */
35
+ #onStopListeners = new Set();
36
+
37
+ /** @type {boolean} */
38
+ #destroyed = false;
39
+
40
+ /**
41
+ * @param {Element|Window} scrollTarget - The element or window to track scrolling on
42
+ * @param {number} [inactivityTime=100] - Time in milliseconds to wait after scroll ends before executing the queue
43
+ * @throws {TypeError} If scrollTarget is not a valid Element or Window
44
+ * @throws {TypeError} If inactivityTime is not a positive number
45
+ */
46
+ constructor(scrollTarget = window, inactivityTime = 100) {
47
+ if (!(scrollTarget instanceof Element) && !(scrollTarget instanceof Window))
48
+ throw new TypeError('scrollTarget must be an Element or the Window object.');
49
+ this.#scrollTarget = scrollTarget;
50
+ this._checkTimer = this._checkTimer.bind(this);
51
+
52
+ this.#scrollTarget.addEventListener('scroll', this._checkTimer);
53
+ this.#inactivityTime = inactivityTime;
54
+ }
55
+
56
+ _checkTimer = () => {
57
+ if (this.#lastScrollTime) clearTimeout(this.#lastScrollTime);
58
+ this.#lastScrollTime = setTimeout(() => {
59
+ this.#lastScrollTime = null;
60
+ this.#checkQueue();
61
+ }, this.#inactivityTime);
62
+ };
63
+
64
+ /**
65
+ * Gets the current inactivity time in milliseconds.
66
+ * @returns {number}
67
+ */
68
+ get inactivityTime() {
69
+ return this.#inactivityTime;
70
+ }
71
+
72
+ /**
73
+ * Sets a new inactivity time.
74
+ * Must be a positive number (in milliseconds).
75
+ * @param {number} value
76
+ * @throws {Error} If value is not a positive number
77
+ */
78
+ set inactivityTime(value) {
79
+ if (typeof value !== 'number' || value <= 0 || !Number.isFinite(value))
80
+ throw new Error('inactivityTime must be a positive number in milliseconds.');
81
+ this.#inactivityTime = value;
82
+ }
83
+
84
+ /**
85
+ * Continuously checks whether the user has stopped scrolling,
86
+ * and if so, runs all queued functions.
87
+ */
88
+ #checkQueue() {
89
+ if (this.#destroyed) return;
90
+ // Runs all onStop first listeners
91
+ for (const fn of this.#onStopListeners) {
92
+ if (typeof fn === 'function') fn();
93
+ }
94
+
95
+ // Then execute the queue afterScrollQueue
96
+ while (this.#afterScrollQueue.length) {
97
+ const fn = this.#afterScrollQueue.pop();
98
+ if (typeof fn === 'function') fn();
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Adds a function to be executed after scroll has stopped.
104
+ * The scroll is considered "stopped" after the configured inactivity time.
105
+ *
106
+ * @param {() => void} fn - A function to execute once scrolling has stopped.
107
+ * @throws {TypeError} If the argument is not a function.
108
+ */
109
+ doAfterScroll(fn) {
110
+ if (typeof fn !== 'function') throw new TypeError('Argument must be a function.');
111
+ this.lastScrollTime = Date.now();
112
+ this.#afterScrollQueue.push(fn);
113
+ }
114
+
115
+ /**
116
+ * Registers a function to run once after scrolling has stopped,
117
+ * before any afterScrollQueue functions.
118
+ *
119
+ * @param {FnData} fn - A function to execute after scroll stop.
120
+ * @throws {TypeError} If the argument is not a function.
121
+ */
122
+ onStop(fn) {
123
+ if (typeof fn !== 'function') throw new TypeError('Argument must be a function.');
124
+ this.#onStopListeners.add(fn);
125
+ }
126
+
127
+ /**
128
+ * Removes a previously registered onStop function.
129
+ *
130
+ * @param {FnData} fn - The function to remove.
131
+ * @throws {TypeError} If the argument is not a function.
132
+ */
133
+ offStop(fn) {
134
+ if (typeof fn !== 'function') throw new TypeError('Argument must be a function.');
135
+ this.#onStopListeners.delete(fn);
136
+ }
137
+
138
+ /**
139
+ * Registers an external scroll listener on the tracked element.
140
+ *
141
+ * @param {OnScrollFunc} fn - The scroll listener to add
142
+ * @throws {TypeError} If the argument is not a function.
143
+ */
144
+ onScroll(fn) {
145
+ if (typeof fn !== 'function') throw new TypeError('Argument must be a function.');
146
+ this.#scrollTarget.addEventListener('scroll', fn);
147
+ this.#externalScrollListeners.add(fn);
148
+ }
149
+
150
+ /**
151
+ * Removes a previously registered scroll listener from the tracked element.
152
+ *
153
+ * @param {OnScrollFunc} fn - The scroll listener to remove
154
+ * @throws {TypeError} If the argument is not a function.
155
+ */
156
+ offScroll(fn) {
157
+ if (typeof fn !== 'function') throw new TypeError('Argument must be a function.');
158
+ if (this.#externalScrollListeners.has(fn)) {
159
+ this.#scrollTarget.removeEventListener('scroll', fn);
160
+ this.#externalScrollListeners.delete(fn);
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Destroys the watcher by removing internal listeners and clearing data.
166
+ */
167
+ destroy() {
168
+ if (this.#destroyed) return;
169
+ this.#destroyed = true;
170
+
171
+ this.#scrollTarget.removeEventListener('scroll', this._checkTimer);
172
+ for (const fn of this.#externalScrollListeners)
173
+ this.#scrollTarget.removeEventListener('scroll', fn);
174
+
175
+ this.#externalScrollListeners.clear();
176
+ this.#onStopListeners.clear();
177
+ }
178
+ }
179
+
180
+ module.exports = TinyAfterScrollWatcher;
@@ -0,0 +1,89 @@
1
+ export default TinyAfterScrollWatcher;
2
+ /**
3
+ * - Function with no arguments and no return value
4
+ */
5
+ export type FnData = (() => void);
6
+ /**
7
+ * A function that handles a scroll event.
8
+ * It receives a standard `Event` object when a scroll occurs.
9
+ */
10
+ export type OnScrollFunc = (ev: Event) => void;
11
+ /**
12
+ * @typedef {(() => void)} FnData - Function with no arguments and no return value
13
+ */
14
+ /**
15
+ * A function that handles a scroll event.
16
+ * It receives a standard `Event` object when a scroll occurs.
17
+ *
18
+ * @typedef {(ev: Event) => void} OnScrollFunc
19
+ */
20
+ /**
21
+ * A scroll tracker that queues functions to be executed
22
+ * after the user stops scrolling a specific element or the window.
23
+ */
24
+ declare class TinyAfterScrollWatcher {
25
+ /**
26
+ * @param {Element|Window} scrollTarget - The element or window to track scrolling on
27
+ * @param {number} [inactivityTime=100] - Time in milliseconds to wait after scroll ends before executing the queue
28
+ * @throws {TypeError} If scrollTarget is not a valid Element or Window
29
+ * @throws {TypeError} If inactivityTime is not a positive number
30
+ */
31
+ constructor(scrollTarget?: Element | Window, inactivityTime?: number);
32
+ _checkTimer: () => void;
33
+ /**
34
+ * Sets a new inactivity time.
35
+ * Must be a positive number (in milliseconds).
36
+ * @param {number} value
37
+ * @throws {Error} If value is not a positive number
38
+ */
39
+ set inactivityTime(value: number);
40
+ /**
41
+ * Gets the current inactivity time in milliseconds.
42
+ * @returns {number}
43
+ */
44
+ get inactivityTime(): number;
45
+ /**
46
+ * Adds a function to be executed after scroll has stopped.
47
+ * The scroll is considered "stopped" after the configured inactivity time.
48
+ *
49
+ * @param {() => void} fn - A function to execute once scrolling has stopped.
50
+ * @throws {TypeError} If the argument is not a function.
51
+ */
52
+ doAfterScroll(fn: () => void): void;
53
+ lastScrollTime: number | undefined;
54
+ /**
55
+ * Registers a function to run once after scrolling has stopped,
56
+ * before any afterScrollQueue functions.
57
+ *
58
+ * @param {FnData} fn - A function to execute after scroll stop.
59
+ * @throws {TypeError} If the argument is not a function.
60
+ */
61
+ onStop(fn: FnData): void;
62
+ /**
63
+ * Removes a previously registered onStop function.
64
+ *
65
+ * @param {FnData} fn - The function to remove.
66
+ * @throws {TypeError} If the argument is not a function.
67
+ */
68
+ offStop(fn: FnData): void;
69
+ /**
70
+ * Registers an external scroll listener on the tracked element.
71
+ *
72
+ * @param {OnScrollFunc} fn - The scroll listener to add
73
+ * @throws {TypeError} If the argument is not a function.
74
+ */
75
+ onScroll(fn: OnScrollFunc): void;
76
+ /**
77
+ * Removes a previously registered scroll listener from the tracked element.
78
+ *
79
+ * @param {OnScrollFunc} fn - The scroll listener to remove
80
+ * @throws {TypeError} If the argument is not a function.
81
+ */
82
+ offScroll(fn: OnScrollFunc): void;
83
+ /**
84
+ * Destroys the watcher by removing internal listeners and clearing data.
85
+ */
86
+ destroy(): void;
87
+ #private;
88
+ }
89
+ //# sourceMappingURL=TinyAfterScrollWatcher.d.mts.map
@@ -0,0 +1,164 @@
1
+ /**
2
+ * @typedef {(() => void)} FnData - Function with no arguments and no return value
3
+ */
4
+ /**
5
+ * A function that handles a scroll event.
6
+ * It receives a standard `Event` object when a scroll occurs.
7
+ *
8
+ * @typedef {(ev: Event) => void} OnScrollFunc
9
+ */
10
+ /**
11
+ * A scroll tracker that queues functions to be executed
12
+ * after the user stops scrolling a specific element or the window.
13
+ */
14
+ class TinyAfterScrollWatcher {
15
+ /** @type {Element|Window} */
16
+ #scrollTarget;
17
+ /** @type {null|NodeJS.Timeout} */
18
+ #lastScrollTime = null;
19
+ /** @type {FnData[]} */
20
+ #afterScrollQueue = [];
21
+ /** @type {number} */
22
+ #inactivityTime = 100;
23
+ /** @type {Set<OnScrollFunc>} */
24
+ #externalScrollListeners = new Set();
25
+ /** @type {Set<FnData>} */
26
+ #onStopListeners = new Set();
27
+ /** @type {boolean} */
28
+ #destroyed = false;
29
+ /**
30
+ * @param {Element|Window} scrollTarget - The element or window to track scrolling on
31
+ * @param {number} [inactivityTime=100] - Time in milliseconds to wait after scroll ends before executing the queue
32
+ * @throws {TypeError} If scrollTarget is not a valid Element or Window
33
+ * @throws {TypeError} If inactivityTime is not a positive number
34
+ */
35
+ constructor(scrollTarget = window, inactivityTime = 100) {
36
+ if (!(scrollTarget instanceof Element) && !(scrollTarget instanceof Window))
37
+ throw new TypeError('scrollTarget must be an Element or the Window object.');
38
+ this.#scrollTarget = scrollTarget;
39
+ this._checkTimer = this._checkTimer.bind(this);
40
+ this.#scrollTarget.addEventListener('scroll', this._checkTimer);
41
+ this.#inactivityTime = inactivityTime;
42
+ }
43
+ _checkTimer = () => {
44
+ if (this.#lastScrollTime)
45
+ clearTimeout(this.#lastScrollTime);
46
+ this.#lastScrollTime = setTimeout(() => {
47
+ this.#lastScrollTime = null;
48
+ this.#checkQueue();
49
+ }, this.#inactivityTime);
50
+ };
51
+ /**
52
+ * Gets the current inactivity time in milliseconds.
53
+ * @returns {number}
54
+ */
55
+ get inactivityTime() {
56
+ return this.#inactivityTime;
57
+ }
58
+ /**
59
+ * Sets a new inactivity time.
60
+ * Must be a positive number (in milliseconds).
61
+ * @param {number} value
62
+ * @throws {Error} If value is not a positive number
63
+ */
64
+ set inactivityTime(value) {
65
+ if (typeof value !== 'number' || value <= 0 || !Number.isFinite(value))
66
+ throw new Error('inactivityTime must be a positive number in milliseconds.');
67
+ this.#inactivityTime = value;
68
+ }
69
+ /**
70
+ * Continuously checks whether the user has stopped scrolling,
71
+ * and if so, runs all queued functions.
72
+ */
73
+ #checkQueue() {
74
+ if (this.#destroyed)
75
+ return;
76
+ // Runs all onStop first listeners
77
+ for (const fn of this.#onStopListeners) {
78
+ if (typeof fn === 'function')
79
+ fn();
80
+ }
81
+ // Then execute the queue afterScrollQueue
82
+ while (this.#afterScrollQueue.length) {
83
+ const fn = this.#afterScrollQueue.pop();
84
+ if (typeof fn === 'function')
85
+ fn();
86
+ }
87
+ }
88
+ /**
89
+ * Adds a function to be executed after scroll has stopped.
90
+ * The scroll is considered "stopped" after the configured inactivity time.
91
+ *
92
+ * @param {() => void} fn - A function to execute once scrolling has stopped.
93
+ * @throws {TypeError} If the argument is not a function.
94
+ */
95
+ doAfterScroll(fn) {
96
+ if (typeof fn !== 'function')
97
+ throw new TypeError('Argument must be a function.');
98
+ this.lastScrollTime = Date.now();
99
+ this.#afterScrollQueue.push(fn);
100
+ }
101
+ /**
102
+ * Registers a function to run once after scrolling has stopped,
103
+ * before any afterScrollQueue functions.
104
+ *
105
+ * @param {FnData} fn - A function to execute after scroll stop.
106
+ * @throws {TypeError} If the argument is not a function.
107
+ */
108
+ onStop(fn) {
109
+ if (typeof fn !== 'function')
110
+ throw new TypeError('Argument must be a function.');
111
+ this.#onStopListeners.add(fn);
112
+ }
113
+ /**
114
+ * Removes a previously registered onStop function.
115
+ *
116
+ * @param {FnData} fn - The function to remove.
117
+ * @throws {TypeError} If the argument is not a function.
118
+ */
119
+ offStop(fn) {
120
+ if (typeof fn !== 'function')
121
+ throw new TypeError('Argument must be a function.');
122
+ this.#onStopListeners.delete(fn);
123
+ }
124
+ /**
125
+ * Registers an external scroll listener on the tracked element.
126
+ *
127
+ * @param {OnScrollFunc} fn - The scroll listener to add
128
+ * @throws {TypeError} If the argument is not a function.
129
+ */
130
+ onScroll(fn) {
131
+ if (typeof fn !== 'function')
132
+ throw new TypeError('Argument must be a function.');
133
+ this.#scrollTarget.addEventListener('scroll', fn);
134
+ this.#externalScrollListeners.add(fn);
135
+ }
136
+ /**
137
+ * Removes a previously registered scroll listener from the tracked element.
138
+ *
139
+ * @param {OnScrollFunc} fn - The scroll listener to remove
140
+ * @throws {TypeError} If the argument is not a function.
141
+ */
142
+ offScroll(fn) {
143
+ if (typeof fn !== 'function')
144
+ throw new TypeError('Argument must be a function.');
145
+ if (this.#externalScrollListeners.has(fn)) {
146
+ this.#scrollTarget.removeEventListener('scroll', fn);
147
+ this.#externalScrollListeners.delete(fn);
148
+ }
149
+ }
150
+ /**
151
+ * Destroys the watcher by removing internal listeners and clearing data.
152
+ */
153
+ destroy() {
154
+ if (this.#destroyed)
155
+ return;
156
+ this.#destroyed = true;
157
+ this.#scrollTarget.removeEventListener('scroll', this._checkTimer);
158
+ for (const fn of this.#externalScrollListeners)
159
+ this.#scrollTarget.removeEventListener('scroll', fn);
160
+ this.#externalScrollListeners.clear();
161
+ this.#onStopListeners.clear();
162
+ }
163
+ }
164
+ export default TinyAfterScrollWatcher;