|
| 1 | +import { Ok } from "effection"; |
| 2 | +import type { Effect, Operation, Result } from "effection"; |
| 3 | + |
| 4 | +export function inline<T>(operation: Operation<T>): Inline<T> { |
| 5 | + return { |
| 6 | + operation, |
| 7 | + description: "inline()", |
| 8 | + enter(resolve, routine) { |
| 9 | + let current = routine.data.iterator; |
| 10 | + if (isInlineIterator(current)) { |
| 11 | + current.stack.push(current.current); |
| 12 | + current.current = operation[Symbol.iterator](); |
| 13 | + } else { |
| 14 | + let inlined = new InlineIterator(operation, current); |
| 15 | + Object.defineProperty(routine.data, "iterator", { |
| 16 | + get: () => inlined, |
| 17 | + configurable: true, |
| 18 | + }); |
| 19 | + } |
| 20 | + resolve(Ok() as Result<T>); |
| 21 | + return (didExit) => didExit(Ok()); |
| 22 | + }, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +interface Inline<T> extends Effect<T> { |
| 27 | + operation: Operation<T>; |
| 28 | +} |
| 29 | + |
| 30 | +function isInline<T>(effect: Effect<T>): effect is Inline<T> { |
| 31 | + return "operation" in effect; |
| 32 | +} |
| 33 | + |
| 34 | +type EffectIterator = Iterator<Effect<unknown>, unknown, unknown>; |
| 35 | + |
| 36 | +function isInlineIterator( |
| 37 | + iterator: EffectIterator, |
| 38 | +): iterator is InlineIterator { |
| 39 | + return "@effectionx/inline" in iterator; |
| 40 | +} |
| 41 | + |
| 42 | +class InlineIterator implements EffectIterator { |
| 43 | + "@effectionx/inline" = true; |
| 44 | + stack: EffectIterator[]; |
| 45 | + current: EffectIterator; |
| 46 | + |
| 47 | + constructor(operation: Operation<unknown>, original: EffectIterator) { |
| 48 | + this.stack = [original]; |
| 49 | + this.current = operation[Symbol.iterator](); |
| 50 | + } |
| 51 | + |
| 52 | + next(value: unknown): IteratorResult<Effect<unknown>, unknown> { |
| 53 | + let next: IteratorResult<Effect<unknown>, unknown>; |
| 54 | + try { |
| 55 | + next = this.current.next(value); |
| 56 | + } catch (error) { |
| 57 | + return this.raise(error); |
| 58 | + } |
| 59 | + return this.step(next); |
| 60 | + } |
| 61 | + |
| 62 | + return(value: unknown): IteratorResult<Effect<unknown>, unknown> { |
| 63 | + if (this.current.return) { |
| 64 | + let result: IteratorResult<Effect<unknown>, unknown>; |
| 65 | + try { |
| 66 | + result = this.current.return(value); |
| 67 | + } catch (error) { |
| 68 | + return this.raise(error); |
| 69 | + } |
| 70 | + if (!result.done) { |
| 71 | + return result; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + while (this.stack.length > 0) { |
| 76 | + let top = this.stack.pop()!; |
| 77 | + this.current = top; |
| 78 | + if (top.return) { |
| 79 | + let result: IteratorResult<Effect<unknown>, unknown>; |
| 80 | + try { |
| 81 | + result = top.return(value); |
| 82 | + } catch (error) { |
| 83 | + return this.raise(error); |
| 84 | + } |
| 85 | + if (!result.done) { |
| 86 | + return result; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return { done: true, value }; |
| 92 | + } |
| 93 | + |
| 94 | + throw(error: unknown): IteratorResult<Effect<unknown>, unknown> { |
| 95 | + return this.raise(error); |
| 96 | + } |
| 97 | + |
| 98 | + step( |
| 99 | + next: IteratorResult<Effect<unknown>, unknown>, |
| 100 | + ): IteratorResult<Effect<unknown>, unknown> { |
| 101 | + while (true) { |
| 102 | + if (next.done) { |
| 103 | + let top = this.stack.pop(); |
| 104 | + if (!top) { |
| 105 | + return next; |
| 106 | + } |
| 107 | + this.current = top; |
| 108 | + try { |
| 109 | + next = this.current.next(next.value); |
| 110 | + } catch (error) { |
| 111 | + return this.raise(error); |
| 112 | + } |
| 113 | + } else { |
| 114 | + let effect = next.value; |
| 115 | + if (isInline(effect)) { |
| 116 | + this.stack.push(this.current); |
| 117 | + this.current = effect.operation[Symbol.iterator](); |
| 118 | + try { |
| 119 | + next = this.current.next(undefined); |
| 120 | + } catch (error) { |
| 121 | + return this.raise(error); |
| 122 | + } |
| 123 | + } else { |
| 124 | + return next; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + raise(error: unknown): IteratorResult<Effect<unknown>, unknown> { |
| 131 | + if (this.current.throw) { |
| 132 | + let next: IteratorResult<Effect<unknown>, unknown>; |
| 133 | + try { |
| 134 | + next = this.current.throw(error); |
| 135 | + } catch (rethrown) { |
| 136 | + return this.propagate(rethrown); |
| 137 | + } |
| 138 | + return this.step(next); |
| 139 | + } |
| 140 | + |
| 141 | + return this.propagate(error); |
| 142 | + } |
| 143 | + |
| 144 | + propagate(error: unknown): IteratorResult<Effect<unknown>, unknown> { |
| 145 | + let top = this.stack.pop(); |
| 146 | + if (!top) { |
| 147 | + throw error; |
| 148 | + } |
| 149 | + this.current = top; |
| 150 | + return this.raise(error); |
| 151 | + } |
| 152 | +} |
0 commit comments