@inglorious/web 6.1.2 → 6.3.0

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/LICENSE CHANGED
@@ -1,9 +1,9 @@
1
- The MIT License (MIT)
2
-
3
- Copyright © 2025 Inglorious Coderz Srl.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
-
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
9
- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2025 Inglorious Coderz Srl.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json CHANGED
@@ -1,8 +1,15 @@
1
1
  {
2
2
  "name": "@inglorious/web",
3
- "version": "6.1.2",
3
+ "version": "6.3.0",
4
4
  "description": "A new web framework that leverages the power of the Inglorious Store combined with the performance and simplicity of lit-html.",
5
- "author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
5
+ "author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
+ "contributors": [
7
+ {
8
+ "name": "Luis Cruz",
9
+ "email": "stackbuilder1228@gmail.com",
10
+ "url": "https://topstar-ai.github.io/"
11
+ }
12
+ ],
6
13
  "license": "MIT",
7
14
  "repository": {
8
15
  "type": "git",
@@ -30,10 +37,18 @@
30
37
  "types": "./types/compass.d.ts",
31
38
  "import": "./src/compass/index.js"
32
39
  },
40
+ "./decorators/with-debounce": {
41
+ "types": "./types/decorators/with-debounce.d.ts",
42
+ "import": "./src/decorators/with-debounce/index.js"
43
+ },
33
44
  "./directives/*": {
34
45
  "types": "./types/directives/*.d.ts",
35
46
  "import": "./src/directives/*.js"
36
47
  },
48
+ "./element-size": {
49
+ "types": "./types/element-size.d.ts",
50
+ "import": "./src/element-size/index.js"
51
+ },
37
52
  "./form": {
38
53
  "types": "./types/form.d.ts",
39
54
  "import": "./src/form/index.js"
@@ -0,0 +1,85 @@
1
+ import { debounce } from "@inglorious/utils/functions/functions.js"
2
+
3
+ /**
4
+ * Wraps selected event handlers of a type with debouncing, scoped per-entity.
5
+ *
6
+ * IMPORTANT: because state is managed with `mutative`, the `entity` object
7
+ * passed into a handler is a draft proxy that gets revoked as soon as the
8
+ * current update tick finishes. Debounced calls fire *later*, on their own
9
+ * timer, well after that draft is dead. So we must never close over `entity`
10
+ * itself across the debounce boundary — only its primitive `id`. When the
11
+ * debounced callback actually runs, we re-fetch a live entity from the
12
+ * current state via `api.getEntity(entityId)`. Primitives and `payload` are safe to
13
+ * hold onto as-is; only draft/entity references are not.
14
+ *
15
+ * @param {number|Object} config - Either a single delay (ms) applied to all
16
+ * handlers listed in `handlerNames`, or a map of handlerName -> delay (ms).
17
+ * @param {string[]} [handlerNames] - Handler names to debounce. Required when
18
+ * `config` is a single number; ignored when `config` is a map (its keys
19
+ * are used instead).
20
+ *
21
+ * @example
22
+ * // same delay for every listed handler
23
+ * const types = {
24
+ * AutosaveForm: [Form, withDebounce(500, ["saveData"])],
25
+ * }
26
+ *
27
+ * @example
28
+ * // per-handler delays
29
+ * const types = {
30
+ * SearchBox: [Combobox, withDebounce({ optionsLoad: 300, saveQuery: 800 })],
31
+ * }
32
+ */
33
+ export function withDebounce(config, handlerNames) {
34
+ const delays =
35
+ typeof config === "number"
36
+ ? Object.fromEntries(handlerNames.map((name) => [name, config]))
37
+ : config
38
+
39
+ return (type) => {
40
+ // one debounced-fn map per entity, so instances don't share timers
41
+ const debouncedByEntity = new Map()
42
+
43
+ const behavior = {}
44
+
45
+ for (const handlerName of Object.keys(delays)) {
46
+ const originalHandler = type[handlerName]
47
+ if (!originalHandler) continue // nothing to wrap if the base type doesn't define it
48
+
49
+ behavior[handlerName] = (entity, payload, api) => {
50
+ const entityId = entity.id // capture the primitive now, while the draft is live
51
+
52
+ let perEntity = debouncedByEntity.get(entityId)
53
+ if (!perEntity) {
54
+ perEntity = {}
55
+ debouncedByEntity.set(entityId, perEntity)
56
+ }
57
+
58
+ if (!perEntity[handlerName]) {
59
+ perEntity[handlerName] = debounce((entityId, payload, api) => {
60
+ const liveEntity = api.getEntity(entityId) // fresh, live reference
61
+ if (!liveEntity) return // entity was destroyed before the debounce fired
62
+ originalHandler(liveEntity, payload, api)
63
+ }, delays[handlerName])
64
+ }
65
+
66
+ perEntity[handlerName](entityId, payload, api)
67
+ }
68
+ }
69
+
70
+ // clean up any pending timers when the entity is destroyed
71
+ behavior.destroy = (entity, payload, api) => {
72
+ const entityId = entity.id // same rule applies here: capture before it's gone
73
+
74
+ const perEntity = debouncedByEntity.get(entityId)
75
+ if (perEntity) {
76
+ Object.values(perEntity).forEach((d) => d.cancel())
77
+ debouncedByEntity.delete(entityId)
78
+ }
79
+
80
+ type.destroy?.(entity, payload, api)
81
+ }
82
+
83
+ return behavior
84
+ }
85
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * @typedef {import("../../types/element-size.js").ElementSizeEntity} ElementSizeEntity
3
+ * @typedef {import("../../types/element-size.js").ElementSizeSize} ElementSizeSize
4
+ */
5
+
6
+ // Observers aren't serializable, so they're kept here by id, not on the entity.
7
+ const observers = new Map()
8
+
9
+ /**
10
+ * Initializes the entity state and starts observing the target element's size.
11
+ * @param {ElementSizeEntity} entity - The element size entity.
12
+ * @param {{ notify: Function }} api - The store API.
13
+ */
14
+ export function create(entity, _, api) {
15
+ entity.width ??= null
16
+ entity.height ??= null
17
+ entity.isWatching ??= false
18
+
19
+ elementSizeWatch(entity, _, api)
20
+ }
21
+
22
+ /**
23
+ * Disconnects the observer before the entity is destroyed.
24
+ * @param {ElementSizeEntity} entity - The element size entity.
25
+ */
26
+ export function destroy(entity) {
27
+ elementSizeUnwatch(entity)
28
+ }
29
+
30
+ /**
31
+ * Starts observing the target element for size changes.
32
+ * @param {ElementSizeEntity} entity - The element size entity.
33
+ * @param {{ notify: Function }} api - The store API.
34
+ */
35
+ export function elementSizeWatch(entity, _, api) {
36
+ entity.isSupported = isSupported()
37
+
38
+ if (!entity.isSupported || observers.has(entity.id)) {
39
+ return
40
+ }
41
+
42
+ const element = getElement(entity)
43
+
44
+ if (!element) {
45
+ return
46
+ }
47
+
48
+ // Snapshot the id while the draft proxy is still live: the callback below
49
+ // runs after this handler returns, when Mutative has revoked the draft.
50
+ const id = entity.id
51
+
52
+ const observer = new globalThis.ResizeObserver((entries) => {
53
+ api.notify(`#${id}:elementSizeChange`, getSize(entries))
54
+ })
55
+ observer.observe(element)
56
+
57
+ observers.set(id, observer)
58
+ entity.isWatching = true
59
+ }
60
+
61
+ /**
62
+ * Stores the latest observed dimensions.
63
+ * @param {ElementSizeEntity} entity - The element size entity.
64
+ * @param {ElementSizeSize} size - The observed dimensions.
65
+ */
66
+ export function elementSizeChange(entity, size) {
67
+ entity.width = size.width
68
+ entity.height = size.height
69
+ }
70
+
71
+ /**
72
+ * Stops observing size changes and drops the observer from the registry.
73
+ * @param {ElementSizeEntity} entity - The element size entity.
74
+ */
75
+ export function elementSizeUnwatch(entity) {
76
+ const observer = observers.get(entity.id)
77
+
78
+ if (observer) {
79
+ observer.disconnect()
80
+ observers.delete(entity.id)
81
+ }
82
+
83
+ entity.isWatching = false
84
+ }
85
+
86
+ function getSize(entries) {
87
+ const [entry] = entries
88
+ const rect = entry.contentRect
89
+ return { width: rect.width, height: rect.height }
90
+ }
91
+
92
+ function getElement(entity) {
93
+ const doc = globalThis.document
94
+
95
+ if (!doc) {
96
+ return null
97
+ }
98
+
99
+ const selected = entity.selector ? doc.querySelector(entity.selector) : null
100
+ return selected ?? doc.documentElement
101
+ }
102
+
103
+ function isSupported() {
104
+ return (
105
+ typeof globalThis.ResizeObserver !== "undefined" &&
106
+ Boolean(globalThis.document?.documentElement)
107
+ )
108
+ }
@@ -0,0 +1,3 @@
1
+ import * as handlers from "./handlers.js"
2
+
3
+ export const ElementSize = { ...handlers }
@@ -0,0 +1,13 @@
1
+ import { Api } from "../mount"
2
+
3
+ export interface DebounceBehavior {
4
+ destroy?: (entity: any, payload?: any, api?: Api) => void
5
+ [handlerName: string]:
6
+ | ((entity: any, payload?: any, api?: Api) => void)
7
+ | undefined
8
+ }
9
+
10
+ export declare function withDebounce(
11
+ config: number | Record<string, number>,
12
+ handlerNames?: string[],
13
+ ): <T extends Record<string, any>>(type: T) => DebounceBehavior
@@ -0,0 +1,26 @@
1
+ import { Api } from "./mount"
2
+
3
+ export interface ElementSizeEntity {
4
+ height: number | null
5
+ id: string | number
6
+ isSupported: boolean
7
+ isWatching: boolean
8
+ selector?: string | null
9
+ type?: "ElementSize"
10
+ width: number | null
11
+ }
12
+
13
+ export interface ElementSizeSize {
14
+ height: number
15
+ width: number
16
+ }
17
+
18
+ export interface ElementSizeType {
19
+ create(entity: ElementSizeEntity, payload: unknown, api: Api): void
20
+ destroy(entity: ElementSizeEntity): void
21
+ elementSizeChange(entity: ElementSizeEntity, size: ElementSizeSize): void
22
+ elementSizeWatch(entity: ElementSizeEntity, payload: unknown, api: Api): void
23
+ elementSizeUnwatch(entity: ElementSizeEntity): void
24
+ }
25
+
26
+ export declare const ElementSize: ElementSizeType
package/types/form.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Api } from "./mount"
2
+
1
3
  /**
2
4
  * Represents the structure of form values, which can be a nested object.
3
5
  */
@@ -286,7 +288,7 @@ export interface FormType {
286
288
  validateAsync<T extends FormValues>(
287
289
  entity: FormEntity<T>,
288
290
  payload: FormValidateAsyncPayload<T>,
289
- api: any,
291
+ api: Api,
290
292
  ): Promise<void>
291
293
 
292
294
  /**