Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/vs/base/common/observableInternal/set.ts
13406 views
1
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'
2
3
/*---------------------------------------------------------------------------------------------
4
* Copyright (c) Microsoft Corporation. All rights reserved.
5
* Licensed under the MIT License. See License.txt in the project root for license information.
6
*--------------------------------------------------------------------------------------------*/
7
8
import { IObservable, ITransaction } from '../observable';
9
import { observableValueOpts } from './observables/observableValueOpts';
10
11
export class ObservableSet<T> implements Set<T> {
12
13
private readonly _data = new Set<T>();
14
15
private _obs = observableValueOpts({ equalsFn: () => false }, this);
16
17
readonly observable: IObservable<Set<T>> = this._obs;
18
19
get size(): number {
20
return this._data.size;
21
}
22
23
has(value: T): boolean {
24
return this._data.has(value);
25
}
26
27
add(value: T, tx?: ITransaction): this {
28
const hadValue = this._data.has(value);
29
if (!hadValue) {
30
this._data.add(value);
31
this._obs.set(this, tx);
32
}
33
return this;
34
}
35
36
delete(value: T, tx?: ITransaction): boolean {
37
const result = this._data.delete(value);
38
if (result) {
39
this._obs.set(this, tx);
40
}
41
return result;
42
}
43
44
clear(tx?: ITransaction): void {
45
if (this._data.size > 0) {
46
this._data.clear();
47
this._obs.set(this, tx);
48
}
49
}
50
51
forEach(callbackfn: (value: T, value2: T, set: Set<T>) => void, thisArg?: any): void {
52
this._data.forEach((value, value2, _set) => {
53
// eslint-disable-next-line local/code-no-any-casts
54
callbackfn.call(thisArg, value, value2, this as any);
55
});
56
}
57
58
*entries(): IterableIterator<[T, T]> {
59
for (const value of this._data) {
60
yield [value, value];
61
}
62
}
63
64
*keys(): IterableIterator<T> {
65
yield* this._data.keys();
66
}
67
68
*values(): IterableIterator<T> {
69
yield* this._data.values();
70
}
71
72
[Symbol.iterator](): IterableIterator<T> {
73
return this.values();
74
}
75
76
get [Symbol.toStringTag](): string {
77
return 'ObservableSet';
78
}
79
}
80
81