@inglorious/web 6.30.0 → 6.31.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/web",
3
- "version": "6.30.0",
3
+ "version": "6.31.1",
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": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "contributors": [
@@ -169,18 +169,18 @@
169
169
  },
170
170
  "sideEffects": false,
171
171
  "dependencies": {
172
- "@inglorious/utils": "^5.4.0",
172
+ "@inglorious/utils": "^5.4.1",
173
173
  "@lit-labs/ssr-client": "^1.1.8",
174
174
  "lit-html": "^3.3.3"
175
175
  },
176
176
  "peerDependencies": {
177
- "@inglorious/store": "^12.7.0"
177
+ "@inglorious/store": "^12.9.1"
178
178
  },
179
179
  "devDependencies": {
180
180
  "@inglorious/eslint-config": "1.2.0",
181
- "prettier": "^3.9.10",
181
+ "prettier": "^3.10.1",
182
182
  "typescript": "~7.0.2",
183
- "vitest": "^5.1.0"
183
+ "vitest": "^5.1.1"
184
184
  },
185
185
  "engines": {
186
186
  "node": ">= 26"
@@ -18,6 +18,9 @@ const DENIED = "denied"
18
18
  *
19
19
  * Everything vendor-specific is the `adapter`'s:
20
20
  *
21
+ * - `isSupported()`, optional: `false` when there is nothing to measure with,
22
+ * such as an analytics project not set up yet. Then the prompt never shows
23
+ * and nothing reaches the adapter.
21
24
  * - `setConsent(isGranted)`: starts or stops measuring.
22
25
  * - `pageView({ route, path })`: one page or screen. An adapter whose
23
26
  * library already counts history changes as page views can do nothing.
@@ -35,15 +38,20 @@ const DENIED = "denied"
35
38
  * api.notify("#analytics:analyticsTrack", { name: "sign_up" })
36
39
  */
37
40
  export function createAnalytics({ adapter }) {
41
+ const isSupported = () => adapter.isSupported?.() !== false
42
+
38
43
  return {
39
44
  create(entity) {
40
45
  entity.consent ??= null
41
- entity.isAsking ??= entity.consent === null
42
- if (entity.consent === GRANTED) call(() => adapter.setConsent(true))
46
+ entity.isAsking =
47
+ isSupported() && (entity.isAsking ?? entity.consent === null)
48
+ if (entity.consent === GRANTED && isSupported()) {
49
+ call(() => adapter.setConsent(true))
50
+ }
43
51
  },
44
52
 
45
53
  routeChange(entity, { route, path } = {}) {
46
- if (entity.consent !== GRANTED) return
54
+ if (entity.consent !== GRANTED || !isSupported()) return
47
55
  call(() => adapter.pageView({ route, path }))
48
56
  },
49
57
 
@@ -51,6 +59,7 @@ export function createAnalytics({ adapter }) {
51
59
  const consent = isGranted ? GRANTED : DENIED
52
60
  entity.consent = consent
53
61
  entity.isAsking = false
62
+ if (!isSupported()) return
54
63
  call(() => adapter.setConsent(isGranted))
55
64
  if (!isGranted) return
56
65
 
@@ -61,11 +70,11 @@ export function createAnalytics({ adapter }) {
61
70
  },
62
71
 
63
72
  analyticsAsk(entity) {
64
- entity.isAsking = true
73
+ entity.isAsking = isSupported()
65
74
  },
66
75
 
67
76
  analyticsTrack(entity, { name, params } = {}) {
68
- if (entity.consent !== GRANTED || !name) return
77
+ if (entity.consent !== GRANTED || !name || !isSupported()) return
69
78
  call(() => adapter.event(name, params))
70
79
  },
71
80
  }
@@ -62,14 +62,15 @@ export function createGeolocation({ plugin } = {}) {
62
62
  entity.error = null
63
63
  entity.isLoading = false
64
64
  entity.position = {
65
+ // A plugin may leave the optional readings out: the entity says null.
65
66
  coords: {
66
67
  accuracy: position.coords.accuracy,
67
- altitude: position.coords.altitude,
68
- altitudeAccuracy: position.coords.altitudeAccuracy,
69
- heading: position.coords.heading,
68
+ altitude: position.coords.altitude ?? null,
69
+ altitudeAccuracy: position.coords.altitudeAccuracy ?? null,
70
+ heading: position.coords.heading ?? null,
70
71
  latitude: position.coords.latitude,
71
72
  longitude: position.coords.longitude,
72
- speed: position.coords.speed,
73
+ speed: position.coords.speed ?? null,
73
74
  },
74
75
  timestamp: position.timestamp,
75
76
  }
@@ -19,9 +19,9 @@ import { isNative } from "../../native/index.js"
19
19
  export function createNativeStorage({ plugin } = {}) {
20
20
  if (!plugin || !isNative()) {
21
21
  return {
22
- getItem: (key) => globalThis.localStorage?.getItem(key) ?? null,
23
- setItem: (key, value) => globalThis.localStorage?.setItem(key, value),
24
- removeItem: (key) => globalThis.localStorage?.removeItem(key),
22
+ getItem: (key) => browserStorage()?.getItem(key) ?? null,
23
+ setItem: (key, value) => browserStorage()?.setItem(key, value),
24
+ removeItem: (key) => browserStorage()?.removeItem(key),
25
25
  }
26
26
  }
27
27
 
@@ -59,3 +59,8 @@ export function createSecureStorage({ plugin } = {}) {
59
59
  removeItem: (key) => plugin.deleteData({ key }),
60
60
  }
61
61
  }
62
+
63
+ // Never Node's own localStorage: on a server it would be shared by every request.
64
+ function browserStorage() {
65
+ return globalThis.window?.localStorage
66
+ }
@@ -2,6 +2,8 @@ import { Api } from "./mount"
2
2
 
3
3
  /** Everything vendor-specific about measuring an app. */
4
4
  export interface AnalyticsAdapter {
5
+ /** `false` when there is nothing to measure with: the prompt never shows. */
6
+ isSupported?(): boolean
5
7
  /** Starts or stops measuring. Not called before the user answered. */
6
8
  setConsent(isGranted: boolean): void | Promise<void>
7
9
  /** One page or screen, on every route change once granted. */
@@ -6,16 +6,20 @@ export interface LiveSource {
6
6
  subscribe(table: string, onChange: () => void): () => void
7
7
  }
8
8
 
9
- export interface LiveReloadConfig<TEntity = any> {
9
+ /**
10
+ * `TApi` is the app's own api, such as `AppApi`, so `payload` and `isLive`
11
+ * can read its entities with their types.
12
+ */
13
+ export interface LiveReloadConfig<TEntity = any, TApi extends Api = Api> {
10
14
  source: LiveSource
11
15
  /** The tables the entity reads. */
12
16
  tables: string[]
13
17
  /** The event that reloads the entity. Defaults to `"load"`. */
14
18
  event?: string
15
19
  /** What the event carries, such as the id of the record the entity shows. */
16
- payload?: (entity: TEntity, api: Api) => unknown
20
+ payload?: (entity: TEntity, api: TApi) => unknown
17
21
  /** Whether the entity is on screen. Defaults to always. */
18
- isLive?: (entity: TEntity, api: Api) => boolean
22
+ isLive?: (entity: TEntity, api: TApi) => boolean
19
23
  /** Gathers a burst of changes into one reload. Defaults to 300 ms. */
20
24
  delay?: number
21
25
  }
@@ -25,6 +29,6 @@ export type LiveReloadBehavior = {
25
29
  destroy: (entity: any, payload?: any, api?: Api) => void
26
30
  }
27
31
 
28
- export declare function withLiveReload<TEntity = any>(
29
- config: LiveReloadConfig<TEntity>,
32
+ export declare function withLiveReload<TEntity = any, TApi extends Api = Api>(
33
+ config: LiveReloadConfig<TEntity, TApi>,
30
34
  ): <T extends Record<string, any>>(type: T) => LiveReloadBehavior
@@ -52,17 +52,35 @@ export type GeolocationType = {
52
52
  geolocationUnwatch(entity: GeolocationEntity): void
53
53
  geolocationSuccess(
54
54
  entity: GeolocationEntity,
55
- position: GeolocationPosition,
55
+ position: GeolocationPosition | PluginPosition,
56
56
  ): void
57
57
  geolocationError(entity: GeolocationEntity, error: GeolocationError): void
58
58
  }
59
59
 
60
+ /**
61
+ * A position as a plugin may report it: the optional readings can be missing
62
+ * altogether, as `@capacitor/geolocation` leaves them. The entity stores them
63
+ * as `null`.
64
+ */
65
+ export interface PluginPosition {
66
+ coords: {
67
+ accuracy: number
68
+ altitude?: number | null
69
+ altitudeAccuracy?: number | null
70
+ heading?: number | null
71
+ latitude: number
72
+ longitude: number
73
+ speed?: number | null
74
+ }
75
+ timestamp: number
76
+ }
77
+
60
78
  /** What `createGeolocation` needs of `Geolocation` from `@capacitor/geolocation`. */
61
79
  export interface GeolocationPlugin {
62
- getCurrentPosition(options?: GeolocationOptions): Promise<GeolocationPosition>
80
+ getCurrentPosition(options?: GeolocationOptions): Promise<PluginPosition>
63
81
  watchPosition(
64
82
  options: GeolocationOptions,
65
- callback: (position: GeolocationPosition | null, error?: unknown) => void,
83
+ callback: (position: PluginPosition | null, error?: unknown) => void,
66
84
  ): Promise<string>
67
85
  clearWatch(options: { id: string }): Promise<void>
68
86
  }