apps 0.0.1-beta.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.
Files changed (135) hide show
  1. package/README.md +191 -0
  2. package/check-publish.mjs +10 -0
  3. package/framework-reference.json +1 -0
  4. package/js/chunk-22WRQZT7.js +389 -0
  5. package/js/chunk-2OTCEKL2.js +29480 -0
  6. package/js/chunk-36DNRFZQ.js +34 -0
  7. package/js/chunk-3ANFZFCV.js +1799 -0
  8. package/js/chunk-7JXHF5IF.js +138 -0
  9. package/js/chunk-BCSNKUAC.js +0 -0
  10. package/js/chunk-BKVL7XAJ.js +47 -0
  11. package/js/chunk-BPEFS4YF.js +1323 -0
  12. package/js/chunk-C24KEZXR.js +59 -0
  13. package/js/chunk-D6BKYP2L.js +877 -0
  14. package/js/chunk-EP6AEUZO.js +19 -0
  15. package/js/chunk-HP7E42X6.js +1719 -0
  16. package/js/chunk-KWTCHVLT.js +284 -0
  17. package/js/chunk-LOKGDT6B.js +28 -0
  18. package/js/chunk-MLKGABMK.js +9 -0
  19. package/js/chunk-OWD2YICM.js +2486 -0
  20. package/js/chunk-Q2FD5IBM.js +899 -0
  21. package/js/chunk-R6JMJSVT.js +590 -0
  22. package/js/chunk-SBKHUC5L.js +56 -0
  23. package/js/chunk-VFUZI3NY.js +75 -0
  24. package/js/chunk-W7Q5EOTS.js +4375 -0
  25. package/js/chunk-WIDTG42X.js +171 -0
  26. package/js/chunk-X3YBZCJW.js +246 -0
  27. package/js/chunk-YDR4CPJA.js +2357 -0
  28. package/js/client.js +1023 -0
  29. package/js/contracts.js +163 -0
  30. package/js/effect.js +44 -0
  31. package/js/graphql.js +392 -0
  32. package/js/host.js +193 -0
  33. package/js/index.js +284 -0
  34. package/js/mcp/effect.js +23 -0
  35. package/js/mcp/stdio.js +123 -0
  36. package/js/mcp.js +37 -0
  37. package/js/openapi.js +246 -0
  38. package/js/operations/approval.js +9 -0
  39. package/js/react.js +37 -0
  40. package/js/storage/facet.js +546 -0
  41. package/js/ui/auth/contracts.js +19 -0
  42. package/js/ui/auth.js +28 -0
  43. package/js/ui/contracts.js +24 -0
  44. package/js/ui/serving.js +286 -0
  45. package/package.json +116 -0
  46. package/runtime.json +1 -0
  47. package/types/app-data/src/contracts/database.d.ts +193 -0
  48. package/types/app-data/src/implementation/cursor.d.ts +10 -0
  49. package/types/app-data/src/implementation/index-key.d.ts +7 -0
  50. package/types/app-data/src/implementation/promise.d.ts +68 -0
  51. package/types/app-data/src/implementation/range.d.ts +8 -0
  52. package/types/app-data/src/implementation/schema.d.ts +33 -0
  53. package/types/app-data/src/implementation/sqlite.d.ts +11 -0
  54. package/types/app-data/src/index.d.ts +4 -0
  55. package/types/apps/src/approval.d.ts +10 -0
  56. package/types/apps/src/client.d.ts +5 -0
  57. package/types/apps/src/contracts/app.d.ts +58 -0
  58. package/types/apps/src/contracts/approval.d.ts +14 -0
  59. package/types/apps/src/contracts/context.d.ts +25 -0
  60. package/types/apps/src/contracts/elicitation.d.ts +86 -0
  61. package/types/apps/src/contracts/graphql.d.ts +102 -0
  62. package/types/apps/src/contracts/host.d.ts +568 -0
  63. package/types/apps/src/contracts/http.d.ts +20 -0
  64. package/types/apps/src/contracts/live.d.ts +26 -0
  65. package/types/apps/src/contracts/mcp.d.ts +93 -0
  66. package/types/apps/src/contracts/openapi.d.ts +105 -0
  67. package/types/apps/src/contracts/operations.d.ts +30 -0
  68. package/types/apps/src/contracts/optimistic.d.ts +22 -0
  69. package/types/apps/src/contracts/provider.d.ts +96 -0
  70. package/types/apps/src/contracts/schedules.d.ts +51 -0
  71. package/types/apps/src/contracts/schema.d.ts +20 -0
  72. package/types/apps/src/contracts/storage.d.ts +68 -0
  73. package/types/apps/src/contracts/tools.d.ts +11 -0
  74. package/types/apps/src/contracts/ui-auth.d.ts +28 -0
  75. package/types/apps/src/contracts/ui.d.ts +90 -0
  76. package/types/apps/src/contracts/webhook-protocol.d.ts +102 -0
  77. package/types/apps/src/contracts/webhooks.d.ts +53 -0
  78. package/types/apps/src/contracts/workflows.d.ts +277 -0
  79. package/types/apps/src/effect.d.ts +10 -0
  80. package/types/apps/src/facet.d.ts +5 -0
  81. package/types/apps/src/graphql.d.ts +10 -0
  82. package/types/apps/src/host.d.ts +16 -0
  83. package/types/apps/src/implementation/account-operations.d.ts +32 -0
  84. package/types/apps/src/implementation/app.d.ts +40 -0
  85. package/types/apps/src/implementation/authoring.d.ts +12 -0
  86. package/types/apps/src/implementation/elicitation.d.ts +103 -0
  87. package/types/apps/src/implementation/graphql.d.ts +5 -0
  88. package/types/apps/src/implementation/host.d.ts +3 -0
  89. package/types/apps/src/implementation/http.d.ts +5 -0
  90. package/types/apps/src/implementation/live.d.ts +18 -0
  91. package/types/apps/src/implementation/mcp-call.d.ts +19 -0
  92. package/types/apps/src/implementation/mcp-client.d.ts +50 -0
  93. package/types/apps/src/implementation/mcp-stdio.d.ts +4 -0
  94. package/types/apps/src/implementation/mcp-tools.d.ts +6 -0
  95. package/types/apps/src/implementation/mcp.d.ts +4 -0
  96. package/types/apps/src/implementation/openapi-request.d.ts +11 -0
  97. package/types/apps/src/implementation/openapi.d.ts +5 -0
  98. package/types/apps/src/implementation/operations.d.ts +65 -0
  99. package/types/apps/src/implementation/optimistic.d.ts +30 -0
  100. package/types/apps/src/implementation/protocol-operations.d.ts +24 -0
  101. package/types/apps/src/implementation/provider.d.ts +16 -0
  102. package/types/apps/src/implementation/query-commit.d.ts +10 -0
  103. package/types/apps/src/implementation/schedules.d.ts +16 -0
  104. package/types/apps/src/implementation/schema.d.ts +93 -0
  105. package/types/apps/src/implementation/storage.d.ts +13 -0
  106. package/types/apps/src/implementation/ui-auth.d.ts +13 -0
  107. package/types/apps/src/implementation/ui-client.d.ts +16 -0
  108. package/types/apps/src/implementation/ui-errors.d.ts +2 -0
  109. package/types/apps/src/implementation/ui-serving.d.ts +16 -0
  110. package/types/apps/src/implementation/ui-watch.d.ts +2 -0
  111. package/types/apps/src/implementation/webhooks.d.ts +29 -0
  112. package/types/apps/src/implementation/workflow-context.d.ts +8 -0
  113. package/types/apps/src/implementation/workflow-rpc.d.ts +5 -0
  114. package/types/apps/src/implementation/workflows.d.ts +27 -0
  115. package/types/apps/src/index.d.ts +58 -0
  116. package/types/apps/src/mcp-stdio.d.ts +8 -0
  117. package/types/apps/src/mcp.d.ts +10 -0
  118. package/types/apps/src/openapi.d.ts +10 -0
  119. package/types/apps/src/react.d.ts +7 -0
  120. package/types/apps/src/ui-auth.d.ts +3 -0
  121. package/types/apps/src/ui-serving.d.ts +3 -0
  122. package/types/telemetry/src/browser-operations.d.ts +16 -0
  123. package/types/telemetry/src/browser-performance.d.ts +11 -0
  124. package/types/telemetry/src/browser-request-timing.d.ts +2 -0
  125. package/types/telemetry/src/browser.d.ts +27 -0
  126. package/types/telemetry/src/config.d.ts +83 -0
  127. package/types/telemetry/src/context.d.ts +14 -0
  128. package/types/telemetry/src/index.d.ts +9 -0
  129. package/types/telemetry/src/layer.d.ts +7 -0
  130. package/types/telemetry/src/measurements.d.ts +8 -0
  131. package/types/telemetry/src/pending-span.d.ts +7 -0
  132. package/types/telemetry/src/relay.d.ts +28 -0
  133. package/types/telemetry/src/span-attributes.d.ts +29 -0
  134. package/types/telemetry/src/trace-context.d.ts +20 -0
  135. package/types/telemetry/src/transport.d.ts +6 -0
@@ -0,0 +1,2357 @@
1
+ import {
2
+ Reactivity,
3
+ invalidate,
4
+ layer,
5
+ mutation
6
+ } from "./chunk-D6BKYP2L.js";
7
+ import {
8
+ Boolean,
9
+ Cause,
10
+ Composite,
11
+ Defect,
12
+ Int,
13
+ InvalidType,
14
+ MixedScheduler,
15
+ Never,
16
+ NoSuchElementError,
17
+ Option,
18
+ Option_exports,
19
+ PipeInspectableProto,
20
+ Pointer,
21
+ Scheduler,
22
+ Scope,
23
+ Service,
24
+ TaggedError,
25
+ TaggedStruct,
26
+ Union,
27
+ acquireRelease,
28
+ add,
29
+ addFinalizer,
30
+ appendAll,
31
+ as,
32
+ asEquivalence,
33
+ asSome,
34
+ assignProperty,
35
+ buildWithMemoMap,
36
+ callback,
37
+ callback2,
38
+ catchCause,
39
+ changes,
40
+ changes2,
41
+ close,
42
+ combine,
43
+ constTrue,
44
+ constVoid,
45
+ constant,
46
+ contextWith,
47
+ declareConstructor,
48
+ decodeEffect,
49
+ decodeExit,
50
+ decodeSync,
51
+ decodeUnknownEffect,
52
+ dual,
53
+ effect,
54
+ empty,
55
+ empty2,
56
+ empty3,
57
+ empty7 as empty4,
58
+ encodeEffect,
59
+ encodeExit,
60
+ encodeSync,
61
+ ensuring2 as ensuring,
62
+ equals,
63
+ fail2 as fail,
64
+ fail3 as fail2,
65
+ fail4 as fail3,
66
+ failCause,
67
+ failCause2,
68
+ filter,
69
+ filter2,
70
+ filterInterruptors,
71
+ findDefect,
72
+ findError,
73
+ findErrorOption,
74
+ flatMap,
75
+ flatMap2,
76
+ flatMapEager,
77
+ flatMapNullishOr,
78
+ forkDetach,
79
+ fromInputUnsafe,
80
+ fromJsonString,
81
+ gen,
82
+ get,
83
+ get2,
84
+ get5 as get3,
85
+ getCurrent,
86
+ getOrElse,
87
+ getOrNull,
88
+ getOrThrowWith,
89
+ getOrUndefined,
90
+ getStackTraceLimit,
91
+ getSuccess,
92
+ getUnsafe,
93
+ hasInterruptsOnly,
94
+ hasProperty,
95
+ hash,
96
+ identity,
97
+ interrupt,
98
+ isDoneCause,
99
+ isEffect,
100
+ isExit,
101
+ isFailure,
102
+ isFinite,
103
+ isIterable,
104
+ isNone,
105
+ isReadonlyArrayNonEmpty,
106
+ isSome,
107
+ isStream,
108
+ isSubscriptionRef,
109
+ joinAll,
110
+ lastNonEmpty,
111
+ link,
112
+ makeCompositeAtKey,
113
+ makeMemoMapUnsafe,
114
+ makeUnsafe2 as makeUnsafe,
115
+ map,
116
+ map3 as map2,
117
+ mapBothEager,
118
+ mapEager,
119
+ mapEffect,
120
+ mapErrorEager,
121
+ match,
122
+ match2,
123
+ matchCauseEffect,
124
+ never,
125
+ none,
126
+ offerUnsafe,
127
+ pipe,
128
+ pipeArguments,
129
+ provide2 as provide,
130
+ provideMerge,
131
+ remove,
132
+ runCallbackWith,
133
+ runForEachArray,
134
+ runForkWith,
135
+ runSync,
136
+ runSyncWith,
137
+ scope,
138
+ scopedWith,
139
+ set,
140
+ set5 as set2,
141
+ some,
142
+ squash,
143
+ string,
144
+ succeed2 as succeed,
145
+ succeed4 as succeed2,
146
+ succeedNone,
147
+ suspend,
148
+ symbol,
149
+ symbol2,
150
+ sync2 as sync,
151
+ toCodecJson,
152
+ toMillis,
153
+ toPull,
154
+ toPullScoped,
155
+ transform,
156
+ unwrap3 as unwrap,
157
+ void_,
158
+ void_2,
159
+ whileLoop,
160
+ zipRight
161
+ } from "./chunk-2OTCEKL2.js";
162
+ import {
163
+ __export
164
+ } from "./chunk-MLKGABMK.js";
165
+
166
+ // ../../node_modules/.bun/effect@https+++pkg.pr.new+Effect-TS+effect+effect@c7d1ffff31a3e2ae0b0b0796b742bddeb797269d/node_modules/effect/dist/unstable/reactivity/AsyncResult.js
167
+ var AsyncResult_exports = {};
168
+ __export(AsyncResult_exports, {
169
+ Schema: () => Schema,
170
+ TypeId: () => TypeId,
171
+ all: () => all,
172
+ builder: () => builder,
173
+ cause: () => cause,
174
+ error: () => error,
175
+ fail: () => fail4,
176
+ failWithPrevious: () => failWithPrevious,
177
+ failure: () => failure,
178
+ failureWithPrevious: () => failureWithPrevious,
179
+ flatMap: () => flatMap3,
180
+ fromExit: () => fromExit,
181
+ fromExitWithPrevious: () => fromExitWithPrevious,
182
+ getOrElse: () => getOrElse2,
183
+ getOrThrow: () => getOrThrow,
184
+ initial: () => initial,
185
+ isAsyncResult: () => isAsyncResult,
186
+ isFailure: () => isFailure2,
187
+ isInitial: () => isInitial,
188
+ isInterrupted: () => isInterrupted,
189
+ isNotInitial: () => isNotInitial,
190
+ isSuccess: () => isSuccess,
191
+ isWaiting: () => isWaiting,
192
+ map: () => map3,
193
+ match: () => match3,
194
+ matchWithError: () => matchWithError,
195
+ matchWithWaiting: () => matchWithWaiting,
196
+ replacePrevious: () => replacePrevious,
197
+ success: () => success,
198
+ toExit: () => toExit,
199
+ touch: () => touch,
200
+ value: () => value,
201
+ waiting: () => waiting,
202
+ waitingFrom: () => waitingFrom
203
+ });
204
+ var TypeId = "~effect/reactivity/AsyncResult";
205
+ var isAsyncResult = (u) => hasProperty(u, TypeId);
206
+ var ResultProto = {
207
+ [TypeId]: {
208
+ E: identity,
209
+ A: identity
210
+ },
211
+ pipe() {
212
+ return pipeArguments(this, arguments);
213
+ },
214
+ [symbol2](that) {
215
+ if (this._tag !== that._tag || this.waiting !== that.waiting) {
216
+ return false;
217
+ }
218
+ switch (this._tag) {
219
+ case "Initial":
220
+ return true;
221
+ case "Success":
222
+ return equals(this.value, that.value);
223
+ case "Failure":
224
+ return equals(this.cause, that.cause);
225
+ }
226
+ },
227
+ [symbol]() {
228
+ const tagHash = string(`${this._tag}:${this.waiting}`);
229
+ if (this._tag === "Initial") {
230
+ return tagHash;
231
+ }
232
+ return combine(tagHash)(this._tag === "Success" ? hash(this.value) : hash(this.cause));
233
+ }
234
+ };
235
+ var isWaiting = (result) => result.waiting;
236
+ var fromExit = (exit) => exit._tag === "Success" ? success(exit.value) : failure(exit.cause);
237
+ var fromExitWithPrevious = (exit, previous) => exit._tag === "Success" ? success(exit.value) : failureWithPrevious(exit.cause, {
238
+ previous
239
+ });
240
+ var waitingFrom = (previous) => {
241
+ if (previous._tag === "None") {
242
+ return initial(true);
243
+ }
244
+ return waiting(previous.value);
245
+ };
246
+ var isInitial = (result) => result._tag === "Initial";
247
+ var isNotInitial = (result) => result._tag !== "Initial";
248
+ var initial = (waiting2 = false) => {
249
+ const result = Object.create(ResultProto);
250
+ result._tag = "Initial";
251
+ result.waiting = waiting2;
252
+ return result;
253
+ };
254
+ var isSuccess = (result) => result._tag === "Success";
255
+ var success = (value2, options) => {
256
+ const result = Object.create(ResultProto);
257
+ result._tag = "Success";
258
+ result.value = value2;
259
+ result.waiting = options?.waiting ?? false;
260
+ result.timestamp = options?.timestamp ?? Date.now();
261
+ return result;
262
+ };
263
+ var isFailure2 = (result) => result._tag === "Failure";
264
+ var isInterrupted = (result) => result._tag === "Failure" && hasInterruptsOnly(result.cause);
265
+ var failure = (cause2, options) => {
266
+ const result = Object.create(ResultProto);
267
+ result._tag = "Failure";
268
+ result.cause = cause2;
269
+ result.previousSuccess = options?.previousSuccess ?? none();
270
+ result.waiting = options?.waiting ?? false;
271
+ return result;
272
+ };
273
+ var failureWithPrevious = (cause2, options) => failure(cause2, {
274
+ previousSuccess: flatMap(options.previous, (result) => isSuccess(result) ? some(result) : isFailure2(result) ? result.previousSuccess : none()),
275
+ waiting: options.waiting
276
+ });
277
+ var fail4 = (error2, options) => failure(fail(error2), options);
278
+ var failWithPrevious = (error2, options) => failureWithPrevious(fail(error2), options);
279
+ var waiting = (self, options) => {
280
+ if (self.waiting) {
281
+ return options?.touch ? touch(self) : self;
282
+ }
283
+ const result = Object.assign(Object.create(ResultProto), self);
284
+ result.waiting = true;
285
+ if (options?.touch && isSuccess(result)) {
286
+ ;
287
+ result.timestamp = Date.now();
288
+ }
289
+ return result;
290
+ };
291
+ var touch = (result) => {
292
+ if (isSuccess(result)) {
293
+ return success(result.value, {
294
+ waiting: result.waiting
295
+ });
296
+ }
297
+ return result;
298
+ };
299
+ var replacePrevious = (self, previous) => {
300
+ if (self._tag === "Failure") {
301
+ return failureWithPrevious(self.cause, {
302
+ previous,
303
+ waiting: self.waiting
304
+ });
305
+ }
306
+ return self;
307
+ };
308
+ var value = (self) => {
309
+ if (self._tag === "Success") {
310
+ return some(self.value);
311
+ } else if (self._tag === "Failure") {
312
+ return map(self.previousSuccess, (s) => s.value);
313
+ }
314
+ return none();
315
+ };
316
+ var getOrElse2 = /* @__PURE__ */ dual(2, (self, orElse) => getOrElse(value(self), orElse));
317
+ var getOrThrow = (self) => getOrThrowWith(value(self), () => new NoSuchElementError("AsyncResult.getOrThrow: no value found"));
318
+ var cause = (self) => self._tag === "Failure" ? some(self.cause) : none();
319
+ var error = (self) => self._tag === "Failure" ? findErrorOption(self.cause) : none();
320
+ var toExit = (self) => {
321
+ switch (self._tag) {
322
+ case "Success": {
323
+ return succeed(self.value);
324
+ }
325
+ case "Failure": {
326
+ return failCause(self.cause);
327
+ }
328
+ default: {
329
+ return fail2(new NoSuchElementError());
330
+ }
331
+ }
332
+ };
333
+ var map3 = /* @__PURE__ */ dual(2, (self, f) => {
334
+ switch (self._tag) {
335
+ case "Initial":
336
+ return self;
337
+ case "Failure":
338
+ return failure(self.cause, {
339
+ previousSuccess: map(self.previousSuccess, (s) => success(f(s.value), s)),
340
+ waiting: self.waiting
341
+ });
342
+ case "Success":
343
+ return success(f(self.value), self);
344
+ }
345
+ });
346
+ var flatMap3 = /* @__PURE__ */ dual(2, (self, f) => {
347
+ switch (self._tag) {
348
+ case "Initial":
349
+ return self;
350
+ case "Failure":
351
+ return failure(self.cause, {
352
+ previousSuccess: flatMap(self.previousSuccess, (s) => {
353
+ const next = f(s.value, s);
354
+ return isSuccess(next) ? some(next) : none();
355
+ }),
356
+ waiting: self.waiting
357
+ });
358
+ case "Success":
359
+ return f(self.value, self);
360
+ }
361
+ });
362
+ var match3 = /* @__PURE__ */ dual(2, (self, options) => {
363
+ switch (self._tag) {
364
+ case "Initial":
365
+ return options.onInitial(self);
366
+ case "Failure":
367
+ return options.onFailure(self);
368
+ case "Success":
369
+ return options.onSuccess(self);
370
+ }
371
+ });
372
+ var matchWithError = /* @__PURE__ */ dual(2, (self, options) => {
373
+ switch (self._tag) {
374
+ case "Initial":
375
+ return options.onInitial(self);
376
+ case "Failure": {
377
+ const result = findError(self.cause);
378
+ if (isFailure(result)) {
379
+ return options.onDefect(squash(result.failure), self);
380
+ }
381
+ return options.onError(result.success, self);
382
+ }
383
+ case "Success":
384
+ return options.onSuccess(self);
385
+ }
386
+ });
387
+ var matchWithWaiting = /* @__PURE__ */ dual(2, (self, options) => {
388
+ if (self.waiting) {
389
+ return options.onWaiting(self);
390
+ }
391
+ switch (self._tag) {
392
+ case "Initial":
393
+ return options.onWaiting(self);
394
+ case "Failure": {
395
+ const e = findError(self.cause);
396
+ if (isFailure(e)) {
397
+ return options.onDefect(squash(e.failure), self);
398
+ }
399
+ return options.onError(e.success, self);
400
+ }
401
+ case "Success":
402
+ return options.onSuccess(self);
403
+ }
404
+ });
405
+ var all = (results) => {
406
+ const isIter = isIterable(results);
407
+ const entries = isIter ? Array.from(results, (result, i) => [i, result]) : Object.entries(results);
408
+ const successes = isIter ? [] : {};
409
+ let waiting2 = false;
410
+ for (let i = 0; i < entries.length; i++) {
411
+ const [key, result] = entries[i];
412
+ if (!isAsyncResult(result)) {
413
+ assignProperty(successes, key, result);
414
+ continue;
415
+ } else if (!isSuccess(result)) {
416
+ return result;
417
+ }
418
+ assignProperty(successes, key, result.value);
419
+ if (result.waiting) {
420
+ waiting2 = true;
421
+ }
422
+ }
423
+ return success(successes, {
424
+ waiting: waiting2
425
+ });
426
+ };
427
+ var builder = (self) => new BuilderImpl(self);
428
+ var BuilderImpl = class {
429
+ constructor(result) {
430
+ this.result = result;
431
+ }
432
+ result;
433
+ output = /* @__PURE__ */ none();
434
+ when(refinement, f) {
435
+ if (isNone(this.output) && refinement(this.result)) {
436
+ const b = f(this.result);
437
+ if (isSome(b)) {
438
+ ;
439
+ this.output = b;
440
+ }
441
+ }
442
+ return this;
443
+ }
444
+ pipe() {
445
+ return pipeArguments(this, arguments);
446
+ }
447
+ onWaiting(f) {
448
+ return this.when((r) => r.waiting, (r) => some(f(r)));
449
+ }
450
+ onInitialOrWaiting(f) {
451
+ return this.when((r) => isInitial(r) || r.waiting, (r) => some(f(r)));
452
+ }
453
+ onInitial(f) {
454
+ return this.when(isInitial, (r) => some(f(r)));
455
+ }
456
+ onSuccess(f) {
457
+ return this.when(isSuccess, (r) => some(f(r.value, r)));
458
+ }
459
+ onFailure(f) {
460
+ return this.when(isFailure2, (r) => some(f(r.cause, r)));
461
+ }
462
+ onError(f) {
463
+ return this.onErrorIf(constTrue, f);
464
+ }
465
+ onErrorIf(refinement, f) {
466
+ return this.when(isFailure2, (result) => findErrorOption(result.cause).pipe(filter(refinement), map((error2) => f(error2, result))));
467
+ }
468
+ onErrorTag(tag, f) {
469
+ return this.onErrorIf((e) => hasProperty(e, "_tag") && (Array.isArray(tag) ? tag.includes(e._tag) : e._tag === tag), f);
470
+ }
471
+ onDefect(f) {
472
+ return this.when(isFailure2, (result) => {
473
+ const defect = findDefect(result.cause);
474
+ return isFailure(defect) ? none() : some(f(defect.success, result));
475
+ });
476
+ }
477
+ onInterrupt(f) {
478
+ return this.when(isFailure2, (result) => {
479
+ const interruptors = filterInterruptors(result.cause);
480
+ return isFailure(interruptors) ? none() : some(f(interruptors.success, result));
481
+ });
482
+ }
483
+ orElse(orElse) {
484
+ return getOrElse(this.output, orElse);
485
+ }
486
+ orNull() {
487
+ return getOrNull(this.output);
488
+ }
489
+ render() {
490
+ if (isSome(this.output)) {
491
+ return this.output.value;
492
+ } else if (isFailure2(this.result)) {
493
+ throw squash(this.result.cause);
494
+ }
495
+ return null;
496
+ }
497
+ exhaustive() {
498
+ return this.render();
499
+ }
500
+ };
501
+ var Schema = (options) => {
502
+ const success_ = options.success ?? Never;
503
+ const error2 = options.error ?? Never;
504
+ const schema = declareConstructor()([success_, Cause(error2, Defect())], ([value2, cause2]) => (input, ast, options2) => {
505
+ if (!isAsyncResult(input)) {
506
+ return fail3(new InvalidType(ast, input, options2));
507
+ }
508
+ switch (input._tag) {
509
+ case "Initial":
510
+ return succeed2(input);
511
+ case "Success":
512
+ return mapBothEager(decodeUnknownEffect(value2)(input.value, options2), {
513
+ onSuccess: (value3) => success(value3, input),
514
+ onFailure: (issue) => makeCompositeAtKey(ast, "value", issue, input, options2)
515
+ });
516
+ case "Failure": {
517
+ const prevSuccessEffect = input.previousSuccess.pipe(map((ps) => mapBothEager(decodeUnknownEffect(value2)(ps.value, options2), {
518
+ onSuccess: (value3) => some(success(value3, ps)),
519
+ onFailure: (issue) => new Composite(ast, [new Pointer(["previousSuccess", "value"], issue)], input, options2)
520
+ })), getOrElse(() => succeedNone));
521
+ const causeEffect = mapErrorEager(decodeUnknownEffect(cause2)(input.cause, options2), (issue) => makeCompositeAtKey(ast, "cause", issue, input, options2));
522
+ return flatMapEager(prevSuccessEffect, (previousSuccess) => mapEager(causeEffect, (cause3) => failure(cause3, {
523
+ previousSuccess,
524
+ waiting: input.waiting
525
+ })));
526
+ }
527
+ }
528
+ }, {
529
+ expected: "AsyncResult",
530
+ toCodec([value2, cause2]) {
531
+ const Success = TaggedStruct("Success", {
532
+ value: value2,
533
+ waiting: Boolean,
534
+ timestamp: Int
535
+ });
536
+ return link()(Union([TaggedStruct("Initial", {
537
+ waiting: Boolean
538
+ }), Success, TaggedStruct("Failure", {
539
+ cause: cause2,
540
+ previousSuccess: Option(Success),
541
+ waiting: Boolean
542
+ })]), transform({
543
+ decode: (encoded) => {
544
+ switch (encoded._tag) {
545
+ case "Initial":
546
+ return initial(encoded.waiting);
547
+ case "Success":
548
+ return success(encoded.value, {
549
+ waiting: encoded.waiting,
550
+ timestamp: encoded.timestamp
551
+ });
552
+ case "Failure": {
553
+ return failure(encoded.cause, {
554
+ previousSuccess: map(encoded.previousSuccess, (ps) => success(ps.value, ps)),
555
+ waiting: encoded.waiting
556
+ });
557
+ }
558
+ }
559
+ },
560
+ encode(result) {
561
+ switch (result._tag) {
562
+ case "Initial":
563
+ return {
564
+ _tag: "Initial",
565
+ waiting: result.waiting
566
+ };
567
+ case "Success":
568
+ return {
569
+ _tag: "Success",
570
+ value: result.value,
571
+ waiting: result.waiting,
572
+ timestamp: result.timestamp
573
+ };
574
+ case "Failure":
575
+ return {
576
+ _tag: "Failure",
577
+ cause: result.cause,
578
+ previousSuccess: result.previousSuccess,
579
+ waiting: result.waiting
580
+ };
581
+ }
582
+ }
583
+ }));
584
+ },
585
+ toEquivalence: asEquivalence,
586
+ toFormatter: ([value2, cause2]) => (t) => {
587
+ switch (t._tag) {
588
+ case "Success":
589
+ return `AsyncResult.Success(${value2(t.value)}, ${t.waiting}, ${t.timestamp})`;
590
+ case "Failure":
591
+ return `AsyncResult.Failure(${cause2(t.cause)}, ${t.waiting})`;
592
+ case "Initial":
593
+ return `AsyncResult.Initial(${t.waiting}, ${t.waiting})`;
594
+ }
595
+ }
596
+ });
597
+ return Object.assign(schema, {
598
+ success: success_,
599
+ error: error2
600
+ });
601
+ };
602
+
603
+ // ../../node_modules/.bun/effect@https+++pkg.pr.new+Effect-TS+effect+effect@c7d1ffff31a3e2ae0b0b0796b742bddeb797269d/node_modules/effect/dist/unstable/reactivity/AtomRegistry.js
604
+ var AtomRegistry_exports = {};
605
+ __export(AtomRegistry_exports, {
606
+ AtomRegistry: () => AtomRegistry,
607
+ BatchPhase: () => BatchPhase,
608
+ TypeId: () => TypeId2,
609
+ batch: () => batch,
610
+ batchState: () => batchState,
611
+ getResult: () => getResult,
612
+ isAtomRegistry: () => isAtomRegistry,
613
+ layer: () => layer2,
614
+ layerOptions: () => layerOptions,
615
+ make: () => make,
616
+ mount: () => mount,
617
+ toStream: () => toStream,
618
+ toStreamResult: () => toStreamResult
619
+ });
620
+ var TypeId2 = "~effect/reactivity/AtomRegistry";
621
+ var isAtomRegistry = (u) => hasProperty(u, TypeId2);
622
+ var make = (options) => new RegistryImpl(options?.initialValues, options?.scheduleTask, options?.timeoutResolution, options?.defaultIdleTTL);
623
+ var AtomRegistry = /* @__PURE__ */ Service(TypeId2);
624
+ var layerOptions = (options) => effect(AtomRegistry, gen(function* () {
625
+ const scope2 = yield* scope;
626
+ const registry = make({
627
+ ...options,
628
+ scheduleTask: options?.scheduleTask
629
+ });
630
+ yield* addFinalizer(scope2, sync(() => registry.dispose()));
631
+ return registry;
632
+ }));
633
+ var layer2 = /* @__PURE__ */ layerOptions();
634
+ var toStream = /* @__PURE__ */ dual(2, (self, atom) => callback2((queue) => suspend(() => {
635
+ const fiber = getCurrent();
636
+ const scope2 = getUnsafe(fiber.context, Scope);
637
+ const cancel = self.subscribe(atom, (value2) => offerUnsafe(queue, value2), {
638
+ immediate: true
639
+ });
640
+ return addFinalizer(scope2, sync(cancel));
641
+ })));
642
+ var toStreamResult = /* @__PURE__ */ dual(2, (self, atom) => toStream(self, atom).pipe(filter2(isNotInitial), mapEffect((result) => result._tag === "Success" ? succeed2(result.value) : failCause2(result.cause)), changes));
643
+ var getResult = /* @__PURE__ */ dual((args) => isAtomRegistry(args[0]), (self, atom, options) => {
644
+ const suspendOnWaiting = options?.suspendOnWaiting ?? false;
645
+ return callback((resume) => {
646
+ const result = self.get(atom);
647
+ if (result._tag !== "Initial" && !(suspendOnWaiting && result.waiting)) {
648
+ return resume(toExit(result));
649
+ }
650
+ const cancel = self.subscribe(atom, (value2) => {
651
+ if (value2._tag !== "Initial" && !(suspendOnWaiting && value2.waiting)) {
652
+ resume(toExit(value2));
653
+ cancel();
654
+ }
655
+ });
656
+ return sync(cancel);
657
+ });
658
+ });
659
+ var mount = /* @__PURE__ */ dual(2, (self, atom) => acquireRelease(sync(() => self.mount(atom)), (release) => sync(release)));
660
+ var constImmediate = {
661
+ immediate: true
662
+ };
663
+ var notifyListener = (listener) => {
664
+ listener();
665
+ };
666
+ var SerializableTypeId = "~effect-atom/atom/Atom/Serializable";
667
+ var atomKey = (atom) => SerializableTypeId in atom ? atom[SerializableTypeId].key : atom;
668
+ var RegistryImpl = class {
669
+ [TypeId2];
670
+ timeoutResolution;
671
+ defaultIdleTTL;
672
+ scheduler;
673
+ schedulerAsync;
674
+ dispatcher;
675
+ onNodeAdded;
676
+ onNodeRemoved;
677
+ constructor(initialValues, scheduleTask, timeoutResolution, defaultIdleTTL) {
678
+ this[TypeId2] = TypeId2;
679
+ this.scheduler = new MixedScheduler("sync", scheduleTask);
680
+ this.schedulerAsync = new MixedScheduler("async", scheduleTask);
681
+ this.dispatcher = this.schedulerAsync.makeDispatcher();
682
+ this.defaultIdleTTL = defaultIdleTTL;
683
+ if (timeoutResolution === void 0 && defaultIdleTTL !== void 0) {
684
+ this.timeoutResolution = Math.round(defaultIdleTTL / 2);
685
+ } else {
686
+ this.timeoutResolution = timeoutResolution ?? 1e3;
687
+ }
688
+ if (initialValues !== void 0) {
689
+ for (const [atom, value2] of initialValues) {
690
+ let target = atom;
691
+ while (target.initialValueTarget) {
692
+ target = target.initialValueTarget;
693
+ }
694
+ this.ensureNode(target).setInitialValue(value2);
695
+ }
696
+ }
697
+ }
698
+ nodes = /* @__PURE__ */ new Map();
699
+ preloadedSerializable = /* @__PURE__ */ new Map();
700
+ timeoutBuckets = /* @__PURE__ */ new Map();
701
+ nodeTimeoutBucket = /* @__PURE__ */ new Map();
702
+ disposed = false;
703
+ getNodes() {
704
+ return this.nodes;
705
+ }
706
+ get(atom) {
707
+ return this.ensureNode(atom).value();
708
+ }
709
+ set(atom, value2) {
710
+ atom.write(this.ensureNode(atom).writeContext, value2);
711
+ }
712
+ setSerializable(key, encoded) {
713
+ this.preloadedSerializable.set(key, encoded);
714
+ }
715
+ modify(atom, f) {
716
+ const node = this.ensureNode(atom);
717
+ const result = f(node.value());
718
+ atom.write(node.writeContext, result[1]);
719
+ return result[0];
720
+ }
721
+ update(atom, f) {
722
+ const node = this.ensureNode(atom);
723
+ atom.write(node.writeContext, f(node.value()));
724
+ }
725
+ refresh = (atom) => {
726
+ if (atom.refresh !== void 0) {
727
+ atom.refresh(this.refresh);
728
+ } else {
729
+ this.invalidateAtom(atom);
730
+ }
731
+ };
732
+ subscribe(atom, f, options) {
733
+ const node = this.ensureNode(atom);
734
+ if (options?.immediate) {
735
+ f(node.value());
736
+ }
737
+ const remove2 = node.subscribe(function() {
738
+ f(node._value);
739
+ });
740
+ return () => {
741
+ remove2();
742
+ if (node.canBeRemoved) {
743
+ this.scheduleNodeRemoval(node);
744
+ }
745
+ };
746
+ }
747
+ mount(atom) {
748
+ return this.subscribe(atom, constVoid, constImmediate);
749
+ }
750
+ atomHasTtl(atom) {
751
+ return !atom.keepAlive && atom.idleTTL !== 0 && (atom.idleTTL !== void 0 || this.defaultIdleTTL !== void 0);
752
+ }
753
+ ensureNode(atom) {
754
+ const key = atomKey(atom);
755
+ let node = this.nodes.get(key);
756
+ if (node === void 0) {
757
+ node = this.createNode(atom);
758
+ this.nodes.set(key, node);
759
+ this.onNodeAdded?.(node);
760
+ } else if (this.atomHasTtl(atom)) {
761
+ this.removeNodeTimeout(node);
762
+ }
763
+ if (typeof key === "string" && this.preloadedSerializable.has(key)) {
764
+ const encoded = this.preloadedSerializable.get(key);
765
+ this.preloadedSerializable.delete(key);
766
+ const decoded = atom[SerializableTypeId].decode(encoded);
767
+ let target = atom;
768
+ while (target.initialValueTarget) {
769
+ target = target.initialValueTarget;
770
+ }
771
+ if (target === atom) {
772
+ node.setValue(decoded);
773
+ } else {
774
+ this.ensureNode(target).setInitialValue(decoded);
775
+ }
776
+ }
777
+ return node;
778
+ }
779
+ createNode(atom) {
780
+ if (this.disposed) {
781
+ throw new Error(`Cannot access Atom ${atom}: registry is disposed`);
782
+ }
783
+ if (!atom.keepAlive) {
784
+ this.scheduleAtomRemoval(atom);
785
+ }
786
+ return new NodeImpl(this, atom);
787
+ }
788
+ invalidateAtom = (atom) => {
789
+ this.ensureNode(atom).invalidate();
790
+ };
791
+ scheduleAtomRemoval(atom) {
792
+ this.dispatcher.scheduleTask(() => {
793
+ const node = this.nodes.get(atomKey(atom));
794
+ if (node !== void 0 && node.canBeRemoved) {
795
+ this.removeNode(node);
796
+ }
797
+ }, 0);
798
+ }
799
+ scheduleNodeRemoval(node) {
800
+ this.dispatcher.scheduleTask(() => {
801
+ if (node.canBeRemoved) {
802
+ this.removeNode(node);
803
+ }
804
+ }, 0);
805
+ }
806
+ removeNode(node) {
807
+ if (this.atomHasTtl(node.atom)) {
808
+ this.setNodeTimeout(node);
809
+ } else {
810
+ this.nodes.delete(atomKey(node.atom));
811
+ node.remove();
812
+ this.onNodeRemoved?.(node);
813
+ }
814
+ }
815
+ setNodeTimeout(node) {
816
+ if (this.nodeTimeoutBucket.has(node)) {
817
+ return;
818
+ }
819
+ let idleTTL = node.atom.idleTTL ?? this.defaultIdleTTL;
820
+ if (this.#currentSweepTTL !== null) {
821
+ idleTTL -= this.#currentSweepTTL;
822
+ if (idleTTL <= 0) {
823
+ this.nodes.delete(atomKey(node.atom));
824
+ node.remove();
825
+ this.onNodeRemoved?.(node);
826
+ return;
827
+ }
828
+ }
829
+ const ttl = Math.ceil(idleTTL / this.timeoutResolution) * this.timeoutResolution;
830
+ const timestamp = Date.now() + ttl;
831
+ const bucket = timestamp - timestamp % this.timeoutResolution + this.timeoutResolution;
832
+ let entry = this.timeoutBuckets.get(bucket);
833
+ if (entry === void 0) {
834
+ entry = [/* @__PURE__ */ new Set(), setTimeout(() => this.sweepBucket(bucket), bucket - Date.now())];
835
+ this.timeoutBuckets.set(bucket, entry);
836
+ }
837
+ entry[0].add(node);
838
+ this.nodeTimeoutBucket.set(node, bucket);
839
+ }
840
+ removeNodeTimeout(node) {
841
+ const bucket = this.nodeTimeoutBucket.get(node);
842
+ if (bucket === void 0) return;
843
+ this.nodeTimeoutBucket.delete(node);
844
+ this.scheduleNodeRemoval(node);
845
+ const [nodes, handle] = this.timeoutBuckets.get(bucket);
846
+ nodes.delete(node);
847
+ if (nodes.size === 0) {
848
+ clearTimeout(handle);
849
+ this.timeoutBuckets.delete(bucket);
850
+ }
851
+ }
852
+ #currentSweepTTL = null;
853
+ sweepBucket(bucket) {
854
+ const nodes = this.timeoutBuckets.get(bucket)[0];
855
+ this.timeoutBuckets.delete(bucket);
856
+ nodes.forEach((node) => {
857
+ this.nodeTimeoutBucket.delete(node);
858
+ if (!node.canBeRemoved) return;
859
+ this.nodes.delete(atomKey(node.atom));
860
+ this.onNodeRemoved?.(node);
861
+ this.#currentSweepTTL = node.atom.idleTTL ?? this.defaultIdleTTL;
862
+ node.remove();
863
+ this.#currentSweepTTL = null;
864
+ });
865
+ }
866
+ reset() {
867
+ this.timeoutBuckets.forEach(([, handle]) => clearTimeout(handle));
868
+ this.timeoutBuckets.clear();
869
+ this.nodeTimeoutBucket.clear();
870
+ this.nodes.forEach((node) => {
871
+ node.remove();
872
+ this.onNodeRemoved?.(node);
873
+ });
874
+ this.nodes.clear();
875
+ }
876
+ dispose() {
877
+ this.disposed = true;
878
+ this.reset();
879
+ }
880
+ };
881
+ var NodeFlags = {
882
+ alive: 1,
883
+ // 1 << 0
884
+ initialized: 2,
885
+ // 1 << 1,
886
+ waitingForValue: 4
887
+ // 1 << 2
888
+ };
889
+ var NodeState = {
890
+ uninitialized: NodeFlags.alive | NodeFlags.waitingForValue,
891
+ stale: NodeFlags.alive | NodeFlags.initialized | NodeFlags.waitingForValue,
892
+ valid: NodeFlags.alive | NodeFlags.initialized,
893
+ removed: 0
894
+ };
895
+ var NodeImpl = class {
896
+ constructor(registry, atom) {
897
+ this.registry = registry;
898
+ this.atom = atom;
899
+ this.writeContext = new WriteContextImpl(registry, this);
900
+ }
901
+ registry;
902
+ atom;
903
+ state = NodeState.uninitialized;
904
+ lifetime;
905
+ writeContext;
906
+ preserveInitialValueOnBuild = false;
907
+ parents = /* @__PURE__ */ new Set();
908
+ previousParents;
909
+ children = /* @__PURE__ */ new Set();
910
+ listeners = /* @__PURE__ */ new Set();
911
+ skipInvalidation = false;
912
+ building = false;
913
+ invalidatedDuringBuild = false;
914
+ currentState() {
915
+ switch (this.state) {
916
+ case NodeState.uninitialized:
917
+ return "uninitialized";
918
+ case NodeState.stale:
919
+ return "stale";
920
+ case NodeState.valid:
921
+ return "valid";
922
+ default:
923
+ return "removed";
924
+ }
925
+ }
926
+ get canBeRemoved() {
927
+ return !this.atom.keepAlive && this.listeners.size === 0 && this.children.size === 0 && this.state !== 0;
928
+ }
929
+ _value = void 0;
930
+ value() {
931
+ if ((this.state & NodeFlags.waitingForValue) !== 0) {
932
+ this.lifetime = makeLifetime(this);
933
+ this.building = true;
934
+ const value2 = this.atom.read(this.lifetime);
935
+ this.building = false;
936
+ if ((this.state & NodeFlags.waitingForValue) !== 0) {
937
+ if (this.preserveInitialValueOnBuild) {
938
+ this.preserveInitialValueOnBuild = false;
939
+ this.state = NodeState.valid;
940
+ } else {
941
+ this.setValue(value2);
942
+ }
943
+ }
944
+ if (this.previousParents) {
945
+ const parents = this.previousParents;
946
+ this.previousParents = void 0;
947
+ for (const parent of parents) {
948
+ parent.removeChild(this);
949
+ if (parent.canBeRemoved) {
950
+ this.registry.scheduleNodeRemoval(parent);
951
+ }
952
+ }
953
+ }
954
+ }
955
+ return this._value;
956
+ }
957
+ valueOption() {
958
+ if ((this.state & NodeFlags.initialized) === 0) {
959
+ return none();
960
+ }
961
+ return some(this._value);
962
+ }
963
+ setInitialValue(value2) {
964
+ if ((this.state & NodeFlags.initialized) === 0) {
965
+ this.preserveInitialValueOnBuild = true;
966
+ this.state = NodeState.stale;
967
+ this._value = value2;
968
+ if (batchState.phase === BatchPhase.collect) {
969
+ batchState.notify.add(this);
970
+ } else {
971
+ this.notify();
972
+ }
973
+ return;
974
+ }
975
+ this.setValue(value2);
976
+ }
977
+ setValue(value2) {
978
+ if ((this.state & NodeFlags.initialized) === 0) {
979
+ this.state = NodeState.valid;
980
+ this._value = value2;
981
+ if (batchState.phase === BatchPhase.collect) {
982
+ batchState.notify.add(this);
983
+ } else {
984
+ this.notify();
985
+ }
986
+ return;
987
+ }
988
+ this.state = NodeState.valid;
989
+ if (this.atom.equals(this._value, value2)) {
990
+ return;
991
+ }
992
+ this._value = value2;
993
+ if (this.skipInvalidation) {
994
+ this.skipInvalidation = false;
995
+ } else {
996
+ this.invalidateChildren();
997
+ }
998
+ if (this.listeners.size > 0) {
999
+ if (batchState.phase === BatchPhase.collect) {
1000
+ batchState.notify.add(this);
1001
+ } else {
1002
+ this.notify();
1003
+ }
1004
+ }
1005
+ }
1006
+ addParent(parent) {
1007
+ this.parents.add(parent);
1008
+ if (this.previousParents !== void 0) {
1009
+ this.previousParents.delete(parent);
1010
+ if (this.previousParents.size === 0) {
1011
+ this.previousParents = void 0;
1012
+ }
1013
+ }
1014
+ if (!parent.children.has(this)) {
1015
+ parent.children.add(this);
1016
+ if (parent.skipInvalidation) {
1017
+ parent.skipInvalidation = false;
1018
+ }
1019
+ }
1020
+ }
1021
+ removeChild(child) {
1022
+ this.children.delete(child);
1023
+ }
1024
+ invalidate() {
1025
+ if (this.building && batchState.phase === BatchPhase.collect) {
1026
+ this.invalidatedDuringBuild = true;
1027
+ }
1028
+ if (this.state === NodeState.valid) {
1029
+ this.state = NodeState.stale;
1030
+ this.disposeLifetime();
1031
+ }
1032
+ if (batchState.phase === BatchPhase.collect) {
1033
+ batchState.stale.push(this);
1034
+ } else if (this.atom.lazy && this.listeners.size === 0 && !childrenAreActive(this.children)) {
1035
+ this.invalidateChildren();
1036
+ this.skipInvalidation = true;
1037
+ } else {
1038
+ this.value();
1039
+ }
1040
+ }
1041
+ invalidateChildren() {
1042
+ if (this.children.size === 0) {
1043
+ return;
1044
+ }
1045
+ const children = this.children;
1046
+ this.children = /* @__PURE__ */ new Set();
1047
+ for (const child of children) {
1048
+ child.invalidate();
1049
+ }
1050
+ }
1051
+ notify() {
1052
+ this.listeners.forEach(notifyListener);
1053
+ if (batchState.phase === BatchPhase.commit) {
1054
+ batchState.notify.delete(this);
1055
+ }
1056
+ }
1057
+ disposeLifetime() {
1058
+ if (this.lifetime !== void 0) {
1059
+ this.lifetime.dispose();
1060
+ this.lifetime = void 0;
1061
+ }
1062
+ if (this.parents.size !== 0) {
1063
+ this.previousParents = this.parents;
1064
+ this.parents = /* @__PURE__ */ new Set();
1065
+ }
1066
+ }
1067
+ remove() {
1068
+ this.state = NodeState.removed;
1069
+ this.listeners.clear();
1070
+ if (this.lifetime !== void 0) {
1071
+ this.disposeLifetime();
1072
+ }
1073
+ if (this.previousParents === void 0) {
1074
+ return;
1075
+ }
1076
+ const parents = this.previousParents;
1077
+ this.previousParents = void 0;
1078
+ for (const parent of parents) {
1079
+ parent.removeChild(this);
1080
+ if (parent.canBeRemoved) {
1081
+ this.registry.removeNode(parent);
1082
+ }
1083
+ }
1084
+ }
1085
+ subscribe(listener) {
1086
+ this.listeners.add(listener);
1087
+ return () => this.listeners.delete(listener);
1088
+ }
1089
+ };
1090
+ function childrenAreActive(children) {
1091
+ if (children.size === 0) {
1092
+ return false;
1093
+ }
1094
+ let current = children;
1095
+ let stack;
1096
+ let stackIndex = 0;
1097
+ while (current !== void 0) {
1098
+ for (const child of current) {
1099
+ if (!child.atom.lazy || child.listeners.size > 0) {
1100
+ return true;
1101
+ } else if (child.children.size > 0) {
1102
+ if (stack === void 0) {
1103
+ stack = [child.children];
1104
+ } else {
1105
+ stack.push(child.children);
1106
+ }
1107
+ }
1108
+ }
1109
+ current = stack?.[stackIndex++];
1110
+ }
1111
+ return false;
1112
+ }
1113
+ var LifetimeProto = {
1114
+ get registry() {
1115
+ return this.node.registry;
1116
+ },
1117
+ addFinalizer(f) {
1118
+ if (this.disposed) return f();
1119
+ this.finalizers ??= [];
1120
+ this.finalizers.push(f);
1121
+ },
1122
+ get(atom) {
1123
+ if (this.disposed) {
1124
+ return this.node.registry.get(atom);
1125
+ }
1126
+ const parent = this.node.registry.ensureNode(atom);
1127
+ const value2 = parent.value();
1128
+ this.node.addParent(parent);
1129
+ return value2;
1130
+ },
1131
+ result(atom, options) {
1132
+ if (this.disposed || this.isFn) {
1133
+ return this.resultOnce(atom, options);
1134
+ }
1135
+ const result = this.get(atom);
1136
+ if (options?.suspendOnWaiting && result.waiting) {
1137
+ return never;
1138
+ }
1139
+ switch (result._tag) {
1140
+ case "Initial": {
1141
+ return never;
1142
+ }
1143
+ case "Failure": {
1144
+ return failCause(result.cause);
1145
+ }
1146
+ case "Success": {
1147
+ return succeed2(result.value);
1148
+ }
1149
+ }
1150
+ },
1151
+ resultOnce(atom, options) {
1152
+ return callback((resume) => {
1153
+ const result = this.once(atom);
1154
+ if (result._tag !== "Initial" && !(options?.suspendOnWaiting && result.waiting)) {
1155
+ return resume(toExit(result));
1156
+ }
1157
+ const cancel = this.node.registry.subscribe(atom, (result2) => {
1158
+ if (result2._tag === "Initial" || options?.suspendOnWaiting && result2.waiting) return;
1159
+ cancel();
1160
+ resume(toExit(result2));
1161
+ }, {
1162
+ immediate: false
1163
+ });
1164
+ return sync(cancel);
1165
+ });
1166
+ },
1167
+ setResult(atom, value2) {
1168
+ if (this.disposed) return never;
1169
+ this.node.registry.set(atom, value2);
1170
+ return this.resultOnce(atom, {
1171
+ suspendOnWaiting: true
1172
+ });
1173
+ },
1174
+ some(atom) {
1175
+ if (this.disposed || this.isFn) {
1176
+ return this.someOnce(atom);
1177
+ }
1178
+ const result = this.get(atom);
1179
+ return result._tag === "None" ? never : succeed2(result.value);
1180
+ },
1181
+ someOnce(atom) {
1182
+ return callback((resume) => {
1183
+ const result = this.once(atom);
1184
+ if (isSome(result)) {
1185
+ return resume(succeed2(result.value));
1186
+ }
1187
+ const cancel = this.node.registry.subscribe(atom, (result2) => {
1188
+ if (isNone(result2)) return;
1189
+ cancel();
1190
+ resume(succeed2(result2.value));
1191
+ }, {
1192
+ immediate: false
1193
+ });
1194
+ return sync(cancel);
1195
+ });
1196
+ },
1197
+ once(atom) {
1198
+ return this.node.registry.get(atom);
1199
+ },
1200
+ self() {
1201
+ if (this.disposed) return none();
1202
+ return this.node.valueOption();
1203
+ },
1204
+ refresh(atom) {
1205
+ if (this.disposed) return;
1206
+ this.node.registry.refresh(atom);
1207
+ },
1208
+ refreshSelf() {
1209
+ if (this.disposed) return;
1210
+ this.node.invalidate();
1211
+ },
1212
+ mount(atom) {
1213
+ if (this.disposed) return;
1214
+ this.addFinalizer(this.node.registry.mount(atom));
1215
+ },
1216
+ subscribe(atom, f, options) {
1217
+ if (this.disposed) return;
1218
+ this.addFinalizer(this.node.registry.subscribe(atom, f, options));
1219
+ },
1220
+ setSelf(a) {
1221
+ if (this.disposed) return;
1222
+ this.node.setValue(a);
1223
+ },
1224
+ set(atom, value2) {
1225
+ if (this.disposed) return;
1226
+ this.node.registry.set(atom, value2);
1227
+ },
1228
+ stream(atom, options) {
1229
+ if (this.disposed) return empty4;
1230
+ return callback2((queue) => sync(() => {
1231
+ this.subscribe(atom, (value2) => offerUnsafe(queue, value2), {
1232
+ immediate: !options?.withoutInitialValue
1233
+ });
1234
+ }));
1235
+ },
1236
+ streamResult(atom, options) {
1237
+ return this.stream(atom, options).pipe(filter2(isNotInitial), mapEffect((result) => result._tag === "Success" ? succeed2(result.value) : failCause2(result.cause)));
1238
+ },
1239
+ dispose() {
1240
+ this.disposed = true;
1241
+ if (this.finalizers === void 0) {
1242
+ return;
1243
+ }
1244
+ const finalizers = this.finalizers;
1245
+ this.finalizers = void 0;
1246
+ for (let i = finalizers.length - 1; i >= 0; i--) {
1247
+ finalizers[i]();
1248
+ }
1249
+ }
1250
+ };
1251
+ var makeLifetime = (node) => {
1252
+ function get5(atom) {
1253
+ if (get5.disposed) {
1254
+ return node.registry.get(atom);
1255
+ } else if (get5.isFn) {
1256
+ return node.registry.get(atom);
1257
+ }
1258
+ const parent = node.registry.ensureNode(atom);
1259
+ const value2 = parent.value();
1260
+ node.addParent(parent);
1261
+ return value2;
1262
+ }
1263
+ Object.setPrototypeOf(get5, LifetimeProto);
1264
+ get5.isFn = false;
1265
+ get5.disposed = false;
1266
+ get5.finalizers = void 0;
1267
+ get5.node = node;
1268
+ return get5;
1269
+ };
1270
+ var WriteContextImpl = class {
1271
+ constructor(registry, node) {
1272
+ this.registry = registry;
1273
+ this.node = node;
1274
+ }
1275
+ registry;
1276
+ node;
1277
+ get(atom) {
1278
+ return this.registry.get(atom);
1279
+ }
1280
+ set(atom, value2) {
1281
+ return this.registry.set(atom, value2);
1282
+ }
1283
+ setSelf(value2) {
1284
+ return this.node.setValue(value2);
1285
+ }
1286
+ refreshSelf() {
1287
+ return this.node.invalidate();
1288
+ }
1289
+ };
1290
+ var BatchPhase = {
1291
+ disabled: 0,
1292
+ collect: 1,
1293
+ commit: 2
1294
+ };
1295
+ var batchState = {
1296
+ phase: BatchPhase.disabled,
1297
+ depth: 0,
1298
+ stale: [],
1299
+ notify: /* @__PURE__ */ new Set()
1300
+ };
1301
+ function batch(f) {
1302
+ batchState.phase = BatchPhase.collect;
1303
+ batchState.depth++;
1304
+ try {
1305
+ f();
1306
+ if (batchState.depth === 1) {
1307
+ for (let i = 0; i < batchState.stale.length; i++) {
1308
+ batchRebuildNode(batchState.stale[i]);
1309
+ }
1310
+ batchState.phase = BatchPhase.commit;
1311
+ for (const node of batchState.notify) {
1312
+ node.notify();
1313
+ }
1314
+ batchState.notify.clear();
1315
+ }
1316
+ } finally {
1317
+ batchState.depth--;
1318
+ if (batchState.depth === 0) {
1319
+ batchState.phase = BatchPhase.disabled;
1320
+ batchState.stale = [];
1321
+ }
1322
+ }
1323
+ }
1324
+ function batchRebuildNode(node) {
1325
+ if (node.state === NodeState.valid) {
1326
+ if (!node.invalidatedDuringBuild) {
1327
+ return;
1328
+ }
1329
+ node.invalidatedDuringBuild = false;
1330
+ node.state = NodeState.stale;
1331
+ node.disposeLifetime();
1332
+ }
1333
+ for (const parent of node.parents) {
1334
+ if (parent.state !== NodeState.valid) {
1335
+ batchRebuildNode(parent);
1336
+ }
1337
+ }
1338
+ if (node.state !== NodeState.valid) {
1339
+ node.value();
1340
+ }
1341
+ }
1342
+
1343
+ // ../../node_modules/.bun/effect@https+++pkg.pr.new+Effect-TS+effect+effect@c7d1ffff31a3e2ae0b0b0796b742bddeb797269d/node_modules/effect/dist/unstable/reactivity/Atom.js
1344
+ var Atom_exports = {};
1345
+ __export(Atom_exports, {
1346
+ Interrupt: () => Interrupt,
1347
+ Reset: () => Reset,
1348
+ SerializableTypeId: () => SerializableTypeId2,
1349
+ ServerValueTypeId: () => ServerValueTypeId,
1350
+ TypeId: () => TypeId3,
1351
+ WritableTypeId: () => WritableTypeId,
1352
+ autoDispose: () => autoDispose,
1353
+ batch: () => batch2,
1354
+ context: () => context,
1355
+ debounce: () => debounce,
1356
+ family: () => family,
1357
+ fn: () => fn,
1358
+ fnSync: () => fnSync,
1359
+ get: () => get4,
1360
+ getResult: () => getResult2,
1361
+ getServerValue: () => getServerValue,
1362
+ initialValue: () => initialValue,
1363
+ isAtom: () => isAtom,
1364
+ isSerializable: () => isSerializable,
1365
+ isWritable: () => isWritable,
1366
+ keepAlive: () => keepAlive,
1367
+ kvs: () => kvs,
1368
+ make: () => make2,
1369
+ makeRefreshOnSignal: () => makeRefreshOnSignal,
1370
+ map: () => map5,
1371
+ mapResult: () => mapResult,
1372
+ modify: () => modify,
1373
+ mount: () => mount2,
1374
+ optimistic: () => optimistic,
1375
+ optimisticFn: () => optimisticFn,
1376
+ pull: () => pull,
1377
+ readable: () => readable,
1378
+ refresh: () => refresh,
1379
+ refreshOnWindowFocus: () => refreshOnWindowFocus,
1380
+ runtime: () => runtime,
1381
+ searchParam: () => searchParam,
1382
+ serializable: () => serializable,
1383
+ set: () => set3,
1384
+ setIdleTTL: () => setIdleTTL,
1385
+ setLazy: () => setLazy,
1386
+ subscriptionRef: () => subscriptionRef,
1387
+ swr: () => swr,
1388
+ toStream: () => toStream2,
1389
+ toStreamResult: () => toStreamResult2,
1390
+ transform: () => transform2,
1391
+ update: () => update,
1392
+ windowFocusSignal: () => windowFocusSignal,
1393
+ withEquality: () => withEquality,
1394
+ withFallback: () => withFallback,
1395
+ withLabel: () => withLabel,
1396
+ withReactivity: () => withReactivity,
1397
+ withRefresh: () => withRefresh,
1398
+ withServerValue: () => withServerValue,
1399
+ withServerValueInitial: () => withServerValueInitial,
1400
+ writable: () => writable
1401
+ });
1402
+
1403
+ // ../../node_modules/.bun/effect@https+++pkg.pr.new+Effect-TS+effect+effect@c7d1ffff31a3e2ae0b0b0796b742bddeb797269d/node_modules/effect/dist/unstable/persistence/KeyValueStore.js
1404
+ var ErrorTypeId = "~effect/persistence/KeyValueStore/KeyValueStoreError";
1405
+ var KeyValueStoreError = class extends (/* @__PURE__ */ TaggedError("KeyValueStoreError")) {
1406
+ /**
1407
+ * Marks this value as a key-value store error for runtime guards.
1408
+ *
1409
+ * @since 4.0.0
1410
+ */
1411
+ [ErrorTypeId] = ErrorTypeId;
1412
+ };
1413
+ var KeyValueStore = /* @__PURE__ */ Service("effect/persistence/KeyValueStore");
1414
+ var SchemaStoreTypeId = "~effect/persistence/KeyValueStore/SchemaStore";
1415
+ var toSchemaStore = (self, schema) => {
1416
+ const serializer = toCodecJson(schema);
1417
+ const jsonSchema = fromJsonString(serializer);
1418
+ const decode = decodeEffect(jsonSchema);
1419
+ const encode = encodeEffect(jsonSchema);
1420
+ const get5 = (key) => flatMap2(self.get(key), match2({
1421
+ onUndefined: () => succeedNone,
1422
+ onDefined: (value2) => asSome(decode(value2))
1423
+ }));
1424
+ const set4 = (key, value2) => flatMap2(encode(value2), (json) => self.set(key, json));
1425
+ const modify2 = (key, f) => flatMap2(get5(key), (o) => {
1426
+ if (isNone(o)) {
1427
+ return succeedNone;
1428
+ }
1429
+ const newValue = f(o.value);
1430
+ return as(set4(key, newValue), some(newValue));
1431
+ });
1432
+ return {
1433
+ [SchemaStoreTypeId]: SchemaStoreTypeId,
1434
+ get: get5,
1435
+ set: set4,
1436
+ modify: modify2,
1437
+ remove: self.remove,
1438
+ clear: self.clear,
1439
+ size: self.size,
1440
+ has: self.has,
1441
+ isEmpty: self.isEmpty
1442
+ };
1443
+ };
1444
+
1445
+ // ../../node_modules/.bun/effect@https+++pkg.pr.new+Effect-TS+effect+effect@c7d1ffff31a3e2ae0b0b0796b742bddeb797269d/node_modules/effect/dist/unstable/reactivity/Atom.js
1446
+ var TypeId3 = "~effect/reactivity/Atom";
1447
+ var isAtom = (u) => hasProperty(u, TypeId3);
1448
+ var WritableTypeId = "~effect/reactivity/Atom/Writable";
1449
+ var setIdleTTL = /* @__PURE__ */ dual(2, (self, durationInput) => {
1450
+ const duration = fromInputUnsafe(durationInput);
1451
+ const isFinite2 = isFinite(duration);
1452
+ return Object.assign(Object.create(Object.getPrototypeOf(self)), {
1453
+ ...self,
1454
+ keepAlive: !isFinite2,
1455
+ idleTTL: isFinite2 ? toMillis(duration) : void 0
1456
+ });
1457
+ });
1458
+ var removeTtl = /* @__PURE__ */ setIdleTTL(0);
1459
+ var AtomProto = {
1460
+ [TypeId3]: TypeId3,
1461
+ equals: Object.is,
1462
+ ...PipeInspectableProto,
1463
+ toJSON() {
1464
+ return {
1465
+ _id: "Atom",
1466
+ keepAlive: this.keepAlive,
1467
+ lazy: this.lazy,
1468
+ label: this.label
1469
+ };
1470
+ }
1471
+ };
1472
+ var RuntimeProto = {
1473
+ ...AtomProto,
1474
+ atom(arg, options) {
1475
+ const read = makeRead(arg, options);
1476
+ return readable((get5) => {
1477
+ const previous = get5.self();
1478
+ const runtimeResult = get5(this);
1479
+ if (runtimeResult._tag !== "Success") {
1480
+ return replacePrevious(runtimeResult, previous);
1481
+ }
1482
+ return read(get5, runtimeResult.value);
1483
+ });
1484
+ },
1485
+ fn(arg, options) {
1486
+ if (arguments.length === 0) {
1487
+ return (arg2, options2) => makeFnRuntime(this, arg2, options2);
1488
+ }
1489
+ return makeFnRuntime(this, arg, options);
1490
+ },
1491
+ pull(arg, options) {
1492
+ const pullSignal = removeTtl(state(0));
1493
+ const pullAtom = readable((get5) => {
1494
+ const previous = get5.self();
1495
+ const runtimeResult = get5(this);
1496
+ if (runtimeResult._tag !== "Success") {
1497
+ return replacePrevious(runtimeResult, previous);
1498
+ }
1499
+ return makeEffect(get5, makeStreamPullEffect(get5, pullSignal, arg, options), initial(true), runtimeResult.value);
1500
+ });
1501
+ return makeStreamPull(pullSignal, pullAtom);
1502
+ },
1503
+ subscriptionRef(ref) {
1504
+ return makeSubRef(removeTtl(readable((get5) => {
1505
+ const previous = get5.self();
1506
+ const runtimeResult = get5(this);
1507
+ if (runtimeResult._tag !== "Success") {
1508
+ return replacePrevious(runtimeResult, previous);
1509
+ }
1510
+ const value2 = typeof ref === "function" ? ref(get5) : ref;
1511
+ return isSubscriptionRef(value2) ? value2 : makeEffect(get5, value2, initial(true), runtimeResult.value);
1512
+ })), (get5, ref2) => {
1513
+ const runtime2 = getOrThrow(get5(this));
1514
+ return readSubscriptionRef(get5, ref2, runtime2);
1515
+ });
1516
+ }
1517
+ };
1518
+ var makeFnRuntime = (self, arg, options) => {
1519
+ const [read, write, argAtom] = makeResultFn(options?.reactivityKeys ? (a, get5) => {
1520
+ const effect3 = arg(a, get5);
1521
+ return isEffect(effect3) ? mutation(effect3, options.reactivityKeys) : ensuring(effect3, invalidate(options.reactivityKeys));
1522
+ } : arg, options);
1523
+ return writable((get5) => {
1524
+ get5.get(argAtom);
1525
+ const previous = get5.self();
1526
+ const runtimeResult = get5.get(self);
1527
+ if (runtimeResult._tag !== "Success") {
1528
+ return replacePrevious(runtimeResult, previous);
1529
+ }
1530
+ return read(get5, runtimeResult.value);
1531
+ }, write);
1532
+ };
1533
+ var WritableProto = {
1534
+ ...AtomProto,
1535
+ [WritableTypeId]: WritableTypeId
1536
+ };
1537
+ var isWritable = (atom) => WritableTypeId in atom;
1538
+ var readable = (read, refresh2) => {
1539
+ const self = Object.create(AtomProto);
1540
+ self.keepAlive = false;
1541
+ self.lazy = true;
1542
+ self.read = read;
1543
+ self.refresh = refresh2;
1544
+ return self;
1545
+ };
1546
+ var writable = (read, write, refresh2) => {
1547
+ const self = Object.create(WritableProto);
1548
+ self.keepAlive = false;
1549
+ self.lazy = true;
1550
+ self.read = read;
1551
+ self.write = write;
1552
+ self.refresh = refresh2;
1553
+ return self;
1554
+ };
1555
+ function constSetSelf(ctx, value2) {
1556
+ ctx.setSelf(value2);
1557
+ }
1558
+ var make2 = (arg, options) => {
1559
+ const readOrAtom = makeRead(arg, options);
1560
+ if (TypeId3 in readOrAtom) {
1561
+ return readOrAtom;
1562
+ }
1563
+ return readable(readOrAtom);
1564
+ };
1565
+ var makeRead = (arg, options) => {
1566
+ if (typeof arg === "function" && !isEffect(arg) && !isStream(arg)) {
1567
+ const create = arg;
1568
+ return function(get5, providedServices) {
1569
+ const value2 = create(get5);
1570
+ switch (typeof value2) {
1571
+ case "function":
1572
+ case "object": {
1573
+ if (value2 === null) return value2;
1574
+ else if (EffectTypeId in value2) {
1575
+ return effect2(get5, value2, options, providedServices);
1576
+ } else if (StreamTypeId in value2) {
1577
+ return stream(get5, value2, options, providedServices);
1578
+ }
1579
+ return value2;
1580
+ }
1581
+ default:
1582
+ return value2;
1583
+ }
1584
+ };
1585
+ } else if (isEffect(arg)) {
1586
+ return function(get5, providedServices) {
1587
+ return effect2(get5, arg, options, providedServices);
1588
+ };
1589
+ } else if (isStream(arg)) {
1590
+ return function(get5, providedServices) {
1591
+ return stream(get5, arg, options, providedServices);
1592
+ };
1593
+ }
1594
+ return state(arg);
1595
+ };
1596
+ var EffectTypeId = "~effect/Effect";
1597
+ var StreamTypeId = "~effect/Stream";
1598
+ var state = (initialValue2) => writable(function(_get) {
1599
+ return initialValue2;
1600
+ }, constSetSelf);
1601
+ var effect2 = (get5, effect3, options, services) => {
1602
+ const initialValue2 = options?.initialValue !== void 0 ? success(options.initialValue) : initial();
1603
+ return makeEffect(get5, effect3, initialValue2, services, options?.uninterruptible);
1604
+ };
1605
+ function makeEffect(ctx, effect3, initialValue2, services = empty2(), uninterruptible = false) {
1606
+ const previous = ctx.self();
1607
+ const scope2 = makeUnsafe();
1608
+ ctx.addFinalizer(() => {
1609
+ runForkWith(services)(close(scope2, void_));
1610
+ });
1611
+ let syncResult;
1612
+ let isAsync = false;
1613
+ const cancel = runCallbackSync(services.pipe(add(Scope, scope2), add(AtomRegistry, ctx.registry), add(Scheduler, ctx.registry.scheduler)), effect3, function(exit) {
1614
+ syncResult = fromExitWithPrevious(exit, previous);
1615
+ if (isAsync) {
1616
+ ctx.setSelf(syncResult);
1617
+ }
1618
+ }, uninterruptible);
1619
+ isAsync = true;
1620
+ if (cancel !== void 0) {
1621
+ ctx.addFinalizer(cancel);
1622
+ }
1623
+ if (syncResult !== void 0) {
1624
+ return syncResult;
1625
+ } else if (previous._tag === "Some") {
1626
+ return waitingFrom(previous);
1627
+ }
1628
+ return waiting(initialValue2);
1629
+ }
1630
+ function runCallbackSync(services, effect3, onExit, uninterruptible = false) {
1631
+ if (isExit(effect3)) {
1632
+ onExit(effect3);
1633
+ return void 0;
1634
+ }
1635
+ const runFork = runForkWith(services);
1636
+ const fiber = runFork(effect3);
1637
+ fiber.currentDispatcher?.flush();
1638
+ const result = fiber.pollUnsafe();
1639
+ if (result) {
1640
+ onExit(result);
1641
+ return void 0;
1642
+ }
1643
+ const remove2 = fiber.addObserver(onExit);
1644
+ function cancel() {
1645
+ remove2();
1646
+ if (!uninterruptible) {
1647
+ fiber.interruptUnsafe();
1648
+ }
1649
+ }
1650
+ return cancel;
1651
+ }
1652
+ function context(options) {
1653
+ const memoMap = options?.memoMap ?? removeTtl(make2(() => makeMemoMapUnsafe()));
1654
+ const resolveMemoMap = (get5) => isAtom(memoMap) ? get5(memoMap) : memoMap;
1655
+ let globalLayer = layer;
1656
+ function factory(create) {
1657
+ const self = Object.create(RuntimeProto);
1658
+ self.keepAlive = false;
1659
+ self.lazy = true;
1660
+ self.refresh = void 0;
1661
+ self.factory = factory;
1662
+ const layerAtom = keepAlive(typeof create === "function" ? readable((get5) => provideMerge(create(get5), globalLayer)) : readable(() => provideMerge(create, globalLayer)));
1663
+ self.layer = layerAtom;
1664
+ self.read = function read(get5) {
1665
+ const layer3 = get5(layerAtom);
1666
+ const build = flatMap2(scope, (scope2) => buildWithMemoMap(layer3, resolveMemoMap(get5), scope2));
1667
+ return effect2(get5, build, {
1668
+ uninterruptible: true
1669
+ });
1670
+ };
1671
+ return self;
1672
+ }
1673
+ factory.memoMap = memoMap;
1674
+ factory.addGlobalLayer = (layer3) => {
1675
+ globalLayer = provideMerge(globalLayer, provide(layer3, layer));
1676
+ };
1677
+ const reactivityAtom = removeTtl(make2((get5) => contextWith((services) => buildWithMemoMap(layer, resolveMemoMap(get5), get(services, Scope))).pipe(map2(get(Reactivity)))));
1678
+ factory.withReactivity = (keys) => (atom) => transform2(atom, (get5) => {
1679
+ const reactivity = getOrThrow(get5(reactivityAtom));
1680
+ get5.addFinalizer(reactivity.registerUnsafe(keys, () => {
1681
+ get5.refresh(atom);
1682
+ }));
1683
+ get5.subscribe(atom, (value2) => get5.setSelf(value2));
1684
+ return get5.once(atom);
1685
+ }, {
1686
+ initialValueTarget: atom
1687
+ });
1688
+ return factory;
1689
+ }
1690
+ var runtime = /* @__PURE__ */ context();
1691
+ var withReactivity = runtime.withReactivity;
1692
+ var stream = (get5, stream2, options, services) => {
1693
+ const initialValue2 = options?.initialValue !== void 0 ? success(options.initialValue) : initial();
1694
+ return makeStream(get5, stream2, initialValue2, services);
1695
+ };
1696
+ function makeStream(ctx, stream2, initialValue2, services = empty2()) {
1697
+ const previous = ctx.self();
1698
+ services = add(services, AtomRegistry, ctx.registry);
1699
+ const run = scopedWith((scope2) => flatMap2(toPullScoped(stream2.channel, scope2), (pull2) => whileLoop({
1700
+ while: constTrue,
1701
+ body: () => pull2,
1702
+ step(arr) {
1703
+ ctx.setSelf(success(lastNonEmpty(arr), {
1704
+ waiting: true
1705
+ }));
1706
+ }
1707
+ }))).pipe(catchCause((cause2) => {
1708
+ if (isDoneCause(cause2)) {
1709
+ pipe(ctx.self(), flatMap(value), match({
1710
+ onNone: () => ctx.setSelf(failWithPrevious(new NoSuchElementError(), {
1711
+ previous: ctx.self()
1712
+ })),
1713
+ onSome: (a) => ctx.setSelf(success(a))
1714
+ }));
1715
+ } else {
1716
+ ctx.setSelf(failureWithPrevious(cause2, {
1717
+ previous: ctx.self()
1718
+ }));
1719
+ }
1720
+ return void_2;
1721
+ }));
1722
+ const cancel = runCallbackSync(services.pipe(add(AtomRegistry, ctx.registry), add(Scheduler, ctx.registry.scheduler)), run, constVoid, false);
1723
+ if (cancel !== void 0) {
1724
+ ctx.addFinalizer(cancel);
1725
+ }
1726
+ if (previous._tag === "Some") {
1727
+ return waitingFrom(previous);
1728
+ }
1729
+ return waiting(initialValue2);
1730
+ }
1731
+ var subscriptionRef = (ref) => makeSubRef(readable((get5) => {
1732
+ const value2 = typeof ref === "function" ? ref(get5) : ref;
1733
+ return isSubscriptionRef(value2) ? value2 : makeEffect(get5, value2, initial(true));
1734
+ }), readSubscriptionRef);
1735
+ var readSubscriptionRef = (get5, sub, services = empty2()) => {
1736
+ if (isSubscriptionRef(sub)) {
1737
+ get5.addFinalizer(changes2(sub).pipe(runForEachArray((arr) => {
1738
+ for (let i = 0; i < arr.length; i++) {
1739
+ get5.setSelf(arr[i]);
1740
+ }
1741
+ return void_2;
1742
+ }), runCallbackWith(services)));
1743
+ return runSyncWith(services)(get3(sub));
1744
+ } else if (sub._tag !== "Success") {
1745
+ return sub;
1746
+ }
1747
+ return makeStream(get5, changes2(sub.value), initial(true), services);
1748
+ };
1749
+ var makeSubRef = (refAtom, read) => {
1750
+ function write(ctx, value2) {
1751
+ const ref = ctx.get(refAtom);
1752
+ if (isSubscriptionRef(ref)) {
1753
+ runSync(set2(ref, value2));
1754
+ } else if (isSuccess(ref)) {
1755
+ runSync(set2(ref.value, value2));
1756
+ }
1757
+ }
1758
+ return writable((get5) => {
1759
+ const ref = get5(refAtom);
1760
+ if (isSubscriptionRef(ref)) {
1761
+ return read(get5, ref);
1762
+ } else if (isSuccess(ref)) {
1763
+ return read(get5, ref);
1764
+ }
1765
+ return ref;
1766
+ }, write);
1767
+ };
1768
+ var fnSync = function(...args) {
1769
+ if (args.length === 0) {
1770
+ return makeFnSync;
1771
+ }
1772
+ return makeFnSync(...args);
1773
+ };
1774
+ var makeFnSync = (f, options) => {
1775
+ const argAtom = removeTtl(state([0, void 0]));
1776
+ const hasInitialValue = options?.initialValue !== void 0;
1777
+ return writable(function(get5) {
1778
+ ;
1779
+ get5.isFn = true;
1780
+ const [counter, arg] = get5.get(argAtom);
1781
+ if (counter === 0) {
1782
+ return hasInitialValue ? options.initialValue : none();
1783
+ }
1784
+ return hasInitialValue ? f(arg, get5) : some(f(arg, get5));
1785
+ }, function(ctx, arg) {
1786
+ batch2(() => {
1787
+ ctx.set(argAtom, [ctx.get(argAtom)[0] + 1, arg]);
1788
+ ctx.refreshSelf();
1789
+ });
1790
+ });
1791
+ };
1792
+ var Reset = /* @__PURE__ */ Symbol.for("effect/reactivity/atom/Atom/Reset");
1793
+ var Interrupt = /* @__PURE__ */ Symbol.for("effect/reactivity/atom/Atom/Interrupt");
1794
+ var fn = function(...args) {
1795
+ if (args.length === 0) {
1796
+ return makeFn;
1797
+ }
1798
+ return makeFn(...args);
1799
+ };
1800
+ var makeFn = (f, options) => {
1801
+ const [read, write] = makeResultFn(f, options);
1802
+ return writable(read, write);
1803
+ };
1804
+ function makeResultFn(f, options) {
1805
+ const argAtom = removeTtl(state([0, void 0]));
1806
+ const initialValue2 = options?.initialValue !== void 0 ? success(options.initialValue) : initial();
1807
+ const fibersAtom = options?.concurrent ? removeTtl(readable((get5) => {
1808
+ const fibers = /* @__PURE__ */ new Set();
1809
+ get5.addFinalizer(() => fibers.forEach((f2) => f2.interruptUnsafe()));
1810
+ return fibers;
1811
+ })) : void 0;
1812
+ function read(get5, services) {
1813
+ const fibers = fibersAtom ? get5(fibersAtom) : void 0;
1814
+ get5.isFn = true;
1815
+ const [counter, arg] = get5.get(argAtom);
1816
+ if (counter === 0) {
1817
+ return initialValue2;
1818
+ } else if (arg === Interrupt) {
1819
+ return failureWithPrevious(interrupt(), {
1820
+ previous: get5.self()
1821
+ });
1822
+ }
1823
+ let value2 = f(arg, get5);
1824
+ if (EffectTypeId in value2) {
1825
+ if (fibers) {
1826
+ const eff = value2;
1827
+ value2 = flatMap2(forkDetach(eff, {
1828
+ startImmediately: true
1829
+ }), (fiber) => {
1830
+ fibers.add(fiber);
1831
+ fiber.addObserver(() => fibers.delete(fiber));
1832
+ return map2(joinAll(fibers), (arr) => arr[0]);
1833
+ });
1834
+ }
1835
+ return makeEffect(get5, value2, initialValue2, services, false);
1836
+ }
1837
+ return makeStream(get5, value2, initialValue2, services);
1838
+ }
1839
+ function write(ctx, arg) {
1840
+ batch2(() => {
1841
+ if (arg === Reset) {
1842
+ ctx.set(argAtom, [0, void 0]);
1843
+ } else if (arg === Interrupt) {
1844
+ ctx.set(argAtom, [ctx.get(argAtom)[0] + 1, Interrupt]);
1845
+ } else {
1846
+ ctx.set(argAtom, [ctx.get(argAtom)[0] + 1, arg]);
1847
+ }
1848
+ ctx.refreshSelf();
1849
+ });
1850
+ }
1851
+ return [read, write, argAtom];
1852
+ }
1853
+ var pull = (create, options) => {
1854
+ const pullSignal = removeTtl(state(0));
1855
+ const pullAtom = readable(makeRead(function(get5) {
1856
+ return makeStreamPullEffect(get5, pullSignal, create, options);
1857
+ }));
1858
+ return makeStreamPull(pullSignal, pullAtom);
1859
+ };
1860
+ var makeStreamPullEffect = (get5, pullSignal, create, options) => flatMap2(toPull(typeof create === "function" ? create(get5) : create), (pullChunk) => {
1861
+ const fiber = getCurrent();
1862
+ const services = fiber.context;
1863
+ let acc = empty();
1864
+ const pull2 = matchCauseEffect(pullChunk, {
1865
+ onFailure(cause2) {
1866
+ if (isDoneCause(cause2)) {
1867
+ if (!isReadonlyArrayNonEmpty(acc)) {
1868
+ return fail3(new NoSuchElementError(`Atom.pull: no items`));
1869
+ }
1870
+ return succeed2({
1871
+ done: true,
1872
+ items: acc
1873
+ });
1874
+ }
1875
+ return failCause2(cause2);
1876
+ },
1877
+ onSuccess(chunk) {
1878
+ let items;
1879
+ if (options?.disableAccumulation) {
1880
+ items = chunk;
1881
+ } else {
1882
+ items = appendAll(acc, chunk);
1883
+ acc = items;
1884
+ }
1885
+ return succeed2({
1886
+ done: false,
1887
+ items
1888
+ });
1889
+ }
1890
+ });
1891
+ const cancels = /* @__PURE__ */ new Set();
1892
+ get5.addFinalizer(() => {
1893
+ for (const cancel of cancels) cancel();
1894
+ });
1895
+ get5.once(pullSignal);
1896
+ get5.subscribe(pullSignal, () => {
1897
+ get5.setSelf(waitingFrom(get5.self()));
1898
+ let cancel;
1899
+ cancel = runCallbackSync(services, pull2, (exit) => {
1900
+ if (cancel) cancels.delete(cancel);
1901
+ const result = fromExitWithPrevious(exit, get5.self());
1902
+ const pending = cancels.size > 0;
1903
+ get5.setSelf(pending ? waiting(result) : result);
1904
+ });
1905
+ if (cancel) cancels.add(cancel);
1906
+ });
1907
+ return pull2;
1908
+ });
1909
+ var makeStreamPull = (pullSignal, pullAtom) => writable(pullAtom.read, function(ctx, _) {
1910
+ ctx.set(pullSignal, ctx.get(pullSignal) + 1);
1911
+ });
1912
+ var family = typeof WeakRef === "undefined" || typeof FinalizationRegistry === "undefined" ? (f) => {
1913
+ const atoms = empty3();
1914
+ return function(arg) {
1915
+ const atomEntry = get2(atoms, arg);
1916
+ if (atomEntry._tag === "Some") {
1917
+ return atomEntry.value;
1918
+ }
1919
+ const newAtom = f(arg);
1920
+ set(atoms, arg, newAtom);
1921
+ return newAtom;
1922
+ };
1923
+ } : (f) => {
1924
+ const atoms = empty3();
1925
+ const registry = new FinalizationRegistry((arg) => {
1926
+ remove(atoms, arg);
1927
+ });
1928
+ return function(arg) {
1929
+ const atomEntry = get2(atoms, arg).pipe(flatMapNullishOr((ref) => ref.deref()));
1930
+ if (atomEntry._tag === "Some") {
1931
+ return atomEntry.value;
1932
+ }
1933
+ const newAtom = f(arg);
1934
+ set(atoms, arg, new WeakRef(newAtom));
1935
+ registry.register(newAtom, arg);
1936
+ return newAtom;
1937
+ };
1938
+ };
1939
+ var withFallback = /* @__PURE__ */ dual(2, (self, fallback) => {
1940
+ function withFallback2(get5) {
1941
+ const result = get5(self);
1942
+ if (result._tag === "Initial") {
1943
+ return waiting(get5(fallback));
1944
+ }
1945
+ return result;
1946
+ }
1947
+ return isWritable(self) ? writable(withFallback2, function(ctx, value2) {
1948
+ ctx.set(self, value2);
1949
+ }, self.refresh ?? function(refresh2) {
1950
+ refresh2(self);
1951
+ }) : readable(withFallback2, self.refresh ?? function(refresh2) {
1952
+ refresh2(self);
1953
+ });
1954
+ });
1955
+ var keepAlive = (self) => Object.assign(Object.create(Object.getPrototypeOf(self)), {
1956
+ ...self,
1957
+ keepAlive: true
1958
+ });
1959
+ var autoDispose = (self) => Object.assign(Object.create(Object.getPrototypeOf(self)), {
1960
+ ...self,
1961
+ keepAlive: false
1962
+ });
1963
+ var setLazy = /* @__PURE__ */ dual(2, (self, lazy) => Object.assign(Object.create(Object.getPrototypeOf(self)), {
1964
+ ...self,
1965
+ lazy
1966
+ }));
1967
+ var withEquality = /* @__PURE__ */ dual(2, (self, equals2) => Object.assign(Object.create(Object.getPrototypeOf(self)), {
1968
+ ...self,
1969
+ equals: equals2
1970
+ }));
1971
+ var withLabel = /* @__PURE__ */ dual(2, (self, name) => Object.assign(Object.create(Object.getPrototypeOf(self)), {
1972
+ ...self,
1973
+ label: [name, getStackTraceLimit() === 0 ? "" : new Error().stack?.split("\n")[5] ?? ""]
1974
+ }));
1975
+ var initialValue = /* @__PURE__ */ dual(2, (self, initialValue2) => [self, initialValue2]);
1976
+ var transform2 = /* @__PURE__ */ dual((args) => isAtom(args[0]), (self, f, options) => {
1977
+ const atom = removeTtl(isWritable(self) ? writable((get5) => f(get5, self), function(ctx, value2) {
1978
+ ctx.set(self, value2);
1979
+ }, self.refresh ?? function(refresh2) {
1980
+ refresh2(self);
1981
+ }) : readable((get5) => f(get5, self), self.refresh ?? function(refresh2) {
1982
+ refresh2(self);
1983
+ }));
1984
+ if (options?.initialValueTarget) {
1985
+ ;
1986
+ atom.initialValueTarget = getInitialValueTarget(options.initialValueTarget);
1987
+ }
1988
+ return atom;
1989
+ });
1990
+ var getInitialValueTarget = (atom) => {
1991
+ let target = atom;
1992
+ while (target.initialValueTarget) {
1993
+ target = target.initialValueTarget;
1994
+ }
1995
+ return target;
1996
+ };
1997
+ var map5 = /* @__PURE__ */ dual(2, (self, f) => transform2(self, (get5) => f(get5(self))));
1998
+ var mapResult = /* @__PURE__ */ dual(2, (self, f) => map5(self, map3(f)));
1999
+ var debounce = /* @__PURE__ */ dual(2, (self, duration) => {
2000
+ const millis = toMillis(fromInputUnsafe(duration));
2001
+ return transform2(self, function(get5) {
2002
+ let timeout;
2003
+ let value2 = get5.once(self);
2004
+ function update2() {
2005
+ timeout = void 0;
2006
+ get5.setSelf(value2);
2007
+ }
2008
+ get5.addFinalizer(function() {
2009
+ if (timeout) clearTimeout(timeout);
2010
+ });
2011
+ get5.subscribe(self, function(val) {
2012
+ value2 = val;
2013
+ if (timeout) clearTimeout(timeout);
2014
+ timeout = setTimeout(update2, millis);
2015
+ });
2016
+ return value2;
2017
+ }, {
2018
+ initialValueTarget: self
2019
+ });
2020
+ });
2021
+ var withRefresh = /* @__PURE__ */ dual(2, (self, duration) => {
2022
+ const millis = toMillis(fromInputUnsafe(duration));
2023
+ return transform2(self, function(get5) {
2024
+ const handle = setTimeout(() => get5.refresh(self), millis);
2025
+ get5.addFinalizer(() => clearTimeout(handle));
2026
+ return get5(self);
2027
+ }, {
2028
+ initialValueTarget: self
2029
+ });
2030
+ });
2031
+ var swr = /* @__PURE__ */ dual(2, (self, options) => {
2032
+ const staleTime = toMillis(fromInputUnsafe(options.staleTime));
2033
+ return transform2(self, (get5) => {
2034
+ const current = get5.once(self);
2035
+ get5.subscribe(self, (value2) => {
2036
+ get5.setSelf(value2);
2037
+ });
2038
+ if (options.revalidateOnFocus && options.focusSignal) {
2039
+ get5.once(options.focusSignal);
2040
+ get5.subscribe(options.focusSignal, options.revalidateOnFocus === "always" ? () => get5.refresh(self) : () => {
2041
+ const current2 = get5.once(self);
2042
+ if (shouldRevalidateSWR(current2, staleTime)) {
2043
+ get5.refresh(self);
2044
+ }
2045
+ });
2046
+ }
2047
+ const firstRead = isNone(get5.self());
2048
+ if (firstRead && options.revalidateOnMount === false) {
2049
+ return current;
2050
+ }
2051
+ if (shouldRevalidateSWR(current, staleTime)) {
2052
+ get5.refresh(self);
2053
+ }
2054
+ return current;
2055
+ }, {
2056
+ initialValueTarget: self
2057
+ });
2058
+ });
2059
+ var swrTimestamp = (result) => {
2060
+ if (result._tag === "Success") {
2061
+ return some(result.timestamp);
2062
+ }
2063
+ if (result._tag === "Failure") {
2064
+ return map(result.previousSuccess, (success2) => success2.timestamp);
2065
+ }
2066
+ return none();
2067
+ };
2068
+ var isFreshWithin = (timestamp, staleTime, now) => now - timestamp < staleTime;
2069
+ var shouldRevalidateSWR = (result, staleTime) => {
2070
+ if (result.waiting) {
2071
+ return false;
2072
+ }
2073
+ const timestamp = getOrUndefined(swrTimestamp(result));
2074
+ if (timestamp === void 0) {
2075
+ return result._tag !== "Initial";
2076
+ }
2077
+ return !isFreshWithin(timestamp, staleTime, Date.now());
2078
+ };
2079
+ var optimistic = (self) => {
2080
+ let counter = 0;
2081
+ const writeAtom = removeTtl(state([counter, void 0]));
2082
+ return writable((get5) => {
2083
+ let lastValue = get5.once(self);
2084
+ let needsRefresh = false;
2085
+ get5.subscribe(self, (value2) => {
2086
+ lastValue = value2;
2087
+ if (transitions.size > 0) {
2088
+ return;
2089
+ }
2090
+ needsRefresh = false;
2091
+ if (!isAsyncResult(value2)) {
2092
+ return get5.setSelf(value2);
2093
+ }
2094
+ const current = getOrUndefined(get5.self());
2095
+ switch (value2._tag) {
2096
+ case "Initial": {
2097
+ if (isInitial(current)) {
2098
+ get5.setSelf(value2);
2099
+ }
2100
+ return;
2101
+ }
2102
+ case "Success": {
2103
+ if (isSuccess(current)) {
2104
+ if (!value2.waiting && value2.timestamp >= current.timestamp) {
2105
+ get5.setSelf(value2);
2106
+ }
2107
+ } else {
2108
+ get5.setSelf(value2);
2109
+ }
2110
+ return;
2111
+ }
2112
+ case "Failure": {
2113
+ return get5.setSelf(value2);
2114
+ }
2115
+ }
2116
+ });
2117
+ const transitions = /* @__PURE__ */ new Set();
2118
+ const cancels = /* @__PURE__ */ new Set();
2119
+ get5.subscribe(writeAtom, ([, atom]) => {
2120
+ if (transitions.has(atom)) return;
2121
+ transitions.add(atom);
2122
+ let cancel;
2123
+ cancel = get5.registry.subscribe(atom, (result) => {
2124
+ if (isSuccess(result) && result.waiting) {
2125
+ return get5.setSelf(result.value);
2126
+ }
2127
+ transitions.delete(atom);
2128
+ if (cancel) {
2129
+ cancels.delete(cancel);
2130
+ cancel();
2131
+ }
2132
+ if (!needsRefresh && !isFailure2(result)) {
2133
+ needsRefresh = true;
2134
+ }
2135
+ if (transitions.size === 0) {
2136
+ if (needsRefresh) {
2137
+ needsRefresh = false;
2138
+ get5.refresh(self);
2139
+ } else {
2140
+ get5.setSelf(lastValue);
2141
+ }
2142
+ }
2143
+ }, {
2144
+ immediate: true
2145
+ });
2146
+ if (transitions.has(atom)) {
2147
+ cancels.add(cancel);
2148
+ } else {
2149
+ cancel();
2150
+ }
2151
+ });
2152
+ get5.addFinalizer(() => {
2153
+ for (const cancel of cancels) cancel();
2154
+ transitions.clear();
2155
+ cancels.clear();
2156
+ });
2157
+ return lastValue;
2158
+ }, (ctx, atom) => ctx.set(writeAtom, [++counter, atom]), (refresh2) => refresh2(self));
2159
+ };
2160
+ var optimisticFn = /* @__PURE__ */ dual(2, (self, options) => {
2161
+ const transition = removeTtl(state(initial()));
2162
+ return fn((arg, get5) => {
2163
+ let value2 = options.reducer(get5(self), arg);
2164
+ if (isAsyncResult(value2)) {
2165
+ value2 = waiting(value2, {
2166
+ touch: true
2167
+ });
2168
+ }
2169
+ get5.set(transition, success(value2, {
2170
+ waiting: true
2171
+ }));
2172
+ get5.set(self, transition);
2173
+ const fn2 = typeof options.fn === "function" ? autoDispose(options.fn((value3) => get5.set(transition, success(isAsyncResult(value3) ? waiting(value3) : value3, {
2174
+ waiting: true
2175
+ })))) : options.fn;
2176
+ get5.set(fn2, arg);
2177
+ return callback((resume) => {
2178
+ get5.subscribe(fn2, (result) => {
2179
+ if (result._tag === "Initial" || result.waiting) return;
2180
+ get5.set(transition, map3(result, () => value2));
2181
+ resume(toExit(result));
2182
+ }, {
2183
+ immediate: true
2184
+ });
2185
+ });
2186
+ });
2187
+ });
2188
+ var batch2 = batch;
2189
+ var windowFocusSignal = /* @__PURE__ */ readable((get5) => {
2190
+ let count = 0;
2191
+ function update2() {
2192
+ if (document.visibilityState === "visible") {
2193
+ get5.setSelf(++count);
2194
+ }
2195
+ }
2196
+ window.addEventListener("visibilitychange", update2);
2197
+ get5.addFinalizer(() => {
2198
+ window.removeEventListener("visibilitychange", update2);
2199
+ });
2200
+ return count;
2201
+ });
2202
+ var makeRefreshOnSignal = (signal) => (self) => transform2(self, (get5) => {
2203
+ get5.once(signal);
2204
+ get5.subscribe(signal, (_) => get5.refresh(self));
2205
+ get5.subscribe(self, (value2) => get5.setSelf(value2));
2206
+ return get5.once(self);
2207
+ }, {
2208
+ initialValueTarget: self
2209
+ });
2210
+ var refreshOnWindowFocus = /* @__PURE__ */ makeRefreshOnSignal(windowFocusSignal);
2211
+ var kvs = (options) => {
2212
+ const setAtom = options.runtime.fn((value2) => KeyValueStore.use((store) => toSchemaStore(store, options.schema).set(options.key, value2)));
2213
+ const resultAtom = options.runtime.atom(KeyValueStore.use((store) => toSchemaStore(store, options.schema).get(options.key)));
2214
+ return writable(options.mode === "async" ? (get5) => {
2215
+ get5.mount(setAtom);
2216
+ const mapper = map3(getOrElse(() => {
2217
+ const value2 = options.defaultValue();
2218
+ get5.set(setAtom, value2);
2219
+ return value2;
2220
+ }));
2221
+ get5.subscribe(resultAtom, (result) => get5.setSelf(mapper(result)));
2222
+ return mapper(get5.once(resultAtom));
2223
+ } : (get5) => {
2224
+ get5.mount(setAtom);
2225
+ get5.subscribe(resultAtom, (result) => {
2226
+ if (!isSuccess(result)) return;
2227
+ if (isSome(result.value)) {
2228
+ get5.setSelf(result.value.value);
2229
+ } else {
2230
+ const value2 = getOrElse(get5.self(), options.defaultValue);
2231
+ get5.setSelf(value2);
2232
+ get5.set(setAtom, value2);
2233
+ }
2234
+ }, {
2235
+ immediate: true
2236
+ });
2237
+ return getOrElse(get5.self(), options.defaultValue);
2238
+ }, (ctx, value2) => {
2239
+ ctx.set(setAtom, value2);
2240
+ ctx.setSelf(options.mode === "async" ? success(value2) : value2);
2241
+ });
2242
+ };
2243
+ var searchParam = (name, options) => {
2244
+ const decode = options?.schema && decodeExit(options.schema);
2245
+ const encode = options?.schema && encodeExit(options.schema);
2246
+ return writable((get5) => {
2247
+ if (typeof window === "undefined") {
2248
+ return decode ? none() : "";
2249
+ }
2250
+ const handleUpdate = () => {
2251
+ if (searchParamState.updating) return;
2252
+ const searchParams = new URLSearchParams(window.location.search);
2253
+ const newValue = searchParams.get(name) || "";
2254
+ if (decode) {
2255
+ get5.setSelf(getSuccess(decode(newValue)));
2256
+ } else if (newValue !== getOrUndefined(get5.self())) {
2257
+ get5.setSelf(newValue);
2258
+ }
2259
+ };
2260
+ window.addEventListener("popstate", handleUpdate);
2261
+ window.addEventListener("pushstate", handleUpdate);
2262
+ get5.addFinalizer(() => {
2263
+ window.removeEventListener("popstate", handleUpdate);
2264
+ window.removeEventListener("pushstate", handleUpdate);
2265
+ });
2266
+ const value2 = new URLSearchParams(window.location.search).get(name) || "";
2267
+ return decode ? getSuccess(decode(value2)) : value2;
2268
+ }, (ctx, value2) => {
2269
+ if (typeof window === "undefined") {
2270
+ ctx.setSelf(value2);
2271
+ return;
2272
+ }
2273
+ if (encode) {
2274
+ const encoded = flatMap(value2, (v) => getSuccess(encode(v)));
2275
+ searchParamState.updates.set(name, getOrElse(encoded, () => ""));
2276
+ value2 = zipRight(encoded, value2);
2277
+ } else {
2278
+ searchParamState.updates.set(name, value2);
2279
+ }
2280
+ ctx.setSelf(value2);
2281
+ if (searchParamState.timeout) {
2282
+ clearTimeout(searchParamState.timeout);
2283
+ }
2284
+ searchParamState.timeout = setTimeout(updateSearchParams, 500);
2285
+ });
2286
+ };
2287
+ var searchParamState = {
2288
+ timeout: void 0,
2289
+ updates: /* @__PURE__ */ new Map(),
2290
+ updating: false
2291
+ };
2292
+ function updateSearchParams() {
2293
+ searchParamState.timeout = void 0;
2294
+ searchParamState.updating = true;
2295
+ const searchParams = new URLSearchParams(window.location.search);
2296
+ for (const [key, value2] of searchParamState.updates.entries()) {
2297
+ if (value2.length > 0) {
2298
+ searchParams.set(key, value2);
2299
+ } else {
2300
+ searchParams.delete(key);
2301
+ }
2302
+ }
2303
+ searchParamState.updates.clear();
2304
+ const newUrl = `${window.location.pathname}?${searchParams.toString()}`;
2305
+ window.history.pushState({}, "", newUrl);
2306
+ searchParamState.updating = false;
2307
+ }
2308
+ var toStream2 = (self) => unwrap(AtomRegistry.use((r) => succeed2(toStream(r, self))));
2309
+ var toStreamResult2 = (self) => unwrap(AtomRegistry.use((r) => succeed2(toStreamResult(r, self))));
2310
+ var get4 = (self) => AtomRegistry.use((r) => succeed2(r.get(self)));
2311
+ var modify = /* @__PURE__ */ dual(2, (self, f) => map2(AtomRegistry, (_) => _.modify(self, f)));
2312
+ var set3 = /* @__PURE__ */ dual(2, (self, value2) => map2(AtomRegistry, (_) => _.set(self, value2)));
2313
+ var update = /* @__PURE__ */ dual(2, (self, f) => map2(AtomRegistry, (_) => _.update(self, f)));
2314
+ var getResult2 = (self, options) => AtomRegistry.use(getResult(self, options));
2315
+ var refresh = (self) => map2(AtomRegistry, (_) => _.refresh(self));
2316
+ var mount2 = (self) => AtomRegistry.use((r) => mount(r, self));
2317
+ var SerializableTypeId2 = "~effect-atom/atom/Atom/Serializable";
2318
+ var isSerializable = (self) => SerializableTypeId2 in self;
2319
+ var serializable = /* @__PURE__ */ dual(2, (self, options) => {
2320
+ const codecJson = toCodecJson(options.schema);
2321
+ return Object.assign(Object.create(Object.getPrototypeOf(self)), {
2322
+ ...self,
2323
+ label: self.label ?? [options.key, getStackTraceLimit() === 0 ? "" : new Error().stack?.split("\n")[5] ?? ""],
2324
+ [SerializableTypeId2]: {
2325
+ key: options.key,
2326
+ encode: encodeSync(codecJson),
2327
+ decode: decodeSync(codecJson)
2328
+ }
2329
+ });
2330
+ });
2331
+ var ServerValueTypeId = "~effect-atom/atom/Atom/ServerValue";
2332
+ var withServerValue = /* @__PURE__ */ dual(2, (self, read) => Object.assign(Object.create(Object.getPrototypeOf(self)), {
2333
+ ...self,
2334
+ [ServerValueTypeId]: read
2335
+ }));
2336
+ var withServerValueInitial = (self) => withServerValue(self, constant(initial(true)));
2337
+ var getServerValue = /* @__PURE__ */ dual(2, (self, registry) => ServerValueTypeId in self ? self[ServerValueTypeId]((atom) => registry.get(atom)) : registry.get(self));
2338
+
2339
+ // src/implementation/query-commit.ts
2340
+ var commits = /* @__PURE__ */ new WeakMap();
2341
+ var queryResult = (result) => {
2342
+ const visible = AsyncResult_exports.map(result, (observed2) => observed2.value);
2343
+ const observed = AsyncResult_exports.value(result);
2344
+ if (Option_exports.isSome(observed)) commits.set(visible, observed.value.commit);
2345
+ return visible;
2346
+ };
2347
+ var queryCommitted = (result) => {
2348
+ commits.get(result)?.();
2349
+ };
2350
+
2351
+ export {
2352
+ AsyncResult_exports,
2353
+ AtomRegistry_exports,
2354
+ Atom_exports,
2355
+ queryResult,
2356
+ queryCommitted
2357
+ };