@@ -8,6 +8,13 @@ import type { Cache } from './types';
8
8
9
9
type ResourceFactory = ( ...args : any [ ] ) => ReturnType < typeof resource > ;
10
10
11
+ interface State {
12
+ cache ?: Cache ;
13
+ fn : any ;
14
+ args : any ;
15
+ _ ?: any ;
16
+ }
17
+
11
18
class ResourceInvokerManager {
12
19
capabilities = helperCapabilities ( '3.23' , {
13
20
hasValue : true ,
@@ -16,35 +23,56 @@ class ResourceInvokerManager {
16
23
17
24
constructor ( protected owner : unknown ) { }
18
25
19
- createHelper ( fn : ResourceFactory , args : any ) {
20
- let helper : object ;
21
-
26
+ createHelper ( fn : ResourceFactory , args : any ) : State {
22
27
/**
23
28
* This cache is for args passed to the ResourceInvoker/Factory
24
29
*
25
30
* We want to cache the helper result, and only re-inoke when the args
26
31
* change.
27
32
*/
28
33
let cache = createCache ( ( ) => {
29
- if ( helper === undefined ) {
30
- let resource = fn ( ...args . positional ) as object ;
34
+ let resource = fn ( ...args . positional ) as object ;
31
35
32
- helper = invokeHelper ( cache , resource ) ;
33
- }
34
-
35
- return helper ;
36
+ return invokeHelper ( cache , resource ) ;
36
37
} ) ;
37
38
38
- return { fn, args, cache : getValue ( cache ) } ;
39
+ return { fn, args, cache, _ : getValue ( cache ) } ;
39
40
}
40
41
41
- getValue ( { cache } : { cache : Cache } ) {
42
- return getValue ( cache ) ;
42
+ /**
43
+ * getValue is re-called when args change
44
+ */
45
+ getValue ( { cache } : State ) {
46
+ let resource = getValue ( cache ) ;
47
+
48
+ return getValue ( resource ) ;
43
49
}
44
50
45
51
getDestroyable ( { fn } : { fn : ResourceFactory } ) {
46
52
return fn ;
47
53
}
54
+
55
+ // createHelper(fn: AnyFunction, args: Arguments): State {
56
+ // return { fn, args };
57
+ // }
58
+
59
+ // getValue({ fn, args }: State): unknown {
60
+ // if (Object.keys(args.named).length > 0) {
61
+ // let argsForFn: FnArgs<Arguments> = [...args.positional, args.named];
62
+
63
+ // return fn(...argsForFn);
64
+ // }
65
+
66
+ // return fn(...args.positional);
67
+ // }
68
+
69
+ // getDebugName(fn: AnyFunction): string {
70
+ // if (fn.name) {
71
+ // return `(helper function ${fn.name})`;
72
+ // }
73
+
74
+ // return '(anonymous helper function)';
75
+ // }
48
76
}
49
77
50
78
/**
0 commit comments