Skip to content

Commit

Permalink
Merge pull request #114 from resonatehq/main
Browse files Browse the repository at this point in the history
Bump 0.5
  • Loading branch information
dfarr authored May 8, 2024
2 parents 1f8ab6d + 6e401ae commit 441e094
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 16 deletions.
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

0 comments on commit 441e094

Please sign in to comment.