tiny-essentials 1.16.0 → 1.16.1

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,86 @@
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
+ /** @type {number} */
33
+ lastScrollTime: number;
34
+ /**
35
+ * Internal handler for scroll events.
36
+ * Updates the last scroll timestamp.
37
+ * @private
38
+ */
39
+ private _onScroll;
40
+ /**
41
+ * Continuously checks whether the user has stopped scrolling,
42
+ * and if so, runs all queued functions.
43
+ * @private
44
+ */
45
+ private _checkQueue;
46
+ /**
47
+ * Sets a new inactivity time.
48
+ * Must be a positive number (in milliseconds).
49
+ * @param {number} value
50
+ * @throws {Error} If value is not a positive number
51
+ */
52
+ set inactivityTime(value: number);
53
+ /**
54
+ * Gets the current inactivity time in milliseconds.
55
+ * @returns {number}
56
+ */
57
+ get inactivityTime(): number;
58
+ /**
59
+ * Adds a function to be executed after scroll has stopped.
60
+ * The scroll is considered "stopped" after the configured inactivity time.
61
+ *
62
+ * @param {() => void} fn - A function to execute once scrolling has stopped.
63
+ * @throws {TypeError} If the argument is not a function.
64
+ */
65
+ doAfterScroll(fn: () => void): void;
66
+ /**
67
+ * Registers an external scroll listener on the tracked element.
68
+ *
69
+ * @param {OnScrollFunc} fn - The scroll listener to add
70
+ * @throws {TypeError} If the argument is not a function.
71
+ */
72
+ onScroll(fn: OnScrollFunc): void;
73
+ /**
74
+ * Removes a previously registered scroll listener from the tracked element.
75
+ *
76
+ * @param {OnScrollFunc} fn - The scroll listener to remove
77
+ * @throws {TypeError} If the argument is not a function.
78
+ */
79
+ offScroll(fn: OnScrollFunc): void;
80
+ /**
81
+ * Destroys the watcher by removing internal listeners and clearing data.
82
+ */
83
+ destroy(): void;
84
+ #private;
85
+ }
86
+ //# sourceMappingURL=TinyAfterScrollWatcher.d.mts.map
@@ -0,0 +1,138 @@
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 {number} */
18
+ lastScrollTime = 0;
19
+ /** @type {FnData[]} */
20
+ #afterScrollQueue = [];
21
+ /** @type {number} */
22
+ #inactivityTime = 100;
23
+ /** @type {Set<OnScrollFunc>} */
24
+ #externalScrollListeners = new Set();
25
+ /** @type {boolean} */
26
+ #destroyed = false;
27
+ /**
28
+ * @param {Element|Window} scrollTarget - The element or window to track scrolling on
29
+ * @param {number} [inactivityTime=100] - Time in milliseconds to wait after scroll ends before executing the queue
30
+ * @throws {TypeError} If scrollTarget is not a valid Element or Window
31
+ * @throws {TypeError} If inactivityTime is not a positive number
32
+ */
33
+ constructor(scrollTarget = window, inactivityTime = 100) {
34
+ if (!(scrollTarget instanceof Element) && !(scrollTarget instanceof Window))
35
+ throw new TypeError('scrollTarget must be an Element or the Window object.');
36
+ this.#scrollTarget = scrollTarget;
37
+ this._onScroll = this._onScroll.bind(this);
38
+ this._checkQueue = this._checkQueue.bind(this);
39
+ this.#scrollTarget.addEventListener('scroll', this._onScroll);
40
+ this.#inactivityTime = inactivityTime;
41
+ requestAnimationFrame(this._checkQueue);
42
+ }
43
+ /**
44
+ * Gets the current inactivity time in milliseconds.
45
+ * @returns {number}
46
+ */
47
+ get inactivityTime() {
48
+ return this.#inactivityTime;
49
+ }
50
+ /**
51
+ * Sets a new inactivity time.
52
+ * Must be a positive number (in milliseconds).
53
+ * @param {number} value
54
+ * @throws {Error} If value is not a positive number
55
+ */
56
+ set inactivityTime(value) {
57
+ if (typeof value !== 'number' || value <= 0 || !Number.isFinite(value))
58
+ throw new Error('inactivityTime must be a positive number in milliseconds.');
59
+ this.#inactivityTime = value;
60
+ }
61
+ /**
62
+ * Internal handler for scroll events.
63
+ * Updates the last scroll timestamp.
64
+ * @private
65
+ */
66
+ _onScroll() {
67
+ this.lastScrollTime = Date.now();
68
+ }
69
+ /**
70
+ * Continuously checks whether the user has stopped scrolling,
71
+ * and if so, runs all queued functions.
72
+ * @private
73
+ */
74
+ _checkQueue() {
75
+ if (this.#destroyed)
76
+ return;
77
+ requestAnimationFrame(this._checkQueue);
78
+ if (Date.now() - this.lastScrollTime > this.#inactivityTime) {
79
+ while (this.#afterScrollQueue.length) {
80
+ const fn = this.#afterScrollQueue.pop();
81
+ if (typeof fn === 'function')
82
+ fn();
83
+ }
84
+ }
85
+ }
86
+ /**
87
+ * Adds a function to be executed after scroll has stopped.
88
+ * The scroll is considered "stopped" after the configured inactivity time.
89
+ *
90
+ * @param {() => void} fn - A function to execute once scrolling has stopped.
91
+ * @throws {TypeError} If the argument is not a function.
92
+ */
93
+ doAfterScroll(fn) {
94
+ if (typeof fn !== 'function')
95
+ throw new TypeError('Argument must be a function.');
96
+ this.lastScrollTime = Date.now();
97
+ this.#afterScrollQueue.push(fn);
98
+ }
99
+ /**
100
+ * Registers an external scroll listener on the tracked element.
101
+ *
102
+ * @param {OnScrollFunc} fn - The scroll listener to add
103
+ * @throws {TypeError} If the argument is not a function.
104
+ */
105
+ onScroll(fn) {
106
+ if (typeof fn !== 'function')
107
+ throw new TypeError('Argument must be a function.');
108
+ this.#scrollTarget.addEventListener('scroll', fn);
109
+ this.#externalScrollListeners.add(fn);
110
+ }
111
+ /**
112
+ * Removes a previously registered scroll listener from the tracked element.
113
+ *
114
+ * @param {OnScrollFunc} fn - The scroll listener to remove
115
+ * @throws {TypeError} If the argument is not a function.
116
+ */
117
+ offScroll(fn) {
118
+ if (typeof fn !== 'function')
119
+ throw new TypeError('Argument must be a function.');
120
+ if (this.#externalScrollListeners.has(fn)) {
121
+ this.#scrollTarget.removeEventListener('scroll', fn);
122
+ this.#externalScrollListeners.delete(fn);
123
+ }
124
+ }
125
+ /**
126
+ * Destroys the watcher by removing internal listeners and clearing data.
127
+ */
128
+ destroy() {
129
+ if (this.#destroyed)
130
+ return;
131
+ this.#destroyed = true;
132
+ this.#scrollTarget.removeEventListener('scroll', this._onScroll);
133
+ for (const fn of this.#externalScrollListeners)
134
+ this.#scrollTarget.removeEventListener('scroll', fn);
135
+ this.#externalScrollListeners.clear();
136
+ }
137
+ }
138
+ export default TinyAfterScrollWatcher;