55 State ,
66 StateConfigInterface ,
77 StateIngestConfigInterface ,
8- StateObserver ,
98} from '../state' ;
109import { Observer } from '../runtime' ;
1110import { ComputedTracker } from './computed.tracker' ;
@@ -16,19 +15,12 @@ export class Computed<
1615> extends State < ComputedValueType > {
1716 public config : ComputedConfigInterface ;
1817
19- // Caches if the compute function is async
18+ // Caches whether the compute function is async
2019 private computeFunctionIsAsync ! : boolean ;
2120
2221 // Function to compute the Computed Class value
2322 private _computeFunction ! : ComputeFunctionType < ComputedValueType > ;
24- public get computeFunction ( ) : ComputeFunctionType < ComputedValueType > {
25- return this . _computeFunction ;
26- }
27- public set computeFunction ( v : ComputeFunctionType < ComputedValueType > ) {
28- this . _computeFunction = v ;
29- this . computeFunctionIsAsync = isAsyncFunction ( v ) ;
30- }
31-
23+
3224 // All dependencies the Computed Class depends on (including hardCoded and automatically detected dependencies)
3325 public deps : Set < Observer > = new Set ( ) ;
3426 // Only hardCoded dependencies the Computed Class depends on
@@ -100,10 +92,32 @@ export class Computed<
10092 }
10193
10294 /**
103- * synchronously computes the value
104- *
105- * @param config ComputeConfigInterface
106- * @returns
95+ * Returns the compute function of the Computed State
96+ *
97+ * @public
98+ */
99+ public get computeFunction ( ) : ComputeFunctionType < ComputedValueType > {
100+ return this . _computeFunction ;
101+ }
102+
103+ /**
104+ * Assigns a new compute function to the Computed State
105+ * and checks whether it's async.
106+ *
107+ * @public
108+ * @param value - New compute function.
109+ */
110+ public set computeFunction ( value : ComputeFunctionType < ComputedValueType > ) {
111+ this . _computeFunction = value ;
112+ this . computeFunctionIsAsync = isAsyncFunction ( value ) ;
113+ }
114+
115+ /**
116+ * Synchronously computes and returns the new value of the Computed Class
117+ * and autodetects used dependencies in the compute function.
118+ *
119+ * @public
120+ * @param config - Configuration object
107121 */
108122 private computeSync ( config : ComputeConfigInterface = { } ) : ComputedValueType {
109123 config = defineConfig ( config , {
@@ -113,7 +127,8 @@ export class Computed<
113127 // Start auto tracking of Observers on which the computeFunction might depend
114128 if ( config . autodetect ) ComputedTracker . track ( ) ;
115129
116- const computeFunction = this . computeFunction as SyncComputeFunctionType < ComputedValueType > ;
130+ const computeFunction = this
131+ . computeFunction as SyncComputeFunctionType < ComputedValueType > ;
117132 const computedValue = computeFunction ( ) ;
118133
119134 // Handle auto tracked Observers
@@ -144,19 +159,26 @@ export class Computed<
144159 }
145160
146161 /**
147- * asynchronously computes the value
148- *
149- * @param config ComputeConfigInterface
150- * @returns
162+ * Asynchronously computes and returns the new value of the Computed Class.
151163 */
152- private async computeAsync ( config : ComputeConfigInterface = { } ) : Promise < ComputedValueType > {
153- config = defineConfig ( config , {
154- autodetect : this . config . autodetect ,
155- } ) ;
156-
164+ private async computeAsync ( ) : Promise < ComputedValueType > {
157165 return this . computeFunction ( ) ;
158166 }
159167
168+ /**
169+ * Computes and returns the new value of the Computed Class
170+ * and autodetects used dependencies in a synchronous compute function.
171+ *
172+ * @internal
173+ * @param config - Configuration object
174+ */
175+ public async compute (
176+ config : ComputeConfigInterface = { }
177+ ) : Promise < ComputedValueType > {
178+ if ( this . computeFunctionIsAsync ) return this . computeAsync ( ) ;
179+ else return this . computeSync ( config ) ;
180+ }
181+
160182 /**
161183 * Forces a recomputation of the cached value with the compute function.
162184 *
@@ -170,27 +192,28 @@ export class Computed<
170192 autodetect : false ,
171193 } ) ;
172194
173- this . computeAndIngest ( this . observers [ 'value' ] , config , { autodetect : config . autodetect } ) ;
174-
195+ this . computeAndIngest ( config ) ;
196+
175197 return this ;
176198 }
177199
178200 /**
179- * Recomputes value and ingests it into the observer
201+ * Recomputes the value and ingests it into the runtime.
180202 *
181203 * @public
182- * @param observer - StateObserver<ComputedValueType> to ingest value into
183- * @param ingestConfig - Configuration object
204+ * @param config - Configuration object
184205 */
185- public computeAndIngest ( observer : StateObserver < ComputedValueType > , ingestConfig : StateIngestConfigInterface , computeConfig : ComputeConfigInterface = { } ) {
206+ public computeAndIngest (
207+ // https://www.reddit.com/r/learnjavascript/comments/q5rvux/pass_parent_config_object_directly_into_child/
208+ config : StateIngestConfigInterface & ComputeConfigInterface = { }
209+ ) {
186210 if ( this . computeFunctionIsAsync ) {
187- this . computeAsync ( computeConfig ) . then ( ( result ) => {
188- observer . ingestValue ( result , ingestConfig ) ;
211+ this . computeAsync ( ) . then ( ( result ) => {
212+ this . observers [ 'value' ] . ingestValue ( result , config ) ;
189213 } ) ;
190- }
191- else {
192- const result = this . computeSync ( computeConfig ) ;
193- observer . ingestValue ( result , ingestConfig ) ;
214+ } else {
215+ const result = this . computeSync ( config ) ;
216+ this . observers [ 'value' ] . ingestValue ( result , config ) ;
194217 }
195218 }
196219
@@ -244,30 +267,16 @@ export class Computed<
244267
245268 return this ;
246269 }
247-
248- /**
249- * Computes and returns the new value of the Computed Class
250- * and autodetects used dependencies in the compute function.
251- *
252- * @internal
253- * @param config - Configuration object
254- */
255- public async compute (
256- config : ComputeConfigInterface = { }
257- ) : Promise < ComputedValueType > {
258- if ( this . computeFunctionIsAsync ) {
259- return this . computeAsync ( config ) ;
260- }
261- else {
262- return this . computeSync ( config ) ;
263- }
264- }
265270}
266271
267- export type SyncComputeFunctionType < ComputedValueType = any > = ( ) => ComputedValueType ;
268- export type AsyncComputeFunctionType < ComputedValueType = any > = ( ) => Promise < ComputedValueType > ;
272+ export type SyncComputeFunctionType < ComputedValueType = any > =
273+ ( ) => ComputedValueType ;
274+ export type AsyncComputeFunctionType < ComputedValueType = any > =
275+ ( ) => Promise < ComputedValueType > ;
269276
270- export type ComputeFunctionType < ComputedValueType = any > = SyncComputeFunctionType < ComputedValueType > | AsyncComputeFunctionType < ComputedValueType > ;
277+ export type ComputeFunctionType < ComputedValueType = any > =
278+ | SyncComputeFunctionType < ComputedValueType >
279+ | AsyncComputeFunctionType < ComputedValueType > ;
271280
272281export interface CreateComputedConfigInterface < ComputedValueType = any >
273282 extends StateConfigInterface {
0 commit comments