| 1 | import * as Cloudflare from "@/Cloudflare/index.ts"; |
| 2 | import * as Alchemy from "@/index.ts"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | |
| 5 | export class LegacyAlarmObject extends Cloudflare.DurableObject<LegacyAlarmObject>()( |
| 6 | "LegacyAlarmObject", |
| 7 | Effect.gen(function* () { |
| 8 | const state = yield* Cloudflare.DurableObjectState; |
| 9 | return Effect.gen(function* () { |
| 10 | const registered = yield* Alchemy.makeCallback( |
| 11 | "registered", |
| 12 | Effect.fn(function* (payload: { value: string }) { |
| 13 | yield* state.storage.put("registered", payload.value); |
| 14 | }), |
| 15 | ); |
| 16 | return { |
| 17 | start: Effect.fn( |
| 18 | function* () { |
| 19 | const at = yield* Effect.sync(() => new Date(Date.now() + 1_500)); |
| 20 | yield* state.storage.transaction( |
| 21 | Effect.gen(function* () { |
| 22 | yield* registered.schedule("shared-id", { |
| 23 | at, |
| 24 | payload: { value: "registered" }, |
| 25 | }); |
| 26 | yield* Cloudflare.scheduleEvent("shared-id", at, { |
| 27 | value: "legacy", |
| 28 | }); |
| 29 | yield* Cloudflare.scheduleEvent("cancelled", at, { |
| 30 | value: "cancelled", |
| 31 | }); |
| 32 | yield* Cloudflare.cancelEvent("cancelled"); |
| 33 | }), |
| 34 | ); |
| 35 | }, |
| 36 | Effect.provideService(Cloudflare.DurableObjectState, state), |
| 37 | ), |
| 38 | alarm: Effect.fn( |
| 39 | function* () { |
| 40 | const events = yield* Cloudflare.processScheduledEvents; |
| 41 | const fired = |
| 42 | (yield* state.storage.get<Cloudflare.ScheduledEvent[]>( |
| 43 | "legacy", |
| 44 | )) ?? []; |
| 45 | yield* state.storage.put("legacy", [...fired, ...events]); |
| 46 | }, |
| 47 | Effect.provideService(Cloudflare.DurableObjectState, state), |
| 48 | ), |
| 49 | snapshot: Effect.fn( |
| 50 | function* () { |
| 51 | return { |
| 52 | registered: |
| 53 | (yield* state.storage.get<string>("registered")) ?? null, |
| 54 | legacy: |
| 55 | (yield* state.storage.get<Cloudflare.ScheduledEvent[]>( |
| 56 | "legacy", |
| 57 | )) ?? [], |
| 58 | pending: yield* Cloudflare.listEvents, |
| 59 | alarm: yield* state.storage.getAlarm(), |
| 60 | }; |
| 61 | }, |
| 62 | Effect.provideService(Cloudflare.DurableObjectState, state), |
| 63 | ), |
| 64 | }; |
| 65 | }); |
| 66 | }), |
| 67 | ) {} |