|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1019 bytes
|
| Line | |
|---|
| 1 | // Duplicate some of the utils in `/src/utils` to ensure
|
|---|
| 2 | // we don't end up dragging in larger chunks of the RTK core
|
|---|
| 3 | // into the RTKQ bundle
|
|---|
| 4 |
|
|---|
| 5 | export function getOrInsert<K extends object, V>(
|
|---|
| 6 | map: WeakMap<K, V>,
|
|---|
| 7 | key: K,
|
|---|
| 8 | value: V,
|
|---|
| 9 | ): V
|
|---|
| 10 | export function getOrInsert<K, V>(map: Map<K, V>, key: K, value: V): V
|
|---|
| 11 | export function getOrInsert<K extends object, V>(
|
|---|
| 12 | map: Map<K, V> | WeakMap<K, V>,
|
|---|
| 13 | key: K,
|
|---|
| 14 | value: V,
|
|---|
| 15 | ): V {
|
|---|
| 16 | if (map.has(key)) return map.get(key) as V
|
|---|
| 17 |
|
|---|
| 18 | return map.set(key, value).get(key) as V
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | export function getOrInsertComputed<K extends object, V>(
|
|---|
| 22 | map: WeakMap<K, V>,
|
|---|
| 23 | key: K,
|
|---|
| 24 | compute: (key: K) => V,
|
|---|
| 25 | ): V
|
|---|
| 26 | export function getOrInsertComputed<K, V>(
|
|---|
| 27 | map: Map<K, V>,
|
|---|
| 28 | key: K,
|
|---|
| 29 | compute: (key: K) => V,
|
|---|
| 30 | ): V
|
|---|
| 31 | export function getOrInsertComputed<K extends object, V>(
|
|---|
| 32 | map: Map<K, V> | WeakMap<K, V>,
|
|---|
| 33 | key: K,
|
|---|
| 34 | compute: (key: K) => V,
|
|---|
| 35 | ): V {
|
|---|
| 36 | if (map.has(key)) return map.get(key) as V
|
|---|
| 37 |
|
|---|
| 38 | return map.set(key, compute(key)).get(key) as V
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | export const createNewMap = () => new Map()
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.