|
| 1 | +/*** |
| 2 | +Bindings to JavaScript's `WeakMap`. |
| 3 | +
|
| 4 | +Weak maps keep key/value pairs where keys must be objects and the references do not prevent garbage collection. |
| 5 | +*/ |
| 6 | + |
| 7 | +/** Mutable weak map storing values of type `'v` with object keys `'k`. */ |
1 | 8 | @notUndefined |
2 | 9 | type t<'k, 'v> |
3 | 10 |
|
| 11 | +/** |
| 12 | +Creates an empty weak map. |
| 13 | +
|
| 14 | +See [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) on MDN. |
| 15 | +
|
| 16 | +## Examples |
| 17 | +
|
| 18 | +```rescript |
| 19 | +let cache = Stdlib_WeakMap.make() |
| 20 | +Stdlib_WeakMap.get(cache, Stdlib_Object.make()) == None |
| 21 | +``` |
| 22 | +*/ |
4 | 23 | @new external make: unit => t<'k, 'v> = "WeakMap" |
5 | 24 |
|
| 25 | +/** |
| 26 | +`get(map, key)` returns `Some(value)` when `key` exists, otherwise `None`. |
| 27 | +
|
| 28 | +## Examples |
| 29 | +
|
| 30 | +```rescript |
| 31 | +let cache = Stdlib_WeakMap.make() |
| 32 | +let key = Stdlib_Object.make() |
| 33 | +Stdlib_WeakMap.get(cache, key) == None |
| 34 | +let _ = Stdlib_WeakMap.set(cache, key, "user") |
| 35 | +Stdlib_WeakMap.get(cache, key) == Some("user") |
| 36 | +``` |
| 37 | +*/ |
6 | 38 | @send external get: (t<'k, 'v>, 'k) => option<'v> = "get" |
| 39 | + |
| 40 | +/** |
| 41 | +`has(map, key)` checks whether `key` exists in the weak map. |
| 42 | +
|
| 43 | +## Examples |
| 44 | +
|
| 45 | +```rescript |
| 46 | +let cache = Stdlib_WeakMap.make() |
| 47 | +let key = Stdlib_Object.make() |
| 48 | +Stdlib_WeakMap.has(cache, key) == false |
| 49 | +let _ = Stdlib_WeakMap.set(cache, key, ()) |
| 50 | +Stdlib_WeakMap.has(cache, key) == true |
| 51 | +``` |
| 52 | +*/ |
7 | 53 | @send external has: (t<'k, 'v>, 'k) => bool = "has" |
| 54 | + |
| 55 | +/** |
| 56 | +`set(map, key, value)` stores `value` for `key` and returns the map for chaining. |
| 57 | +
|
| 58 | +## Examples |
| 59 | +
|
| 60 | +```rescript |
| 61 | +let cache = Stdlib_WeakMap.make() |
| 62 | +let key = Stdlib_Object.make() |
| 63 | +let _ = Stdlib_WeakMap.set(cache, key, 42) |
| 64 | +Stdlib_WeakMap.get(cache, key) == Some(42) |
| 65 | +``` |
| 66 | +*/ |
8 | 67 | @send external set: (t<'k, 'v>, 'k, 'v) => t<'k, 'v> = "set" |
| 68 | + |
| 69 | +/** |
| 70 | +`delete(map, key)` removes `key` and returns `true` if an entry existed. |
| 71 | +
|
| 72 | +## Examples |
| 73 | +
|
| 74 | +```rescript |
| 75 | +let cache = Stdlib_WeakMap.make() |
| 76 | +let key = Stdlib_Object.make() |
| 77 | +Stdlib_WeakMap.delete(cache, key) == false |
| 78 | +let _ = Stdlib_WeakMap.set(cache, key, 1) |
| 79 | +Stdlib_WeakMap.delete(cache, key) == true |
| 80 | +``` |
| 81 | +*/ |
9 | 82 | @send external delete: (t<'k, 'v>, 'k) => bool = "delete" |
10 | 83 |
|
11 | 84 | /** |
|
0 commit comments