@splendidlabz/utils 1.5.0-beta.5 → 1.5.0-beta.6
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/CHANGELOG.md +6 -0
- package/dom/observers/resize-observer.js +33 -3
- package/lib/index.js +1 -0
- package/lib/sse.js +64 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,10 +2,40 @@
|
|
|
2
2
|
import { dispatchEvent } from '../events.js'
|
|
3
3
|
import { useObserverMethodOnTarget } from './observer.js'
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Creates and manages a ResizeObserver instance to monitor size changes of a target element.
|
|
7
|
+
*
|
|
8
|
+
* @param {Element|Window|NodeList|Element[]} target - The element(s) to observe.
|
|
9
|
+
* - If window is provided, document.body will be observed instead.
|
|
10
|
+
* - If NodeList or Array of elements is provided, all elements will be observed.
|
|
11
|
+
* @param {Object} options - Configuration options for the resize observer
|
|
12
|
+
* @param {boolean} [options.observe=true] - Whether to start observing immediately. If false, the observer won't be created.
|
|
13
|
+
* @param {Function} [options.callback] - Optional callback function that will be called when resize changes are detected.
|
|
14
|
+
* If not provided, a 'resize-obs' event will be dispatched on the target.
|
|
15
|
+
* @param {Object} [options...] - Additional options to pass to ResizeObserver.observe()
|
|
16
|
+
*
|
|
17
|
+
* @returns {Object} An object with methods to control the observer:
|
|
18
|
+
* - observe(target, options): Start observing a new target element
|
|
19
|
+
* - unobserve(target): Stop observing a target element
|
|
20
|
+
* - disconnect(): Disconnect the observer and stop all observations
|
|
21
|
+
* - destroy(): Alias for disconnect()
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* // Basic usage with callback
|
|
25
|
+
* resizeObserver(element, {
|
|
26
|
+
* callback: ({ entry, entries, observer }) => {
|
|
27
|
+
* console.log('Element resized:', entry.contentRect);
|
|
28
|
+
* }
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // Usage with event listener
|
|
33
|
+
* resizeObserver(element);
|
|
34
|
+
* element.addEventListener('resize-obs', ({ detail }) => {
|
|
35
|
+
* console.log('Element resized:', detail.entry.contentRect);
|
|
36
|
+
* });
|
|
37
|
+
*/
|
|
5
38
|
export function resizeObserver(target, options) {
|
|
6
|
-
// We set the option here to prevent observer from being created unless necessary.
|
|
7
|
-
options = { observe: true, ...options }
|
|
8
|
-
if (!options.observe) return
|
|
9
39
|
const { callback, ...opts } = options
|
|
10
40
|
const observer = new ResizeObserver(observerFn)
|
|
11
41
|
|
package/lib/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export * from './functions/index.js'
|
|
|
7
7
|
export * from './numbers/index.js'
|
|
8
8
|
export * from './objects/index.js'
|
|
9
9
|
export * from './promises/index.js'
|
|
10
|
+
export * from './sse.js'
|
|
10
11
|
export * from './strings/index.js'
|
|
11
12
|
export * from './style/index.js'
|
|
12
13
|
export * from './symbols/index.js'
|
package/lib/sse.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Server Sent Events (SSE)
|
|
2
|
+
import { omitEmpty } from './objects/omit-empty.js'
|
|
3
|
+
|
|
4
|
+
export function createSSE({ event, data, id, retry }) {
|
|
5
|
+
const eventString = event ? `event: ${event}\n` : ''
|
|
6
|
+
const idString = id ? `id: ${id}\n` : ''
|
|
7
|
+
const retryString = retry ? `retry: ${retry}\n` : ''
|
|
8
|
+
|
|
9
|
+
if (typeof data === 'object') data = JSON.stringify(data)
|
|
10
|
+
const dataString = data ? `data: ${data}\n` : ''
|
|
11
|
+
|
|
12
|
+
return `${eventString}${dataString}${idString}${retryString}\n`
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parses a single SSE message into a structured object
|
|
17
|
+
* @param {string} message - The raw SSE message
|
|
18
|
+
* @returns {Object|null} The parsed message or null if invalid
|
|
19
|
+
* @property {string} [event] - The event type if specified
|
|
20
|
+
* @property {string|Object} data - The message data (parsed as JSON if possible)
|
|
21
|
+
* @property {string} [id] - The message ID if specified
|
|
22
|
+
* @property {number} [retry] - The retry interval if specified
|
|
23
|
+
*/
|
|
24
|
+
export function parseSSE(message) {
|
|
25
|
+
// console.log('parsing SSE', message);
|
|
26
|
+
const lines = message.split('\n')
|
|
27
|
+
const result = {
|
|
28
|
+
event: 'message', // Default event type
|
|
29
|
+
data: '',
|
|
30
|
+
id: null,
|
|
31
|
+
retry: null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (const line of lines) {
|
|
35
|
+
if (line.startsWith('data:')) {
|
|
36
|
+
const data = line.slice(5).trim()
|
|
37
|
+
// Try to parse as JSON
|
|
38
|
+
try { result.data = JSON.parse(data) }
|
|
39
|
+
// If not JSON, use as string
|
|
40
|
+
catch { result.data = data }
|
|
41
|
+
continue
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (line.startsWith('event:')) {
|
|
45
|
+
result.event = line.slice(6).trim()
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (line.startsWith('id:')) {
|
|
50
|
+
result.id = line.slice(3).trim()
|
|
51
|
+
continue
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (line.startsWith('retry:')) {
|
|
55
|
+
result.retry = parseInt(line.slice(6).trim(), 10)
|
|
56
|
+
continue
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
const ret = omitEmpty(result)
|
|
62
|
+
// Only return if we have data
|
|
63
|
+
return ret.data ? ret : null
|
|
64
|
+
}
|