Skip to content
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

Bump 0.5 #114

Merged
merged 8 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,31 @@ export class Context {
}));
}

/**
* Sleep for the specified time.
*
* @param ms Amount of time to sleep in milliseconds.
* @returns A Promise that resolves after the specified time has elapsed.
*/
async sleep(ms: number): Promise<void> {
// generate id
const id = `${this.invocation.id}.${this.invocation.counter++}`;

// create a promise that resolves when it times out
const promise = await this.resonate.promises.create(id, Date.now() + ms, {
tags: { "resonate:timeout": "true" },
});

// wait for the promise to resolve
// if wait time < 1, delay will be set to 1
if (promise.pending) {
await new Promise((resolve) => setTimeout(resolve, promise.timeout - Date.now()));
}

// tight loop in case the promise is not yet resolved
await promise.wait(Infinity, this.invocation.opts.poll);
}

/**
* Generate a deterministic random number.
*
Expand Down
3 changes: 1 addition & 2 deletions lib/core/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ export class DeferredExecution<T> extends Execution<T> {
headers: this.invocation.headers,
param: this.invocation.param,
tags: this.invocation.opts.tags,
poll: this.invocation.opts.poll,
},
);

Expand All @@ -240,7 +239,7 @@ export class DeferredExecution<T> extends Execution<T> {
protected async join(future: Future<T>) {
if (this.durablePromise) {
// poll the completion of the durable promise
await this.durablePromise.completed;
await this.durablePromise.sync(Infinity, this.invocation.opts.poll);

if (this.durablePromise.resolved) {
this.invocation.resolve(this.durablePromise.value());
Expand Down
Loading
Loading