Skip to content

Commit 705bcca

Browse files
committed
meh
1 parent c949a29 commit 705bcca

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.1.83",
2+
"version": "1.1.84",
33
"devDependencies": {
44
"@types/ws": "^8.5.13",
55
"esbuild": "^0.23.0",

src/memory.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Internal node mostly for {@link WeakIdentityCache}, although has nothing to do with {@link WeakRef}.
3+
*/
14
class TupleNode {
25
private hasValue = false;
36
private value?: any | undefined;
@@ -115,6 +118,29 @@ export class WeakIdentityCache<K extends [...any], V extends WeakKey> {
115118
});
116119
}
117120

121+
/**
122+
* Provides a {@link V} for the given {@link K}.
123+
* This allows you to skip the builder, however, this {@link K} will still be passed to the cleanup function when it is GC'ed.
124+
*
125+
* If this {@link K} already has a value, rejects the update and returns `false`.
126+
* Otherwise, returns `true`.
127+
*/
128+
provide(v: V, ...k: K): boolean {
129+
const wr = this.#root.get(k);
130+
let curr = wr?.deref();
131+
if (curr !== undefined) {
132+
return false;
133+
}
134+
135+
curr = v;
136+
const update = new WeakRef(curr);
137+
this.#root.set(k, update);
138+
this.#reg.register(curr, k, curr);
139+
this.#count++;
140+
141+
return true;
142+
}
143+
118144
/**
119145
* Gets this {@link K} from the cache.
120146
*
@@ -123,15 +149,16 @@ export class WeakIdentityCache<K extends [...any], V extends WeakKey> {
123149
get(...k: K): V {
124150
const wr = this.#root.get(k);
125151
let curr = wr?.deref();
126-
127-
if (curr === undefined) {
128-
curr = this.#build(...k);
129-
const update = new WeakRef(curr);
130-
this.#root.set(k, update);
131-
this.#reg.register(curr, k, curr);
132-
this.#count++;
152+
if (curr !== undefined) {
153+
return curr;
133154
}
134155

156+
curr = this.#build(...k);
157+
const update = new WeakRef(curr);
158+
this.#root.set(k, update);
159+
this.#reg.register(curr, k, curr);
160+
this.#count++;
161+
135162
return curr;
136163
}
137164

@@ -156,8 +183,9 @@ export class WeakIdentityCache<K extends [...any], V extends WeakKey> {
156183

157184
/**
158185
* Deletes this {@link K} from the cache.
186+
* Returns `true` if it existed and was deleted.
159187
*
160-
* This will prevent it from being passed to the cleanup function.
188+
* This will prevent the given {@link K} from being passed to the cleanup function.
161189
*/
162190
delete(...k: K): boolean {
163191
const wr = this.#root.get(k);
@@ -176,7 +204,20 @@ export class WeakIdentityCache<K extends [...any], V extends WeakKey> {
176204
return false;
177205
}
178206

207+
/**
208+
* Gets the current size of actual values.
209+
*
210+
* This is useful for testing but might change as things are GC'ed.
211+
*/
179212
get size() {
180213
return this.#count;
181214
}
182215
}
216+
217+
/**
218+
* Builds a helper which provides shared named symbols.
219+
*/
220+
export function buildScopedSymbolCache(id: string): (s: string) => Symbol {
221+
const cache = new WeakIdentityCache<[string], Symbol>((s) => Symbol(`${id}:${s}`));
222+
return cache.get.bind(cache);
223+
}

src/rope.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ export class Rope<K, T = void> {
684684
/**
685685
* Iterate from after the given ID, to the target ID inclusive (i.e., `(afterId,untilId]`).
686686
*
687-
* If no `untilId` is passed or the IDs are in the wrong order, iterates from after `afterId` until the end of this {@link Rope.}.
687+
* If no `untilId` is passed or the IDs are in the wrong order, iterates from after `afterId` until the end of this {@link Rope}.
688688
*/
689689
*iter(
690690
afterId: K = this._zeroId,

test/memory.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'node:test';
22
import * as assert from 'node:assert';
3-
import { WeakIdentityCache } from '../src/memory.ts';
3+
import { buildScopedSymbolCache, WeakIdentityCache } from '../src/memory.ts';
44
import { checkGC } from './support/node.ts';
55

66
class Blah {}
@@ -66,3 +66,19 @@ test('under', () => {
6666
assert.strictEqual(c.all(2, 'hello').length, 2);
6767
assert.strictEqual(c.all(2, 'hello', 'x').length, 0);
6868
});
69+
70+
test('symbol', async () => {
71+
const s = buildScopedSymbolCache('blah');
72+
73+
let x1 = s('x1');
74+
let x2 = s('x2');
75+
76+
assert.notStrictEqual(x1, x2);
77+
assert.strictEqual(x1, s('x1'));
78+
assert.strictEqual(x2, s('x2'));
79+
80+
await Promise.resolve();
81+
82+
assert.strictEqual(x1, s('x1'));
83+
assert.strictEqual(x2, s('x2'));
84+
});

0 commit comments

Comments
 (0)