@inglorious/web 6.1.1 → 6.2.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,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/web",
3
- "version": "6.1.1",
3
+ "version": "6.2.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
5
  "author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
@@ -30,6 +30,10 @@
30
30
  "types": "./types/compass.d.ts",
31
31
  "import": "./src/compass/index.js"
32
32
  },
33
+ "./decorators/with-debounce": {
34
+ "types": "./types/decorators/with-debounce.d.ts",
35
+ "import": "./src/decorators/with-debounce/index.js"
36
+ },
33
37
  "./directives/*": {
34
38
  "types": "./types/directives/*.d.ts",
35
39
  "import": "./src/directives/*.js"
@@ -19,11 +19,8 @@ export function create(entity) {
19
19
  entity.isSupported = isCompassSupported()
20
20
  entity.manualOffset ??= 0
21
21
  entity._absoluteListener ??= null
22
+ entity._cancelTimeout ??= null
22
23
  entity._orientationListener ??= null
23
- // True once a deviceorientationabsolute event fires; used to drop the
24
- // fallback deviceorientation listener so only one path updates heading.
25
- entity._isUsingAbsolute ??= false
26
- entity.compassTimeout ??= NO_TIMEOUT
27
24
  }
28
25
 
29
26
  export function destroy(entity) {
@@ -44,16 +41,15 @@ export function compassUnwatch(entity) {
44
41
  )
45
42
  }
46
43
 
47
- if (entity.compassTimeout !== NO_TIMEOUT) {
48
- clearTimeout(entity.compassTimeout)
44
+ if (entity._cancelTimeout) {
45
+ entity._cancelTimeout()
49
46
  }
50
47
 
51
48
  entity.isLoading = false
52
49
  entity.isActive = false
53
- entity.compassTimeout = NO_TIMEOUT
54
50
  entity._orientationListener = null
55
51
  entity._absoluteListener = null
56
- entity._isUsingAbsolute = false
52
+ entity._cancelTimeout = null
57
53
  }
58
54
 
59
55
  export function compassError(entity, error) {
@@ -101,12 +97,30 @@ export function compassPermissionsGrant(entity) {
101
97
  }
102
98
 
103
99
  export function compassWatch(entity, _, api) {
104
- entity._isUsingAbsolute = false
100
+ // Snapshot values from the entity while the proxy is still live.
101
+ // Closures must not access `entity` after this handler returns, as
102
+ // Mutative revokes the draft proxy at that point.
103
+ const manualOffset = entity.manualOffset
104
+
105
+ let isUsingAbsolute = false
106
+ let timeoutId = NO_TIMEOUT
107
+
108
+ const cancelTimeout = () => {
109
+ if (timeoutId !== NO_TIMEOUT) {
110
+ clearTimeout(timeoutId)
111
+ timeoutId = NO_TIMEOUT
112
+ }
113
+ }
114
+
115
+ const orientationListener = createOrientationChangeListener(
116
+ cancelTimeout,
117
+ manualOffset,
118
+ api,
119
+ )
105
120
 
106
- const orientationListener = createOrientationChangeListener(entity, api)
107
121
  const absoluteListener = (event) => {
108
- if (!entity._isUsingAbsolute) {
109
- entity._isUsingAbsolute = true
122
+ if (!isUsingAbsolute) {
123
+ isUsingAbsolute = true
110
124
  getWindow()?.removeEventListener("deviceorientation", orientationListener)
111
125
  }
112
126
 
@@ -115,23 +129,16 @@ export function compassWatch(entity, _, api) {
115
129
 
116
130
  entity._orientationListener = orientationListener
117
131
  entity._absoluteListener = absoluteListener
132
+ entity._cancelTimeout = cancelTimeout
118
133
 
119
134
  const win = getWindow()
120
135
  win?.addEventListener("deviceorientationabsolute", absoluteListener)
121
136
  win?.addEventListener("deviceorientation", orientationListener)
122
137
 
123
- entity.compassTimeout = globalThis.setTimeout(
124
- () => checkTimeout(entity, api),
125
- COMPASS_TIMEOUT,
126
- )
127
- }
128
-
129
- function checkTimeout(entity, api) {
130
- entity.compassTimeout = NO_TIMEOUT
131
-
132
- if (!entity.heading) {
138
+ timeoutId = globalThis.setTimeout(() => {
139
+ timeoutId = NO_TIMEOUT
133
140
  api.notify("compassActiveChange", false)
134
- }
141
+ }, COMPASS_TIMEOUT)
135
142
  }
136
143
 
137
144
  export function compassActiveChange(entity, value) {
@@ -143,22 +150,18 @@ export function compassHeadingChange(entity, value) {
143
150
  entity.heading = value
144
151
  }
145
152
 
146
- function createOrientationChangeListener(entity, api) {
153
+ function createOrientationChangeListener(cancelTimeout, manualOffset, api) {
147
154
  return (event) => {
148
155
  const heading = event?.alpha
149
156
  if (typeof heading !== "number") {
150
157
  return
151
158
  }
152
159
 
153
- if (entity.compassTimeout !== NO_TIMEOUT) {
154
- clearTimeout(entity.compassTimeout)
155
- entity.compassTimeout = NO_TIMEOUT
156
- }
160
+ cancelTimeout()
157
161
 
158
162
  const screenAngle = getScreenAngle()
159
163
  const adjustedHeading =
160
- (((heading - screenAngle + entity.manualOffset) % FULL_CIRCLE) +
161
- FULL_CIRCLE) %
164
+ (((heading - screenAngle + manualOffset) % FULL_CIRCLE) + FULL_CIRCLE) %
162
165
  FULL_CIRCLE
163
166
 
164
167
  api.notify("compassActiveChange", true)
@@ -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,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
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
  /**