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,5 +1,7 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
1
2
|
import { isJsonObject } from '../basics/objChecker.mjs';
|
|
2
|
-
import
|
|
3
|
+
import { createCheckDestroyed } from './utils.mjs';
|
|
4
|
+
const checkDestroy = createCheckDestroyed('TinyLocalStorage');
|
|
3
5
|
/** @type {Map<any, EncodeFn>} */
|
|
4
6
|
const customEncoders = new Map();
|
|
5
7
|
/** @type {Map<any, DecodeFn>} */
|
|
@@ -73,7 +75,7 @@ const customTypesFreezed = new Set([
|
|
|
73
75
|
* - Type-specific `set` and `get` methods (`setDate`, `getBool`, etc.)
|
|
74
76
|
* - `getValue()` to retrieve any structure regardless of type
|
|
75
77
|
* - Auto-encoding/decoding with support for custom types via `registerJsonType`
|
|
76
|
-
* - Built-in event system
|
|
78
|
+
* - Built-in event system to listen for changes
|
|
77
79
|
* - Optional fallback values on decoding errors
|
|
78
80
|
*
|
|
79
81
|
* Supports registering and unregistering custom types via:
|
|
@@ -82,181 +84,7 @@ const customTypesFreezed = new Set([
|
|
|
82
84
|
*
|
|
83
85
|
* This class is suitable for applications that require structured persistence in the browser.
|
|
84
86
|
*/
|
|
85
|
-
class TinyLocalStorage {
|
|
86
|
-
/** @typedef {import('./TinyEvents.mjs').handler} handler */
|
|
87
|
-
#events = new TinyEvents();
|
|
88
|
-
/**
|
|
89
|
-
* Enables or disables throwing an error when the maximum number of listeners is exceeded.
|
|
90
|
-
*
|
|
91
|
-
* @param {boolean} shouldThrow - If true, an error will be thrown when the max is exceeded.
|
|
92
|
-
*/
|
|
93
|
-
setThrowOnMaxListeners(shouldThrow) {
|
|
94
|
-
return this.#events.setThrowOnMaxListeners(shouldThrow);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Checks whether an error will be thrown when the max listener limit is exceeded.
|
|
98
|
-
*
|
|
99
|
-
* @returns {boolean} True if an error will be thrown, false if only a warning is shown.
|
|
100
|
-
*/
|
|
101
|
-
getThrowOnMaxListeners() {
|
|
102
|
-
return this.#events.getThrowOnMaxListeners();
|
|
103
|
-
}
|
|
104
|
-
/////////////////////////////////////////////////////////////
|
|
105
|
-
/**
|
|
106
|
-
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
107
|
-
*
|
|
108
|
-
* @param {string|string[]} event - Event name.
|
|
109
|
-
* @param {handler} handler - The callback function.
|
|
110
|
-
*/
|
|
111
|
-
prependListener(event, handler) {
|
|
112
|
-
return this.#events.prependListener(event, handler);
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
116
|
-
*
|
|
117
|
-
* @param {string|string[]} event - Event name.
|
|
118
|
-
* @param {handler} handler - The callback function.
|
|
119
|
-
* @returns {handler[]} - The wrapped handler used internally.
|
|
120
|
-
*/
|
|
121
|
-
prependListenerOnce(event, handler) {
|
|
122
|
-
return this.#events.prependListenerOnce(event, handler);
|
|
123
|
-
}
|
|
124
|
-
//////////////////////////////////////////////////////////////////////
|
|
125
|
-
/**
|
|
126
|
-
* Adds a event listener.
|
|
127
|
-
*
|
|
128
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
129
|
-
* @param {handler} handler - Callback function to be called when event fires.
|
|
130
|
-
*/
|
|
131
|
-
appendListener(event, handler) {
|
|
132
|
-
return this.#events.appendListener(event, handler);
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Registers an event listener that runs only once, then is removed.
|
|
136
|
-
*
|
|
137
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
138
|
-
* @param {handler} handler - The callback function to run on event.
|
|
139
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
140
|
-
*/
|
|
141
|
-
appendListenerOnce(event, handler) {
|
|
142
|
-
return this.#events.appendListenerOnce(event, handler);
|
|
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
|
-
on(event, handler) {
|
|
151
|
-
return this.#events.on(event, handler);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Registers an event listener that runs only once, then is removed.
|
|
155
|
-
*
|
|
156
|
-
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
157
|
-
* @param {handler} handler - The callback function to run on event.
|
|
158
|
-
* @returns {handler[]} - The wrapped version of the handler.
|
|
159
|
-
*/
|
|
160
|
-
once(event, handler) {
|
|
161
|
-
return this.#events.once(event, handler);
|
|
162
|
-
}
|
|
163
|
-
////////////////////////////////////////////////////////////////////
|
|
164
|
-
/**
|
|
165
|
-
* Removes a previously registered event listener.
|
|
166
|
-
*
|
|
167
|
-
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
168
|
-
* @param {handler} handler - The specific callback function to remove.
|
|
169
|
-
*/
|
|
170
|
-
off(event, handler) {
|
|
171
|
-
return this.#events.off(event, handler);
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Removes all event listeners of a specific type from the element.
|
|
175
|
-
*
|
|
176
|
-
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
177
|
-
*/
|
|
178
|
-
offAll(event) {
|
|
179
|
-
return this.#events.offAll(event);
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Removes all event listeners of all types from the element.
|
|
183
|
-
*/
|
|
184
|
-
offAllTypes() {
|
|
185
|
-
return this.#events.offAllTypes();
|
|
186
|
-
}
|
|
187
|
-
////////////////////////////////////////////////////////////
|
|
188
|
-
/**
|
|
189
|
-
* Returns the number of listeners for a given event.
|
|
190
|
-
*
|
|
191
|
-
* @param {string} event - The name of the event.
|
|
192
|
-
* @returns {number} Number of listeners for the event.
|
|
193
|
-
*/
|
|
194
|
-
listenerCount(event) {
|
|
195
|
-
return this.#events.listenerCount(event);
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
199
|
-
*
|
|
200
|
-
* @param {string} event - The name of the event.
|
|
201
|
-
* @returns {handler[]} Array of listener functions.
|
|
202
|
-
*/
|
|
203
|
-
listeners(event) {
|
|
204
|
-
return this.#events.listeners(event);
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* Returns a copy of the array of listeners for the specified event.
|
|
208
|
-
*
|
|
209
|
-
* @param {string} event - The name of the event.
|
|
210
|
-
* @returns {handler[]} Array of listener functions.
|
|
211
|
-
*/
|
|
212
|
-
onceListeners(event) {
|
|
213
|
-
return this.#events.onceListeners(event);
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Returns a copy of the internal listeners array for the specified event,
|
|
217
|
-
* including wrapper functions like those used by `.once()`.
|
|
218
|
-
* @param {string | symbol} event - The event name.
|
|
219
|
-
* @returns {handler[]} An array of raw listener functions.
|
|
220
|
-
*/
|
|
221
|
-
allListeners(event) {
|
|
222
|
-
return this.#events.allListeners(event);
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Returns an array of event names for which there are registered listeners.
|
|
226
|
-
*
|
|
227
|
-
* @returns {string[]} Array of registered event names.
|
|
228
|
-
*/
|
|
229
|
-
eventNames() {
|
|
230
|
-
return this.#events.eventNames();
|
|
231
|
-
}
|
|
232
|
-
//////////////////////////////////////////////////////
|
|
233
|
-
/**
|
|
234
|
-
* Emits an event, triggering all registered handlers for that event.
|
|
235
|
-
*
|
|
236
|
-
* @param {string} event - The event name to emit.
|
|
237
|
-
* @param {...any} payload - Optional data to pass to each handler.
|
|
238
|
-
* @returns {boolean[]} True if any listeners were called, false otherwise.
|
|
239
|
-
*/
|
|
240
|
-
emit(event, ...payload) {
|
|
241
|
-
return this.#events.emit(event, ...payload);
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Sets the maximum number of listeners per event before a warning is shown.
|
|
245
|
-
*
|
|
246
|
-
* @param {number} n - The maximum number of listeners.
|
|
247
|
-
*/
|
|
248
|
-
setMaxListeners(n) {
|
|
249
|
-
return this.#events.setMaxListeners(n);
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Gets the maximum number of listeners allowed per event.
|
|
253
|
-
*
|
|
254
|
-
* @returns {number} The maximum number of listeners.
|
|
255
|
-
*/
|
|
256
|
-
getMaxListeners() {
|
|
257
|
-
return this.#events.getMaxListeners();
|
|
258
|
-
}
|
|
259
|
-
///////////////////////////////////////////////////
|
|
87
|
+
class TinyLocalStorage extends EventEmitter {
|
|
260
88
|
/**
|
|
261
89
|
* Checks whether a JSON-serializable type is already registered.
|
|
262
90
|
*
|
|
@@ -372,6 +200,19 @@ class TinyLocalStorage {
|
|
|
372
200
|
return value;
|
|
373
201
|
}
|
|
374
202
|
//////////////////////////////////////////////////////
|
|
203
|
+
/**
|
|
204
|
+
* Tracks whether the instance has been destroyed.
|
|
205
|
+
* @type {boolean}
|
|
206
|
+
*/
|
|
207
|
+
#destroyed = false;
|
|
208
|
+
/**
|
|
209
|
+
* Gets the current destruction status of the instance.
|
|
210
|
+
*
|
|
211
|
+
* @returns {boolean} True if the instance is destroyed, false otherwise.
|
|
212
|
+
*/
|
|
213
|
+
get destroyed() {
|
|
214
|
+
return this.#destroyed;
|
|
215
|
+
}
|
|
375
216
|
/** @type {Storage} */
|
|
376
217
|
#localStorage = window.localStorage;
|
|
377
218
|
/** @type {string|null} */
|
|
@@ -387,6 +228,7 @@ class TinyLocalStorage {
|
|
|
387
228
|
* @param {string} [dbName] - Unique database name.
|
|
388
229
|
*/
|
|
389
230
|
constructor(dbName) {
|
|
231
|
+
super();
|
|
390
232
|
if (typeof dbName !== 'undefined' && typeof dbName !== 'string')
|
|
391
233
|
throw new TypeError('TinyLocalStorage: dbName must be a string if provided.');
|
|
392
234
|
if (typeof dbName === 'string')
|
|
@@ -417,6 +259,7 @@ class TinyLocalStorage {
|
|
|
417
259
|
* @throws {TypeError} If `onUpgrade` is not a function.
|
|
418
260
|
*/
|
|
419
261
|
updateStorageVersion(version, onUpgrade) {
|
|
262
|
+
checkDestroy(this.#destroyed);
|
|
420
263
|
if (typeof this.#dbKey !== 'string')
|
|
421
264
|
throw new Error('TinyLocalStorage: Database key is not initialized. Set a valid dbName in the constructor.');
|
|
422
265
|
if (typeof version !== 'number' || Number.isNaN(version) || version < 1)
|
|
@@ -441,6 +284,7 @@ class TinyLocalStorage {
|
|
|
441
284
|
* @returns {string|null} The database key, or null if not set.
|
|
442
285
|
*/
|
|
443
286
|
getDbKey() {
|
|
287
|
+
checkDestroy(this.#destroyed);
|
|
444
288
|
return this.#dbKey;
|
|
445
289
|
}
|
|
446
290
|
/**
|
|
@@ -449,6 +293,7 @@ class TinyLocalStorage {
|
|
|
449
293
|
* @returns {number} The current version number.
|
|
450
294
|
*/
|
|
451
295
|
getVersion() {
|
|
296
|
+
checkDestroy(this.#destroyed);
|
|
452
297
|
return this.#version;
|
|
453
298
|
}
|
|
454
299
|
/**
|
|
@@ -457,6 +302,7 @@ class TinyLocalStorage {
|
|
|
457
302
|
* @param {Storage} localstorage - A valid Storage object (localStorage or sessionStorage).
|
|
458
303
|
*/
|
|
459
304
|
setLocalStorage(localstorage) {
|
|
305
|
+
checkDestroy(this.#destroyed);
|
|
460
306
|
if (!(localstorage instanceof Storage))
|
|
461
307
|
throw new TypeError('Argument must be a valid instance of Storage.');
|
|
462
308
|
this.#localStorage = localstorage;
|
|
@@ -467,6 +313,7 @@ class TinyLocalStorage {
|
|
|
467
313
|
* @returns {boolean} True if `localStorage` exists, false otherwise.
|
|
468
314
|
*/
|
|
469
315
|
localStorageExists() {
|
|
316
|
+
checkDestroy(this.#destroyed);
|
|
470
317
|
return this.#localStorage instanceof Storage;
|
|
471
318
|
}
|
|
472
319
|
/**
|
|
@@ -490,6 +337,7 @@ class TinyLocalStorage {
|
|
|
490
337
|
* @param {LocalStorageJsonValue} data - The data to be serialized and stored.
|
|
491
338
|
*/
|
|
492
339
|
setJson(name, data) {
|
|
340
|
+
checkDestroy(this.#destroyed);
|
|
493
341
|
this.#isProtectedDbKey(name);
|
|
494
342
|
if (!isJsonObject(data) &&
|
|
495
343
|
!Array.isArray(data) &&
|
|
@@ -544,6 +392,7 @@ class TinyLocalStorage {
|
|
|
544
392
|
* @returns {LocalStorageJsonValue|null} The parsed object or fallback.
|
|
545
393
|
*/
|
|
546
394
|
getJson(name, defaultData) {
|
|
395
|
+
checkDestroy(this.#destroyed);
|
|
547
396
|
const { decoded, fallback } = this.#getJson(name, defaultData);
|
|
548
397
|
if (decoded instanceof Map ||
|
|
549
398
|
decoded instanceof Set ||
|
|
@@ -558,6 +407,7 @@ class TinyLocalStorage {
|
|
|
558
407
|
* @param {Date} data
|
|
559
408
|
*/
|
|
560
409
|
setDate(name, data) {
|
|
410
|
+
checkDestroy(this.#destroyed);
|
|
561
411
|
this.#isProtectedDbKey(name);
|
|
562
412
|
if (!(data instanceof Date))
|
|
563
413
|
throw new TypeError('Value must be a Date.');
|
|
@@ -571,6 +421,7 @@ class TinyLocalStorage {
|
|
|
571
421
|
* @returns {Date|null}
|
|
572
422
|
*/
|
|
573
423
|
getDate(name) {
|
|
424
|
+
checkDestroy(this.#destroyed);
|
|
574
425
|
const value = this.#getJson(name).decoded;
|
|
575
426
|
return value instanceof Date ? value : null;
|
|
576
427
|
}
|
|
@@ -580,6 +431,7 @@ class TinyLocalStorage {
|
|
|
580
431
|
* @param {RegExp} data
|
|
581
432
|
*/
|
|
582
433
|
setRegExp(name, data) {
|
|
434
|
+
checkDestroy(this.#destroyed);
|
|
583
435
|
this.#isProtectedDbKey(name);
|
|
584
436
|
if (!(data instanceof RegExp))
|
|
585
437
|
throw new TypeError('Value must be a RegExp.');
|
|
@@ -593,6 +445,7 @@ class TinyLocalStorage {
|
|
|
593
445
|
* @returns {RegExp|null}
|
|
594
446
|
*/
|
|
595
447
|
getRegExp(name) {
|
|
448
|
+
checkDestroy(this.#destroyed);
|
|
596
449
|
const value = this.#getJson(name).decoded;
|
|
597
450
|
return value instanceof RegExp ? value : null;
|
|
598
451
|
}
|
|
@@ -602,6 +455,7 @@ class TinyLocalStorage {
|
|
|
602
455
|
* @param {bigint} data
|
|
603
456
|
*/
|
|
604
457
|
setBigInt(name, data) {
|
|
458
|
+
checkDestroy(this.#destroyed);
|
|
605
459
|
this.#isProtectedDbKey(name);
|
|
606
460
|
if (typeof data !== 'bigint')
|
|
607
461
|
throw new TypeError('Value must be a BigInt.');
|
|
@@ -615,6 +469,7 @@ class TinyLocalStorage {
|
|
|
615
469
|
* @returns {bigint|null}
|
|
616
470
|
*/
|
|
617
471
|
getBigInt(name) {
|
|
472
|
+
checkDestroy(this.#destroyed);
|
|
618
473
|
const value = this.#getJson(name).decoded;
|
|
619
474
|
return typeof value === 'bigint' ? value : null;
|
|
620
475
|
}
|
|
@@ -625,6 +480,7 @@ class TinyLocalStorage {
|
|
|
625
480
|
* @param {symbol} data
|
|
626
481
|
*/
|
|
627
482
|
setSymbol(name, data) {
|
|
483
|
+
checkDestroy(this.#destroyed);
|
|
628
484
|
this.#isProtectedDbKey(name);
|
|
629
485
|
if (typeof data !== 'symbol')
|
|
630
486
|
throw new TypeError('Value must be a Symbol.');
|
|
@@ -638,6 +494,7 @@ class TinyLocalStorage {
|
|
|
638
494
|
* @returns {symbol|null}
|
|
639
495
|
*/
|
|
640
496
|
getSymbol(name) {
|
|
497
|
+
checkDestroy(this.#destroyed);
|
|
641
498
|
const value = this.#getJson(name).decoded;
|
|
642
499
|
return typeof value === 'symbol' ? value : null;
|
|
643
500
|
}
|
|
@@ -648,6 +505,7 @@ class TinyLocalStorage {
|
|
|
648
505
|
* @returns {any} The stored value or null if not found.
|
|
649
506
|
*/
|
|
650
507
|
getValue(name) {
|
|
508
|
+
checkDestroy(this.#destroyed);
|
|
651
509
|
return this.#getJson(name).decoded ?? null;
|
|
652
510
|
}
|
|
653
511
|
/**
|
|
@@ -657,6 +515,7 @@ class TinyLocalStorage {
|
|
|
657
515
|
* @param {any} data - The data to store.
|
|
658
516
|
*/
|
|
659
517
|
setItem(name, data) {
|
|
518
|
+
checkDestroy(this.#destroyed);
|
|
660
519
|
this.#isProtectedDbKey(name);
|
|
661
520
|
if (typeof name !== 'string' || !name.length)
|
|
662
521
|
throw new TypeError('Key must be a non-empty string.');
|
|
@@ -670,6 +529,7 @@ class TinyLocalStorage {
|
|
|
670
529
|
* @returns {string|null} The stored value or null if not found.
|
|
671
530
|
*/
|
|
672
531
|
getItem(name) {
|
|
532
|
+
checkDestroy(this.#destroyed);
|
|
673
533
|
if (typeof name !== 'string' || !name.length)
|
|
674
534
|
throw new TypeError('Key must be a non-empty string.');
|
|
675
535
|
return this.#localStorage.getItem(name);
|
|
@@ -681,6 +541,7 @@ class TinyLocalStorage {
|
|
|
681
541
|
* @param {string} data - The string to store.
|
|
682
542
|
*/
|
|
683
543
|
setString(name, data) {
|
|
544
|
+
checkDestroy(this.#destroyed);
|
|
684
545
|
this.#isProtectedDbKey(name);
|
|
685
546
|
if (typeof name !== 'string' || !name.length)
|
|
686
547
|
throw new TypeError('Key must be a non-empty string.');
|
|
@@ -699,6 +560,7 @@ class TinyLocalStorage {
|
|
|
699
560
|
* @returns {string|null} The string if valid, or null.
|
|
700
561
|
*/
|
|
701
562
|
getString(name) {
|
|
563
|
+
checkDestroy(this.#destroyed);
|
|
702
564
|
if (typeof name !== 'string' || !name.length)
|
|
703
565
|
throw new TypeError('Key must be a non-empty string.');
|
|
704
566
|
let value = this.#localStorage.getItem(name);
|
|
@@ -724,6 +586,7 @@ class TinyLocalStorage {
|
|
|
724
586
|
* @param {number} data - The number to store.
|
|
725
587
|
*/
|
|
726
588
|
setNumber(name, data) {
|
|
589
|
+
checkDestroy(this.#destroyed);
|
|
727
590
|
this.#isProtectedDbKey(name);
|
|
728
591
|
if (typeof name !== 'string' || !name.length)
|
|
729
592
|
throw new TypeError('Key must be a non-empty string.');
|
|
@@ -739,6 +602,7 @@ class TinyLocalStorage {
|
|
|
739
602
|
* @returns {number|null} The number or null if invalid.
|
|
740
603
|
*/
|
|
741
604
|
getNumber(name) {
|
|
605
|
+
checkDestroy(this.#destroyed);
|
|
742
606
|
if (typeof name !== 'string' || !name.length)
|
|
743
607
|
throw new TypeError('Key must be a non-empty string.');
|
|
744
608
|
/** @type {number|string|null} */
|
|
@@ -759,6 +623,7 @@ class TinyLocalStorage {
|
|
|
759
623
|
* @param {boolean} data - The boolean value to store.
|
|
760
624
|
*/
|
|
761
625
|
setBool(name, data) {
|
|
626
|
+
checkDestroy(this.#destroyed);
|
|
762
627
|
this.#isProtectedDbKey(name);
|
|
763
628
|
if (typeof name !== 'string' || !name.length)
|
|
764
629
|
throw new TypeError('Key must be a non-empty string.');
|
|
@@ -774,6 +639,7 @@ class TinyLocalStorage {
|
|
|
774
639
|
* @returns {boolean|null} The boolean or null if invalid.
|
|
775
640
|
*/
|
|
776
641
|
getBool(name) {
|
|
642
|
+
checkDestroy(this.#destroyed);
|
|
777
643
|
if (typeof name !== 'string' || !name.length)
|
|
778
644
|
throw new TypeError('Key must be a non-empty string.');
|
|
779
645
|
const value = this.#localStorage.getItem(name);
|
|
@@ -793,6 +659,7 @@ class TinyLocalStorage {
|
|
|
793
659
|
* @param {string} name - The key to remove.
|
|
794
660
|
*/
|
|
795
661
|
removeItem(name) {
|
|
662
|
+
checkDestroy(this.#destroyed);
|
|
796
663
|
if (typeof name !== 'string' || !name.length)
|
|
797
664
|
throw new TypeError('Key must be a non-empty string.');
|
|
798
665
|
this.emit('removeItem', name);
|
|
@@ -802,14 +669,19 @@ class TinyLocalStorage {
|
|
|
802
669
|
* Clears all data from `localStorage`.
|
|
803
670
|
*/
|
|
804
671
|
clearLocalStorage() {
|
|
672
|
+
checkDestroy(this.#destroyed);
|
|
805
673
|
return this.#localStorage.clear();
|
|
806
674
|
}
|
|
807
675
|
/**
|
|
808
676
|
* Destroys the storage instance by removing the storage event listener.
|
|
809
677
|
*/
|
|
810
678
|
destroy() {
|
|
679
|
+
if (this.#destroyed)
|
|
680
|
+
return;
|
|
811
681
|
window.removeEventListener('storage', this.#storageEvent);
|
|
812
|
-
this
|
|
682
|
+
this.removeAllListeners();
|
|
683
|
+
// Lock the instance
|
|
684
|
+
this.#destroyed = true;
|
|
813
685
|
}
|
|
814
686
|
}
|
|
815
687
|
// First registers
|