| 1 | import * as Effect from "effect/Effect" |
| 2 | import * as TxRef from "effect/TxRef" |
| 3 | |
| 4 | const program = Effect.gen(function*() { |
| 5 | const ref = yield* TxRef.make(0) |
| 6 | |
| 7 | yield* Effect.forkChild(Effect.forever( |
| 8 | TxRef.update(ref, (n) => n + 1).pipe(Effect.delay("100 millis")) |
| 9 | )) |
| 10 | |
| 11 | yield* Effect.tx(Effect.gen(function*() { |
| 12 | const value = yield* TxRef.get(ref) |
| 13 | if (value < 10) { |
| 14 | yield* Effect.log(`retry due to value: ${value}`) |
| 15 | return yield* Effect.txRetry |
| 16 | } |
| 17 | yield* Effect.log(`transaction done with value: ${value}`) |
| 18 | })) |
| 19 | }) |
| 20 | |
| 21 | Effect.runPromise(program).catch(console.error) |