UNPKG

1.75 kBJavaScriptView Raw
1"use strict";
2/*! *****************************************************************************
3Copyright (c) Microsoft Corporation.
4Licensed under the Apache License, Version 2.0.
5
6See LICENSE file in the project root for details.
7***************************************************************************** */
8Object.defineProperty(exports, "__esModule", { value: true });
9const list_1 = require("./list");
10const cancellation_1 = require("./cancellation");
11const adapter_1 = require("./adapter");
12/**
13 * Asynchronously notifies one or more waiting Promises that an event has occurred.
14 */
15class 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}
56exports.Pulsar = Pulsar;