-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(daemon): Retain multiple formula numbers per value (merge #2095
) Replaces the `formulaIdentifierForRef` WeakMap with a WeakMap-backed multimap, enabling the maintenance of one-to-many relationships between values and formula identifiers.
- Loading branch information
Showing
4 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @returns {import('./types.js').WeakMultimap<WeakKey, any>} | ||
*/ | ||
export const makeWeakMultimap = () => { | ||
/** @type {WeakMap<WeakKey, Set<unknown>>} */ | ||
const map = new WeakMap(); | ||
return { | ||
add: (ref, formulaIdentifier) => { | ||
let set = map.get(ref); | ||
if (set === undefined) { | ||
set = new Set(); | ||
map.set(ref, set); | ||
} | ||
set.add(formulaIdentifier); | ||
}, | ||
|
||
delete: (ref, formulaIdentifier) => { | ||
const set = map.get(ref); | ||
if (set !== undefined) { | ||
const result = set.delete(formulaIdentifier); | ||
if (set.size === 0) { | ||
map.delete(ref); | ||
} | ||
return result; | ||
} | ||
return false; | ||
}, | ||
|
||
deleteAll: ref => map.delete(ref), | ||
|
||
get: ref => map.get(ref)?.keys().next().value, | ||
|
||
getAll: ref => Array.from(map.get(ref) ?? []), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import test from 'ava'; | ||
import { makeWeakMultimap } from '../src/weak-multimap.js'; | ||
|
||
test('add and get', t => { | ||
const multimap = makeWeakMultimap(); | ||
const ref = {}; | ||
const value = 'foo'; | ||
|
||
multimap.add(ref, value); | ||
t.is(multimap.get(ref), value); | ||
|
||
// Adding a value for a key should be idempotent. | ||
multimap.add(ref, value); | ||
t.is(multimap.get(ref), value); | ||
}); | ||
|
||
test('add and get with multiple refs', t => { | ||
const multimap = makeWeakMultimap(); | ||
const ref1 = {}; | ||
const ref2 = {}; | ||
const value1 = 'foo'; | ||
const value2 = 'bar'; | ||
|
||
multimap.add(ref1, value1); | ||
multimap.add(ref1, value2); | ||
multimap.add(ref2, value1); | ||
|
||
t.is(multimap.get(ref1), value1); | ||
t.deepEqual(multimap.getAll(ref1), [value1, value2]); | ||
t.is(multimap.get(ref2), value1); | ||
t.deepEqual(multimap.getAll(ref2), [value1]); | ||
}); | ||
|
||
test('getAll', t => { | ||
const multimap = makeWeakMultimap(); | ||
const ref = {}; | ||
const value1 = 'foo'; | ||
const value2 = 'bar'; | ||
|
||
multimap.add(ref, value1); | ||
multimap.add(ref, value2); | ||
t.deepEqual(multimap.getAll(ref), [value1, value2]); | ||
|
||
// Adding a value for a key should be idempotent. | ||
multimap.add(ref, value1); | ||
multimap.add(ref, value2); | ||
t.deepEqual(multimap.getAll(ref), [value1, value2]); | ||
}); | ||
|
||
test('delete', t => { | ||
const multimap = makeWeakMultimap(); | ||
const ref = {}; | ||
const value = 'foo'; | ||
|
||
multimap.add(ref, value); | ||
|
||
t.is(multimap.get(ref), value); | ||
t.is(multimap.delete(ref, value), true); | ||
t.is(multimap.get(ref), undefined); | ||
|
||
// Deleting should be idempotent. | ||
t.is(multimap.delete(ref, value), false); | ||
t.is(multimap.get(ref), undefined); | ||
}); | ||
|
||
test('deleteAll', t => { | ||
const multimap = makeWeakMultimap(); | ||
const ref = {}; | ||
const value1 = 'foo'; | ||
const value2 = 'bar'; | ||
|
||
multimap.add(ref, value1); | ||
multimap.add(ref, value2); | ||
|
||
t.deepEqual(multimap.getAll(ref), [value1, value2]); | ||
t.is(multimap.deleteAll(ref), true); | ||
t.is(multimap.get(ref), undefined); | ||
|
||
// Deleting should be idempotent. | ||
t.is(multimap.deleteAll(ref), false); | ||
t.is(multimap.get(ref), undefined); | ||
}); |