1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | exports.checkCancelled = exports.isCancelled = exports.cancelled = exports.CancellationTokenSource = exports.CancellationError = exports.CancellationToken = void 0;
|
23 | const event_1 = require("./event");
|
24 | const types_1 = require("./types");
|
25 |
|
26 | const shortcutEvent = Object.freeze(Object.assign(function (callback, context) {
|
27 | const handle = setTimeout(callback.bind(context), 0);
|
28 | return { dispose() { clearTimeout(handle); } };
|
29 | }, {
|
30 | get maxListeners() { return 0; },
|
31 | set maxListeners(maxListeners) { }
|
32 | }));
|
33 | var CancellationToken;
|
34 | (function (CancellationToken) {
|
35 | CancellationToken.None = Object.freeze({
|
36 | isCancellationRequested: false,
|
37 | onCancellationRequested: event_1.Event.None
|
38 | });
|
39 | CancellationToken.Cancelled = Object.freeze({
|
40 | isCancellationRequested: true,
|
41 | onCancellationRequested: shortcutEvent
|
42 | });
|
43 | function is(value) {
|
44 | return (0, types_1.isObject)(value) && (value === CancellationToken.None
|
45 | || value === CancellationToken.Cancelled
|
46 | || ((0, types_1.isBoolean)(value.isCancellationRequested) && !!value.onCancellationRequested));
|
47 | }
|
48 | CancellationToken.is = is;
|
49 | })(CancellationToken = exports.CancellationToken || (exports.CancellationToken = {}));
|
50 | class CancellationError extends Error {
|
51 | constructor() {
|
52 | super('Canceled');
|
53 | this.name = this.message;
|
54 | }
|
55 | }
|
56 | exports.CancellationError = CancellationError;
|
57 | class MutableToken {
|
58 | constructor() {
|
59 | this._isCancelled = false;
|
60 | }
|
61 | cancel() {
|
62 | if (!this._isCancelled) {
|
63 | this._isCancelled = true;
|
64 | if (this._emitter) {
|
65 | this._emitter.fire(undefined);
|
66 | this._emitter = undefined;
|
67 | }
|
68 | }
|
69 | }
|
70 | get isCancellationRequested() {
|
71 | return this._isCancelled;
|
72 | }
|
73 | get onCancellationRequested() {
|
74 | if (this._isCancelled) {
|
75 | return shortcutEvent;
|
76 | }
|
77 | if (!this._emitter) {
|
78 | this._emitter = new event_1.Emitter();
|
79 | }
|
80 | return this._emitter.event;
|
81 | }
|
82 | }
|
83 | class CancellationTokenSource {
|
84 | get token() {
|
85 | if (!this._token) {
|
86 |
|
87 |
|
88 | this._token = new MutableToken();
|
89 | }
|
90 | return this._token;
|
91 | }
|
92 | cancel() {
|
93 | if (!this._token) {
|
94 |
|
95 |
|
96 |
|
97 | this._token = CancellationToken.Cancelled;
|
98 | }
|
99 | else if (this._token !== CancellationToken.Cancelled) {
|
100 | this._token.cancel();
|
101 | }
|
102 | }
|
103 | dispose() {
|
104 | this.cancel();
|
105 | }
|
106 | }
|
107 | exports.CancellationTokenSource = CancellationTokenSource;
|
108 | const cancelledMessage = 'Cancelled';
|
109 | function cancelled() {
|
110 | return new Error(cancelledMessage);
|
111 | }
|
112 | exports.cancelled = cancelled;
|
113 | function isCancelled(err) {
|
114 | return !!err && err.message === cancelledMessage;
|
115 | }
|
116 | exports.isCancelled = isCancelled;
|
117 | function checkCancelled(token) {
|
118 | if (!!token && token.isCancellationRequested) {
|
119 | throw cancelled();
|
120 | }
|
121 | }
|
122 | exports.checkCancelled = checkCancelled;
|
123 |
|
\ | No newline at end of file |