forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtectableMap.ts
More file actions
125 lines (109 loc) · 3.94 KB
/
ProtectableMap.ts
File metadata and controls
125 lines (109 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { ProtectableMapView } from './ProtectableMapView';
/**
* Constructor parameters for {@link ProtectableMap}
*
* @public
*/
export interface IProtectableMapParameters<K, V> {
/**
* An optional hook that will be invoked before Map.clear() is performed.
*/
onClear?: (source: ProtectableMap<K, V>) => void;
/**
* An optional hook that will be invoked before Map.delete() is performed.
*/
onDelete?: (source: ProtectableMap<K, V>, key: K) => void;
/**
* An optional hook that will be invoked before Map.set() is performed.
* @remarks
* If this hook is provided, the function MUST return the `value` parameter.
* This provides the opportunity to modify the value before it is added
* to the map.
*/
onSet?: (source: ProtectableMap<K, V>, key: K, value: V) => V;
}
/**
* The ProtectableMap provides an easy way for an API to expose a `Map<K, V>` property
* while intercepting and validating any write operations that are performed by
* consumers of the API.
*
* @remarks
* The ProtectableMap itself is intended to be a private object that only its owner
* can access directly. Any operations performed directly on the ProtectableMap will
* bypass the hooks and any validation they perform. The public property that is exposed
* to API consumers should return {@link ProtectableMap.protectedView} instead.
*
* For example, suppose you want to share your `Map<string, number>` data structure,
* but you want to enforce that the key must always be an upper case string:
* You could use the onSet() hook to validate the keys and throw an exception
* if the key is not uppercase.
*
* @public
*/
export class ProtectableMap<K, V> {
private readonly _protectedView: ProtectableMapView<K, V>;
public constructor(parameters: IProtectableMapParameters<K, V>) {
this._protectedView = new ProtectableMapView<K, V>(this, parameters);
}
/**
* The owner of the protectable map should return this object via its public API.
*/
public get protectedView(): Map<K, V> {
return this._protectedView;
}
// ---------------------------------------------------------------------------
// lib.es2015.collections contract - write operations
/**
* Removes all entries from the map.
* This operation does NOT invoke the ProtectableMap onClear() hook.
*/
public clear(): void {
this._protectedView._clearUnprotected();
}
/**
* Removes the specified key from the map.
* This operation does NOT invoke the ProtectableMap onDelete() hook.
*/
public delete(key: K): boolean {
return this._protectedView._deleteUnprotected(key);
}
/**
* Sets a value for the specified key.
* This operation does NOT invoke the ProtectableMap onSet() hook.
*/
public set(key: K, value: V): this {
this._protectedView._setUnprotected(key, value);
return this;
}
// ---------------------------------------------------------------------------
// lib.es2015.collections contract - read operations
/**
* Performs an operation for each (key, value) entries in the map.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void {
this._protectedView.forEach(callbackfn);
}
/**
* Retrieves the value for the specified key.
* @returns undefined if the value is undefined OR if the key is missing;
* otherwise returns the value associated with the key.
*/
public get(key: K): V | undefined {
return this._protectedView.get(key);
}
/**
* Returns true if the specified key belongs to the map.
*/
public has(key: K): boolean {
return this._protectedView.has(key);
}
/**
* Returns the number of (key, value) entries in the map.
*/
public get size(): number {
return this._protectedView.size;
}
}