@inglorious/web 6.26.2 → 6.27.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
@@ -1725,6 +1725,30 @@ api.notify("#share:share", {
1725
1725
 
1726
1726
  ---
1727
1727
 
1728
+ ## Native Storage
1729
+
1730
+ iOS and Android can clear a WebView's `localStorage` when the device runs low on space. `createNativeStorage` keeps values in Capacitor Preferences instead, in the native app, and in `localStorage` in a browser. Give it to [`withStorage`](https://www.npmjs.com/package/@inglorious/store#withstorage):
1731
+
1732
+ ```javascript
1733
+ import { Preferences } from "@capacitor/preferences"
1734
+ import { withStorage } from "@inglorious/store/storage"
1735
+ import { createNativeStorage } from "@inglorious/web/mobile"
1736
+
1737
+ const preferences = createNativeStorage({ plugin: Preferences })
1738
+
1739
+ const types = {
1740
+ Settings: [Settings, withStorage(["theme"], { storage: () => preferences })],
1741
+ }
1742
+ ```
1743
+
1744
+ Preferences answers with promises, so in the native app the entity starts with its defaults and gets the stored fields back a moment later, through `storageRestore`. In a browser it is `localStorage` itself.
1745
+
1746
+ It works as Supabase's session storage too, so signing in survives the same clean-ups:
1747
+
1748
+ ```javascript
1749
+ createClient(url, key, { auth: { storage: preferences } })
1750
+ ```
1751
+
1728
1752
  ## Haptics
1729
1753
 
1730
1754
  `@inglorious/web/haptics` plays taps the user can feel. In a native app it uses the system's haptics through [`@capacitor/haptics`](https://capacitorjs.com/docs/apis/haptics), once you hand the plugin over. Elsewhere it vibrates where the browser can (Chrome on Android), once the user has tapped the page; iOS browsers stay silent. Every call returns whether it played.
@@ -2090,8 +2114,8 @@ import { render, trigger } from "@inglorious/web/test"
2090
2114
 
2091
2115
  import { Form } from "@inglorious/web/form"
2092
2116
  import { Router, setRoutes } from "@inglorious/web/router"
2093
- import { Geolocation } from "@inglorious/web/geolocation"
2094
- import { Compass } from "@inglorious/web/compass"
2117
+ import { Geolocation } from "@inglorious/web/sensors/geolocation"
2118
+ import { Compass } from "@inglorious/web/sensors/compass"
2095
2119
  ```
2096
2120
 
2097
2121
  This keeps `lit-html` as an internal implementation detail while still giving applications stable, tree-shakeable import paths.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/web",
3
- "version": "6.26.2",
3
+ "version": "6.27.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": [
@@ -157,12 +157,12 @@
157
157
  },
158
158
  "sideEffects": false,
159
159
  "dependencies": {
160
- "@inglorious/utils": "^5.3.0",
160
+ "@inglorious/utils": "^5.4.0",
161
161
  "@lit-labs/ssr-client": "^1.1.8",
162
162
  "lit-html": "^3.3.3"
163
163
  },
164
164
  "peerDependencies": {
165
- "@inglorious/store": "^12.5.0"
165
+ "@inglorious/store": "^12.6.0"
166
166
  },
167
167
  "devDependencies": {
168
168
  "@inglorious/eslint-config": "1.2.0",
@@ -15,3 +15,29 @@ export function isNative() {
15
15
 
16
16
  return capacitor.isNative === true
17
17
  }
18
+
19
+ /**
20
+ * A storage for `withStorage` that survives the operating system clearing the
21
+ * WebView's data: in a native app, Capacitor Preferences (`Preferences` from
22
+ * `@capacitor/preferences`, given as `plugin`), answering with promises; in a
23
+ * browser, `localStorage` itself. It also serves as Supabase's `auth.storage`,
24
+ * keeping the session there too.
25
+ *
26
+ * @param {{ plugin?: { get(options: { key: string }): Promise<{ value: string | null }>, set(options: { key: string, value: string }): Promise<void>, remove(options: { key: string }): Promise<void> } }} [config]
27
+ *
28
+ * @example
29
+ * import { Preferences } from "@capacitor/preferences"
30
+ * const preferences = createNativeStorage({ plugin: Preferences })
31
+ * const types = {
32
+ * Settings: [Settings, withStorage(["theme"], { storage: () => preferences })],
33
+ * }
34
+ */
35
+ export function createNativeStorage({ plugin } = {}) {
36
+ if (!plugin || !isNative()) return globalThis.localStorage
37
+
38
+ return {
39
+ getItem: async (key) => (await plugin.get({ key })).value,
40
+ setItem: (key, value) => plugin.set({ key, value }),
41
+ removeItem: (key) => plugin.remove({ key }),
42
+ }
43
+ }
package/types/mobile.d.ts CHANGED
@@ -1 +1,20 @@
1
1
  export declare function isNative(): boolean
2
+
3
+ /** What `createNativeStorage` needs of `Preferences` from `@capacitor/preferences`. */
4
+ export interface PreferencesPlugin {
5
+ get(options: { key: string }): Promise<{ value: string | null }>
6
+ set(options: { key: string; value: string }): Promise<void>
7
+ remove(options: { key: string }): Promise<void>
8
+ }
9
+
10
+ /** A storage for `withStorage`: answering with promises in a native app. */
11
+ export interface NativeStorage {
12
+ getItem(key: string): string | null | Promise<string | null>
13
+ setItem(key: string, value: string): void | Promise<void>
14
+ removeItem(key: string): void | Promise<void>
15
+ }
16
+
17
+ /** Capacitor Preferences in a native app, `localStorage` itself in a browser. */
18
+ export declare function createNativeStorage(config?: {
19
+ plugin?: PreferencesPlugin
20
+ }): NativeStorage | Storage | undefined