Skip to content

Commit

Permalink
Added cancellation overload to delay.
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Apr 30, 2016
1 parent d9efcaa commit 85da900
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
21 changes: 18 additions & 3 deletions docs/scheduling.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright (c) Microsoft Corporation.
Copyright (c) Microsoft Corporation.
Licensed under the Apache License, Version 2.0.
See LICENSE file in the project root for details.
Expand All @@ -18,6 +18,7 @@ See LICENSE file in the project root for details.
* [stack.push(value)](#stackpushvalue)
* [stack.pop()](#stackpop)
* [Function: delay(msec, value?)](#function-delaymsec-value)
* [Function: delay(token, msec, value?)](#function-delaytoken-msec-value)

# Class: AsyncQueue
An asynchronous queue.
Expand Down Expand Up @@ -72,7 +73,20 @@ returns a Promise for the next value to be pushed on to the stack.
* Returns: [&lt;Promise&gt;][Promise]

# Function: delay(msec, value?)
Waits the specified number of milliseconds and resolves with the provided value.
Waits the specified number of milliseconds before resolving with the provided value.
* `msec` [&lt;Number&gt;][Number] The number of milliseconds to delay.
* `value` &lt;`any`&gt; The resolution value for the promise.
* Returns: [&lt;Promise&gt;][Promise]

### Syntax
```ts
export declare function delay(token: CancellationToken, msec: number): Promise<void>;
export declare function delay<T>(token: CancellationToken, msec: number, value?: T | PromiseLike<T>): Promise<T>;
```

# Function: delay(token, msec, value?)
Waits the specified number of milliseconds before resolving with the provided value.
* `token` [&lt;CancellationToken&gt;][CancellationToken] A CancellationToken.
* `msec` [&lt;Number&gt;][Number] The number of milliseconds to delay.
* `value` &lt;`any`&gt; The resolution value for the promise.
* Returns: [&lt;Promise&gt;][Promise]
Expand All @@ -85,4 +99,5 @@ export declare function delay<T>(msec: number, value?: T | PromiseLike<T>): Prom

[Number]: http://ecma-international.org/ecma-262/6.0/index.html#sec-number-constructor
[Promise]: http://ecma-international.org/ecma-262/6.0/index.html#sec-promise-constructor
[Iterable]: http://ecma-international.org/ecma-262/6.0/index.html#sec-symbol.iterator
[Iterable]: http://ecma-international.org/ecma-262/6.0/index.html#sec-symbol.iterator
[CancellationToken]: ./cancellation.md#class-cancellationtoken
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prex",
"version": "0.1.0",
"version": "0.1.1",
"description": "Async coordination primitives and extensions on top of ES6 Promises",
"license": "Apache-2.0",
"keywords": [
Expand Down
76 changes: 74 additions & 2 deletions src/lib/delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,80 @@ Licensed under the Apache License, Version 2.0.
See LICENSE file in the project root for details.
***************************************************************************** */

import { CancellationToken } from "./cancellation";

/**
* Waits the specified number of milliseconds before resolving.
*
* @param msec The number of milliseconds to wait before resolving.
*/
export function delay(msec: number): Promise<void>;

/**
* Waits the specified number of milliseconds before resolving with the provided value.
*
* @param msec The number of milliseconds to wait before resolving.
* @param value An optional value for the resulting Promise.
*/
export function delay<T>(msec: number, value: T | PromiseLike<T>): Promise<T>;
export function delay<T>(msec: number, value?: T | PromiseLike<T>) {
return new Promise<T>(resolve => setTimeout(resolve, msec, value));

/**
* Waits the specified number of milliseconds before resolving.
*
* @param token A CancellationToken
* @param msec The number of milliseconds to wait before resolving.
*/
export function delay(token: CancellationToken, msec: number): Promise<void>;

/**
* Waits the specified number of milliseconds before resolving with the provided value.
*
* @param token A CancellationToken
* @param msec The number of milliseconds to wait before resolving.
* @param value An optional value for the resulting Promise.
*/
export function delay<T>(token: CancellationToken, msec: number, value: T | PromiseLike<T>): Promise<T>;

/**
* Waits the specified number of milliseconds before resolving with the provided value.
*
* @param token A CancellationToken
* @param msec The number of milliseconds to wait before resolving.
* @param value An optional value for the resulting Promise.
*/
export function delay<T>(token_: number | CancellationToken, msec_?: T | PromiseLike<T> | number, value?: T | PromiseLike<T>) {
let token: CancellationToken;
let msec: number;
if (typeof token_ === "number") {
value = msec_ as T | PromiseLike<T>;
msec = token_;
token = CancellationToken.none;
}
else {
msec = msec_ as number;
token = token_ as CancellationToken;
}

if (!token.canBeCanceled) {
return new Promise<T>(resolve => setTimeout(resolve, msec, value));
}

return new Promise<T>((resolve, reject) => {
token.throwIfCancellationRequested();

const handle = setTimeout(() => {
registration.unregister();
resolve(value);
}, msec);

const registration = token.register(() => {
clearTimeout(handle);
try {
token.throwIfCancellationRequested();
}
catch (e) {
reject(e);
}
});
});
}

0 comments on commit 85da900

Please sign in to comment.