observablemap.ts 6.8 KB

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