Path: blob/main/extensions/copilot/src/util/vs/base/common/observableInternal/map.ts
13406 views
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'12/*---------------------------------------------------------------------------------------------3* Copyright (c) Microsoft Corporation. All rights reserved.4* Licensed under the MIT License. See License.txt in the project root for license information.5*--------------------------------------------------------------------------------------------*/67import { IObservable, ITransaction } from '../observable';8import { observableValueOpts } from './observables/observableValueOpts';91011export class ObservableMap<K, V> implements Map<K, V> {12private readonly _data = new Map<K, V>();1314private readonly _obs = observableValueOpts({ equalsFn: () => false }, this);1516readonly observable: IObservable<Map<K, V>> = this._obs;1718get size(): number {19return this._data.size;20}2122has(key: K): boolean {23return this._data.has(key);24}2526get(key: K): V | undefined {27return this._data.get(key);28}2930set(key: K, value: V, tx?: ITransaction): this {31const hadKey = this._data.has(key);32const oldValue = this._data.get(key);33if (!hadKey || oldValue !== value) {34this._data.set(key, value);35this._obs.set(this, tx);36}37return this;38}3940delete(key: K, tx?: ITransaction): boolean {41const result = this._data.delete(key);42if (result) {43this._obs.set(this, tx);44}45return result;46}4748clear(tx?: ITransaction): void {49if (this._data.size > 0) {50this._data.clear();51this._obs.set(this, tx);52}53}5455forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: unknown): void {56this._data.forEach((value, key, _map) => {57callbackfn.call(thisArg, value, key, this);58});59}6061*entries(): IterableIterator<[K, V]> {62yield* this._data.entries();63}6465*keys(): IterableIterator<K> {66yield* this._data.keys();67}6869*values(): IterableIterator<V> {70yield* this._data.values();71}7273[Symbol.iterator](): IterableIterator<[K, V]> {74return this.entries();75}7677get [Symbol.toStringTag](): string {78return 'ObservableMap';79}80}818283