|
| 1 | +import type { Operation, WithResolvers } from "effection"; |
| 2 | +import { call, withResolvers } from "effection"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Resolve a Chain |
| 6 | + */ |
| 7 | +export type Resolve<T> = WithResolvers<T>["resolve"]; |
| 8 | + |
| 9 | +/** |
| 10 | + * Reject a chain |
| 11 | + */ |
| 12 | +export type Reject = WithResolvers<unknown>["reject"]; |
| 13 | + |
| 14 | +/** |
| 15 | + * Represent the eventual completion of an [Operation] in a fashion |
| 16 | + * that mirrors the * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise Promise} |
| 17 | + * implementation |
| 18 | + */ |
| 19 | +export class Chain<T> implements From<T> { |
| 20 | + private resolvers = withResolvers<T>(); |
| 21 | + |
| 22 | + /** |
| 23 | + * Create a chain from any operation. |
| 24 | + */ |
| 25 | + static from = from; |
| 26 | + |
| 27 | + constructor(compute: (resolve: Resolve<T>, reject: Reject) => void) { |
| 28 | + let { resolve, reject } = this.resolvers; |
| 29 | + compute(resolve, reject); |
| 30 | + } |
| 31 | + |
| 32 | + then<B>(fn: (value: T) => Operation<B>): From<B> { |
| 33 | + let { operation } = this.resolvers; |
| 34 | + return from(operation).then(fn); |
| 35 | + } |
| 36 | + |
| 37 | + catch<B>(fn: (error: unknown | Error) => Operation<B>): From<T | B> { |
| 38 | + let { operation } = this.resolvers; |
| 39 | + return from(operation).catch(fn); |
| 40 | + } |
| 41 | + |
| 42 | + finally(fn: () => Operation<void>): From<T> { |
| 43 | + let { operation } = this.resolvers; |
| 44 | + return from(operation).finally(fn); |
| 45 | + } |
| 46 | + |
| 47 | + [Symbol.iterator]() { |
| 48 | + return this.resolvers.operation[Symbol.iterator](); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export interface From<A> extends Operation<A> { |
| 53 | + /** |
| 54 | + * Create a new chain that will resolve to the current chain's value after |
| 55 | + * applying `fn`; |
| 56 | + * |
| 57 | + * @param `fn` - is applied to the source operation's result to create the chained operation's result |
| 58 | + * |
| 59 | + * @returns a new {Chain} representing this application |
| 60 | + */ |
| 61 | + then<B>(fn: (value: A) => Operation<B>): From<B>; |
| 62 | + |
| 63 | + /** |
| 64 | + * Create a new chain that will resolve to the original chain's |
| 65 | + * value, or the result of `fn` in the event that the current chain |
| 66 | + * rejects. applying `fn`; |
| 67 | + * |
| 68 | + * @param `fn` - applied when the current chain rejects and becomes the result of chain |
| 69 | + * |
| 70 | + * @returns a new {Chain} representing the potentially caught rejection |
| 71 | + */ |
| 72 | + catch<B>(fn: (error: unknown | Error) => Operation<B>): From<A | B>; |
| 73 | + |
| 74 | + /** |
| 75 | + * Create a new {Chain} that behaves exactly like the original chain, except that operation specified with |
| 76 | + * `fn` will run in all cases. |
| 77 | + * |
| 78 | + * @param `fn` - a function returning an operation that is always |
| 79 | + * evaluate just before the current chain yields its value. |
| 80 | + */ |
| 81 | + finally(fn: () => Operation<void>): From<A>; |
| 82 | +} |
| 83 | + |
| 84 | +function from<T>(source: Operation<T>): From<T> { |
| 85 | + let resolvers: WithResolvers<T> | undefined = undefined; |
| 86 | + let chain: From<T> = { |
| 87 | + *[Symbol.iterator]() { |
| 88 | + if (!resolvers) { |
| 89 | + resolvers = withResolvers<T>(); |
| 90 | + try { |
| 91 | + resolvers.resolve(yield* source); |
| 92 | + } catch (e) { |
| 93 | + resolvers.reject(e as Error); |
| 94 | + } |
| 95 | + } |
| 96 | + return yield* resolvers.operation; |
| 97 | + }, |
| 98 | + then: (fn) => |
| 99 | + from(call(function* () { |
| 100 | + return yield* fn(yield* chain); |
| 101 | + })), |
| 102 | + |
| 103 | + catch: (fn) => |
| 104 | + from(call(function* () { |
| 105 | + try { |
| 106 | + return yield* chain; |
| 107 | + } catch (e) { |
| 108 | + return yield* fn(e); |
| 109 | + } |
| 110 | + })), |
| 111 | + |
| 112 | + finally: (fn) => |
| 113 | + from(call(function* () { |
| 114 | + try { |
| 115 | + return yield* chain; |
| 116 | + } finally { |
| 117 | + yield* fn(); |
| 118 | + } |
| 119 | + })), |
| 120 | + }; |
| 121 | + return chain; |
| 122 | +} |
0 commit comments