1+ import { set_hydratable_key } from '../context.js' ;
12import { tick } from '../runtime.js' ;
23import { render_effect } from './effects.js' ;
34
@@ -6,52 +7,49 @@ import { render_effect } from './effects.js';
67const client_cache = new Map ( ) ;
78
89/**
9- * @template TReturn
10- * @template {unknown} TArg
11- * @param {string } name
12- * @param {(arg: TArg, key: string) => TReturn } fn
13- * @param {{ hash?: (arg: TArg) => string } } [options]
14- * @returns {(arg: TArg) => TReturn }
10+ * @template {(...args: any[]) => any} TFn
11+ * @param {string } key
12+ * @param {TFn } fn
13+ * @returns {ReturnType<TFn> }
1514 */
16- export function cache ( name , fn , { hash = default_hash } = { } ) {
17- return ( arg ) => {
18- const key = `${ name } ::::${ hash ( arg ) } ` ;
19- const cached = client_cache . has ( key ) ;
20- const entry = client_cache . get ( key ) ;
21- const maybe_remove = create_remover ( key ) ;
22-
23- let tracking = true ;
24- try {
25- render_effect ( ( ) => {
26- if ( entry ) entry . count ++ ;
27- return ( ) => {
28- const entry = client_cache . get ( key ) ;
29- if ( ! entry ) return ;
30- entry . count -- ;
31- maybe_remove ( entry ) ;
32- } ;
33- } ) ;
34- } catch {
35- tracking = false ;
36- }
15+ export function cache ( key , fn ) {
16+ const cached = client_cache . has ( key ) ;
17+ const entry = client_cache . get ( key ) ;
18+ const maybe_remove = create_remover ( key ) ;
19+
20+ let tracking = true ;
21+ try {
22+ render_effect ( ( ) => {
23+ if ( entry ) entry . count ++ ;
24+ return ( ) => {
25+ const entry = client_cache . get ( key ) ;
26+ if ( ! entry ) return ;
27+ entry . count -- ;
28+ maybe_remove ( entry ) ;
29+ } ;
30+ } ) ;
31+ } catch {
32+ tracking = false ;
33+ }
3734
38- if ( cached ) {
39- return entry ?. item ;
40- }
35+ if ( cached ) {
36+ return entry ?. item ;
37+ }
4138
42- const item = fn ( arg , key ) ;
43- const new_entry = {
44- item,
45- count : tracking ? 1 : 0
46- } ;
47- client_cache . set ( key , new_entry ) ;
48-
49- Promise . resolve ( item ) . then (
50- ( ) => maybe_remove ( new_entry ) ,
51- ( ) => maybe_remove ( new_entry )
52- ) ;
53- return item ;
39+ set_hydratable_key ( key ) ;
40+ const item = fn ( ) ;
41+ set_hydratable_key ( null ) ;
42+ const new_entry = {
43+ item,
44+ count : tracking ? 1 : 0
5445 } ;
46+ client_cache . set ( key , new_entry ) ;
47+
48+ Promise . resolve ( item ) . then (
49+ ( ) => maybe_remove ( new_entry ) ,
50+ ( ) => maybe_remove ( new_entry )
51+ ) ;
52+ return item ;
5553}
5654
5755/**
@@ -124,11 +122,3 @@ const readonly_cache = new ReadonlyCache();
124122export function get_cache ( ) {
125123 return readonly_cache ;
126124}
127-
128- /**
129- * @param {...any } args
130- * @returns
131- */
132- function default_hash ( ...args ) {
133- return JSON . stringify ( args ) ;
134- }
0 commit comments