-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #963 from thefrontside/cl/with-resolvers-v3-backport
✨ Backport `withResolvers()` from v4 to v3
- Loading branch information
Showing
7 changed files
with
200 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Err, Ok } from "./result.ts"; | ||
import { action } from "./instructions.ts"; | ||
import type { Operation, Result } from "./types.ts"; | ||
|
||
/** | ||
* The return type of {@link withResolvers}. It contains an operation bundled with | ||
* synchronous functions that determine its outcome. | ||
*/ | ||
export interface WithResolvers<T> { | ||
/* | ||
* An {@link Operation} that will either produce a value or raise an | ||
* exception when either `resolve` or `reject` is called. No matter | ||
* how many times this operation is yielded to, it will always | ||
* produce the same effect. | ||
*/ | ||
operation: Operation<T>; | ||
|
||
/** | ||
* Cause {@link operation} to produce `value`. If either `resolve` | ||
* or`reject` has been called before, this will have no effect. | ||
* | ||
* @param value - the value to produce | ||
*/ | ||
resolve(value: T): void; | ||
|
||
/** | ||
* Cause {@link operation} to raise `Error`. Any calling operation | ||
* waiting on `operation` will. Yielding to `operation` subsequently | ||
* will also raise the same error. * If either `resolve` or`reject` | ||
* has been called before, this will have no effect. | ||
* | ||
* @param error - the error to raise | ||
*/ | ||
reject(error: Error): void; | ||
} | ||
|
||
/** | ||
* Create an {link @Operation} and two functions to resolve or reject | ||
* it, corresponding to the two parameters passed to the executor of | ||
* the {@link action} constructor. This is the Effection equivalent of | ||
* [Promise.withResolvers()]{@link | ||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers} | ||
* | ||
* @returns an operation and its resolvers. | ||
*/ | ||
|
||
export function withResolvers<T>(): WithResolvers<T> { | ||
let continuations = new Set<(result: Result<T>) => void>(); | ||
let result: Result<T> | undefined = undefined; | ||
|
||
let operation: Operation<T> = action<T>( | ||
(resolve, reject) => { | ||
let settle = (outcome: Result<T>) => { | ||
if (outcome.ok) { | ||
resolve(outcome.value); | ||
} else { | ||
reject(outcome.error); | ||
} | ||
}; | ||
|
||
if (result) { | ||
settle(result); | ||
return () => {}; | ||
} else { | ||
continuations.add(settle); | ||
return () => continuations.delete(settle); | ||
} | ||
}, | ||
); | ||
|
||
let settle = (outcome: Result<T>) => { | ||
if (!result) { | ||
result = outcome; | ||
} | ||
for (let continuation of continuations) { | ||
continuation(result); | ||
} | ||
}; | ||
|
||
let resolve = (value: T) => settle(Ok(value)); | ||
let reject = (error: Error) => settle(Err(error)); | ||
|
||
return { operation, resolve, reject }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { run, withResolvers } from "../mod.ts"; | ||
import { describe, expect, it } from "./suite.ts"; | ||
|
||
describe("withResolvers()", () => { | ||
it("resolves", async () => { | ||
let { operation, resolve } = withResolvers<string>(); | ||
resolve("hello"); | ||
await expect(run(() => operation)).resolves.toEqual("hello"); | ||
}); | ||
it("resolves only once", async () => { | ||
let { operation, resolve, reject } = withResolvers<string>(); | ||
resolve("hello"); | ||
reject(new Error("boom!")); | ||
resolve("goodbye"); | ||
await expect(run(() => operation)).resolves.toEqual("hello"); | ||
}); | ||
it("rejects", async () => { | ||
let { operation, reject } = withResolvers<string>(); | ||
reject(new Error("boom!")); | ||
await expect(run(() => operation)).rejects.toMatchObject({ | ||
message: "boom!", | ||
}); | ||
}); | ||
it("rejects only once", async () => { | ||
let { operation, reject } = withResolvers<string>(); | ||
reject(new Error("boom!")); | ||
reject(new Error("bam!")); | ||
await expect(run(() => operation)).rejects.toMatchObject({ | ||
message: "boom!", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters