@effect/platform-browser 0.74.0 → 4.0.0-beta.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.
Files changed (170) hide show
  1. package/README.md +0 -6
  2. package/dist/BrowserHttpClient.d.ts +49 -0
  3. package/dist/BrowserHttpClient.d.ts.map +1 -0
  4. package/dist/{esm/internal/httpClient.js → BrowserHttpClient.js} +98 -60
  5. package/dist/BrowserHttpClient.js.map +1 -0
  6. package/dist/BrowserKeyValueStore.d.ts +24 -0
  7. package/dist/BrowserKeyValueStore.d.ts.map +1 -0
  8. package/dist/BrowserKeyValueStore.js +20 -0
  9. package/dist/BrowserKeyValueStore.js.map +1 -0
  10. package/dist/BrowserRuntime.d.ts +28 -0
  11. package/dist/BrowserRuntime.d.ts.map +1 -0
  12. package/dist/BrowserRuntime.js +13 -0
  13. package/dist/BrowserRuntime.js.map +1 -0
  14. package/dist/{dts/BrowserSocket.d.ts → BrowserSocket.d.ts} +4 -4
  15. package/dist/BrowserSocket.d.ts.map +1 -0
  16. package/dist/{esm/BrowserSocket.js → BrowserSocket.js} +5 -5
  17. package/dist/BrowserSocket.js.map +1 -0
  18. package/dist/BrowserStream.d.ts +35 -0
  19. package/dist/BrowserStream.d.ts.map +1 -0
  20. package/dist/BrowserStream.js +25 -0
  21. package/dist/BrowserStream.js.map +1 -0
  22. package/dist/BrowserWorker.d.ts +13 -0
  23. package/dist/BrowserWorker.d.ts.map +1 -0
  24. package/dist/{esm/internal/worker.js → BrowserWorker.js} +23 -17
  25. package/dist/BrowserWorker.js.map +1 -0
  26. package/dist/BrowserWorkerRunner.d.ts +18 -0
  27. package/dist/BrowserWorkerRunner.d.ts.map +1 -0
  28. package/dist/BrowserWorkerRunner.js +143 -0
  29. package/dist/BrowserWorkerRunner.js.map +1 -0
  30. package/dist/{dts/Clipboard.d.ts → Clipboard.d.ts} +13 -30
  31. package/dist/Clipboard.d.ts.map +1 -0
  32. package/dist/{esm/Clipboard.js → Clipboard.js} +12 -18
  33. package/dist/Clipboard.js.map +1 -0
  34. package/dist/Geolocation.d.ts +97 -0
  35. package/dist/Geolocation.d.ts.map +1 -0
  36. package/dist/Geolocation.js +99 -0
  37. package/dist/Geolocation.js.map +1 -0
  38. package/dist/Permissions.d.ts +80 -0
  39. package/dist/Permissions.d.ts.map +1 -0
  40. package/dist/Permissions.js +70 -0
  41. package/dist/Permissions.js.map +1 -0
  42. package/dist/index.d.ts +44 -0
  43. package/dist/index.d.ts.map +1 -0
  44. package/dist/{esm/index.js → index.js} +4 -0
  45. package/dist/index.js.map +1 -0
  46. package/package.json +51 -111
  47. package/src/BrowserHttpClient.ts +364 -16
  48. package/src/BrowserKeyValueStore.ts +15 -8
  49. package/src/BrowserRuntime.ts +30 -4
  50. package/src/BrowserSocket.ts +5 -5
  51. package/src/BrowserStream.ts +20 -10
  52. package/src/BrowserWorker.ts +58 -21
  53. package/src/BrowserWorkerRunner.ts +151 -18
  54. package/src/Clipboard.ts +16 -38
  55. package/src/Geolocation.ts +80 -40
  56. package/src/Permissions.ts +49 -34
  57. package/src/index.ts +16 -10
  58. package/BrowserHttpClient/package.json +0 -6
  59. package/BrowserKeyValueStore/package.json +0 -6
  60. package/BrowserRuntime/package.json +0 -6
  61. package/BrowserSocket/package.json +0 -6
  62. package/BrowserStream/package.json +0 -6
  63. package/BrowserWorker/package.json +0 -6
  64. package/BrowserWorkerRunner/package.json +0 -6
  65. package/Clipboard/package.json +0 -6
  66. package/Geolocation/package.json +0 -6
  67. package/Permissions/package.json +0 -6
  68. package/dist/cjs/BrowserHttpClient.js +0 -31
  69. package/dist/cjs/BrowserHttpClient.js.map +0 -1
  70. package/dist/cjs/BrowserKeyValueStore.js +0 -23
  71. package/dist/cjs/BrowserKeyValueStore.js.map +0 -1
  72. package/dist/cjs/BrowserRuntime.js +0 -14
  73. package/dist/cjs/BrowserRuntime.js.map +0 -1
  74. package/dist/cjs/BrowserSocket.js +0 -27
  75. package/dist/cjs/BrowserSocket.js.map +0 -1
  76. package/dist/cjs/BrowserStream.js +0 -23
  77. package/dist/cjs/BrowserStream.js.map +0 -1
  78. package/dist/cjs/BrowserWorker.js +0 -29
  79. package/dist/cjs/BrowserWorker.js.map +0 -1
  80. package/dist/cjs/BrowserWorkerRunner.js +0 -31
  81. package/dist/cjs/BrowserWorkerRunner.js.map +0 -1
  82. package/dist/cjs/Clipboard.js +0 -86
  83. package/dist/cjs/Clipboard.js.map +0 -1
  84. package/dist/cjs/Geolocation.js +0 -73
  85. package/dist/cjs/Geolocation.js.map +0 -1
  86. package/dist/cjs/Permissions.js +0 -59
  87. package/dist/cjs/Permissions.js.map +0 -1
  88. package/dist/cjs/index.js +0 -28
  89. package/dist/cjs/index.js.map +0 -1
  90. package/dist/cjs/internal/httpClient.js +0 -266
  91. package/dist/cjs/internal/httpClient.js.map +0 -1
  92. package/dist/cjs/internal/keyValueStore.js +0 -13
  93. package/dist/cjs/internal/keyValueStore.js.map +0 -1
  94. package/dist/cjs/internal/runtime.js +0 -16
  95. package/dist/cjs/internal/runtime.js.map +0 -1
  96. package/dist/cjs/internal/stream.js +0 -19
  97. package/dist/cjs/internal/stream.js.map +0 -1
  98. package/dist/cjs/internal/worker.js +0 -60
  99. package/dist/cjs/internal/worker.js.map +0 -1
  100. package/dist/cjs/internal/workerRunner.js +0 -144
  101. package/dist/cjs/internal/workerRunner.js.map +0 -1
  102. package/dist/dts/BrowserHttpClient.d.ts +0 -33
  103. package/dist/dts/BrowserHttpClient.d.ts.map +0 -1
  104. package/dist/dts/BrowserKeyValueStore.d.ts +0 -20
  105. package/dist/dts/BrowserKeyValueStore.d.ts.map +0 -1
  106. package/dist/dts/BrowserRuntime.d.ts +0 -10
  107. package/dist/dts/BrowserRuntime.d.ts.map +0 -1
  108. package/dist/dts/BrowserSocket.d.ts.map +0 -1
  109. package/dist/dts/BrowserStream.d.ts +0 -25
  110. package/dist/dts/BrowserStream.d.ts.map +0 -1
  111. package/dist/dts/BrowserWorker.d.ts +0 -26
  112. package/dist/dts/BrowserWorker.d.ts.map +0 -1
  113. package/dist/dts/BrowserWorkerRunner.d.ts +0 -27
  114. package/dist/dts/BrowserWorkerRunner.d.ts.map +0 -1
  115. package/dist/dts/Clipboard.d.ts.map +0 -1
  116. package/dist/dts/Geolocation.d.ts +0 -67
  117. package/dist/dts/Geolocation.d.ts.map +0 -1
  118. package/dist/dts/Permissions.d.ts +0 -67
  119. package/dist/dts/Permissions.d.ts.map +0 -1
  120. package/dist/dts/index.d.ts +0 -41
  121. package/dist/dts/index.d.ts.map +0 -1
  122. package/dist/dts/internal/httpClient.d.ts +0 -2
  123. package/dist/dts/internal/httpClient.d.ts.map +0 -1
  124. package/dist/dts/internal/keyValueStore.d.ts +0 -2
  125. package/dist/dts/internal/keyValueStore.d.ts.map +0 -1
  126. package/dist/dts/internal/runtime.d.ts +0 -2
  127. package/dist/dts/internal/runtime.d.ts.map +0 -1
  128. package/dist/dts/internal/stream.d.ts +0 -5
  129. package/dist/dts/internal/stream.d.ts.map +0 -1
  130. package/dist/dts/internal/worker.d.ts +0 -2
  131. package/dist/dts/internal/worker.d.ts.map +0 -1
  132. package/dist/dts/internal/workerRunner.d.ts +0 -2
  133. package/dist/dts/internal/workerRunner.d.ts.map +0 -1
  134. package/dist/esm/BrowserHttpClient.js +0 -23
  135. package/dist/esm/BrowserHttpClient.js.map +0 -1
  136. package/dist/esm/BrowserKeyValueStore.js +0 -16
  137. package/dist/esm/BrowserKeyValueStore.js.map +0 -1
  138. package/dist/esm/BrowserRuntime.js +0 -7
  139. package/dist/esm/BrowserRuntime.js.map +0 -1
  140. package/dist/esm/BrowserSocket.js.map +0 -1
  141. package/dist/esm/BrowserStream.js +0 -15
  142. package/dist/esm/BrowserStream.js.map +0 -1
  143. package/dist/esm/BrowserWorker.js +0 -22
  144. package/dist/esm/BrowserWorker.js.map +0 -1
  145. package/dist/esm/BrowserWorkerRunner.js +0 -23
  146. package/dist/esm/BrowserWorkerRunner.js.map +0 -1
  147. package/dist/esm/Clipboard.js.map +0 -1
  148. package/dist/esm/Geolocation.js +0 -63
  149. package/dist/esm/Geolocation.js.map +0 -1
  150. package/dist/esm/Permissions.js +0 -50
  151. package/dist/esm/Permissions.js.map +0 -1
  152. package/dist/esm/index.js.map +0 -1
  153. package/dist/esm/internal/httpClient.js.map +0 -1
  154. package/dist/esm/internal/keyValueStore.js +0 -6
  155. package/dist/esm/internal/keyValueStore.js.map +0 -1
  156. package/dist/esm/internal/runtime.js +0 -10
  157. package/dist/esm/internal/runtime.js.map +0 -1
  158. package/dist/esm/internal/stream.js +0 -9
  159. package/dist/esm/internal/stream.js.map +0 -1
  160. package/dist/esm/internal/worker.js.map +0 -1
  161. package/dist/esm/internal/workerRunner.js +0 -135
  162. package/dist/esm/internal/workerRunner.js.map +0 -1
  163. package/dist/esm/package.json +0 -4
  164. package/index/package.json +0 -6
  165. package/src/internal/httpClient.ts +0 -324
  166. package/src/internal/keyValueStore.ts +0 -7
  167. package/src/internal/runtime.ts +0 -8
  168. package/src/internal/stream.ts +0 -27
  169. package/src/internal/worker.ts +0 -58
  170. package/src/internal/workerRunner.ts +0 -155
@@ -1,33 +1,166 @@
1
1
  /**
2
2
  * @since 1.0.0
3
3
  */
4
- import type * as Runner from "@effect/platform/WorkerRunner"
5
- import type * as Layer from "effect/Layer"
6
- import * as internal from "./internal/workerRunner.js"
7
-
8
- export {
9
- /**
10
- * @since 1.0.0
11
- * @category re-exports
12
- */
13
- launch
14
- } from "@effect/platform/WorkerRunner"
4
+ import * as Cause from "effect/Cause"
5
+ import * as Deferred from "effect/Deferred"
6
+ import * as Effect from "effect/Effect"
7
+ import * as Exit from "effect/Exit"
8
+ import * as Fiber from "effect/Fiber"
9
+ import { identity } from "effect/Function"
10
+ import * as Layer from "effect/Layer"
11
+ import * as Queue from "effect/Queue"
12
+ import * as Scope from "effect/Scope"
13
+ import { WorkerError, WorkerReceiveError } from "effect/unstable/workers/WorkerError"
14
+ import * as WorkerRunner from "effect/unstable/workers/WorkerRunner"
15
+
16
+ const cachedPorts = new Set<MessagePort>()
17
+ function globalHandleConnect(event: MessageEvent) {
18
+ cachedPorts.add((event as MessageEvent).ports[0])
19
+ }
20
+ if (typeof self !== "undefined" && "onconnect" in self) {
21
+ self.onconnect = globalHandleConnect
22
+ }
15
23
 
16
24
  /**
17
25
  * @since 1.0.0
18
- * @category constructors
26
+ * @category Constructors
19
27
  */
20
- export const make: (self: MessagePort | Window) => Runner.PlatformRunner = internal.make
28
+ export const make = (self: MessagePort | Window): WorkerRunner.WorkerRunnerPlatform["Service"] => ({
29
+ start: Effect.fnUntraced(function*<O = unknown, I = unknown>() {
30
+ const disconnects = yield* Queue.make<number>()
31
+ let currentPortId = 0
32
+
33
+ const ports = new Map<number, readonly [MessagePort, Scope.Closeable]>()
34
+ const sendUnsafe = (portId: number, message: O, transfer?: ReadonlyArray<unknown>) =>
35
+ (ports.get(portId)?.[0] ?? self).postMessage([1, message], {
36
+ transfer: transfer as any
37
+ })
38
+ const send = (portId: number, message: O, transfer?: ReadonlyArray<unknown>) =>
39
+ Effect.sync(() => sendUnsafe(portId, message, transfer))
40
+
41
+ const run = <A, E, R>(
42
+ handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void
43
+ ) =>
44
+ Effect.scopedWith(Effect.fnUntraced(function*(scope) {
45
+ const closeLatch = Deferred.makeUnsafe<void, WorkerError>()
46
+ const trackFiber = Fiber.runIn(scope)
47
+ const services = yield* Effect.services<R>()
48
+ const runFork = Effect.runForkWith(services)
49
+ const onExit = (exit: Exit.Exit<any, E>) => {
50
+ if (exit._tag === "Failure" && !Cause.hasInterruptsOnly(exit.cause)) {
51
+ runFork(Effect.logError("unhandled error in worker", exit.cause))
52
+ }
53
+ }
54
+
55
+ function onMessage(portId: number) {
56
+ return function(event: MessageEvent) {
57
+ const message = event.data as WorkerRunner.PlatformMessage<I>
58
+ if (message[0] === 0) {
59
+ const result = handler(portId, message[1])
60
+ if (Effect.isEffect(result)) {
61
+ const fiber = runFork(result)
62
+ fiber.addObserver(onExit)
63
+ trackFiber(fiber)
64
+ }
65
+ } else {
66
+ const port = ports.get(portId)
67
+ if (!port) {
68
+ return
69
+ } else if (ports.size === 1) {
70
+ // let the last port close with the outer scope
71
+ return Deferred.doneUnsafe(closeLatch, Exit.void)
72
+ }
73
+ ports.delete(portId)
74
+ Effect.runFork(Scope.close(port[1], Exit.void))
75
+ }
76
+ }
77
+ }
78
+ function onMessageError(error: MessageEvent) {
79
+ Deferred.doneUnsafe(
80
+ closeLatch,
81
+ new WorkerError({
82
+ reason: new WorkerReceiveError({
83
+ message: "An messageerror event was emitted",
84
+ cause: error.data
85
+ })
86
+ }).asEffect()
87
+ )
88
+ }
89
+ function onError(error: any) {
90
+ Deferred.doneUnsafe(
91
+ closeLatch,
92
+ new WorkerError({
93
+ reason: new WorkerReceiveError({
94
+ message: "An error event was emitted",
95
+ cause: error.data
96
+ })
97
+ }).asEffect()
98
+ )
99
+ }
100
+ function handlePort(port: MessagePort) {
101
+ const portScope = Scope.forkUnsafe(scope)
102
+ const portId = currentPortId++
103
+ ports.set(portId, [port, portScope])
104
+ const onMsg = onMessage(portId)
105
+ port.addEventListener("message", onMsg)
106
+ port.addEventListener("messageerror", onMessageError)
107
+ if ("start" in port) {
108
+ port.start()
109
+ }
110
+ port.postMessage([0])
111
+ Effect.runSync(Scope.addFinalizer(
112
+ portScope,
113
+ Effect.sync(() => {
114
+ port.removeEventListener("message", onMsg)
115
+ port.removeEventListener("messageerror", onError)
116
+ port.close()
117
+ })
118
+ ))
119
+ }
120
+ self.addEventListener("error", onError)
121
+ let prevOnConnect: unknown | undefined
122
+ if ("onconnect" in self) {
123
+ prevOnConnect = self.onconnect
124
+ self.onconnect = function(event: MessageEvent) {
125
+ const port = (event as MessageEvent).ports[0]
126
+ handlePort(port)
127
+ }
128
+ for (const port of cachedPorts) {
129
+ handlePort(port)
130
+ }
131
+ cachedPorts.clear()
132
+ } else {
133
+ handlePort(self as any)
134
+ }
135
+ yield* Scope.addFinalizer(
136
+ scope,
137
+ Effect.sync(() => {
138
+ self.removeEventListener("error", onError)
139
+ if ("onconnect" in self) {
140
+ self.close()
141
+ self.onconnect = prevOnConnect
142
+ }
143
+ })
144
+ )
145
+
146
+ yield* Deferred.await(closeLatch)
147
+ }))
148
+
149
+ return identity<WorkerRunner.WorkerRunner<O, I>>({ run, send, sendUnsafe, disconnects })
150
+ }) as any
151
+ })
21
152
 
22
153
  /**
23
154
  * @since 1.0.0
24
- * @category layers
155
+ * @category Layers
25
156
  */
26
- export const layer: Layer.Layer<Runner.PlatformRunner> = internal.layer
157
+ export const layer: Layer.Layer<WorkerRunner.WorkerRunnerPlatform> = Layer.sync(WorkerRunner.WorkerRunnerPlatform)(() =>
158
+ make(self)
159
+ )
27
160
 
28
161
  /**
29
162
  * @since 1.0.0
30
- * @category layers
163
+ * @category Layers
31
164
  */
32
- export const layerMessagePort: (port: MessagePort | Window) => Layer.Layer<Runner.PlatformRunner> =
33
- internal.layerMessagePort
165
+ export const layerMessagePort = (port: MessagePort | Window): Layer.Layer<WorkerRunner.WorkerRunnerPlatform> =>
166
+ Layer.succeed(WorkerRunner.WorkerRunnerPlatform)(make(port))
package/src/Clipboard.ts CHANGED
@@ -1,30 +1,20 @@
1
1
  /**
2
2
  * @since 1.0.0
3
3
  */
4
- import { TypeIdError } from "@effect/platform/Error"
5
- import * as Context from "effect/Context"
4
+ import * as Data from "effect/Data"
6
5
  import * as Effect from "effect/Effect"
7
6
  import * as Layer from "effect/Layer"
7
+ import * as ServiceMap from "effect/ServiceMap"
8
8
 
9
- /**
10
- * @since 1.0.0
11
- * @category type ids
12
- */
13
- export const TypeId: unique symbol = Symbol.for("@effect/platform-browser/Clipboard")
9
+ const TypeId = "~@effect/platform-browser/Clipboard"
10
+ const ErrorTypeId = "~@effect/platform-browser/Clipboard/ClipboardError"
14
11
 
15
12
  /**
16
13
  * @since 1.0.0
17
- * @category type ids
18
- */
19
- export type TypeId = typeof TypeId
20
-
21
- /**
22
- * @since 1.0.0
23
- * @category interface
14
+ * @category Models
24
15
  */
25
16
  export interface Clipboard {
26
- readonly [TypeId]: TypeId
27
-
17
+ readonly [TypeId]: typeof TypeId
28
18
  readonly read: Effect.Effect<ClipboardItems, ClipboardError>
29
19
  readonly readString: Effect.Effect<string, ClipboardError>
30
20
  readonly write: (items: ClipboardItems) => Effect.Effect<void, ClipboardError>
@@ -35,39 +25,27 @@ export interface Clipboard {
35
25
 
36
26
  /**
37
27
  * @since 1.0.0
38
- * @category type ids
39
- */
40
- export const ErrorTypeId: unique symbol = Symbol.for("@effect/platform-browser/Clipboard/ClipboardError")
41
-
42
- /**
43
- * @since 1.0.0
44
- * @category type ids
28
+ * @category Errors
45
29
  */
46
- export type ErrorTypeId = typeof ErrorTypeId
47
-
48
- /**
49
- * @since 1.0.0
50
- * @category errors
51
- */
52
- export class ClipboardError extends TypeIdError(ErrorTypeId, "ClipboardError")<{
30
+ export class ClipboardError extends Data.TaggedError("ClipboardError")<{
53
31
  readonly message: string
54
32
  readonly cause: unknown
55
- }> {}
33
+ }> {
34
+ readonly [ErrorTypeId] = ErrorTypeId
35
+ }
56
36
 
57
37
  /**
58
38
  * @since 1.0.0
59
- * @category tag
39
+ * @category Service
60
40
  */
61
- export const Clipboard: Context.Tag<Clipboard, Clipboard> = Context.GenericTag<Clipboard>(
62
- "@effect/platform-browser/Clipboard"
63
- )
41
+ export const Clipboard: ServiceMap.Service<Clipboard, Clipboard> = ServiceMap.Service<Clipboard>(TypeId)
64
42
 
65
43
  /**
66
44
  * @since 1.0.0
67
- * @category constructor
45
+ * @category Constructors
68
46
  */
69
47
  export const make = (
70
- impl: Omit<Clipboard, "clear" | "writeBlob" | TypeId>
48
+ impl: Omit<Clipboard, "clear" | "writeBlob" | typeof TypeId>
71
49
  ): Clipboard =>
72
50
  Clipboard.of({
73
51
  ...impl,
@@ -80,7 +58,7 @@ export const make = (
80
58
  * A layer that directly interfaces with the navigator.clipboard api
81
59
  *
82
60
  * @since 1.0.0
83
- * @category layers
61
+ * @category Layers
84
62
  */
85
63
  export const layer: Layer.Layer<Clipboard> = Layer.succeed(
86
64
  Clipboard,
@@ -1,33 +1,23 @@
1
1
  /**
2
2
  * @since 1.0.0
3
3
  */
4
- import { TypeIdError } from "@effect/platform/Error"
5
- import * as Context from "effect/Context"
4
+ import * as Cause from "effect/Cause"
5
+ import * as Data from "effect/Data"
6
6
  import * as Effect from "effect/Effect"
7
- import * as Either from "effect/Either"
8
- import { identity } from "effect/Function"
9
7
  import * as Layer from "effect/Layer"
10
8
  import * as Queue from "effect/Queue"
9
+ import * as ServiceMap from "effect/ServiceMap"
11
10
  import * as Stream from "effect/Stream"
12
11
 
13
- /**
14
- * @since 1.0.0
15
- * @category type ids
16
- */
17
- export const TypeId: unique symbol = Symbol.for("@effect/platform-browser/Geolocation")
18
-
19
- /**
20
- * @since 1.0.0
21
- * @category type ids
22
- */
23
- export type TypeId = typeof TypeId
12
+ const TypeId = "~@effect/platform-browser/Geolocation"
13
+ const ErrorTypeId = "~@effect/platform-browser/Geolocation/GeolocationError"
24
14
 
25
15
  /**
26
16
  * @since 1.0.0
27
- * @category models
17
+ * @category Models
28
18
  */
29
19
  export interface Geolocation {
30
- readonly [TypeId]: TypeId
20
+ readonly [TypeId]: typeof TypeId
31
21
  readonly getCurrentPosition: (
32
22
  options?: PositionOptions | undefined
33
23
  ) => Effect.Effect<GeolocationPosition, GeolocationError>
@@ -42,37 +32,75 @@ export interface Geolocation {
42
32
 
43
33
  /**
44
34
  * @since 1.0.0
45
- * @category tags
35
+ * @category Service
46
36
  */
47
- export const Geolocation: Context.Tag<Geolocation, Geolocation> = Context.GenericTag<Geolocation>(
48
- "@effect/platform-browser/Geolocation"
49
- )
37
+ export const Geolocation: ServiceMap.Service<Geolocation, Geolocation> = ServiceMap.Service<Geolocation>(TypeId)
38
+
39
+ /**
40
+ * @since 1.0.0
41
+ * @category Errors
42
+ */
43
+ export class GeolocationError extends Data.TaggedError("GeolocationError")<{
44
+ readonly reason: GeolocationErrorReason
45
+ }> {
46
+ constructor(props: {
47
+ readonly reason: GeolocationErrorReason
48
+ }) {
49
+ super({
50
+ ...props,
51
+ cause: props.reason.cause
52
+ } as any)
53
+ }
54
+
55
+ readonly [ErrorTypeId] = ErrorTypeId
56
+
57
+ override get message(): string {
58
+ return this.reason.message
59
+ }
60
+ }
50
61
 
51
62
  /**
52
63
  * @since 1.0.0
53
- * @category type ids
64
+ * @category Errors
54
65
  */
55
- export const ErrorTypeId: unique symbol = Symbol.for("@effect/platform-browser/Geolocation/GeolocationError")
66
+ export class PositionUnavailable extends Data.TaggedError("PositionUnavailable")<{
67
+ readonly cause: unknown
68
+ }> {
69
+ override get message(): string {
70
+ return this._tag
71
+ }
72
+ }
56
73
 
57
74
  /**
58
75
  * @since 1.0.0
59
- * @category type ids
76
+ * @category Errors
60
77
  */
61
- export type ErrorTypeId = typeof ErrorTypeId
78
+ export class PermissionDenied extends Data.TaggedError("PermissionDenied")<{
79
+ readonly cause: unknown
80
+ }> {
81
+ override get message(): string {
82
+ return this._tag
83
+ }
84
+ }
62
85
 
63
86
  /**
64
87
  * @since 1.0.0
65
- * @category errors
88
+ * @category Errors
66
89
  */
67
- export class GeolocationError extends TypeIdError(ErrorTypeId, "GeolocationError")<{
68
- readonly reason: "PositionUnavailable" | "PermissionDenied" | "Timeout"
90
+ export class Timeout extends Data.TaggedError("Timeout")<{
69
91
  readonly cause: unknown
70
92
  }> {
71
- get message() {
72
- return this.reason
93
+ override get message(): string {
94
+ return this._tag
73
95
  }
74
96
  }
75
97
 
98
+ /**
99
+ * @since 1.0.0
100
+ * @category Errors
101
+ */
102
+ export type GeolocationErrorReason = PositionUnavailable | PermissionDenied | Timeout
103
+
76
104
  const makeQueue = (
77
105
  options:
78
106
  | PositionOptions & {
@@ -80,17 +108,28 @@ const makeQueue = (
80
108
  }
81
109
  | undefined
82
110
  ) =>
83
- Queue.sliding<Either.Either<GeolocationPosition, GeolocationError>>(options?.bufferSize ?? 16).pipe(
111
+ Queue.sliding<GeolocationPosition, GeolocationError>(options?.bufferSize ?? 16).pipe(
84
112
  Effect.tap((queue) =>
85
113
  Effect.acquireRelease(
86
114
  Effect.sync(() =>
87
115
  navigator.geolocation.watchPosition(
88
- (position) => queue.unsafeOffer(Either.right(position)),
116
+ (position) => Queue.offerUnsafe(queue, position),
89
117
  (cause) => {
90
118
  if (cause.code === cause.PERMISSION_DENIED) {
91
- queue.unsafeOffer(Either.left(new GeolocationError({ reason: "PermissionDenied", cause })))
119
+ const error = new GeolocationError({
120
+ reason: new PermissionDenied({ cause })
121
+ })
122
+ Queue.failCauseUnsafe(queue, Cause.fail(error))
92
123
  } else if (cause.code === cause.TIMEOUT) {
93
- queue.unsafeOffer(Either.left(new GeolocationError({ reason: "Timeout", cause })))
124
+ const error = new GeolocationError({
125
+ reason: new Timeout({ cause })
126
+ })
127
+ Queue.failCauseUnsafe(queue, Cause.fail(error))
128
+ } else if (cause.code === cause.POSITION_UNAVAILABLE) {
129
+ const error = new GeolocationError({
130
+ reason: new PositionUnavailable({ cause })
131
+ })
132
+ Queue.failCauseUnsafe(queue, Cause.fail(error))
94
133
  }
95
134
  },
96
135
  options
@@ -103,7 +142,7 @@ const makeQueue = (
103
142
 
104
143
  /**
105
144
  * @since 1.0.0
106
- * @category layers
145
+ * @category Layers
107
146
  */
108
147
  export const layer: Layer.Layer<Geolocation> = Layer.succeed(
109
148
  Geolocation,
@@ -112,21 +151,19 @@ export const layer: Layer.Layer<Geolocation> = Layer.succeed(
112
151
  getCurrentPosition: (options) =>
113
152
  makeQueue(options).pipe(
114
153
  Effect.flatMap(Queue.take),
115
- Effect.flatten,
116
154
  Effect.scoped
117
155
  ),
118
156
  watchPosition: (options) =>
119
157
  makeQueue(options).pipe(
120
158
  Effect.map(Stream.fromQueue),
121
- Stream.unwrapScoped,
122
- Stream.mapEffect(identity)
159
+ Stream.unwrap
123
160
  )
124
161
  })
125
162
  )
126
163
 
127
164
  /**
128
165
  * @since 1.0.0
129
- * @category accessors
166
+ * @category Accessors
130
167
  */
131
168
  export const watchPosition = (
132
169
  options?:
@@ -135,4 +172,7 @@ export const watchPosition = (
135
172
  }
136
173
  | undefined
137
174
  ): Stream.Stream<GeolocationPosition, GeolocationError, Geolocation> =>
138
- Stream.unwrap(Effect.map(Geolocation, (geolocation) => geolocation.watchPosition(options)))
175
+ Stream.unwrap(Effect.map(
176
+ Effect.service(Geolocation),
177
+ (geolocation) => geolocation.watchPosition(options)
178
+ ))
@@ -1,32 +1,23 @@
1
1
  /**
2
2
  * @since 1.0.0
3
3
  */
4
- import { TypeIdError } from "@effect/platform/Error"
5
- import * as Context from "effect/Context"
4
+ import * as Data from "effect/Data"
6
5
  import * as Effect from "effect/Effect"
7
6
  import * as Layer from "effect/Layer"
7
+ import * as ServiceMap from "effect/ServiceMap"
8
8
 
9
- /**
10
- * @since 1.0.0
11
- * @category type ids
12
- */
13
- export const TypeId: unique symbol = Symbol.for("@effect/platform-browser/Permissions")
14
-
15
- /**
16
- * @since 1.0.0
17
- * @category type ids
18
- */
19
- export type TypeId = typeof TypeId
9
+ const TypeId = "~@effect/platform-browser/Permissions"
10
+ const ErrorTypeId = "~@effect/platform-browser/Permissions/PermissionsError"
20
11
 
21
12
  /**
22
- * Wrapper on the Permission API (`navigator.permissions`)
23
- * with methods for querying status of permissions.
13
+ * Wrapper on the Permission API (`navigator.permissions`) with methods for
14
+ * querying status of permissions.
24
15
  *
25
16
  * @since 1.0.0
26
- * @category interface
17
+ * @category Models
27
18
  */
28
19
  export interface Permissions {
29
- readonly [TypeId]: TypeId
20
+ readonly [TypeId]: typeof TypeId
30
21
 
31
22
  /**
32
23
  * Returns the state of a user permission on the global scope.
@@ -43,43 +34,66 @@ export interface Permissions {
43
34
 
44
35
  /**
45
36
  * @since 1.0.0
46
- * @category type ids
37
+ * @category errors
47
38
  */
48
- export const ErrorTypeId: unique symbol = Symbol.for("@effect/platform-browser/Permissions/PermissionsError")
39
+ export class PermissionsInvalidStateError extends Data.TaggedError("InvalidStateError")<{
40
+ readonly cause: unknown
41
+ }> {
42
+ override get message(): string {
43
+ return this._tag
44
+ }
45
+ }
49
46
 
50
47
  /**
51
48
  * @since 1.0.0
52
- * @category type ids
49
+ * @category errors
53
50
  */
54
- export type ErrorTypeId = typeof ErrorTypeId
51
+ export class PermissionsTypeError extends Data.TaggedError("TypeError")<{
52
+ readonly cause: unknown
53
+ }> {
54
+ override get message(): string {
55
+ return this._tag
56
+ }
57
+ }
55
58
 
56
59
  /**
57
60
  * @since 1.0.0
58
61
  * @category errors
59
62
  */
60
- export class PermissionsError extends TypeIdError(ErrorTypeId, "PermissionsError")<{
61
- /** https://developer.mozilla.org/en-US/docs/Web/API/Permissions/query#exceptions */
62
- readonly reason: "InvalidStateError" | "TypeError"
63
- readonly cause: unknown
63
+ export type PermissionsErrorReason = PermissionsInvalidStateError | PermissionsTypeError
64
+
65
+ /**
66
+ * @since 1.0.0
67
+ * @category errors
68
+ */
69
+ export class PermissionsError extends Data.TaggedError("PermissionsError")<{
70
+ readonly reason: PermissionsErrorReason
64
71
  }> {
65
- get message() {
66
- return this.reason
72
+ constructor(props: { readonly reason: PermissionsErrorReason }) {
73
+ super({
74
+ ...props,
75
+ cause: props.reason.cause
76
+ } as any)
77
+ }
78
+
79
+ readonly [ErrorTypeId] = ErrorTypeId
80
+
81
+ override get message(): string {
82
+ return this.reason.message
67
83
  }
68
84
  }
69
85
 
70
86
  /**
71
87
  * @since 1.0.0
72
- * @category tags
88
+ * @category Service
73
89
  */
74
- export const Permissions: Context.Tag<Permissions, Permissions> = Context.GenericTag<Permissions>(
75
- "@effect/platform-browser/Permissions"
76
- )
90
+ export const Permissions: ServiceMap.Service<Permissions, Permissions> = ServiceMap.Service<Permissions>(TypeId)
77
91
 
78
92
  /**
79
93
  * A layer that directly interfaces with the `navigator.permissions` api
80
94
  *
81
95
  * @since 1.0.0
82
- * @category layers
96
+ * @category Layers
83
97
  */
84
98
  export const layer: Layer.Layer<Permissions> = Layer.succeed(
85
99
  Permissions,
@@ -90,8 +104,9 @@ export const layer: Layer.Layer<Permissions> = Layer.succeed(
90
104
  try: () => navigator.permissions.query({ name }) as Promise<any>,
91
105
  catch: (cause) =>
92
106
  new PermissionsError({
93
- reason: cause instanceof DOMException ? "InvalidStateError" : "TypeError",
94
- cause
107
+ reason: cause instanceof DOMException
108
+ ? new PermissionsInvalidStateError({ cause })
109
+ : new PermissionsTypeError({ cause })
95
110
  })
96
111
  })
97
112
  })
package/src/index.ts CHANGED
@@ -1,49 +1,55 @@
1
1
  /**
2
2
  * @since 1.0.0
3
3
  */
4
- export * as BrowserHttpClient from "./BrowserHttpClient.js"
4
+
5
+ // @barrel: Auto-generated exports. Do not edit manually.
6
+
7
+ /**
8
+ * @since 1.0.0
9
+ */
10
+ export * as BrowserHttpClient from "./BrowserHttpClient.ts"
5
11
 
6
12
  /**
7
13
  * @since 1.0.0
8
14
  */
9
- export * as BrowserKeyValueStore from "./BrowserKeyValueStore.js"
15
+ export * as BrowserKeyValueStore from "./BrowserKeyValueStore.ts"
10
16
 
11
17
  /**
12
18
  * @since 1.0.0
13
19
  */
14
- export * as BrowserRuntime from "./BrowserRuntime.js"
20
+ export * as BrowserRuntime from "./BrowserRuntime.ts"
15
21
 
16
22
  /**
17
23
  * @since 1.0.0
18
24
  */
19
- export * as BrowserSocket from "./BrowserSocket.js"
25
+ export * as BrowserSocket from "./BrowserSocket.ts"
20
26
 
21
27
  /**
22
28
  * @since 1.0.0
23
29
  */
24
- export * as BrowserStream from "./BrowserStream.js"
30
+ export * as BrowserStream from "./BrowserStream.ts"
25
31
 
26
32
  /**
27
33
  * @since 1.0.0
28
34
  */
29
- export * as BrowserWorker from "./BrowserWorker.js"
35
+ export * as BrowserWorker from "./BrowserWorker.ts"
30
36
 
31
37
  /**
32
38
  * @since 1.0.0
33
39
  */
34
- export * as BrowserWorkerRunner from "./BrowserWorkerRunner.js"
40
+ export * as BrowserWorkerRunner from "./BrowserWorkerRunner.ts"
35
41
 
36
42
  /**
37
43
  * @since 1.0.0
38
44
  */
39
- export * as Clipboard from "./Clipboard.js"
45
+ export * as Clipboard from "./Clipboard.ts"
40
46
 
41
47
  /**
42
48
  * @since 1.0.0
43
49
  */
44
- export * as Geolocation from "./Geolocation.js"
50
+ export * as Geolocation from "./Geolocation.ts"
45
51
 
46
52
  /**
47
53
  * @since 1.0.0
48
54
  */
49
- export * as Permissions from "./Permissions.js"
55
+ export * as Permissions from "./Permissions.ts"
@@ -1,6 +0,0 @@
1
- {
2
- "sideEffects": [],
3
- "main": "../dist/cjs/BrowserHttpClient.js",
4
- "module": "../dist/esm/BrowserHttpClient.js",
5
- "types": "../dist/dts/BrowserHttpClient.d.ts"
6
- }