-
Notifications
You must be signed in to change notification settings - Fork 143
feat: construct RpcPromise from a Promise #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ndisidore
wants to merge
4
commits into
main
Choose a base branch
from
feat/from-promise
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d1fb86d
feat: construct RpcPromise from a Promise
ndisidore 8c51593
fix: address review feedback on RpcPromise promise construction
ndisidore 49d3b4e
fix: address code-review findings on RpcPromise-from-Promise
ndisidore 52e5f90
docs: remove constructor special-case paragraphs per review
ndisidore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "capnweb": minor | ||
| --- | ||
|
|
||
| `RpcPromise` can now be constructed from a `Promise`: pipelined calls queue in order until it settles, so you can publish a capability that doesn't exist yet. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -561,9 +561,63 @@ export class RpcStub extends RpcTarget { | |
| } | ||
|
|
||
| export class RpcPromise extends RpcStub { | ||
| // TODO: Support passing target value or promise to constructor. | ||
| constructor(hook: StubHook, pathIfPromise: PropertyPath) { | ||
| super(hook, pathIfPromise); | ||
| // Internally, an `RpcPromise` is constructed from a `StubHook` plus a property path. The | ||
| // application may instead pass a promise for the eventual resolution; calls made before it | ||
| // settles are queued and delivered, in order, once it does. | ||
| constructor(hook: StubHook | PromiseLike<unknown>, pathIfPromise?: PropertyPath) { | ||
| if (hook instanceof StubHook) { | ||
| super(hook, pathIfPromise ?? []); | ||
| } else { | ||
| if (pathIfPromise !== undefined) { | ||
| throw new TypeError("RpcPromise constructor expected one argument, received two."); | ||
| } | ||
|
|
||
| let kind = typeForRpc(hook); | ||
| if (kind === "rpc-promise") { | ||
| // Adopt an existing `RpcPromise` directly, transferring ownership of its hook: the source | ||
| // promise is neutered, as if disposed, and must not be used afterwards. In particular, | ||
| // adoption keeps the promise lazy -- assimilating it as a thenable would instead force its | ||
| // resolution to be pulled -- and preserves hook-local behavior such as brokenness. This | ||
| // applies only to promises, not bare stubs: a non-promise hook may not implement pull(), | ||
| // so a bare stub takes the generic path below, which adopts the stub into the resolution | ||
| // payload. | ||
| let raw = unwrapStubAndPath(<RpcStub><unknown>hook); | ||
| if (raw.pathIfPromise!.length > 0) { | ||
| // Property promise: get() returns an independent hook, and properties have no | ||
| // disposer, so there is nothing to neuter. | ||
| super(raw.hook.get(raw.pathIfPromise!), []); | ||
| } else { | ||
| let adopted = raw.hook; | ||
| raw.hook = DISPOSED_HOOK; | ||
| super(adopted, []); | ||
| } | ||
| } else if (kind === "rpc-thenable") { | ||
| // Workerd-native RpcPromise/RpcProperty: wrap in a TargetStubHook, which pipelines calls | ||
| // directly on the thenable and awaits it only on pull(). | ||
| super(TargetStubHook.create(<RpcTarget><unknown>hook, undefined), []); | ||
| } else { | ||
| // `Promise.resolve()` natively handles the hazards of assimilating an arbitrary thenable | ||
| // (`then` getters with side effects, self-resolution, cross-realm thenables), so the | ||
| // resolution callback below only ever sees settled, non-thenable values. | ||
| // | ||
| // The resolution is adopted with "return" semantics, taking ownership of any stubs | ||
| // within (including a stub as the root value). This is the same representation used for | ||
| // the resolution of a local async call: pull() delivers the value, pipelined calls | ||
| // forward through the payload without forcing a pull, and a single-stub payload forwards | ||
| // onBroken(), preserving brokenness. | ||
| // | ||
| // A rejection is adopted as an ErrorStubHook rather than left to reject the backing | ||
| // promise. This way the promise chains backing queued calls never reject -- the calls | ||
| // land on the ErrorStubHook, which disposes their arguments -- and the error only | ||
| // surfaces through pull(), so a discarded pipelined call can't produce an unhandled | ||
| // rejection event. | ||
| let promiseHook = new PromiseStubHook(Promise.resolve(hook).then( | ||
| value => new PayloadStubHook(RpcPayload.fromAppReturn(value)), | ||
| err => new ErrorStubHook(err))); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PromiseStubHook already handles the error case. We should not be handling it explicitly here. From the comment above it sounds like this may have been done due to the missing disposal of arguments in that case -- but you've now fixed that. |
||
| promiseHook.ignoreUnhandledRejections(); | ||
| super(promiseHook, []); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| then(onfulfilled?: ((value: unknown) => unknown) | undefined | null, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was some discussion abut what happens when
new RpcPromise(existingRpcPromise)adopts the source's hook. Opted for this consume-and-neuter approach (e.g. treated like amove) for consistencypractically this means a user who wants copy semantics writes
new RpcPromise(source.dup())themselves which is in line with convention for the rest of the libThe alternative was something like
to auto-dup which would leave the original promise usable.
I'm impartial, but what is here seems marginally more consistent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think taking ownership is consistent with what happens with
Promise<T>-- theRpcPromisewill take responsibility for disposing theT.Worth noting, though, that
dup()won't work here: it returns anRpcStub, even when called on anRpcPromise.