1 | ;
|
2 | /*! *****************************************************************************
|
3 | Copyright (c) Microsoft Corporation.
|
4 | Licensed under the Apache License, Version 2.0.
|
5 |
|
6 | See LICENSE file in the project root for details.
|
7 | ***************************************************************************** */
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | const list_1 = require("./list");
|
10 | const cancellation_1 = require("./cancellation");
|
11 | const adapter_1 = require("./adapter");
|
12 | /**
|
13 | * Asynchronously notifies one or more waiting Promises that an event has occurred.
|
14 | */
|
15 | class Pulsar {
|
16 | constructor() {
|
17 | this._waiters = new list_1.LinkedList();
|
18 | }
|
19 | /**
|
20 | * Notifies the next waiter.
|
21 | */
|
22 | pulse() {
|
23 | const waiter = this._waiters.shift();
|
24 | if (waiter)
|
25 | waiter();
|
26 | }
|
27 | /**
|
28 | * Notifies all waiters.
|
29 | */
|
30 | pulseAll() {
|
31 | for (const waiter of this._waiters.drain()) {
|
32 | if (waiter)
|
33 | waiter();
|
34 | }
|
35 | }
|
36 | /**
|
37 | * Asynchronously waits for the the next pulse.
|
38 | *
|
39 | * @param token A CancellationToken used to cancel the request.
|
40 | */
|
41 | wait(token) {
|
42 | return new Promise((resolve, reject) => {
|
43 | const _token = adapter_1.getToken(token);
|
44 | _token.throwIfCancellationRequested();
|
45 | const node = this._waiters.push(() => {
|
46 | registration.unregister();
|
47 | resolve();
|
48 | });
|
49 | const registration = _token.register(() => {
|
50 | node.list.deleteNode(node);
|
51 | reject(new cancellation_1.CancelError());
|
52 | });
|
53 | });
|
54 | }
|
55 | }
|
56 | exports.Pulsar = Pulsar;
|