remove cruft, fix some type errors

pull/17124/head
Elliott Johnson 2 weeks ago
parent 816ddcafa2
commit 7d44a1d1a3

@ -1,11 +1,11 @@
/** @import { CacheEntry } from '#shared' */
import { BaseCacheObserver } from '../../shared/cache-observer.js'; import { BaseCacheObserver } from '../../shared/cache-observer.js';
import { ObservableCache } from '../../shared/observable-cache.js';
import { tick } from '../runtime.js'; import { tick } from '../runtime.js';
import { get_effect_validation_error_code, render_effect } from './effects.js'; import { get_effect_validation_error_code, render_effect } from './effects.js';
/** @typedef {{ count: number, item: any }} Entry */ /** @typedef {{ count: number, item: any }} Entry */
/** @type {ObservableCache} */ /** @type {Map<string, CacheEntry>} */
const client_cache = new ObservableCache(); const client_cache = new Map();
/** /**
* @template {(...args: any[]) => any} TFn * @template {(...args: any[]) => any} TFn

@ -1,7 +1,6 @@
/** @import { AsyncLocalStorage } from 'node:async_hooks' */ /** @import { AsyncLocalStorage } from 'node:async_hooks' */
/** @import { RenderContext } from '#server' */ /** @import { RenderContext } from '#server' */
import { ObservableCache } from '../shared/observable-cache';
import { deferred } from '../shared/utils'; import { deferred } from '../shared/utils';
/** @type {Promise<void> | null} */ /** @type {Promise<void> | null} */
@ -64,7 +63,7 @@ export async function with_render_context(fn) {
try { try {
sync_context = { sync_context = {
hydratables: new Map(), hydratables: new Map(),
cache: new ObservableCache() cache: new Map()
}; };
if (in_webcontainer()) { if (in_webcontainer()) {
const { promise, resolve } = deferred(); const { promise, resolve } = deferred();
@ -93,6 +92,7 @@ export async function init_render_context() {
} }
function in_webcontainer() { function in_webcontainer() {
// @ts-ignore -- this will fail when we run typecheck because we exclude node types
// eslint-disable-next-line n/prefer-global/process // eslint-disable-next-line n/prefer-global/process
return !!globalThis.process?.versions?.webcontainer; return !!globalThis.process?.versions?.webcontainer;
} }

@ -1,5 +1,4 @@
import type { Stringify, Transport } from '#shared'; import type { CacheEntry, Stringify } from '#shared';
import type { ObservableCache } from '../shared/observable-cache';
import type { Element } from './dev'; import type { Element } from './dev';
import type { Renderer } from './renderer'; import type { Renderer } from './renderer';
@ -24,7 +23,7 @@ export interface RenderContext {
stringify: Stringify<any> | undefined; stringify: Stringify<any> | undefined;
} }
>; >;
cache: ObservableCache; cache: Map<string, CacheEntry>;
} }
export interface SyncRenderOutput { export interface SyncRenderOutput {

@ -1,4 +1,4 @@
/** @import { ObservableCache } from './observable-cache.js' */ /** @import { CacheEntry } from '#shared' */
/** /**
* @template T * @template T
@ -7,7 +7,7 @@ export class BaseCacheObserver {
/** /**
* This is a function so that you can create an ObservableCache instance globally and as long as you don't actually * This is a function so that you can create an ObservableCache instance globally and as long as you don't actually
* use it until you're inside the server render lifecycle you'll be okay * use it until you're inside the server render lifecycle you'll be okay
* @type {() => ObservableCache} * @type {() => Map<string, CacheEntry>}
*/ */
#get_cache; #get_cache;
@ -15,7 +15,7 @@ export class BaseCacheObserver {
#prefix; #prefix;
/** /**
* @param {() => ObservableCache} get_cache * @param {() => Map<string, CacheEntry>} get_cache
* @param {string} [prefix] * @param {string} [prefix]
*/ */
constructor(get_cache, prefix = '') { constructor(get_cache, prefix = '') {
@ -23,42 +23,6 @@ export class BaseCacheObserver {
this.#prefix = prefix; this.#prefix = prefix;
} }
/**
* Register a callback to be called when a new key is inserted
* @param {(key: string, value: T) => void} callback
* @returns {() => void} Function to unregister the callback
*/
onInsert(callback) {
return this.#get_cache().on_insert((key, value) => {
if (!key.startsWith(this.#prefix)) return;
callback(key, value.item);
});
}
/**
* Register a callback to be called when an existing key is updated
* @param {(key: string, value: T, old_value: T) => void} callback
* @returns {() => void} Function to unregister the callback
*/
onUpdate(callback) {
return this.#get_cache().on_update((key, value, old_value) => {
if (!key.startsWith(this.#prefix)) return;
callback(key, value.item, old_value.item);
});
}
/**
* Register a callback to be called when a key is deleted
* @param {(key: string, old_value: T) => void} callback
* @returns {() => void} Function to unregister the callback
*/
onDelete(callback) {
return this.#get_cache().on_delete((key, old_value) => {
if (!key.startsWith(this.#prefix)) return;
callback(key, old_value.item);
});
}
/** @param {string} key */ /** @param {string} key */
get(key) { get(key) {
const entry = this.#get_cache().get(this.#key(key)); const entry = this.#get_cache().get(this.#key(key));
@ -76,7 +40,9 @@ export class BaseCacheObserver {
/** @param {(item: T, key: string, map: ReadonlyMap<string, T>) => void} cb */ /** @param {(item: T, key: string, map: ReadonlyMap<string, T>) => void} cb */
forEach(cb) { forEach(cb) {
this.entries().forEach(([key, entry]) => cb(entry, key, this)); for (const [key, entry] of this.entries()) {
cb(entry, key, this);
}
} }
*entries() { *entries() {

@ -1,88 +0,0 @@
/** @import { CacheEntry } from '#shared' */
/**
* @extends {Map<string, CacheEntry>}
*/
export class ObservableCache extends Map {
/** @type {Set<(key: string, value: CacheEntry) => void>} */
#insert_callbacks = new Set();
/** @type {Set<(key: string, value: CacheEntry, old_value: CacheEntry) => void>} */
#update_callbacks = new Set();
/** @type {Set<(key: string, old_value: CacheEntry) => void>} */
#delete_callbacks = new Set();
/**
* @param {(key: string, value: CacheEntry) => void} callback
* @returns {() => void} Function to unregister the callback
*/
on_insert(callback) {
this.#insert_callbacks.add(callback);
return () => this.#insert_callbacks.delete(callback);
}
/**
* @param {(key: string, value: CacheEntry, old_value: CacheEntry) => void} callback
* @returns {() => void} Function to unregister the callback
*/
on_update(callback) {
this.#update_callbacks.add(callback);
return () => this.#update_callbacks.delete(callback);
}
/**
* @param {(key: string, old_value: CacheEntry) => void} callback
* @returns {() => void} Function to unregister the callback
*/
on_delete(callback) {
this.#delete_callbacks.add(callback);
return () => this.#delete_callbacks.delete(callback);
}
/**
* @param {string} key
* @param {CacheEntry} value
* @returns {this}
*/
set(key, value) {
const had = this.has(key);
if (had) {
const old_value = /** @type {CacheEntry} */ (super.get(key));
super.set(key, value);
for (const callback of this.#update_callbacks) {
callback(key, value, old_value);
}
} else {
super.set(key, value);
for (const callback of this.#insert_callbacks) {
callback(key, value);
}
}
return this;
}
/**
* @param {string} key
* @returns {boolean}
*/
delete(key) {
const old_value = super.get(key);
const deleted = super.delete(key);
if (deleted) {
for (const callback of this.#delete_callbacks) {
callback(key, /** @type {CacheEntry} */ (old_value));
}
}
return deleted;
}
clear() {
for (const [key, value] of this) {
for (const callback of this.#delete_callbacks) {
callback(key, value);
}
}
super.clear();
}
}

@ -2491,22 +2491,7 @@ declare module 'svelte/reactivity' {
} }
class BaseCacheObserver<T> implements ReadonlyMap<string, T> { class BaseCacheObserver<T> implements ReadonlyMap<string, T> {
constructor(get_cache: () => ObservableCache, prefix?: string | undefined); constructor(get_cache: () => Map<string, CacheEntry>, prefix?: string | undefined);
/**
* Register a callback to be called when a new key is inserted
* @returns Function to unregister the callback
*/
onInsert(callback: (key: string, value: T) => void): () => void;
/**
* Register a callback to be called when an existing key is updated
* @returns Function to unregister the callback
*/
onUpdate(callback: (key: string, value: T, old_value: T) => void): () => void;
/**
* Register a callback to be called when a key is deleted
* @returns Function to unregister the callback
*/
onDelete(callback: (key: string, old_value: T) => void): () => void;
get(key: string): any; get(key: string): any;
@ -2520,27 +2505,6 @@ declare module 'svelte/reactivity' {
[Symbol.iterator](): Generator<[string, T], undefined, unknown>; [Symbol.iterator](): Generator<[string, T], undefined, unknown>;
#private; #private;
} }
class ObservableCache extends Map<string, CacheEntry> {
constructor();
constructor(entries?: readonly (readonly [string, CacheEntry])[] | null | undefined);
constructor();
constructor(iterable?: Iterable<readonly [string, CacheEntry]> | null | undefined);
/**
* @returns Function to unregister the callback
*/
on_insert(callback: (key: string, value: CacheEntry) => void): () => void;
/**
* @returns Function to unregister the callback
*/
on_update(callback: (key: string, value: CacheEntry, old_value: CacheEntry) => void): () => void;
/**
* @returns Function to unregister the callback
*/
on_delete(callback: (key: string, old_value: CacheEntry) => void): () => void;
set(key: string, value: CacheEntry): this;
#private;
}
export {}; export {};
} }

Loading…
Cancel
Save