UNPKG

4.25 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.disposableTimeout = exports.DisposableGroup = exports.DisposableCollection = exports.Disposable = void 0;
19const event_1 = require("./event");
20const types_1 = require("./types");
21var Disposable;
22(function (Disposable) {
23 function is(arg) {
24 return (0, types_1.isObject)(arg) && (0, types_1.isFunction)(arg.dispose);
25 }
26 Disposable.is = is;
27 function create(func) {
28 return { dispose: func };
29 }
30 Disposable.create = create;
31})(Disposable = exports.Disposable || (exports.Disposable = {}));
32/**
33 * Ensures that every reference to {@link Disposable.NULL} returns a new object,
34 * as sharing a disposable between multiple {@link DisposableCollection} can have unexpected side effects
35 */
36Object.defineProperty(Disposable, 'NULL', {
37 configurable: false,
38 enumerable: true,
39 get() {
40 return { dispose: () => { } };
41 }
42});
43class DisposableCollection {
44 constructor(...toDispose) {
45 this.disposables = [];
46 this.onDisposeEmitter = new event_1.Emitter();
47 this.disposingElements = false;
48 toDispose.forEach(d => this.push(d));
49 }
50 /**
51 * This event is fired only once
52 * on first dispose of not empty collection.
53 */
54 get onDispose() {
55 return this.onDisposeEmitter.event;
56 }
57 checkDisposed() {
58 if (this.disposed && !this.disposingElements) {
59 this.onDisposeEmitter.fire(undefined);
60 this.onDisposeEmitter.dispose();
61 }
62 }
63 get disposed() {
64 return this.disposables.length === 0;
65 }
66 dispose() {
67 if (this.disposed || this.disposingElements) {
68 return;
69 }
70 this.disposingElements = true;
71 while (!this.disposed) {
72 try {
73 this.disposables.pop().dispose();
74 }
75 catch (e) {
76 console.error(e);
77 }
78 }
79 this.disposingElements = false;
80 this.checkDisposed();
81 }
82 push(disposable) {
83 const disposables = this.disposables;
84 disposables.push(disposable);
85 const originalDispose = disposable.dispose.bind(disposable);
86 const toRemove = Disposable.create(() => {
87 const index = disposables.indexOf(disposable);
88 if (index !== -1) {
89 disposables.splice(index, 1);
90 }
91 this.checkDisposed();
92 });
93 disposable.dispose = () => {
94 toRemove.dispose();
95 disposable.dispose = originalDispose;
96 originalDispose();
97 };
98 return toRemove;
99 }
100 pushAll(disposables) {
101 return disposables.map(disposable => this.push(disposable));
102 }
103}
104exports.DisposableCollection = DisposableCollection;
105var DisposableGroup;
106(function (DisposableGroup) {
107 function canPush(candidate) {
108 return Boolean(candidate && candidate.push);
109 }
110 DisposableGroup.canPush = canPush;
111 function canAdd(candidate) {
112 return Boolean(candidate && candidate.add);
113 }
114 DisposableGroup.canAdd = canAdd;
115})(DisposableGroup = exports.DisposableGroup || (exports.DisposableGroup = {}));
116function disposableTimeout(...args) {
117 const handle = setTimeout(...args);
118 return { dispose: () => clearTimeout(handle) };
119}
120exports.disposableTimeout = disposableTimeout;
121//# sourceMappingURL=disposable.js.map
\No newline at end of file