tiny-essentials 1.12.2 โ†’ 1.13.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,198 @@
1
+ # ๐Ÿ•’ TinyDomReadyManager
2
+
3
+ A flexible, customizable JavaScript class to execute code **only when the DOM is ready**, or when additional asynchronous requirements (like Promises or asset loads) are met.
4
+ Supports **prioritized callbacks**, **filters**, and now even **DOM-only fast execution**!
5
+
6
+ ---
7
+
8
+ ## ๐Ÿง  Type Definitions
9
+
10
+ ### `Fn` ๐Ÿ› ๏ธ
11
+
12
+ ```ts
13
+ type Fn = () => void;
14
+ ```
15
+
16
+ A basic function to be executed once the system is ready.
17
+
18
+ ---
19
+
20
+ ### `FnFilter` ๐Ÿ”
21
+
22
+ ```ts
23
+ type FnFilter = () => boolean;
24
+ ```
25
+
26
+ A conditional function that decides whether a handler should be executed.
27
+ Returns `true` to allow execution, or `false` to skip.
28
+
29
+ ---
30
+
31
+ ### `Handler` ๐Ÿงฉ
32
+
33
+ ```ts
34
+ interface Handler {
35
+ fn: Fn;
36
+ once: boolean;
37
+ priority: number;
38
+ filter: FnFilter | null;
39
+ domOnly: boolean;
40
+ }
41
+ ```
42
+
43
+ Describes a registered handler and how it should be treated during readiness flow.
44
+
45
+ ---
46
+
47
+ ## ๐Ÿš€ Class: `TinyDomReadyManager`
48
+
49
+ ### `init()` ๐Ÿ•“
50
+
51
+ ```ts
52
+ init(): void
53
+ ```
54
+
55
+ Initializes the manager and waits for the `DOMContentLoaded` event.
56
+ If the DOM is already ready, it triggers immediately.
57
+
58
+ * โš ๏ธ Throws an `Error` if called more than once.
59
+
60
+ ---
61
+
62
+ ### `addPromise(promise)` โณ
63
+
64
+ ```ts
65
+ addPromise(promise: Promise<any>): void
66
+ ```
67
+
68
+ Adds a custom `Promise` that delays full readiness.
69
+ This allows tasks like `fetch()` or image preloading to block late-stage handlers.
70
+
71
+ * โš ๏ธ Throws a `TypeError` if `promise` is not a real `Promise`.
72
+
73
+ ---
74
+
75
+ ### `onReady(fn, options?)` โœ…
76
+
77
+ ```ts
78
+ onReady(fn: Fn, options?: {
79
+ once?: boolean;
80
+ priority?: number;
81
+ filter?: FnFilter | null;
82
+ domOnly?: boolean;
83
+ }): void
84
+ ```
85
+
86
+ Registers a function to run at the right moment:
87
+
88
+ | Option | Type | Default | Description |
89
+ | ---------- | ------------------ | ------- | ------------------------------------------------------------------------ |
90
+ | `once` | `boolean` | `true` | Whether to execute only once |
91
+ | `priority` | `number` | `0` | Higher values run earlier |
92
+ | `filter` | `FnFilter \| null` | `null` | Optional condition before executing |
93
+ | `domOnly` | `boolean` | `false` | ๐Ÿš€ If `true`, runs immediately after **DOM is ready**, skipping promises |
94
+
95
+ * โš ๏ธ Throws a `TypeError` if `fn` is not a function.
96
+
97
+ ---
98
+
99
+ ### `isReady()` ๐Ÿ“ก
100
+
101
+ ```ts
102
+ isReady(): boolean
103
+ ```
104
+
105
+ Returns `true` only after both the DOM is ready **and** all added promises have resolved.
106
+
107
+ ---
108
+
109
+ ### `isDomReady()` ๐Ÿงฑ
110
+
111
+ ```ts
112
+ isDomReady(): boolean
113
+ ```
114
+
115
+ Returns `true` if the DOM is already ready (i.e., `DOMContentLoaded` has fired).
116
+ Returns `false` if the DOM is still loading.
117
+
118
+ > โœ… Useful when you want to know *just* when the document is interactive, regardless of async operations.
119
+
120
+ ---
121
+
122
+ ## ๐Ÿงช Readiness Phases
123
+
124
+ | Phase | Description |
125
+ | --------------- | ---------------------------------------- |
126
+ | **DOM Ready** | Triggers `domOnly: true` handlers |
127
+ | **Fully Ready** | Triggers all normal `onReady()` handlers |
128
+
129
+ ---
130
+
131
+ ## ๐Ÿ’ก Example Usage
132
+
133
+ ### Basic Usage
134
+
135
+ ```js
136
+ import TinyDomReadyManager from './TinyDomReadyManager.js';
137
+
138
+ const manager = new TinyDomReadyManager();
139
+
140
+ manager.onReady(() => {
141
+ console.log('Ready after DOM + Promises!');
142
+ });
143
+
144
+ manager.addPromise(fetch('/config.json'));
145
+ manager.init();
146
+ ```
147
+
148
+ ---
149
+
150
+ ### DOM-Only Handler (Faster)
151
+
152
+ ```js
153
+ manager.onReady(() => {
154
+ console.log('DOM is ready, skipping Promises!');
155
+ }, { domOnly: true, priority: 100 });
156
+ ```
157
+
158
+ ---
159
+
160
+ ### Conditional + Prioritized
161
+
162
+ ```js
163
+ manager.onReady(() => {
164
+ console.log('Only runs if in dark mode');
165
+ }, {
166
+ priority: 5,
167
+ filter: () => document.documentElement.classList.contains('dark')
168
+ });
169
+ ```
170
+
171
+ ---
172
+
173
+ ### Mixing Both Types
174
+
175
+ ```js
176
+ manager.onReady(() => {
177
+ console.log('DOM-only logic A');
178
+ }, { domOnly: true, priority: 10 });
179
+
180
+ manager.onReady(() => {
181
+ console.log('DOM-only logic B');
182
+ }, { domOnly: true, priority: 5 });
183
+
184
+ manager.onReady(() => {
185
+ console.log('Waits for API call');
186
+ });
187
+
188
+ manager.addPromise(fetch('/api/user'));
189
+ manager.init();
190
+ ```
191
+
192
+ ---
193
+
194
+ ## ๐Ÿ“Œ Notes
195
+
196
+ * `domOnly` handlers are ideal for UI setup or event binding.
197
+ * Regular handlers are best for things that depend on data loading or external states.
198
+ * All handlers are executed safely, and exceptions are caught with warning logs.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-essentials",
3
- "version": "1.12.2",
3
+ "version": "1.13.1",
4
4
  "description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
5
5
  "scripts": {
6
6
  "test": "npm run test:mjs && npm run test:cjs && npm run test:js",