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

Commit bee7d78

Browse files
committed
minor code style changes
1 parent 9acb5b4 commit bee7d78

File tree

4 files changed

+84
-76
lines changed

4 files changed

+84
-76
lines changed
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: 🐛 Bug report
3-
about: Create a report to help us improve AgileTs
3+
about: Create a bug report to help us improve AgileTs
44
title: ''
55
labels: 'Type: Bug'
66
assignees: ''
@@ -11,35 +11,36 @@ assignees: ''
1111

1212
### 🤖 Current Behavior
1313

14-
<!-- Explain your problem (with screenshots, videos, text) in detail -->
14+
<!-- Explain your problem (e.g. with screenshots, text, code snippets) in detail -->
1515

1616
### 🎯 Expected behavior
1717

1818
<!-- A clear and concise description of what you expected to happen. -->
1919

2020
### 📄 Reproducible example
2121

22-
<!-- Create a simple example in Codebox -->
22+
<!-- Create a simple example that reproduces your problem in a code sandbox
23+
(like this one: https://codesandbox.io/s/issue-219-ufcck?file=/src/main.js) -->
2324

2425
### 💡 Suggested solution(s)
2526

26-
<!-- How could we solve this bug? What changes would need to made to AgileTs? -->
27+
<!-- How could we solve this bug? What changes would need to made to AgileTs? Any idea? -->
2728

2829
### ➕ Additional notes
2930

30-
<!-- Add any other context about the problem here. -->
31+
<!-- Add additional context about the problem here. (optional) -->
3132

3233
### 💻 Your environment
3334

34-
<!-- PLEASE FILL THIS OUT -->
35+
<!-- What version/s of AgileTs are you using? -->
3536

3637
| Software | Version(s) |
3738
| ----------------------| ---------- |
38-
| TypeScript |
39-
| npm/Yarn |
40-
| NodeJs |
41-
| @agile-ts/core |
42-
| @agile-ts/react |
43-
| @agile-ts/api |
44-
| @agile-ts/multieditor |
45-
<!-- Any additional important Version Notes? -->
39+
| @agile-ts/core | N/A
40+
| @agile-ts/react | N/A
41+
| @agile-ts/api | N/A
42+
| @agile-ts/multieditor | N/A
43+
| TypeScript | N/A
44+
| npm/Yarn | N/A
45+
| NodeJs | N/A
46+
<!-- Any additional important version notes? -->

packages/core/src/computed/computed.ts

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
State,
66
StateConfigInterface,
77
StateIngestConfigInterface,
8-
StateObserver,
98
} from '../state';
109
import { Observer } from '../runtime';
1110
import { 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

272281
export interface CreateComputedConfigInterface<ComputedValueType = any>
273282
extends StateConfigInterface {

packages/core/src/state/state.enhanced.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
CreateStatePersistentConfigInterface,
1616
StatePersistent,
1717
} from './state.persistent';
18-
import { PersistentKey, StorageKey } from '../storages';
1918

2019
export class EnhancedState<ValueType = any> extends State<ValueType> {
2120
// Whether the State is persisted in an external Storage

packages/core/src/state/state.observer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ export class StateObserver<ValueType = any> extends Observer<ValueType> {
6363
* @param config - Configuration object
6464
*/
6565
public ingest(config: StateIngestConfigInterface = {}): void {
66-
const state = this.state() as any;
66+
const state = this.state();
6767

68-
if (state.isComputed) {
69-
const computedState = state as Computed;
70-
computedState.computeAndIngest(this, config);
68+
if (state instanceof Computed && state.isComputed) {
69+
state.computeAndIngest(config);
7170
} else {
7271
this.ingestValue(state.nextStateValue, config);
7372
}

0 commit comments

Comments
 (0)