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 utils_1 = require("./utils");
|
12 | const adapter_1 = require("./adapter");
|
13 | /**
|
14 | * Asynchronously notifies one or more waiting Promises that an event has occurred.
|
15 | */
|
16 | class ManualResetEvent {
|
17 | /**
|
18 | * Initializes a new instance of the ManualResetEvent class.
|
19 | *
|
20 | * @param initialState A value indicating whether to set the initial state to signaled.
|
21 | */
|
22 | constructor(initialState) {
|
23 | this._waiters = new list_1.LinkedList();
|
24 | if (utils_1.isMissing(initialState))
|
25 | initialState = false;
|
26 | if (!utils_1.isBoolean(initialState))
|
27 | throw new TypeError("Boolean expected: initialState.");
|
28 | this._signaled = !!initialState;
|
29 | }
|
30 | /**
|
31 | * Gets a value indicating whether the event is signaled.
|
32 | */
|
33 | get isSet() {
|
34 | return this._signaled;
|
35 | }
|
36 | /**
|
37 | * Sets the state of the event to signaled, resolving one or more waiting Promises.
|
38 | */
|
39 | set() {
|
40 | if (!this._signaled) {
|
41 | this._signaled = true;
|
42 | for (const waiter of this._waiters.drain()) {
|
43 | if (waiter)
|
44 | waiter();
|
45 | }
|
46 | }
|
47 | }
|
48 | /**
|
49 | * Sets the state of the event to nonsignaled, causing asynchronous operations to pause.
|
50 | */
|
51 | reset() {
|
52 | this._signaled = false;
|
53 | }
|
54 | /**
|
55 | * Asynchronously waits for the event to become signaled.
|
56 | *
|
57 | * @param token A CancellationToken used to cancel the request.
|
58 | */
|
59 | wait(token) {
|
60 | return new Promise((resolve, reject) => {
|
61 | const _token = adapter_1.getToken(token);
|
62 | _token.throwIfCancellationRequested();
|
63 | if (this._signaled) {
|
64 | resolve();
|
65 | return;
|
66 | }
|
67 | const node = this._waiters.push(() => {
|
68 | registration.unregister();
|
69 | if (_token.cancellationRequested) {
|
70 | reject(new cancellation_1.CancelError());
|
71 | }
|
72 | else {
|
73 | resolve();
|
74 | }
|
75 | });
|
76 | const registration = _token.register(() => {
|
77 | if (node.list) {
|
78 | node.list.deleteNode(node);
|
79 | reject(new cancellation_1.CancelError());
|
80 | }
|
81 | });
|
82 | });
|
83 | }
|
84 | }
|
85 | exports.ManualResetEvent = ManualResetEvent;
|