tiny-essentials 1.27.1 → 1.28.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.
- package/README.md +1 -0
- package/changelog/1/28/0.md +16 -0
- package/changelog/1/28/1.md +23 -0
- package/dist/v1/TinyAdvancedRaffle.min.js +1 -1
- package/dist/v1/TinyAfterScrollWatcher.min.js +1 -1
- package/dist/v1/TinyAnalogClock.min.js +1 -1
- package/dist/v1/TinyBasicsEs.min.js +1 -2
- package/dist/v1/TinyDayNightCycle.min.js +1 -1
- package/dist/v1/TinyDragDropDetector.min.js +1 -1
- package/dist/v1/TinyDragger.min.js +1 -1
- package/dist/v1/TinyElementObserver.min.js +1 -1
- package/dist/v1/TinyEssentials.min.js +1 -2
- package/dist/v1/TinyGamepad.min.js +1 -1
- package/dist/v1/TinyHtml.min.js +1 -1
- package/dist/v1/TinyI18.min.js +1 -1
- package/dist/v1/TinyIframeEvents.min.js +1 -1
- package/dist/v1/TinyLocalStorage.min.js +1 -1
- package/dist/v1/TinyMediaPlayer.min.js +1 -1
- package/dist/v1/TinyNewWinEvents.min.js +1 -1
- package/dist/v1/TinyNotifyCenter.min.js +1 -1
- package/dist/v1/TinyRadioFm.min.js +1 -1
- package/dist/v1/TinyRateLimiter.min.js +1 -1
- package/dist/v1/TinySmartScroller.min.js +1 -1
- package/dist/v1/TinyTextDiffer.min.js +1 -1
- package/dist/v1/TinyTextarea.min.js +1 -1
- package/dist/v1/TinyTimeout.min.js +1 -1
- package/dist/v1/TinyToastNotify.min.js +1 -1
- package/dist/v1/TinyUploadClicker.min.js +1 -2
- package/dist/v1/UltraRandomMsgGen.min.js +1 -1
- package/dist/v1/basics/array.cjs +57 -9
- package/dist/v1/basics/array.d.mts +27 -11
- package/dist/v1/basics/array.mjs +51 -9
- package/dist/v1/basics/clock.cjs +94 -42
- package/dist/v1/basics/clock.d.mts +10 -10
- package/dist/v1/basics/clock.mjs +91 -54
- package/dist/v1/basics/deprecated/html.cjs +246 -0
- package/dist/v1/basics/deprecated/html.d.mts +63 -0
- package/dist/v1/basics/deprecated/html.mjs +190 -0
- package/dist/v1/basics/extendObjType/Buffer.cjs +13 -0
- package/dist/v1/basics/extendObjType/Buffer.d.mts +4 -0
- package/dist/v1/basics/extendObjType/Buffer.mjs +8 -0
- package/dist/v1/basics/html.cjs +149 -130
- package/dist/v1/basics/html.d.mts +26 -61
- package/dist/v1/basics/html.mjs +113 -105
- package/dist/v1/basics/index.cjs +2 -0
- package/dist/v1/basics/index.d.mts +3 -1
- package/dist/v1/basics/index.mjs +3 -3
- package/dist/v1/basics/mediaContent.cjs +51 -9
- package/dist/v1/basics/mediaContent.d.mts +0 -87
- package/dist/v1/basics/mediaContent.mjs +40 -10
- package/dist/v1/basics/objChecker.cjs +7 -5
- package/dist/v1/basics/objChecker.d.mts +3 -2
- package/dist/v1/basics/objChecker.mjs +6 -6
- package/dist/v1/basics/objFilter.cjs +40 -18
- package/dist/v1/basics/objFilter.d.mts +6 -6
- package/dist/v1/basics/objFilter.mjs +32 -17
- package/dist/v1/basics/simpleMath.cjs +81 -25
- package/dist/v1/basics/simpleMath.d.mts +2 -2
- package/dist/v1/basics/simpleMath.mjs +70 -27
- package/dist/v1/basics/text.cjs +76 -31
- package/dist/v1/basics/text.d.mts +69 -7
- package/dist/v1/basics/text.mjs +62 -31
- package/dist/v1/index.cjs +4 -2
- package/dist/v1/index.d.mts +4 -2
- package/dist/v1/index.mjs +4 -4
- package/dist/v1/libs/TinyAdvancedRaffle.cjs +71 -255
- package/dist/v1/libs/TinyAdvancedRaffle.d.mts +6 -134
- package/dist/v1/libs/TinyAdvancedRaffle.mjs +69 -228
- package/dist/v1/libs/TinyAfterScrollWatcher.cjs +17 -1
- package/dist/v1/libs/TinyAfterScrollWatcher.d.mts +4 -1
- package/dist/v1/libs/TinyAfterScrollWatcher.mjs +15 -1
- package/dist/v1/libs/TinyAnalogClock.cjs +60 -0
- package/dist/v1/libs/TinyAnalogClock.d.mts +6 -0
- package/dist/v1/libs/TinyAnalogClock.mjs +57 -0
- package/dist/v1/libs/TinyClipboard.cjs +2 -0
- package/dist/v1/libs/TinyClipboard.d.mts +4 -2
- package/dist/v1/libs/TinyClipboard.mjs +2 -0
- package/dist/v1/libs/TinyColorConverter.cjs +1 -0
- package/dist/v1/libs/TinyColorConverter.d.mts +2 -1
- package/dist/v1/libs/TinyColorConverter.mjs +1 -0
- package/dist/v1/libs/TinyDayNightCycle.cjs +67 -42
- package/dist/v1/libs/TinyDayNightCycle.d.mts +2 -7
- package/dist/v1/libs/TinyDayNightCycle.mjs +65 -41
- package/dist/v1/libs/TinyDragDropDetector.cjs +25 -0
- package/dist/v1/libs/TinyDragDropDetector.d.mts +6 -0
- package/dist/v1/libs/TinyDragDropDetector.mjs +22 -0
- package/dist/v1/libs/TinyDragger.cjs +46 -19
- package/dist/v1/libs/TinyDragger.mjs +45 -18
- package/dist/v1/libs/TinyElementObserver.cjs +39 -0
- package/dist/v1/libs/TinyElementObserver.d.mts +6 -0
- package/dist/v1/libs/TinyElementObserver.mjs +36 -0
- package/dist/v1/libs/TinyGamepad.cjs +131 -1
- package/dist/v1/libs/TinyGamepad.mjs +129 -1
- package/dist/v1/libs/TinyHtml.cjs +1 -1
- package/dist/v1/libs/TinyHtml.mjs +2 -2
- package/dist/v1/libs/TinyI18.cjs +61 -0
- package/dist/v1/libs/TinyI18.d.mts +14 -0
- package/dist/v1/libs/TinyI18.mjs +55 -0
- package/dist/v1/libs/TinyIframeEvents.cjs +26 -196
- package/dist/v1/libs/TinyIframeEvents.d.mts +3 -122
- package/dist/v1/libs/TinyIframeEvents.mjs +26 -171
- package/dist/v1/libs/TinyLocalStorage.cjs +53 -205
- package/dist/v1/libs/TinyLocalStorage.d.mts +6 -127
- package/dist/v1/libs/TinyLocalStorage.mjs +50 -178
- package/dist/v1/libs/TinyMediaPlayer.cjs +74 -2
- package/dist/v1/libs/TinyMediaPlayer.d.mts +12 -2
- package/dist/v1/libs/TinyMediaPlayer.mjs +68 -2
- package/dist/v1/libs/TinyNewWinEvents.cjs +39 -197
- package/dist/v1/libs/TinyNewWinEvents.d.mts +6 -119
- package/dist/v1/libs/TinyNewWinEvents.mjs +36 -172
- package/dist/v1/libs/TinyNotifyCenter.cjs +36 -0
- package/dist/v1/libs/TinyNotifyCenter.d.mts +6 -0
- package/dist/v1/libs/TinyNotifyCenter.mjs +32 -0
- package/dist/v1/libs/TinyRadioFm.cjs +40 -3
- package/dist/v1/libs/TinyRadioFm.d.mts +3 -3
- package/dist/v1/libs/TinyRadioFm.mjs +37 -3
- package/dist/v1/libs/TinyRateLimiter.cjs +52 -0
- package/dist/v1/libs/TinyRateLimiter.d.mts +6 -0
- package/dist/v1/libs/TinyRateLimiter.mjs +48 -0
- package/dist/v1/libs/TinySmartScroller.cjs +56 -205
- package/dist/v1/libs/TinySmartScroller.d.mts +19 -152
- package/dist/v1/libs/TinySmartScroller.mjs +55 -181
- package/dist/v1/libs/TinyTextDiffer.cjs +19 -18
- package/dist/v1/libs/TinyTextDiffer.d.mts +1 -0
- package/dist/v1/libs/TinyTextDiffer.mjs +16 -17
- package/dist/v1/libs/TinyTextarea.cjs +19 -0
- package/dist/v1/libs/TinyTextarea.d.mts +6 -0
- package/dist/v1/libs/TinyTextarea.mjs +17 -0
- package/dist/v1/libs/TinyTimeout.cjs +11 -1
- package/dist/v1/libs/TinyTimeout.mjs +9 -1
- package/dist/v1/libs/TinyToastNotify.cjs +35 -0
- package/dist/v1/libs/TinyToastNotify.d.mts +6 -0
- package/dist/v1/libs/TinyToastNotify.mjs +31 -0
- package/dist/v1/libs/TinyUploadClicker.cjs +20 -0
- package/dist/v1/libs/TinyUploadClicker.d.mts +6 -0
- package/dist/v1/libs/TinyUploadClicker.mjs +17 -0
- package/dist/v1/libs/UltraRandomMsgGen.cjs +148 -35
- package/dist/v1/libs/UltraRandomMsgGen.d.mts +17 -3
- package/dist/v1/libs/UltraRandomMsgGen.mjs +133 -34
- package/dist/v1/libs/utils.cjs +21 -0
- package/dist/v1/libs/utils.d.mts +2 -0
- package/dist/v1/libs/utils.mjs +16 -0
- package/docs/v1/Exports.md +0 -1
- package/docs/v1/README.md +1 -1
- package/docs/v1/basics/array.md +2 -2
- package/docs/v1/basics/deprecated/html.md +17 -0
- package/docs/v1/basics/fullScreen.md +1 -1
- package/docs/v1/basics/html.md +12 -26
- package/docs/v1/basics/mediaContent.md +1 -1
- package/docs/v1/basics/objChecker.md +1 -1
- package/docs/v1/basics/objFilter.md +1 -1
- package/docs/v1/basics/text.md +2 -2
- package/docs/v1/fileManager/main.md +1 -1
- package/docs/v1/libs/ColorSafeStringify.md +1 -1
- package/docs/v1/libs/TinyAfterScrollWatcher.md +1 -1
- package/docs/v1/libs/TinyAnalogClock.md +1 -1
- package/docs/v1/libs/TinyArrayComparator.md +1 -1
- package/docs/v1/libs/TinyArrayPaginator.md +1 -1
- package/docs/v1/libs/TinyClassManager/TinyPluginInliner.md +2 -2
- package/docs/v1/libs/TinyClassManager.md +1 -1
- package/docs/v1/libs/TinyClipboard.md +1 -1
- package/docs/v1/libs/TinyCookieConsent.md +1 -1
- package/docs/v1/libs/TinyDomReadyManager.md +1 -1
- package/docs/v1/libs/TinyDragDropDetector.md +1 -1
- package/docs/v1/libs/TinyDragger.md +1 -1
- package/docs/v1/libs/TinyElementObserver.md +1 -1
- package/docs/v1/libs/TinyHtml.md +1 -1
- package/docs/v1/libs/TinyIframeEvents.md +10 -10
- package/docs/v1/libs/TinyLevelUp.md +1 -1
- package/docs/v1/libs/TinyLoadingScreen.md +1 -1
- package/docs/v1/libs/TinyLocalStorage.md +3 -3
- package/docs/v1/libs/TinyNewWinEvents.md +10 -11
- package/docs/v1/libs/TinyNotifications.md +1 -1
- package/docs/v1/libs/TinyPromiseQueue.md +2 -2
- package/docs/v1/libs/TinyTextDiffer.md +1 -1
- package/docs/v1/libs/TinyTextarea.md +1 -1
- package/package.json +15 -5
- package/dist/v1/TinyBasicsEs.min.js.LICENSE.txt +0 -8
- package/dist/v1/TinyEssentials.min.js.LICENSE.txt +0 -8
- package/dist/v1/TinyEvents.min.js +0 -1
- package/dist/v1/TinyUploadClicker.min.js.LICENSE.txt +0 -8
- package/dist/v1/build/TinyEvents.cjs +0 -7
- package/dist/v1/build/TinyEvents.d.mts +0 -3
- package/dist/v1/build/TinyEvents.mjs +0 -2
- package/dist/v1/libs/TinyEvents.cjs +0 -395
- package/dist/v1/libs/TinyEvents.d.mts +0 -164
- package/dist/v1/libs/TinyEvents.mjs +0 -360
- package/docs/v1/libs/TinyEvents.md +0 -187
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var events = require('events');
|
|
3
4
|
var objChecker = require('../basics/objChecker.cjs');
|
|
4
|
-
var
|
|
5
|
+
var utils = require('./utils.cjs');
|
|
6
|
+
|
|
7
|
+
const checkDestroy = utils.createCheckDestroyed('TinyLocalStorage');
|
|
5
8
|
|
|
6
9
|
/** @type {Map<any, EncodeFn>} */
|
|
7
10
|
const customEncoders = new Map();
|
|
@@ -84,7 +87,7 @@ const customTypesFreezed = new Set([
|
|
|
84
87
|
* - Type-specific `set` and `get` methods (`setDate`, `getBool`, etc.)
|
|
85
88
|
* - `getValue()` to retrieve any structure regardless of type
|
|
86
89
|
* - Auto-encoding/decoding with support for custom types via `registerJsonType`
|
|
87
|
-
* - Built-in event system
|
|
90
|
+
* - Built-in event system to listen for changes
|
|
88
91
|
* - Optional fallback values on decoding errors
|
|
89
92
|
*
|
|
90
93
|
* Supports registering and unregistering custom types via:
|
|
@@ -93,208 +96,7 @@ const customTypesFreezed = new Set([
|
|
|
93
96
|
*
|
|
94
97
|
* This class is suitable for applications that require structured persistence in the browser.
|
|
95
98
|
*/
|
|
96
|
-
class TinyLocalStorage {
|
|
97
|
-
/** @typedef {import('./TinyEvents.mjs').handler} handler */
|
|
98
|
-
|
|
99
|
-
#events = new TinyEvents();
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Enables or disables throwing an error when the maximum number of listeners is exceeded.
|
|
103
|
-
*
|
|
104
|
-
* @param {boolean} shouldThrow - If true, an error will be thrown when the max is exceeded.
|
|
105
|
-
*/
|
|
106
|
-
setThrowOnMaxListeners(shouldThrow) {
|
|
107
|
-
return this.#events.setThrowOnMaxListeners(shouldThrow);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Checks whether an error will be thrown when the max listener limit is exceeded.
|
|
112
|
-
*
|
|
113
|
-
* @returns {boolean} True if an error will be thrown, false if only a warning is shown.
|
|
114
|
-
*/
|
|
115
|
-
getThrowOnMaxListeners() {
|
|
116
|
-
return this.#events.getThrowOnMaxListeners();
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/////////////////////////////////////////////////////////////
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
123
|
-
*
|
|
124
|
-
* @param {string|string[]} event - Event name.
|
|
125
|
-
* @param {handler} handler - The callback function.
|
|
126
|
-
*/
|
|
127
|
-
prependListener(event, handler) {
|
|
128
|
-
return this.#events.prependListener(event, handler);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
133
|
-
*
|
|
134
|
-
* @param {string|string[]} event - Event name.
|
|
135
|
-
* @param {handler} handler - The callback function.
|
|
136
|
-
* @returns {handler[]} - The wrapped handler used internally.
|
|
137
|
-
*/
|
|
138
|
-
prependListenerOnce(event, handler) {
|
|
139
|
-
return this.#events.prependListenerOnce(event, handler);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
//////////////////////////////////////////////////////////////////////
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Adds a event listener.
|
|
146
|
-
*
|
|
147
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
148
|
-
* @param {handler} handler - Callback function to be called when event fires.
|
|
149
|
-
*/
|
|
150
|
-
appendListener(event, handler) {
|
|
151
|
-
return this.#events.appendListener(event, handler);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Registers an event listener that runs only once, then is removed.
|
|
156
|
-
*
|
|
157
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
158
|
-
* @param {handler} handler - The callback function to run on event.
|
|
159
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
160
|
-
*/
|
|
161
|
-
appendListenerOnce(event, handler) {
|
|
162
|
-
return this.#events.appendListenerOnce(event, handler);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Adds a event listener.
|
|
167
|
-
*
|
|
168
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
169
|
-
* @param {handler} handler - Callback function to be called when event fires.
|
|
170
|
-
*/
|
|
171
|
-
on(event, handler) {
|
|
172
|
-
return this.#events.on(event, handler);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Registers an event listener that runs only once, then is removed.
|
|
177
|
-
*
|
|
178
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
179
|
-
* @param {handler} handler - The callback function to run on event.
|
|
180
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
181
|
-
*/
|
|
182
|
-
once(event, handler) {
|
|
183
|
-
return this.#events.once(event, handler);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
////////////////////////////////////////////////////////////////////
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Removes a previously registered event listener.
|
|
190
|
-
*
|
|
191
|
-
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
192
|
-
* @param {handler} handler - The specific callback function to remove.
|
|
193
|
-
*/
|
|
194
|
-
off(event, handler) {
|
|
195
|
-
return this.#events.off(event, handler);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Removes all event listeners of a specific type from the element.
|
|
200
|
-
*
|
|
201
|
-
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
202
|
-
*/
|
|
203
|
-
offAll(event) {
|
|
204
|
-
return this.#events.offAll(event);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Removes all event listeners of all types from the element.
|
|
209
|
-
*/
|
|
210
|
-
offAllTypes() {
|
|
211
|
-
return this.#events.offAllTypes();
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
////////////////////////////////////////////////////////////
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Returns the number of listeners for a given event.
|
|
218
|
-
*
|
|
219
|
-
* @param {string} event - The name of the event.
|
|
220
|
-
* @returns {number} Number of listeners for the event.
|
|
221
|
-
*/
|
|
222
|
-
listenerCount(event) {
|
|
223
|
-
return this.#events.listenerCount(event);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
228
|
-
*
|
|
229
|
-
* @param {string} event - The name of the event.
|
|
230
|
-
* @returns {handler[]} Array of listener functions.
|
|
231
|
-
*/
|
|
232
|
-
listeners(event) {
|
|
233
|
-
return this.#events.listeners(event);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
238
|
-
*
|
|
239
|
-
* @param {string} event - The name of the event.
|
|
240
|
-
* @returns {handler[]} Array of listener functions.
|
|
241
|
-
*/
|
|
242
|
-
onceListeners(event) {
|
|
243
|
-
return this.#events.onceListeners(event);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Returns a copy of the internal listeners array for the specified event,
|
|
248
|
-
* including wrapper functions like those used by `.once()`.
|
|
249
|
-
* @param {string | symbol} event - The event name.
|
|
250
|
-
* @returns {handler[]} An array of raw listener functions.
|
|
251
|
-
*/
|
|
252
|
-
allListeners(event) {
|
|
253
|
-
return this.#events.allListeners(event);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Returns an array of event names for which there are registered listeners.
|
|
258
|
-
*
|
|
259
|
-
* @returns {string[]} Array of registered event names.
|
|
260
|
-
*/
|
|
261
|
-
eventNames() {
|
|
262
|
-
return this.#events.eventNames();
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
//////////////////////////////////////////////////////
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Emits an event, triggering all registered handlers for that event.
|
|
269
|
-
*
|
|
270
|
-
* @param {string} event - The event name to emit.
|
|
271
|
-
* @param {...any} payload - Optional data to pass to each handler.
|
|
272
|
-
* @returns {boolean[]} True if any listeners were called, false otherwise.
|
|
273
|
-
*/
|
|
274
|
-
emit(event, ...payload) {
|
|
275
|
-
return this.#events.emit(event, ...payload);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Sets the maximum number of listeners per event before a warning is shown.
|
|
280
|
-
*
|
|
281
|
-
* @param {number} n - The maximum number of listeners.
|
|
282
|
-
*/
|
|
283
|
-
setMaxListeners(n) {
|
|
284
|
-
return this.#events.setMaxListeners(n);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Gets the maximum number of listeners allowed per event.
|
|
289
|
-
*
|
|
290
|
-
* @returns {number} The maximum number of listeners.
|
|
291
|
-
*/
|
|
292
|
-
getMaxListeners() {
|
|
293
|
-
return this.#events.getMaxListeners();
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
///////////////////////////////////////////////////
|
|
297
|
-
|
|
99
|
+
class TinyLocalStorage extends events.EventEmitter {
|
|
298
100
|
/**
|
|
299
101
|
* Checks whether a JSON-serializable type is already registered.
|
|
300
102
|
*
|
|
@@ -417,6 +219,21 @@ class TinyLocalStorage {
|
|
|
417
219
|
|
|
418
220
|
//////////////////////////////////////////////////////
|
|
419
221
|
|
|
222
|
+
/**
|
|
223
|
+
* Tracks whether the instance has been destroyed.
|
|
224
|
+
* @type {boolean}
|
|
225
|
+
*/
|
|
226
|
+
#destroyed = false;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Gets the current destruction status of the instance.
|
|
230
|
+
*
|
|
231
|
+
* @returns {boolean} True if the instance is destroyed, false otherwise.
|
|
232
|
+
*/
|
|
233
|
+
get destroyed() {
|
|
234
|
+
return this.#destroyed;
|
|
235
|
+
}
|
|
236
|
+
|
|
420
237
|
/** @type {Storage} */
|
|
421
238
|
#localStorage = window.localStorage;
|
|
422
239
|
|
|
@@ -436,6 +253,7 @@ class TinyLocalStorage {
|
|
|
436
253
|
* @param {string} [dbName] - Unique database name.
|
|
437
254
|
*/
|
|
438
255
|
constructor(dbName) {
|
|
256
|
+
super();
|
|
439
257
|
if (typeof dbName !== 'undefined' && typeof dbName !== 'string')
|
|
440
258
|
throw new TypeError('TinyLocalStorage: dbName must be a string if provided.');
|
|
441
259
|
if (typeof dbName === 'string') this.#dbKey = `LSDB::${dbName}`;
|
|
@@ -467,6 +285,7 @@ class TinyLocalStorage {
|
|
|
467
285
|
* @throws {TypeError} If `onUpgrade` is not a function.
|
|
468
286
|
*/
|
|
469
287
|
updateStorageVersion(version, onUpgrade) {
|
|
288
|
+
checkDestroy(this.#destroyed);
|
|
470
289
|
if (typeof this.#dbKey !== 'string')
|
|
471
290
|
throw new Error(
|
|
472
291
|
'TinyLocalStorage: Database key is not initialized. Set a valid dbName in the constructor.',
|
|
@@ -499,6 +318,7 @@ class TinyLocalStorage {
|
|
|
499
318
|
* @returns {string|null} The database key, or null if not set.
|
|
500
319
|
*/
|
|
501
320
|
getDbKey() {
|
|
321
|
+
checkDestroy(this.#destroyed);
|
|
502
322
|
return this.#dbKey;
|
|
503
323
|
}
|
|
504
324
|
|
|
@@ -508,6 +328,7 @@ class TinyLocalStorage {
|
|
|
508
328
|
* @returns {number} The current version number.
|
|
509
329
|
*/
|
|
510
330
|
getVersion() {
|
|
331
|
+
checkDestroy(this.#destroyed);
|
|
511
332
|
return this.#version;
|
|
512
333
|
}
|
|
513
334
|
|
|
@@ -517,6 +338,7 @@ class TinyLocalStorage {
|
|
|
517
338
|
* @param {Storage} localstorage - A valid Storage object (localStorage or sessionStorage).
|
|
518
339
|
*/
|
|
519
340
|
setLocalStorage(localstorage) {
|
|
341
|
+
checkDestroy(this.#destroyed);
|
|
520
342
|
if (!(localstorage instanceof Storage))
|
|
521
343
|
throw new TypeError('Argument must be a valid instance of Storage.');
|
|
522
344
|
this.#localStorage = localstorage;
|
|
@@ -528,6 +350,7 @@ class TinyLocalStorage {
|
|
|
528
350
|
* @returns {boolean} True if `localStorage` exists, false otherwise.
|
|
529
351
|
*/
|
|
530
352
|
localStorageExists() {
|
|
353
|
+
checkDestroy(this.#destroyed);
|
|
531
354
|
return this.#localStorage instanceof Storage;
|
|
532
355
|
}
|
|
533
356
|
|
|
@@ -553,6 +376,7 @@ class TinyLocalStorage {
|
|
|
553
376
|
* @param {LocalStorageJsonValue} data - The data to be serialized and stored.
|
|
554
377
|
*/
|
|
555
378
|
setJson(name, data) {
|
|
379
|
+
checkDestroy(this.#destroyed);
|
|
556
380
|
this.#isProtectedDbKey(name);
|
|
557
381
|
if (
|
|
558
382
|
!objChecker.isJsonObject(data) &&
|
|
@@ -615,6 +439,7 @@ class TinyLocalStorage {
|
|
|
615
439
|
* @returns {LocalStorageJsonValue|null} The parsed object or fallback.
|
|
616
440
|
*/
|
|
617
441
|
getJson(name, defaultData) {
|
|
442
|
+
checkDestroy(this.#destroyed);
|
|
618
443
|
const { decoded, fallback } = this.#getJson(name, defaultData);
|
|
619
444
|
if (
|
|
620
445
|
decoded instanceof Map ||
|
|
@@ -632,6 +457,7 @@ class TinyLocalStorage {
|
|
|
632
457
|
* @param {Date} data
|
|
633
458
|
*/
|
|
634
459
|
setDate(name, data) {
|
|
460
|
+
checkDestroy(this.#destroyed);
|
|
635
461
|
this.#isProtectedDbKey(name);
|
|
636
462
|
if (!(data instanceof Date)) throw new TypeError('Value must be a Date.');
|
|
637
463
|
const encoded = this.#setJson(name, data);
|
|
@@ -645,6 +471,7 @@ class TinyLocalStorage {
|
|
|
645
471
|
* @returns {Date|null}
|
|
646
472
|
*/
|
|
647
473
|
getDate(name) {
|
|
474
|
+
checkDestroy(this.#destroyed);
|
|
648
475
|
const value = this.#getJson(name).decoded;
|
|
649
476
|
return value instanceof Date ? value : null;
|
|
650
477
|
}
|
|
@@ -655,6 +482,7 @@ class TinyLocalStorage {
|
|
|
655
482
|
* @param {RegExp} data
|
|
656
483
|
*/
|
|
657
484
|
setRegExp(name, data) {
|
|
485
|
+
checkDestroy(this.#destroyed);
|
|
658
486
|
this.#isProtectedDbKey(name);
|
|
659
487
|
if (!(data instanceof RegExp)) throw new TypeError('Value must be a RegExp.');
|
|
660
488
|
const encoded = this.#setJson(name, data);
|
|
@@ -668,6 +496,7 @@ class TinyLocalStorage {
|
|
|
668
496
|
* @returns {RegExp|null}
|
|
669
497
|
*/
|
|
670
498
|
getRegExp(name) {
|
|
499
|
+
checkDestroy(this.#destroyed);
|
|
671
500
|
const value = this.#getJson(name).decoded;
|
|
672
501
|
return value instanceof RegExp ? value : null;
|
|
673
502
|
}
|
|
@@ -678,6 +507,7 @@ class TinyLocalStorage {
|
|
|
678
507
|
* @param {bigint} data
|
|
679
508
|
*/
|
|
680
509
|
setBigInt(name, data) {
|
|
510
|
+
checkDestroy(this.#destroyed);
|
|
681
511
|
this.#isProtectedDbKey(name);
|
|
682
512
|
if (typeof data !== 'bigint') throw new TypeError('Value must be a BigInt.');
|
|
683
513
|
const encoded = this.#setJson(name, data);
|
|
@@ -691,6 +521,7 @@ class TinyLocalStorage {
|
|
|
691
521
|
* @returns {bigint|null}
|
|
692
522
|
*/
|
|
693
523
|
getBigInt(name) {
|
|
524
|
+
checkDestroy(this.#destroyed);
|
|
694
525
|
const value = this.#getJson(name).decoded;
|
|
695
526
|
return typeof value === 'bigint' ? value : null;
|
|
696
527
|
}
|
|
@@ -702,6 +533,7 @@ class TinyLocalStorage {
|
|
|
702
533
|
* @param {symbol} data
|
|
703
534
|
*/
|
|
704
535
|
setSymbol(name, data) {
|
|
536
|
+
checkDestroy(this.#destroyed);
|
|
705
537
|
this.#isProtectedDbKey(name);
|
|
706
538
|
if (typeof data !== 'symbol') throw new TypeError('Value must be a Symbol.');
|
|
707
539
|
const encoded = this.#setJson(name, data);
|
|
@@ -715,6 +547,7 @@ class TinyLocalStorage {
|
|
|
715
547
|
* @returns {symbol|null}
|
|
716
548
|
*/
|
|
717
549
|
getSymbol(name) {
|
|
550
|
+
checkDestroy(this.#destroyed);
|
|
718
551
|
const value = this.#getJson(name).decoded;
|
|
719
552
|
return typeof value === 'symbol' ? value : null;
|
|
720
553
|
}
|
|
@@ -726,6 +559,7 @@ class TinyLocalStorage {
|
|
|
726
559
|
* @returns {any} The stored value or null if not found.
|
|
727
560
|
*/
|
|
728
561
|
getValue(name) {
|
|
562
|
+
checkDestroy(this.#destroyed);
|
|
729
563
|
return this.#getJson(name).decoded ?? null;
|
|
730
564
|
}
|
|
731
565
|
|
|
@@ -736,6 +570,7 @@ class TinyLocalStorage {
|
|
|
736
570
|
* @param {any} data - The data to store.
|
|
737
571
|
*/
|
|
738
572
|
setItem(name, data) {
|
|
573
|
+
checkDestroy(this.#destroyed);
|
|
739
574
|
this.#isProtectedDbKey(name);
|
|
740
575
|
if (typeof name !== 'string' || !name.length)
|
|
741
576
|
throw new TypeError('Key must be a non-empty string.');
|
|
@@ -750,6 +585,7 @@ class TinyLocalStorage {
|
|
|
750
585
|
* @returns {string|null} The stored value or null if not found.
|
|
751
586
|
*/
|
|
752
587
|
getItem(name) {
|
|
588
|
+
checkDestroy(this.#destroyed);
|
|
753
589
|
if (typeof name !== 'string' || !name.length)
|
|
754
590
|
throw new TypeError('Key must be a non-empty string.');
|
|
755
591
|
return this.#localStorage.getItem(name);
|
|
@@ -762,6 +598,7 @@ class TinyLocalStorage {
|
|
|
762
598
|
* @param {string} data - The string to store.
|
|
763
599
|
*/
|
|
764
600
|
setString(name, data) {
|
|
601
|
+
checkDestroy(this.#destroyed);
|
|
765
602
|
this.#isProtectedDbKey(name);
|
|
766
603
|
if (typeof name !== 'string' || !name.length)
|
|
767
604
|
throw new TypeError('Key must be a non-empty string.');
|
|
@@ -784,6 +621,7 @@ class TinyLocalStorage {
|
|
|
784
621
|
* @returns {string|null} The string if valid, or null.
|
|
785
622
|
*/
|
|
786
623
|
getString(name) {
|
|
624
|
+
checkDestroy(this.#destroyed);
|
|
787
625
|
if (typeof name !== 'string' || !name.length)
|
|
788
626
|
throw new TypeError('Key must be a non-empty string.');
|
|
789
627
|
let value = this.#localStorage.getItem(name);
|
|
@@ -807,6 +645,7 @@ class TinyLocalStorage {
|
|
|
807
645
|
* @param {number} data - The number to store.
|
|
808
646
|
*/
|
|
809
647
|
setNumber(name, data) {
|
|
648
|
+
checkDestroy(this.#destroyed);
|
|
810
649
|
this.#isProtectedDbKey(name);
|
|
811
650
|
if (typeof name !== 'string' || !name.length)
|
|
812
651
|
throw new TypeError('Key must be a non-empty string.');
|
|
@@ -822,6 +661,7 @@ class TinyLocalStorage {
|
|
|
822
661
|
* @returns {number|null} The number or null if invalid.
|
|
823
662
|
*/
|
|
824
663
|
getNumber(name) {
|
|
664
|
+
checkDestroy(this.#destroyed);
|
|
825
665
|
if (typeof name !== 'string' || !name.length)
|
|
826
666
|
throw new TypeError('Key must be a non-empty string.');
|
|
827
667
|
|
|
@@ -842,6 +682,7 @@ class TinyLocalStorage {
|
|
|
842
682
|
* @param {boolean} data - The boolean value to store.
|
|
843
683
|
*/
|
|
844
684
|
setBool(name, data) {
|
|
685
|
+
checkDestroy(this.#destroyed);
|
|
845
686
|
this.#isProtectedDbKey(name);
|
|
846
687
|
if (typeof name !== 'string' || !name.length)
|
|
847
688
|
throw new TypeError('Key must be a non-empty string.');
|
|
@@ -857,6 +698,7 @@ class TinyLocalStorage {
|
|
|
857
698
|
* @returns {boolean|null} The boolean or null if invalid.
|
|
858
699
|
*/
|
|
859
700
|
getBool(name) {
|
|
701
|
+
checkDestroy(this.#destroyed);
|
|
860
702
|
if (typeof name !== 'string' || !name.length)
|
|
861
703
|
throw new TypeError('Key must be a non-empty string.');
|
|
862
704
|
|
|
@@ -876,6 +718,7 @@ class TinyLocalStorage {
|
|
|
876
718
|
* @param {string} name - The key to remove.
|
|
877
719
|
*/
|
|
878
720
|
removeItem(name) {
|
|
721
|
+
checkDestroy(this.#destroyed);
|
|
879
722
|
if (typeof name !== 'string' || !name.length)
|
|
880
723
|
throw new TypeError('Key must be a non-empty string.');
|
|
881
724
|
|
|
@@ -887,6 +730,7 @@ class TinyLocalStorage {
|
|
|
887
730
|
* Clears all data from `localStorage`.
|
|
888
731
|
*/
|
|
889
732
|
clearLocalStorage() {
|
|
733
|
+
checkDestroy(this.#destroyed);
|
|
890
734
|
return this.#localStorage.clear();
|
|
891
735
|
}
|
|
892
736
|
|
|
@@ -894,8 +738,12 @@ class TinyLocalStorage {
|
|
|
894
738
|
* Destroys the storage instance by removing the storage event listener.
|
|
895
739
|
*/
|
|
896
740
|
destroy() {
|
|
741
|
+
if (this.#destroyed) return;
|
|
897
742
|
window.removeEventListener('storage', this.#storageEvent);
|
|
898
|
-
this
|
|
743
|
+
this.removeAllListeners();
|
|
744
|
+
|
|
745
|
+
// Lock the instance
|
|
746
|
+
this.#destroyed = true;
|
|
899
747
|
}
|
|
900
748
|
}
|
|
901
749
|
|
|
@@ -91,7 +91,7 @@ export type LocalStorageJsonValue = (Record<string | number | symbol, any> | any
|
|
|
91
91
|
* - Type-specific `set` and `get` methods (`setDate`, `getBool`, etc.)
|
|
92
92
|
* - `getValue()` to retrieve any structure regardless of type
|
|
93
93
|
* - Auto-encoding/decoding with support for custom types via `registerJsonType`
|
|
94
|
-
* - Built-in event system
|
|
94
|
+
* - Built-in event system to listen for changes
|
|
95
95
|
* - Optional fallback values on decoding errors
|
|
96
96
|
*
|
|
97
97
|
* Supports registering and unregistering custom types via:
|
|
@@ -100,7 +100,7 @@ export type LocalStorageJsonValue = (Record<string | number | symbol, any> | any
|
|
|
100
100
|
*
|
|
101
101
|
* This class is suitable for applications that require structured persistence in the browser.
|
|
102
102
|
*/
|
|
103
|
-
declare class TinyLocalStorage {
|
|
103
|
+
declare class TinyLocalStorage extends EventEmitter<any> {
|
|
104
104
|
/**
|
|
105
105
|
* Checks whether a JSON-serializable type is already registered.
|
|
106
106
|
*
|
|
@@ -138,133 +138,11 @@ declare class TinyLocalStorage {
|
|
|
138
138
|
*/
|
|
139
139
|
constructor(dbName?: string);
|
|
140
140
|
/**
|
|
141
|
-
*
|
|
141
|
+
* Gets the current destruction status of the instance.
|
|
142
142
|
*
|
|
143
|
-
* @
|
|
143
|
+
* @returns {boolean} True if the instance is destroyed, false otherwise.
|
|
144
144
|
*/
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Checks whether an error will be thrown when the max listener limit is exceeded.
|
|
148
|
-
*
|
|
149
|
-
* @returns {boolean} True if an error will be thrown, false if only a warning is shown.
|
|
150
|
-
*/
|
|
151
|
-
getThrowOnMaxListeners(): boolean;
|
|
152
|
-
/**
|
|
153
|
-
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
154
|
-
*
|
|
155
|
-
* @param {string|string[]} event - Event name.
|
|
156
|
-
* @param {handler} handler - The callback function.
|
|
157
|
-
*/
|
|
158
|
-
prependListener(event: string | string[], handler: import("./TinyEvents.mjs").handler): void;
|
|
159
|
-
/**
|
|
160
|
-
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
161
|
-
*
|
|
162
|
-
* @param {string|string[]} event - Event name.
|
|
163
|
-
* @param {handler} handler - The callback function.
|
|
164
|
-
* @returns {handler[]} - The wrapped handler used internally.
|
|
165
|
-
*/
|
|
166
|
-
prependListenerOnce(event: string | string[], handler: import("./TinyEvents.mjs").handler): import("./TinyEvents.mjs").handler[];
|
|
167
|
-
/**
|
|
168
|
-
* Adds a event listener.
|
|
169
|
-
*
|
|
170
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
171
|
-
* @param {handler} handler - Callback function to be called when event fires.
|
|
172
|
-
*/
|
|
173
|
-
appendListener(event: string | string[], handler: import("./TinyEvents.mjs").handler): void;
|
|
174
|
-
/**
|
|
175
|
-
* Registers an event listener that runs only once, then is removed.
|
|
176
|
-
*
|
|
177
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
178
|
-
* @param {handler} handler - The callback function to run on event.
|
|
179
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
180
|
-
*/
|
|
181
|
-
appendListenerOnce(event: string | string[], handler: import("./TinyEvents.mjs").handler): import("./TinyEvents.mjs").handler[];
|
|
182
|
-
/**
|
|
183
|
-
* Adds a event listener.
|
|
184
|
-
*
|
|
185
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
186
|
-
* @param {handler} handler - Callback function to be called when event fires.
|
|
187
|
-
*/
|
|
188
|
-
on(event: string | string[], handler: import("./TinyEvents.mjs").handler): void;
|
|
189
|
-
/**
|
|
190
|
-
* Registers an event listener that runs only once, then is removed.
|
|
191
|
-
*
|
|
192
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
193
|
-
* @param {handler} handler - The callback function to run on event.
|
|
194
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
195
|
-
*/
|
|
196
|
-
once(event: string | string[], handler: import("./TinyEvents.mjs").handler): import("./TinyEvents.mjs").handler[];
|
|
197
|
-
/**
|
|
198
|
-
* Removes a previously registered event listener.
|
|
199
|
-
*
|
|
200
|
-
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
201
|
-
* @param {handler} handler - The specific callback function to remove.
|
|
202
|
-
*/
|
|
203
|
-
off(event: string | string[], handler: import("./TinyEvents.mjs").handler): void;
|
|
204
|
-
/**
|
|
205
|
-
* Removes all event listeners of a specific type from the element.
|
|
206
|
-
*
|
|
207
|
-
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
208
|
-
*/
|
|
209
|
-
offAll(event: string | string[]): void;
|
|
210
|
-
/**
|
|
211
|
-
* Removes all event listeners of all types from the element.
|
|
212
|
-
*/
|
|
213
|
-
offAllTypes(): void;
|
|
214
|
-
/**
|
|
215
|
-
* Returns the number of listeners for a given event.
|
|
216
|
-
*
|
|
217
|
-
* @param {string} event - The name of the event.
|
|
218
|
-
* @returns {number} Number of listeners for the event.
|
|
219
|
-
*/
|
|
220
|
-
listenerCount(event: string): number;
|
|
221
|
-
/**
|
|
222
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
223
|
-
*
|
|
224
|
-
* @param {string} event - The name of the event.
|
|
225
|
-
* @returns {handler[]} Array of listener functions.
|
|
226
|
-
*/
|
|
227
|
-
listeners(event: string): import("./TinyEvents.mjs").handler[];
|
|
228
|
-
/**
|
|
229
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
230
|
-
*
|
|
231
|
-
* @param {string} event - The name of the event.
|
|
232
|
-
* @returns {handler[]} Array of listener functions.
|
|
233
|
-
*/
|
|
234
|
-
onceListeners(event: string): import("./TinyEvents.mjs").handler[];
|
|
235
|
-
/**
|
|
236
|
-
* Returns a copy of the internal listeners array for the specified event,
|
|
237
|
-
* including wrapper functions like those used by `.once()`.
|
|
238
|
-
* @param {string | symbol} event - The event name.
|
|
239
|
-
* @returns {handler[]} An array of raw listener functions.
|
|
240
|
-
*/
|
|
241
|
-
allListeners(event: string | symbol): import("./TinyEvents.mjs").handler[];
|
|
242
|
-
/**
|
|
243
|
-
* Returns an array of event names for which there are registered listeners.
|
|
244
|
-
*
|
|
245
|
-
* @returns {string[]} Array of registered event names.
|
|
246
|
-
*/
|
|
247
|
-
eventNames(): string[];
|
|
248
|
-
/**
|
|
249
|
-
* Emits an event, triggering all registered handlers for that event.
|
|
250
|
-
*
|
|
251
|
-
* @param {string} event - The event name to emit.
|
|
252
|
-
* @param {...any} payload - Optional data to pass to each handler.
|
|
253
|
-
* @returns {boolean[]} True if any listeners were called, false otherwise.
|
|
254
|
-
*/
|
|
255
|
-
emit(event: string, ...payload: any[]): boolean[];
|
|
256
|
-
/**
|
|
257
|
-
* Sets the maximum number of listeners per event before a warning is shown.
|
|
258
|
-
*
|
|
259
|
-
* @param {number} n - The maximum number of listeners.
|
|
260
|
-
*/
|
|
261
|
-
setMaxListeners(n: number): void;
|
|
262
|
-
/**
|
|
263
|
-
* Gets the maximum number of listeners allowed per event.
|
|
264
|
-
*
|
|
265
|
-
* @returns {number} The maximum number of listeners.
|
|
266
|
-
*/
|
|
267
|
-
getMaxListeners(): number;
|
|
145
|
+
get destroyed(): boolean;
|
|
268
146
|
/**
|
|
269
147
|
* Updates the version of the storage and triggers migration if needed.
|
|
270
148
|
*
|
|
@@ -446,4 +324,5 @@ declare class TinyLocalStorage {
|
|
|
446
324
|
destroy(): void;
|
|
447
325
|
#private;
|
|
448
326
|
}
|
|
327
|
+
import { EventEmitter } from 'events';
|
|
449
328
|
//# sourceMappingURL=TinyLocalStorage.d.mts.map
|