1
1
import { defineConfig , isAsyncFunction } from '@agile-ts/utils' ;
2
- import { Agile } from '../agile' ;
2
+ import type { Agile } from '../agile' ;
3
3
import { extractRelevantObservers } from '../utils' ;
4
4
import {
5
5
State ,
6
6
StateConfigInterface ,
7
7
StateIngestConfigInterface ,
8
8
} from '../state' ;
9
- import { Observer } from '../runtime' ;
9
+ import type { Observer } from '../runtime' ;
10
10
import { ComputedTracker } from './computed.tracker' ;
11
- import { Collection } from '../collection' ;
11
+ import type { Collection } from '../collection' ;
12
+ import { logCodeManager } from '../logCodeManager' ;
12
13
13
14
export class Computed <
14
15
ComputedValueType = any
15
16
> extends State < ComputedValueType > {
16
17
public config : ComputedConfigInterface ;
17
18
18
19
// Caches whether the compute function is async
19
- private computeFunctionIsAsync ! : boolean ;
20
+ private isComuteFunctionAsync ! : boolean ;
20
21
21
22
// Function to compute the Computed Class value
22
23
private _computeFunction ! : ComputeFunctionType < ComputedValueType > ;
@@ -54,6 +55,7 @@ export class Computed<
54
55
) {
55
56
super (
56
57
agileInstance ,
58
+ // Assign inital value (if property set or not async)
57
59
Object . prototype . hasOwnProperty . call ( config , 'initialValue' )
58
60
? config . initialValue
59
61
: ! isAsyncFunction ( computeFunction )
@@ -68,7 +70,7 @@ export class Computed<
68
70
69
71
config = defineConfig ( config , {
70
72
computedDeps : [ ] ,
71
- autodetect : ! this . computeFunctionIsAsync ,
73
+ autodetect : ! this . isComuteFunctionAsync ,
72
74
} ) ;
73
75
this . agileInstance = ( ) => agileInstance ;
74
76
this . config = {
@@ -109,14 +111,14 @@ export class Computed<
109
111
*/
110
112
public set computeFunction ( value : ComputeFunctionType < ComputedValueType > ) {
111
113
this . _computeFunction = value ;
112
- this . computeFunctionIsAsync = isAsyncFunction ( value ) ;
114
+ this . isComuteFunctionAsync = isAsyncFunction ( value ) ;
113
115
}
114
116
115
117
/**
116
118
* Synchronously computes and returns the new value of the Computed Class
117
119
* and autodetects used dependencies in the compute function.
118
120
*
119
- * @public
121
+ * @internal
120
122
* @param config - Configuration object
121
123
*/
122
124
private computeSync ( config : ComputeConfigInterface = { } ) : ComputedValueType {
@@ -160,6 +162,9 @@ export class Computed<
160
162
161
163
/**
162
164
* Asynchronously computes and returns the new value of the Computed Class.
165
+ * !! Since its async it can't autodetect used dependencies in the compute function.
166
+ *
167
+ * @internal
163
168
*/
164
169
private async computeAsync ( ) : Promise < ComputedValueType > {
165
170
return this . computeFunction ( ) ;
@@ -175,26 +180,12 @@ export class Computed<
175
180
public async compute (
176
181
config : ComputeConfigInterface = { }
177
182
) : Promise < ComputedValueType > {
178
- if ( this . computeFunctionIsAsync ) return this . computeAsync ( ) ;
179
- else return this . computeSync ( config ) ;
180
- }
181
-
182
- /**
183
- * Forces a recomputation of the cached value with the compute function.
184
- *
185
- * [Learn more..](https://agile-ts.org/docs/core/computed/methods/#recompute)
186
- *
187
- * @public
188
- * @param config - Configuration object
189
- */
190
- public recompute ( config : RecomputeConfigInterface = { } ) : this {
191
- config = defineConfig ( config , {
192
- autodetect : false ,
193
- } ) ;
194
-
195
- this . computeAndIngest ( config ) ;
196
-
197
- return this ;
183
+ if ( config . autodetect && this . isComuteFunctionAsync ) {
184
+ logCodeManager . log ( '19:00:01' ) ;
185
+ }
186
+ return this . isComuteFunctionAsync
187
+ ? this . computeAsync ( )
188
+ : this . computeSync ( config ) ;
198
189
}
199
190
200
191
/**
@@ -207,7 +198,7 @@ export class Computed<
207
198
// https://www.reddit.com/r/learnjavascript/comments/q5rvux/pass_parent_config_object_directly_into_child/
208
199
config : StateIngestConfigInterface & ComputeConfigInterface = { }
209
200
) {
210
- if ( this . computeFunctionIsAsync ) {
201
+ if ( this . isComuteFunctionAsync ) {
211
202
this . computeAsync ( ) . then ( ( result ) => {
212
203
this . observers [ 'value' ] . ingestValue ( result , config ) ;
213
204
} ) ;
@@ -217,6 +208,24 @@ export class Computed<
217
208
}
218
209
}
219
210
211
+ /**
212
+ * Forces a recomputation of the cached value with the compute function.
213
+ *
214
+ * [Learn more..](https://agile-ts.org/docs/core/computed/methods/#recompute)
215
+ *
216
+ * @public
217
+ * @param config - Configuration object
218
+ */
219
+ public recompute ( config : RecomputeConfigInterface = { } ) : this {
220
+ config = defineConfig ( config , {
221
+ autodetect : false ,
222
+ } ) ;
223
+
224
+ this . computeAndIngest ( config ) ;
225
+
226
+ return this ;
227
+ }
228
+
220
229
/**
221
230
* Assigns a new function to the Computed Class for computing its value.
222
231
*
0 commit comments