Skip to content

Commit 105cedd

Browse files
authored
Merge pull request #21081 from emberjs/nvp/fix-ts-nightly
2 parents 7b7bb60 + 05bb41e commit 105cedd

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

packages/@glimmer/validator/lib/collections/map.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ class TrackedMap<K = unknown, V = unknown> implements Map<K, V> {
5757
return this.#vals.entries();
5858
}
5959

60+
getOrInsert(key: K, defaultValue: V): V {
61+
consumeTag(this.#storageFor(key));
62+
63+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
64+
// @ts-ignore -- older versions of TS don't yet have this method
65+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
66+
return this.#vals.getOrInsert(key, defaultValue);
67+
}
68+
69+
getOrInsertComputed(key: K, creator: (key: K) => V): V {
70+
consumeTag(this.#storageFor(key));
71+
72+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
73+
// @ts-ignore -- older versions of TS don't yet have this method
74+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
75+
return this.#vals.getOrInsertComputed(key, creator);
76+
}
77+
6078
keys() {
6179
consumeTag(this.#collection);
6280

packages/@glimmer/validator/test/collections/map-test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,46 @@ module('@glimmer/validator: trackedMap', function () {
4949
assert.strictEqual(map.get('foo'), 456);
5050
});
5151

52+
test('getOrInsert', (assert) => {
53+
const map = trackedMap();
54+
55+
if (
56+
'getOrInsert' in map &&
57+
typeof map.getOrInsert === 'function' &&
58+
'getOrInsert' in Map.prototype
59+
) {
60+
let v = map.getOrInsert('foo', 123);
61+
assert.strictEqual(v, 123);
62+
63+
assert.strictEqual(map.get('foo'), 123);
64+
65+
// eslint-disable-next-line qunit/no-early-return
66+
return;
67+
}
68+
69+
assert.ok('This browser has not implemented getOrInsert');
70+
});
71+
72+
test('getOrInsertComputed', (assert) => {
73+
const map = trackedMap();
74+
75+
if (
76+
'getOrInsertComputed' in map &&
77+
typeof map.getOrInsertComputed === 'function' &&
78+
'getOrInsertComputed' in Map.prototype
79+
) {
80+
let v = map.getOrInsertComputed('foo', (key: string) => `${key}!`);
81+
assert.strictEqual(v, 'foo!');
82+
83+
assert.strictEqual(map.get('foo'), 'foo!');
84+
85+
// eslint-disable-next-line qunit/no-early-return
86+
return;
87+
}
88+
89+
assert.ok('This browser has not implemented getOrInsert');
90+
});
91+
5292
test('has', (assert) => {
5393
const map = trackedMap();
5494

0 commit comments

Comments
 (0)