@inglorious/web 6.16.0 → 6.18.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/README.md CHANGED
@@ -272,7 +272,7 @@ If you want a framework that **does not fight JavaScript**, this is the one.
272
272
  ## Installation
273
273
 
274
274
  ```bash
275
- npm install @inglorious/web
275
+ pnpm add @inglorious/web
276
276
  ```
277
277
 
278
278
  ---
@@ -429,7 +429,7 @@ test("counter button has click handler", () => {
429
429
  **No special setup required:**
430
430
 
431
431
  ```bash
432
- npm install --save-dev vitest # or jest, or node:test
432
+ pnpm add -D vitest # or jest, or node:test
433
433
  ```
434
434
 
435
435
  That's it. No `@testing-library/react`, no `renderHook`, no `act()` wrappers.
@@ -697,7 +697,7 @@ This Vite plugin transforms standard JSX/TSX into optimized `lit-html` templates
697
697
 
698
698
  To use it:
699
699
 
700
- 1. Install the plugin: `npm install -D @inglorious/vite-plugin-jsx`
700
+ 1. Install the plugin: `pnpm add -D @inglorious/vite-plugin-jsx`
701
701
  2. Add it to your `vite.config.js`
702
702
  3. Write your render functions using JSX
703
703
 
@@ -1509,6 +1509,41 @@ Inglorious UI's `Snackbar` is the usual place for it. `watchPwaUpdate` takes `{
1509
1509
 
1510
1510
  ---
1511
1511
 
1512
+ ## Native App Updates
1513
+
1514
+ A Capacitor app carries its web code inside the binary, so only the stores update it. `@inglorious/web/native-update` makes an update mandatory: the app reads a minimum build per platform, and a build below it is blocked until the user updates. `@inglorious/create-app --mobile --push` sets it up with Supabase.
1515
+
1516
+ ```javascript
1517
+ import { App } from "@capacitor/app"
1518
+ import {
1519
+ createNativeUpdate,
1520
+ watchNativeUpdate,
1521
+ } from "@inglorious/web/native-update"
1522
+
1523
+ const types = { NativeUpdate: [createNativeUpdate({ adapter }), template] }
1524
+ const entities = { nativeUpdate: { type: "NativeUpdate" } }
1525
+
1526
+ watchNativeUpdate(App, store)
1527
+ ```
1528
+
1529
+ `watchNativeUpdate` reads the installed build at launch and on every resume, and does nothing outside a native app. `{ id }` as a third argument notifies another entity; it returns a function that stops watching.
1530
+
1531
+ The adapter says where the minimum lives: `requirement(platform)` resolves to `{ minBuild, storeUrl }`, or `null` for no minimum. `supabaseNativeUpdate` from `@inglorious/supabase` reads it from a table.
1532
+
1533
+ The `nativeUpdate` entity contains:
1534
+
1535
+ - `isUpdateRequired` — the installed build is below the minimum
1536
+ - `storeUrl` — where the update button goes, or `null`
1537
+
1538
+ Events:
1539
+
1540
+ - `nativeUpdateCheck` — `{ platform, build }`, sent by `watchNativeUpdate`
1541
+ - `nativeUpdateApply` — opens the store
1542
+
1543
+ A failed check keeps the last answer, so nobody is blocked by being offline. Render the whole app as the update page while `isUpdateRequired`, and raise the minimum only once the new build is live in that store.
1544
+
1545
+ ---
1546
+
1512
1547
  ## Forms
1513
1548
 
1514
1549
  `@inglorious/web` includes a small but powerful `form` type for managing form state inside your entity store. It offers:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/web",
3
- "version": "6.16.0",
3
+ "version": "6.18.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": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "contributors": [
@@ -66,6 +66,10 @@
66
66
  "types": "./types/mobile.d.ts",
67
67
  "import": "./src/mobile/index.js"
68
68
  },
69
+ "./native-update": {
70
+ "types": "./types/native-update.d.ts",
71
+ "import": "./src/native-update/index.js"
72
+ },
69
73
  "./pwa": {
70
74
  "types": "./types/pwa.d.ts",
71
75
  "import": "./src/pwa/index.js"
@@ -121,12 +125,12 @@
121
125
  },
122
126
  "sideEffects": false,
123
127
  "dependencies": {
124
- "@inglorious/utils": "^5.1.0",
128
+ "@inglorious/utils": "^5.3.0",
125
129
  "@lit-labs/ssr-client": "^1.1.8",
126
130
  "lit-html": "^3.3.3"
127
131
  },
128
132
  "peerDependencies": {
129
- "@inglorious/store": "^12.2.0"
133
+ "@inglorious/store": "^12.3.0"
130
134
  },
131
135
  "devDependencies": {
132
136
  "@inglorious/eslint-config": "1.2.0",
@@ -28,7 +28,7 @@ const DENIED = "denied"
28
28
  * @param {string} [config.consentKey] - The localStorage key of the choice.
29
29
  *
30
30
  * @example
31
- * const types = { Analytics: [createAnalytics({ adapter }), renderers] }
31
+ * const types = { Analytics: [createAnalytics({ adapter }), template] }
32
32
  * const entities = { analytics: { type: "Analytics" } }
33
33
  * // later, anywhere
34
34
  * api.notify("#analytics:analyticsTrack", { name: "sign_up" })
@@ -0,0 +1,89 @@
1
+ import { handleAsync } from "@inglorious/store/async"
2
+
3
+ import { isNative } from "../mobile/index.js"
4
+
5
+ const DEFAULT_ID = "nativeUpdate"
6
+ const RESUME_EVENT = "resume"
7
+
8
+ /**
9
+ * The type that blocks a native build older than the minimum allowed. A
10
+ * native app carries its web code inside the binary, so only the stores can
11
+ * update it: the app reads a minimum build per platform, and a build below it
12
+ * sets `isUpdateRequired`, which the app renders as a page with nothing but
13
+ * a way to the store (`nativeUpdateApply`, opening `storeUrl`).
14
+ *
15
+ * A failed check keeps the last answer: the gate never closes or opens on a
16
+ * guess.
17
+ *
18
+ * The `adapter` says where the minimum lives: `requirement(platform)`
19
+ * resolves to `{ minBuild, storeUrl }`, or `null` for no minimum.
20
+ *
21
+ * @param {Object} config
22
+ * @param {Object} config.adapter
23
+ *
24
+ * @example
25
+ * const types = { NativeUpdate: [createNativeUpdate({ adapter }), template] }
26
+ * watchNativeUpdate(App, store)
27
+ */
28
+ export function createNativeUpdate({ adapter }) {
29
+ return {
30
+ create(entity) {
31
+ entity.isUpdateRequired ??= false
32
+ entity.storeUrl ??= null
33
+ },
34
+
35
+ ...handleAsync("nativeUpdateCheck", {
36
+ async run({ platform, build }) {
37
+ return { build, requirement: await adapter.requirement(platform) }
38
+ },
39
+ success(entity, { build, requirement }) {
40
+ entity.isUpdateRequired = isOutdated(build, requirement)
41
+ entity.storeUrl = requirement?.storeUrl ?? null
42
+ },
43
+ error() {},
44
+ }),
45
+
46
+ nativeUpdateApply(entity) {
47
+ if (entity.storeUrl) window.location.assign(entity.storeUrl)
48
+ },
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Checks the installed build at launch and each time the app comes back to
54
+ * the foreground, notifying `#${id}:nativeUpdateCheck`. Does nothing outside
55
+ * a native app.
56
+ *
57
+ * @param {Object} app `App` from `@capacitor/app`.
58
+ * @param {{ notify: (type: string, payload?: unknown) => void }} store
59
+ * @param {{ id?: string }} [options]
60
+ * @returns {() => Promise<void>} Stops watching.
61
+ */
62
+ export function watchNativeUpdate(app, store, { id = DEFAULT_ID } = {}) {
63
+ if (!isNative()) return async () => {}
64
+
65
+ check()
66
+ const listener = app.addListener(RESUME_EVENT, check)
67
+
68
+ return async function stop() {
69
+ const handle = await listener
70
+ await handle.remove()
71
+ }
72
+
73
+ async function check() {
74
+ try {
75
+ const { build } = await app.getInfo()
76
+ store.notify(`#${id}:nativeUpdateCheck`, {
77
+ platform: globalThis.Capacitor.getPlatform(),
78
+ build: Number(build),
79
+ })
80
+ } catch {
81
+ // An unreadable build is no reason to block anyone.
82
+ }
83
+ }
84
+ }
85
+
86
+ function isOutdated(build, requirement) {
87
+ if (!requirement || !Number.isFinite(build)) return false
88
+ return build < requirement.minBuild
89
+ }
package/src/push/index.js CHANGED
@@ -45,7 +45,7 @@ const tappedAdapters = new WeakSet()
45
45
  * const types = {
46
46
  * PushPrompt: {
47
47
  * ...prompt,
48
- * ...renderers,
48
+ * ...template,
49
49
  * sessionStart: prompt.pushStart,
50
50
  * sessionEnd: prompt.pushStop,
51
51
  * },
package/types/mount.d.ts CHANGED
@@ -1,16 +1,19 @@
1
1
  import type { nothing, TemplateResult } from "lit-html"
2
2
  import type {
3
+ AnyEvents,
3
4
  AnyTypeDefinition,
5
+ Api,
4
6
  BaseEntity,
5
7
  EntitiesState,
6
8
  Store,
7
- Api as StoreApi,
8
9
  } from "@inglorious/store"
9
10
 
10
11
  declare module "@inglorious/store" {
11
12
  interface Api<
12
13
  TEntity extends BaseEntity = BaseEntity,
13
14
  TState extends EntitiesState<TEntity> = EntitiesState<TEntity>,
15
+ // Unused here, but every declaration of `Api` must take the same parameters.
16
+ TEvents = AnyEvents,
14
17
  > {
15
18
  /**
16
19
  * Renders an entity or a type component by its ID.
@@ -31,10 +34,12 @@ declare module "@inglorious/store" {
31
34
  }
32
35
  }
33
36
 
34
- export type Api<
35
- TEntity extends BaseEntity = BaseEntity,
36
- TState extends EntitiesState<TEntity> = EntitiesState<TEntity>,
37
- > = StoreApi<TEntity, TState>
37
+ /**
38
+ * The store's api, with `render` added above. It is the store's own interface,
39
+ * re-exported rather than aliased: an interface is resolved lazily, so an app
40
+ * can tell its api the events of types whose handlers take that same api.
41
+ */
42
+ export type { Api }
38
43
 
39
44
  /**
40
45
  * Root error-boundary options for `mount`.
@@ -77,9 +82,10 @@ export interface MountOptions<
77
82
  export function mount<
78
83
  TEntity extends BaseEntity = BaseEntity,
79
84
  TState extends EntitiesState<TEntity> = EntitiesState<TEntity>,
85
+ TEvents = AnyEvents,
80
86
  >(
81
- store: Store<TEntity, TState>,
82
- renderFn: (api: Api<TEntity, TState>) => TemplateResult | null,
87
+ store: Store<TEntity, TState, TEvents>,
88
+ renderFn: (api: Api<TEntity, TState, TEvents>) => TemplateResult | null,
83
89
  element: HTMLElement | DocumentFragment,
84
90
  options?: MountOptions<TEntity, TState>,
85
91
  ): () => void
@@ -0,0 +1,76 @@
1
+ import { Api } from "./mount"
2
+
3
+ export interface NativeUpdateRequirement {
4
+ /** The oldest build allowed to run. */
5
+ minBuild: number
6
+ /** Where the update button sends users; none hides it. */
7
+ storeUrl?: string | null
8
+ }
9
+
10
+ /** Where the minimum build of each platform lives. */
11
+ export interface NativeUpdateAdapter {
12
+ /** The platform's minimum, or `null` for none. */
13
+ requirement(
14
+ platform: "android" | "ios",
15
+ ): Promise<NativeUpdateRequirement | null>
16
+ }
17
+
18
+ export interface NativeUpdateConfig {
19
+ adapter: NativeUpdateAdapter
20
+ }
21
+
22
+ export interface NativeUpdateEntity {
23
+ id?: string
24
+ type: string
25
+ /** The installed build is below the minimum. */
26
+ isUpdateRequired: boolean
27
+ storeUrl: string | null
28
+ }
29
+
30
+ export interface NativeUpdateCheckPayload {
31
+ platform: "android" | "ios"
32
+ build: number
33
+ }
34
+
35
+ export type NativeUpdateType = {
36
+ create(entity: NativeUpdateEntity): void
37
+ /** Compares the installed build with the platform's minimum. */
38
+ nativeUpdateCheck(
39
+ entity: NativeUpdateEntity,
40
+ payload: NativeUpdateCheckPayload,
41
+ api?: Api,
42
+ ): void
43
+ /** Opens the store. */
44
+ nativeUpdateApply(entity: NativeUpdateEntity): void
45
+ [handler: string]: unknown
46
+ }
47
+
48
+ /** The type that blocks a native build older than the minimum allowed. */
49
+ export declare function createNativeUpdate(
50
+ config: NativeUpdateConfig,
51
+ ): NativeUpdateType
52
+
53
+ /** What `watchNativeUpdate` needs of `App` from `@capacitor/app`. */
54
+ export interface NativeApp {
55
+ getInfo(): Promise<{ build: string }>
56
+ addListener(
57
+ event: "resume",
58
+ listener: () => void,
59
+ ): Promise<{ remove(): Promise<void> }>
60
+ }
61
+
62
+ export interface WatchNativeUpdateOptions {
63
+ /** The NativeUpdate entity to notify. Defaults to `nativeUpdate`. */
64
+ id?: string
65
+ }
66
+
67
+ /**
68
+ * Checks the installed build at launch and on every resume, notifying
69
+ * `#${id}:nativeUpdateCheck`. Does nothing outside a native app.
70
+ * @returns A function that stops watching.
71
+ */
72
+ export declare function watchNativeUpdate(
73
+ app: NativeApp,
74
+ store: Pick<Api, "notify">,
75
+ options?: WatchNativeUpdateOptions,
76
+ ): () => Promise<void>