observablemap.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { IDisposable } from '@lumino/disposable';
  4. import { ISignal, Signal } from '@lumino/signaling';
  5. import { IObservable } from './modeldb';
  6. /**
  7. * A map which can be observed for changes.
  8. */
  9. export interface IObservableMap<T> extends IDisposable, IObservable {
  10. /**
  11. * The type of the Observable.
  12. */
  13. type: 'Map';
  14. /**
  15. * A signal emitted when the map has changed.
  16. */
  17. readonly changed: ISignal<this, IObservableMap.IChangedArgs<T>>;
  18. /**
  19. * The number of key-value pairs in the map.
  20. */
  21. readonly size: number;
  22. /**
  23. * Set a key-value pair in the map
  24. *
  25. * @param key - The key to set.
  26. *
  27. * @param value - The value for the key.
  28. *
  29. * @returns the old value for the key, or undefined
  30. * if that did not exist.
  31. */
  32. set(key: string, value: T): T | undefined;
  33. /**
  34. * Get a value for a given key.
  35. *
  36. * @param key - the key.
  37. *
  38. * @returns the value for that key.
  39. */
  40. get(key: string): T | undefined;
  41. /**
  42. * Check whether the map has a key.
  43. *
  44. * @param key - the key to check.
  45. *
  46. * @returns `true` if the map has the key, `false` otherwise.
  47. */
  48. has(key: string): boolean;
  49. /**
  50. * Get a list of the keys in the map.
  51. *
  52. * @returns - a list of keys.
  53. */
  54. keys(): string[];
  55. /**
  56. * Get a list of the values in the map.
  57. *
  58. * @returns - a list of values.
  59. */
  60. values(): T[];
  61. /**
  62. * Remove a key from the map
  63. *
  64. * @param key - the key to remove.
  65. *
  66. * @returns the value of the given key,
  67. * or undefined if that does not exist.
  68. */
  69. delete(key: string): T | undefined;
  70. /**
  71. * Set the ObservableMap to an empty map.
  72. */
  73. clear(): void;
  74. /**
  75. * Dispose of the resources held by the map.
  76. */
  77. dispose(): void;
  78. }
  79. /**
  80. * The interfaces associated with an IObservableMap.
  81. */
  82. export namespace IObservableMap {
  83. /**
  84. * The change types which occur on an observable map.
  85. */
  86. export type ChangeType =
  87. /**
  88. * An entry was added.
  89. */
  90. | 'add'
  91. /**
  92. * An entry was removed.
  93. */
  94. | 'remove'
  95. /**
  96. * An entry was changed.
  97. */
  98. | 'change';
  99. /**
  100. * The changed args object which is emitted by an observable map.
  101. */
  102. export interface IChangedArgs<T> {
  103. /**
  104. * The type of change undergone by the map.
  105. */
  106. type: ChangeType;
  107. /**
  108. * The key of the change.
  109. */
  110. key: string;
  111. /**
  112. * The old value of the change.
  113. */
  114. oldValue: T | undefined;
  115. /**
  116. * The new value of the change.
  117. */
  118. newValue: T | undefined;
  119. }
  120. }
  121. /**
  122. * A concrete implementation of IObservbleMap<T>.
  123. */
  124. export class ObservableMap<T> implements IObservableMap<T> {
  125. /**
  126. * Construct a new observable map.
  127. */
  128. constructor(options: ObservableMap.IOptions<T> = {}) {
  129. this._itemCmp = options.itemCmp || Private.itemCmp;
  130. if (options.values) {
  131. for (const key in options.values) {
  132. this._map.set(key, options.values[key]);
  133. }
  134. }
  135. }
  136. /**
  137. * The type of the Observable.
  138. */
  139. get type(): 'Map' {
  140. return 'Map';
  141. }
  142. /**
  143. * A signal emitted when the map has changed.
  144. */
  145. get changed(): ISignal<this, IObservableMap.IChangedArgs<T>> {
  146. return this._changed;
  147. }
  148. /**
  149. * Whether this map has been disposed.
  150. */
  151. get isDisposed(): boolean {
  152. return this._isDisposed;
  153. }
  154. /**
  155. * The number of key-value pairs in the map.
  156. */
  157. get size(): number {
  158. return this._map.size;
  159. }
  160. /**
  161. * Set a key-value pair in the map
  162. *
  163. * @param key - The key to set.
  164. *
  165. * @param value - The value for the key.
  166. *
  167. * @returns the old value for the key, or undefined
  168. * if that did not exist.
  169. *
  170. * @throws if the new value is undefined.
  171. *
  172. * #### Notes
  173. * This is a no-op if the value does not change.
  174. */
  175. set(key: string, value: T): T | undefined {
  176. const oldVal = this._map.get(key);
  177. if (value === undefined) {
  178. throw Error('Cannot set an undefined value, use remove');
  179. }
  180. // Bail if the value does not change.
  181. const itemCmp = this._itemCmp;
  182. if (oldVal !== undefined && itemCmp(oldVal, value)) {
  183. return oldVal;
  184. }
  185. this._map.set(key, value);
  186. this._changed.emit({
  187. type: oldVal ? 'change' : 'add',
  188. key: key,
  189. oldValue: oldVal,
  190. newValue: value
  191. });
  192. return oldVal;
  193. }
  194. /**
  195. * Get a value for a given key.
  196. *
  197. * @param key - the key.
  198. *
  199. * @returns the value for that key.
  200. */
  201. get(key: string): T | undefined {
  202. return this._map.get(key);
  203. }
  204. /**
  205. * Check whether the map has a key.
  206. *
  207. * @param key - the key to check.
  208. *
  209. * @returns `true` if the map has the key, `false` otherwise.
  210. */
  211. has(key: string): boolean {
  212. return this._map.has(key);
  213. }
  214. /**
  215. * Get a list of the keys in the map.
  216. *
  217. * @returns - a list of keys.
  218. */
  219. keys(): string[] {
  220. const keyList: string[] = [];
  221. this._map.forEach((v: T, k: string) => {
  222. keyList.push(k);
  223. });
  224. return keyList;
  225. }
  226. /**
  227. * Get a list of the values in the map.
  228. *
  229. * @returns - a list of values.
  230. */
  231. values(): T[] {
  232. const valList: T[] = [];
  233. this._map.forEach((v: T, k: string) => {
  234. valList.push(v);
  235. });
  236. return valList;
  237. }
  238. /**
  239. * Remove a key from the map
  240. *
  241. * @param key - the key to remove.
  242. *
  243. * @returns the value of the given key,
  244. * or undefined if that does not exist.
  245. *
  246. * #### Notes
  247. * This is a no-op if the value does not change.
  248. */
  249. delete(key: string): T | undefined {
  250. const oldVal = this._map.get(key);
  251. const removed = this._map.delete(key);
  252. if (removed) {
  253. this._changed.emit({
  254. type: 'remove',
  255. key: key,
  256. oldValue: oldVal,
  257. newValue: undefined
  258. });
  259. }
  260. return oldVal;
  261. }
  262. /**
  263. * Set the ObservableMap to an empty map.
  264. */
  265. clear(): void {
  266. // Delete one by one to emit the correct signals.
  267. const keyList = this.keys();
  268. for (let i = 0; i < keyList.length; i++) {
  269. this.delete(keyList[i]);
  270. }
  271. }
  272. /**
  273. * Dispose of the resources held by the map.
  274. */
  275. dispose(): void {
  276. if (this.isDisposed) {
  277. return;
  278. }
  279. this._isDisposed = true;
  280. Signal.clearData(this);
  281. this._map.clear();
  282. }
  283. private _map: Map<string, T> = new Map<string, T>();
  284. private _itemCmp: (first: T, second: T) => boolean;
  285. private _changed = new Signal<this, IObservableMap.IChangedArgs<T>>(this);
  286. private _isDisposed = false;
  287. }
  288. /**
  289. * The namespace for `ObservableMap` class statics.
  290. */
  291. export namespace ObservableMap {
  292. /**
  293. * The options used to initialize an observable map.
  294. */
  295. export interface IOptions<T> {
  296. /**
  297. * An optional initial set of values.
  298. */
  299. values?: { [key: string]: T };
  300. /**
  301. * The item comparison function for change detection on `set`.
  302. *
  303. * If not given, strict `===` equality will be used.
  304. */
  305. itemCmp?: (first: T, second: T) => boolean;
  306. }
  307. }
  308. /**
  309. * The namespace for module private data.
  310. */
  311. namespace Private {
  312. /**
  313. * The default strict equality item comparator.
  314. */
  315. export function itemCmp(first: any, second: any): boolean {
  316. return first === second;
  317. }
  318. }