The best way to control Claude Code, Codex and more from any computer or phone

main
ts 120 lines 3.46 KB
Raw
1 /**
2 * Browser IndexedDB primitives and key schemas for Effect applications.
3 *
4 * This module is the low-level bridge used by the platform-browser IndexedDB
5 * integration. It provides an `IndexedDb` service around the browser
6 * `indexedDB` factory and `IDBKeyRange` constructor, a `layerWindow` layer for
7 * wiring those primitives from `window`, and schemas for the key shapes accepted
8 * by IndexedDB object stores and indexes.
9 *
10 * @stability unstable
11 * @since 4.0.0
12 */
13 import * as Context from "effect/Context"
14 import * as Effect from "effect/Effect"
15 import * as Layer from "effect/Layer"
16 import * as Schema from "effect/Schema"
17
18 const TypeId = "~@effect/platform-browser/IndexedDb"
19
20 /**
21 * Service interface that provides the browser `indexedDB` factory and `IDBKeyRange` constructor.
22 *
23 * @stability unstable
24 * @category services
25 * @since 4.0.0
26 */
27 export interface IndexedDb {
28 readonly [TypeId]: typeof TypeId
29 readonly indexedDB: globalThis.IDBFactory
30 readonly IDBKeyRange: typeof globalThis.IDBKeyRange
31 }
32
33 /**
34 * Service tag for browser IndexedDB primitives.
35 *
36 * @stability unstable
37 * @category services
38 * @since 4.0.0
39 */
40 export const IndexedDb: Context.Service<IndexedDb, IndexedDb> = Context.Service<IndexedDb, IndexedDb>(TypeId)
41
42 /** @internal */
43 const IDBFlatKey = Schema.Union([
44 Schema.String,
45 Schema.Number.check(Schema.makeFilter((input) => !Number.isNaN(input))),
46 Schema.Date,
47 Schema.declare(
48 (input): input is BufferSource =>
49 input instanceof ArrayBuffer ||
50 (ArrayBuffer.isView(input) && input.buffer instanceof ArrayBuffer)
51 )
52 ])
53
54 /**
55 * Schema for IndexedDB keys: strings, non-NaN numbers, valid dates, buffer sources, or arrays of those flat key values.
56 *
57 * @stability unstable
58 * @category schemas
59 * @since 4.0.0
60 */
61 export const IDBValidKey = Schema.Union([IDBFlatKey, Schema.Array(IDBFlatKey)])
62
63 /**
64 * Schema for auto-incremented IndexedDB keys, accepting integers from 1 through `2 ** 53`.
65 *
66 * **When to use**
67 *
68 * Use when you need to define numeric key-path fields for `IndexedDbTable`
69 * definitions that use IndexedDB auto-increment keys.
70 *
71 * **Details**
72 *
73 * The schema accepts integer values from `1` through `2 ** 53`, matching the
74 * range used for generated IndexedDB auto-increment keys.
75 *
76 * @see {@link IDBValidKey} for the broader IndexedDB key schema
77 *
78 * @stability unstable
79 * @category schemas
80 * @since 4.0.0
81 */
82 export const AutoIncrement = Schema.Int.check(
83 Schema.isBetween({ minimum: 1, maximum: 2 ** 53 })
84 ).annotate({
85 identifier: "AutoIncrement",
86 title: "autoIncrement",
87 description: "Defines a valid autoIncrement key path for the IndexedDb table"
88 })
89
90 /**
91 * Creates an `IndexedDb` service from an `IDBFactory` and `IDBKeyRange` constructor.
92 *
93 * @stability unstable
94 * @category constructors
95 * @since 4.0.0
96 */
97 export const make = (impl: Omit<IndexedDb, typeof TypeId>): IndexedDb => IndexedDb.of({ [TypeId]: TypeId, ...impl })
98
99 /**
100 * Layer that provides `IndexedDb` from `window.indexedDB` and `window.IDBKeyRange`, dying with a defect when they are unavailable.
101 *
102 * @stability unstable
103 * @category layers
104 * @since 4.0.0
105 */
106 export const layerWindow: Layer.Layer<IndexedDb> = Layer.effect(
107 IndexedDb,
108 Effect.suspend(() => {
109 if (window.indexedDB && window.IDBKeyRange) {
110 return Effect.succeed(
111 make({
112 indexedDB: window.indexedDB,
113 IDBKeyRange: window.IDBKeyRange
114 })
115 )
116 } else {
117 return Effect.die(new Error("window.indexedDB is not available"))
118 }
119 })
120 )