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/map.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
12
export class ObservableMap<K, V> implements Map<K, V> {
13
private readonly _data = new Map<K, V>();
14
15
private readonly _obs = observableValueOpts({ equalsFn: () => false }, this);
16
17
readonly observable: IObservable<Map<K, V>> = this._obs;
18
19
get size(): number {
20
return this._data.size;
21
}
22
23
has(key: K): boolean {
24
return this._data.has(key);
25
}
26
27
get(key: K): V | undefined {
28
return this._data.get(key);
29
}
30
31
set(key: K, value: V, tx?: ITransaction): this {
32
const hadKey = this._data.has(key);
33
const oldValue = this._data.get(key);
34
if (!hadKey || oldValue !== value) {
35
this._data.set(key, value);
36
this._obs.set(this, tx);
37
}
38
return this;
39
}
40
41
delete(key: K, tx?: ITransaction): boolean {
42
const result = this._data.delete(key);
43
if (result) {
44
this._obs.set(this, tx);
45
}
46
return result;
47
}
48
49
clear(tx?: ITransaction): void {
50
if (this._data.size > 0) {
51
this._data.clear();
52
this._obs.set(this, tx);
53
}
54
}
55
56
forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: unknown): void {
57
this._data.forEach((value, key, _map) => {
58
callbackfn.call(thisArg, value, key, this);
59
});
60
}
61
62
*entries(): IterableIterator<[K, V]> {
63
yield* this._data.entries();
64
}
65
66
*keys(): IterableIterator<K> {
67
yield* this._data.keys();
68
}
69
70
*values(): IterableIterator<V> {
71
yield* this._data.values();
72
}
73
74
[Symbol.iterator](): IterableIterator<[K, V]> {
75
return this.entries();
76
}
77
78
get [Symbol.toStringTag](): string {
79
return 'ObservableMap';
80
}
81
}
82
83