Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit 359fd81

Browse files
committed
added initial value support for Computed Class
1 parent 0c2503b commit 359fd81

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

packages/core/src/computed/computed.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,20 @@ export class Computed<ComputedValueType = any> extends State<
5050
constructor(
5151
agileInstance: Agile,
5252
computeFunction: ComputeFunctionType<ComputedValueType>,
53-
config: CreateComputedConfigInterface = {}
53+
config: CreateComputedConfigInterface<ComputedValueType> = {}
5454
) {
55-
super(agileInstance, null as any, {
56-
key: config.key,
57-
dependents: config.dependents,
58-
});
55+
super(
56+
agileInstance,
57+
Object.prototype.hasOwnProperty.call(config, 'initialValue')
58+
? config.initialValue
59+
: !isAsyncFunction(computeFunction)
60+
? computeFunction()
61+
: (null as any),
62+
{
63+
key: config.key,
64+
dependents: config.dependents,
65+
}
66+
);
5967
config = defineConfig(config, {
6068
computedDeps: [],
6169
autodetect: !isAsyncFunction(computeFunction),
@@ -205,7 +213,8 @@ export type ComputeFunctionType<ComputedValueType = any> = () =>
205213
| ComputedValueType
206214
| Promise<ComputedValueType>;
207215

208-
export interface CreateComputedConfigInterface extends StateConfigInterface {
216+
export interface CreateComputedConfigInterface<ComputedValueType = any>
217+
extends StateConfigInterface {
209218
/**
210219
* Hard-coded dependencies the Computed Class should depend on.
211220
* @default []
@@ -221,6 +230,15 @@ export interface CreateComputedConfigInterface extends StateConfigInterface {
221230
* @default true if the compute method isn't asynchronous, otherwise false
222231
*/
223232
autodetect?: boolean;
233+
/**
234+
* Initial value of the Computed
235+
* which is temporarily set until the first computation has been completed.
236+
*
237+
* Note: Only really relevant if an async compute method is used.
238+
*
239+
* @default undefined
240+
*/
241+
initialValue?: ComputedValueType;
224242
}
225243

226244
export interface ComputedConfigInterface {

packages/core/tests/unit/computed/computed.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ describe('Computed Tests', () => {
4545
expect(computed._key).toBeUndefined();
4646
expect(computed.isSet).toBeFalsy();
4747
expect(computed.isPlaceholder).toBeFalsy();
48-
expect(computed.initialStateValue).toBe(null);
49-
expect(computed._value).toBe(null);
50-
expect(computed.previousStateValue).toBe(null);
51-
expect(computed.nextStateValue).toBe(null);
48+
expect(computed.initialStateValue).toBe(computedFunction());
49+
expect(computed._value).toBe(computedFunction());
50+
expect(computed.previousStateValue).toBe(computedFunction());
51+
expect(computed.nextStateValue).toBe(computedFunction());
5252
expect(computed.observers['value']).toBeInstanceOf(StateObserver);
5353
expect(Array.from(computed.observers['value'].dependents)).toStrictEqual(
5454
[]
@@ -73,6 +73,7 @@ describe('Computed Tests', () => {
7373
dependents: [dummyObserver1],
7474
computedDeps: [dummyObserver2, undefined as any, dummyState],
7575
autodetect: false,
76+
initialValue: 'initialValue',
7677
});
7778

7879
expect(computed.computeFunction).toBe(computedFunction);
@@ -109,10 +110,10 @@ describe('Computed Tests', () => {
109110
expect(computed._key).toBe('coolComputed');
110111
expect(computed.isSet).toBeFalsy();
111112
expect(computed.isPlaceholder).toBeFalsy();
112-
expect(computed.initialStateValue).toBe(null);
113-
expect(computed._value).toBe(null);
114-
expect(computed.previousStateValue).toBe(null);
115-
expect(computed.nextStateValue).toBe(null);
113+
expect(computed.initialStateValue).toBe('initialValue');
114+
expect(computed._value).toBe('initialValue');
115+
expect(computed.previousStateValue).toBe('initialValue');
116+
expect(computed.nextStateValue).toBe('initialValue');
116117
expect(computed.observers['value']).toBeInstanceOf(StateObserver);
117118
expect(Array.from(computed.observers['value'].dependents)).toStrictEqual([
118119
dummyObserver1,

0 commit comments

Comments
 (0)