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

Commit ac9ae74

Browse files
committed
fixed cycle deps
1 parent bee7d78 commit ac9ae74

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3725
-5631
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ yarn-debug.log*
1313
yarn-error.log*
1414
.eslintcache
1515
package-lock.json
16-
yarn.lock
1716

1817
# Editor directories and files
1918
.idea

examples/react/develop/fields/src/App.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export default function App() {
5252
id: FIELDS.size,
5353
name: `Field #${FIELDS.size + 1}`,
5454
});
55-
}}>
55+
}}
56+
>
5657
Add Field
5758
</button>
5859
</div>

examples/react/develop/simple-todo-list/src/App.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const App = () => {
3030
method: 'unshift', // to add todo at the beginning of the Collection
3131
});
3232
setCurrentInput('');
33-
}}>
33+
}}
34+
>
3435
Add
3536
</button>
3637
{todos.map((value) => (
@@ -41,7 +42,8 @@ const App = () => {
4142
onClick={() => {
4243
// Remove Todo at specific primary Key
4344
TODOS.remove(value.id).everywhere();
44-
}}>
45+
}}
46+
>
4547
Remove
4648
</button>
4749
</div>

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"pack:react": "cd packages/react && yarn run prepare && yarn run pack",
4242
"pack:multieditor": "cd packages/multieditor && yarn run prepare && yarn run pack",
4343
"pack:api": "cd packages/api && yarn run prepare && yarn run pack",
44-
"pack:event": "cd packages/event && yarn run prepare && yarn run pack"
44+
"pack:event": "cd packages/event && yarn run prepare && yarn run pack",
45+
"install:clean": "shx rm -rf yarn.lock && shx rm -rf node_modules && yarn install && lerna run install:clean"
4546
},
4647
"repository": {
4748
"type": "git",

packages/core/src/collection/collection.persistent.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from '@agile-ts/utils';
22
import { logCodeManager } from '../logCodeManager';
33
import { Collection, CollectionKey, DefaultItem, ItemKey } from './collection';
4-
import { Group, GroupKey } from './group';
4+
import type { Group, GroupKey } from './group';
55
import {
66
CreatePersistentConfigInterface,
77
getSharedStorageManager,
@@ -137,9 +137,8 @@ export class CollectionPersistent<
137137
// that it was loaded completely.
138138
// After a successful loading assign the now valid Item to the Collection.
139139
else {
140-
const placeholderItem = this.collection().getItemWithReference(
141-
itemKey
142-
);
140+
const placeholderItem =
141+
this.collection().getItemWithReference(itemKey);
143142
placeholderItem?.persist({
144143
key: itemStorageKey,
145144
loadValue: false,
@@ -148,7 +147,8 @@ export class CollectionPersistent<
148147
followCollectionPersistKeyPattern: false, // Because of the dynamic 'storageItemKey', the key is already formatted above
149148
});
150149
if (placeholderItem?.persistent?.ready) {
151-
const loadedPersistedValueIntoItem = await placeholderItem.persistent.loadPersistedValue();
150+
const loadedPersistedValueIntoItem =
151+
await placeholderItem.persistent.loadPersistedValue();
152152

153153
// If successfully loaded Item value, assign Item to Collection
154154
if (loadedPersistedValueIntoItem) {

packages/core/src/collection/collection.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
normalizeArray,
88
} from '@agile-ts/utils';
99
import { logCodeManager } from '../logCodeManager';
10-
import { Agile } from '../agile';
10+
import type { Agile } from '../agile';
1111
import { PatchOptionConfigInterface } from '../state';
12-
import { ComputedTracker } from '../computed';
12+
import { ComputedTracker } from '../computed/computed.tracker'; // Not imported directly from '../computed' due circular dependencies
1313
import { Item } from './item';
1414
import { SelectorConfigInterface, Selector, SelectorKey } from './selector';
1515
import {
@@ -1181,9 +1181,7 @@ export class Collection<DataType extends DefaultItem = DefaultItem> {
11811181
* @public
11821182
* @param itemKeys - Item/s with identifier/s to be removed.
11831183
*/
1184-
public remove(
1185-
itemKeys: ItemKey | Array<ItemKey>
1186-
): {
1184+
public remove(itemKeys: ItemKey | Array<ItemKey>): {
11871185
fromGroups: (groups: Array<ItemKey> | ItemKey) => Collection<DataType>;
11881186
everywhere: (config?: RemoveItemsConfigInterface) => Collection<DataType>;
11891187
} {
@@ -1518,13 +1516,12 @@ export interface CreateCollectionConfigImpl<
15181516
initialData?: Array<DataType>;
15191517
}
15201518

1521-
export type CreateCollectionConfig<
1522-
DataType extends DefaultItem = DefaultItem
1523-
> =
1524-
| CreateCollectionConfigImpl<DataType>
1525-
| ((
1526-
collection: Collection<DataType>
1527-
) => CreateCollectionConfigImpl<DataType>);
1519+
export type CreateCollectionConfig<DataType extends DefaultItem = DefaultItem> =
1520+
1521+
| CreateCollectionConfigImpl<DataType>
1522+
| ((
1523+
collection: Collection<DataType>
1524+
) => CreateCollectionConfigImpl<DataType>);
15281525

15291526
export interface CollectionConfigInterface {
15301527
/**
@@ -1542,8 +1539,9 @@ export interface CollectionConfigInterface {
15421539
defaultGroupKey: ItemKey;
15431540
}
15441541

1545-
export interface CollectConfigInterface<DataType = any>
1546-
extends AssignDataConfigInterface {
1542+
export interface CollectConfigInterface<
1543+
DataType extends DefaultItem = DefaultItem
1544+
> extends AssignDataConfigInterface {
15471545
/**
15481546
* In which way the collected data should be added to the Collection.
15491547
* - 'push' = at the end

packages/core/src/collection/group/group.observer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
Observer,
77
RuntimeJob,
88
} from '../../runtime';
9-
import { Group } from './index';
9+
import type { Group } from './index';
1010
import { DefaultItem } from '../collection';
1111
import { logCodeManager } from '../../logCodeManager';
1212

packages/core/src/collection/group/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import {
77
StateObserver,
88
StateObserversInterface,
99
} from '../../state';
10-
import { Collection, DefaultItem, ItemKey } from '../collection';
10+
import type { Collection, DefaultItem, ItemKey } from '../collection';
1111
import { GroupIngestConfigInterface, GroupObserver } from './group.observer';
12-
import { ComputedTracker } from '../../computed';
13-
import { Item } from '../item';
12+
import { ComputedTracker } from '../../computed/computed.tracker'; // Not imported directly from '../computed' due circular dependencies
13+
import type { Item } from '../item';
1414
import { CollectionPersistent } from '../collection.persistent';
1515

1616
export class Group<
@@ -432,8 +432,10 @@ export class Group<
432432

433433
export type GroupKey = string | number;
434434

435-
export interface GroupObservers<ValueType = any, DataType = any>
436-
extends StateObserversInterface<ValueType> {
435+
export interface GroupObservers<
436+
ValueType = any,
437+
DataType extends DefaultItem = any
438+
> extends StateObserversInterface<ValueType> {
437439
/**
438440
* Observer responsible for the output of the Group.
439441
*/

packages/core/src/collection/item/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
StateKey,
66
StateRuntimeJobConfigInterface,
77
} from '../../state';
8-
import { Collection, DefaultItem } from '../collection';
9-
import { SelectorKey } from '../selector';
8+
import type { Collection, DefaultItem } from '../collection';
9+
import type { SelectorKey } from '../selector';
1010
import { CollectionPersistent } from '../collection.persistent';
1111

1212
export class Item<

packages/core/src/collection/public/createCollection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Collection, CreateCollectionConfig, DefaultItem } from '../collection';
2-
import { Agile } from '../../agile';
2+
import type { Agile } from '../../agile';
33
import { shared } from '../../shared';
44

55
/**

packages/core/src/collection/selector/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from '@agile-ts/utils';
22
import { EnhancedState, StateRuntimeJobConfigInterface } from '../../state';
3-
import { Item } from '../item';
4-
import { Collection, DefaultItem, ItemKey } from '../collection';
3+
import type { Item } from '../item';
4+
import type { Collection, DefaultItem, ItemKey } from '../collection';
55

66
export class Selector<
77
DataType extends DefaultItem = DefaultItem

packages/core/src/computed/computed.tracker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Observer } from '../runtime';
1+
import type { Observer } from '../runtime';
22

33
export class ComputedTracker {
44
static isTracking = false;

packages/core/src/computed/computed.ts

+37-28
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import { defineConfig, isAsyncFunction } from '@agile-ts/utils';
2-
import { Agile } from '../agile';
2+
import type { Agile } from '../agile';
33
import { extractRelevantObservers } from '../utils';
44
import {
55
State,
66
StateConfigInterface,
77
StateIngestConfigInterface,
88
} from '../state';
9-
import { Observer } from '../runtime';
9+
import type { Observer } from '../runtime';
1010
import { ComputedTracker } from './computed.tracker';
11-
import { Collection } from '../collection';
11+
import type { Collection } from '../collection';
12+
import { logCodeManager } from '../logCodeManager';
1213

1314
export class Computed<
1415
ComputedValueType = any
1516
> extends State<ComputedValueType> {
1617
public config: ComputedConfigInterface;
1718

1819
// Caches whether the compute function is async
19-
private computeFunctionIsAsync!: boolean;
20+
private isComuteFunctionAsync!: boolean;
2021

2122
// Function to compute the Computed Class value
2223
private _computeFunction!: ComputeFunctionType<ComputedValueType>;
@@ -54,6 +55,7 @@ export class Computed<
5455
) {
5556
super(
5657
agileInstance,
58+
// Assign inital value (if property set or not async)
5759
Object.prototype.hasOwnProperty.call(config, 'initialValue')
5860
? config.initialValue
5961
: !isAsyncFunction(computeFunction)
@@ -68,7 +70,7 @@ export class Computed<
6870

6971
config = defineConfig(config, {
7072
computedDeps: [],
71-
autodetect: !this.computeFunctionIsAsync,
73+
autodetect: !this.isComuteFunctionAsync,
7274
});
7375
this.agileInstance = () => agileInstance;
7476
this.config = {
@@ -109,14 +111,14 @@ export class Computed<
109111
*/
110112
public set computeFunction(value: ComputeFunctionType<ComputedValueType>) {
111113
this._computeFunction = value;
112-
this.computeFunctionIsAsync = isAsyncFunction(value);
114+
this.isComuteFunctionAsync = isAsyncFunction(value);
113115
}
114116

115117
/**
116118
* Synchronously computes and returns the new value of the Computed Class
117119
* and autodetects used dependencies in the compute function.
118120
*
119-
* @public
121+
* @internal
120122
* @param config - Configuration object
121123
*/
122124
private computeSync(config: ComputeConfigInterface = {}): ComputedValueType {
@@ -160,6 +162,9 @@ export class Computed<
160162

161163
/**
162164
* 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
163168
*/
164169
private async computeAsync(): Promise<ComputedValueType> {
165170
return this.computeFunction();
@@ -175,26 +180,12 @@ export class Computed<
175180
public async compute(
176181
config: ComputeConfigInterface = {}
177182
): 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);
198189
}
199190

200191
/**
@@ -207,7 +198,7 @@ export class Computed<
207198
// https://www.reddit.com/r/learnjavascript/comments/q5rvux/pass_parent_config_object_directly_into_child/
208199
config: StateIngestConfigInterface & ComputeConfigInterface = {}
209200
) {
210-
if (this.computeFunctionIsAsync) {
201+
if (this.isComuteFunctionAsync) {
211202
this.computeAsync().then((result) => {
212203
this.observers['value'].ingestValue(result, config);
213204
});
@@ -217,6 +208,24 @@ export class Computed<
217208
}
218209
}
219210

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+
220229
/**
221230
* Assigns a new function to the Computed Class for computing its value.
222231
*

packages/core/src/computed/public/createComputed.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '../computed';
66
import { defineConfig } from '@agile-ts/utils';
77
import { shared } from '../../shared';
8-
import { CreateComputedConfigInterfaceWithAgile } from './index';
8+
import { CreateComputedConfigInterfaceWithAgile } from './types';
99

1010
/**
1111
* Returns a newly created Computed.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
import { CreateAgileSubInstanceInterface } from '../../shared';
2-
import { CreateComputedConfigInterface } from '../computed';
3-
41
export * from './createComputed';
5-
6-
export interface CreateComputedConfigInterfaceWithAgile
7-
extends CreateAgileSubInstanceInterface,
8-
CreateComputedConfigInterface {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { CreateAgileSubInstanceInterface } from '../../shared';
2+
import { CreateComputedConfigInterface } from '../computed';
3+
4+
export interface CreateComputedConfigInterfaceWithAgile
5+
extends CreateAgileSubInstanceInterface,
6+
CreateComputedConfigInterface {}

packages/core/src/integrations/integration.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Agile } from '../agile';
1+
import type { Agile } from '../agile';
22

33
export class Integration<F = any, C = any> {
44
// Key/Name identifier of the Integration

packages/core/src/integrations/integrations.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from '@agile-ts/utils';
22
import { logCodeManager } from '../logCodeManager';
3-
import { Agile } from '../agile';
3+
import type { Agile } from '../agile';
44
import { Integration } from './integration';
55

66
const onRegisterInitialIntegrationCallbacks: ((

packages/core/src/logCodeManager.ts

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ const logCodeMessages = {
102102
'18:03:00': "Failed to integrate Framework '${0}' into AgileTs '${1}'!",
103103

104104
// Computed
105+
'19:00:01': `Can't autodetect dependencies of async compute function!`,
105106

106107
// Collection Persistent
107108
'1A:02:00': 'Failed to build unique Item StorageKey!',

0 commit comments

Comments
 (0)