Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .changeset/strong-jars-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@agile-ts/api': patch
'@agile-ts/core': patch
'@agile-ts/event': patch
'@agile-ts/logger': patch
'@agile-ts/multieditor': patch
'@agile-ts/react': patch
'@agile-ts/utils': patch
---

#### :bug: Bug Fix
* `core`
* [#176](https://github.com/agile-ts/agile/pull/176) Fix persisting dynamically added items ([@bennodev19](https://github.com/bennodev19))

#### :nail_care: Polish
* `api`, `core`, `event`, `logger`, `multieditor`, `react`, `utils`
* [#177](https://github.com/agile-ts/agile/pull/177) Optimize default configurations ([@bennodev19](https://github.com/bennodev19))

#### Committers: 1
- BennoDev ([@bennodev19](https://github.com/bennodev19))

20 changes: 10 additions & 10 deletions benchmark/benchmarks/react/counter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ const results: CycleResultInterface[] = [];
// Add Tests to the Benchmark Test Suite
suite
.add('AgileTs', configTest(agilets))
// .add('Hookstate', configTest(hookstate))
// .add('Jotai', configTest(jotai))
// .add('Mobx', configTest(mobx))
// .add('Nano Stores', configTest(nanostores))
// .add('PulseJs', configTest(pulsejs))
// .add('Recoil', configTest(recoil))
// .add('Redux', configTest(redux))
// .add('Redux-Toolkit', configTest(reduxToolkit))
// .add('Valtio', configTest(valtio))
// .add('Zustand', configTest(zustand))
.add('Hookstate', configTest(hookstate))
.add('Jotai', configTest(jotai))
.add('Mobx', configTest(mobx))
.add('Nano Stores', configTest(nanostores))
.add('PulseJs', configTest(pulsejs))
.add('Recoil', configTest(recoil))
.add('Redux', configTest(redux))
.add('Redux-Toolkit', configTest(reduxToolkit))
.add('Valtio', configTest(valtio))
.add('Zustand', configTest(zustand))

// Add Listener
.on('start', function (this: any) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function defineConfig(
config: any,
defaults: any,
overwriteUndefinedProperties?: boolean
): any {
if (overwriteUndefinedProperties === undefined)
overwriteUndefinedProperties = true;

const shallowCopiedConfig = { ...config };

for (const defaultKey in defaults) {
if (
!Object.prototype.hasOwnProperty.call(shallowCopiedConfig, defaultKey) ||
(overwriteUndefinedProperties &&
shallowCopiedConfig[defaultKey] === undefined)
)
shallowCopiedConfig[defaultKey] = defaults[defaultKey];
}

return shallowCopiedConfig;
}
11 changes: 11 additions & 0 deletions benchmark/benchmarks/typescript/defineConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
// Files to run the Benchmark on
import * as referencer from './bench/referencer';
import * as spreader from './bench/spreader';
import * as spreadReferencer from './bench/spreadReferencer';

interface ConfigInterface {
x1?: boolean;
Expand Down Expand Up @@ -63,6 +64,16 @@ suite
x5: 'hans',
});
})
.add('SpreadReferencer', function () {
let config = defaultConfig;
config = spreadReferencer.defineConfig(config, {
x1: false,
x2: 'jeff',
x3: 10,
x4: false,
x5: 'hans',
});
})

// Add Listener
.on('start', function (this: any) {
Expand Down
41 changes: 21 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"watch:event": "cd packages/event && yarn run watch",
"test": "jest --passWithNoTests",
"test:coverage": "jest --coverage",
"test:coverage:pushToCoveralls": "yarn run test:coverage && coveralls < coverage/lcov.info",
"dev:publish": "lerna run build && lerna run dev:publish",
"dev:push": "lerna run build && lerna run dev:push",
"install:packages": "lerna exec yarn install",
Expand All @@ -46,30 +47,30 @@
"url": "git+https://github.com/agile-ts/agile.git"
},
"devDependencies": {
"@changesets/cli": "^2.12.0",
"@size-limit/file": "^4.12.0",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.7",
"@typescript-eslint/eslint-plugin": "^4.12.0",
"@typescript-eslint/parser": "^4.12.0",
"coveralls": "^3.1.0",
"eslint": "^7.17.0",
"@changesets/cli": "^2.16.0",
"@size-limit/file": "^5.0.1",
"@types/jest": "^26.0.24",
"@types/node": "^16.3.2",
"@typescript-eslint/eslint-plugin": "^4.28.3",
"@typescript-eslint/parser": "^4.28.3",
"coveralls": "^3.1.1",
"eslint": "^7.30.0",
"eslint-config-node": "^4.1.0",
"eslint-config-prettier": "^6.11.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-prettier": "^3.4.0",
"jest": "^26.6.3",
"jest-mock-console": "^1.0.1",
"lerna": "^3.22.1",
"jest-mock-console": "^1.1.0",
"lerna": "^4.0.0",
"lerna-changelog": "^1.0.1",
"nodemon": "^2.0.6",
"prettier": "2.1.2",
"size-limit": "^4.12.0",
"ts-jest": "^26.4.4",
"ts-node": "^8.10.2",
"tsc-watch": "^4.1.0",
"tslib": "^2.0.0",
"typescript": "^3.9.7",
"nodemon": "^2.0.12",
"prettier": "^2.3.2",
"size-limit": "^5.0.1",
"ts-jest": "^26.5.6",
"ts-node": "^10.1.0",
"tsc-watch": "^4.4.0",
"tslib": "^2.3.0",
"typescript": "^4.3.5",
"wait-for-expect": "^3.0.2"
},
"workspaces": [
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clone, copy, isValidObject } from '@agile-ts/utils';
import { clone, copy, defineConfig, isValidObject } from '@agile-ts/utils';

export default class API {
public config: ApiConfig;
Expand All @@ -21,7 +21,7 @@ export default class API {
*/
public with(config: ApiConfig = {}): API {
const modifiedApi = clone(this);
modifiedApi.config = { ...this.config, ...config };
modifiedApi.config = defineConfig(config, this.config);
return modifiedApi;
}

Expand Down Expand Up @@ -113,7 +113,7 @@ export default class API {

// Configure request Options
const config = copy(this.config);
config.options = { ...config.options, ...options };
config.options = defineConfig(options, config.options || {});
config.options.method = method;
if (!config.options.headers) config.options.headers = {};

Expand Down
22 changes: 13 additions & 9 deletions packages/core/src/agile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
createCollection,
createComputed,
IntegrationsConfigInterface,
defineConfig,
} from './internal';

export class Agile {
Expand Down Expand Up @@ -72,14 +73,13 @@ export class Agile {
* @param config - Configuration object
*/
constructor(config: CreateAgileConfigInterface = {}) {
config = {
config = defineConfig(config, {
localStorage: false,
waitForMount: true,
bindGlobal: false,
autoIntegrate: true,
bucket: true,
...config,
};
});
this.config = {
waitForMount: config.waitForMount as any,
bucket: config.bucket as any,
Expand Down Expand Up @@ -143,10 +143,12 @@ export class Agile {
initialValue: ValueType,
config: StateConfigInterface = {}
): State<ValueType> {
return createState<ValueType>(initialValue, {
...config,
...{ agileInstance: this },
});
return createState<ValueType>(
initialValue,
defineConfig(config, {
agileInstance: this,
})
);
}

/**
Expand Down Expand Up @@ -226,13 +228,15 @@ export class Agile {
let _config: CreateComputedConfigInterface = {};

if (Array.isArray(configOrDeps)) {
_config = flatMerge(_config, {
_config = defineConfig(_config, {
computedDeps: configOrDeps,
agileInstance: this,
});
} else {
if (configOrDeps)
_config = { ...configOrDeps, ...{ agileInstance: this } };
_config = defineConfig(configOrDeps, {
agileInstance: this,
});
}

return createComputed<ComputedValueType>(computeFunction, _config);
Expand Down
48 changes: 31 additions & 17 deletions packages/core/src/collection/collection.persistent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CollectionKey,
CreatePersistentConfigInterface,
DefaultItem,
defineConfig,
Group,
GroupKey,
ItemKey,
Expand Down Expand Up @@ -36,12 +37,11 @@ export class CollectionPersistent<
super(collection.agileInstance(), {
instantiate: false,
});
config = {
config = defineConfig(config, {
instantiate: true,
storageKeys: [],
defaultStorageKey: null as any,
...config,
};
});
this.collection = () => collection;
this.instantiatePersistent({
key: config.key,
Expand Down Expand Up @@ -128,35 +128,49 @@ export class CollectionPersistent<
_storageItemKey
);

// Persist and therefore load already present Item
// Persist an already present Item and load its value manually to be 100% sure
// that it was loaded completely
if (item != null) {
item.persist(itemStorageKey, {
loadValue: false,
defaultStorageKey: this.config.defaultStorageKey || undefined,
storageKeys: this.storageKeys,
followCollectionPersistKeyPattern: false, // Because of the dynamic 'storageItemKey', the key is already formatted above
});
if (item?.persistent?.ready) await item.persistent.initialLoading();
}
// Persist and therefore load not present Item
// Persist an already present placeholder Item
// or create a new placeholder Item to load the value in
// and load its value manually to be 100% sure
// that it was loaded completely.
// After a successful loading assign the now valid Item to the Collection.
else {
// Create temporary placeholder Item in which the Item value will be loaded
const dummyItem = this.collection().createPlaceholderItem(itemKey);

// Persist dummy Item and load its value manually to be 100% sure
// that it was loaded completely and exists at all
dummyItem?.persist(itemStorageKey, {
const placeholderItem = this.collection().getItemWithReference(
itemKey
);
placeholderItem?.persist(itemStorageKey, {
loadValue: false,
defaultStorageKey: this.config.defaultStorageKey as any,
storageKeys: this.storageKeys,
followCollectionPersistKeyPattern: false, // Because of the dynamic 'storageItemKey', the key is already formatted above
});
if (dummyItem?.persistent?.ready) {
const loadedPersistedValueIntoItem = await dummyItem.persistent.loadPersistedValue(
itemStorageKey
); // TODO FIRST GROUP REBUILD (by assigning loaded value to Item)
if (placeholderItem?.persistent?.ready) {
const loadedPersistedValueIntoItem = await placeholderItem.persistent.loadPersistedValue(); // TODO FIRST GROUP REBUILD (by assigning loaded value to Item)

// If successfully loaded Item value, assign Item to Collection
if (loadedPersistedValueIntoItem)
this.collection().assignItem(dummyItem, { overwrite: false }); // TODO SECOND GROUP REBUILD (by calling rebuildGroupThatInclude() in the assignItem() method)
if (loadedPersistedValueIntoItem) {
this.collection().assignItem(placeholderItem, {
overwrite: true, // Overwrite to overwrite the existing placeholder Item, if one exists
}); // TODO SECOND GROUP REBUILD (by calling rebuildGroupThatInclude() in the assignItem() method)

placeholderItem.isPersisted = true;

// Manually increase Collection size,
// since these Items already exist in the Collection (because of 'getItemWithReference()')
// but were placeholder before the persisted value got loaded
// -> Collection size wasn't increased in 'assignItem()'
this.collection().size += 1;
}
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/collection/group/group.observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Item,
IngestConfigInterface,
CreateRuntimeJobConfigInterface,
defineConfig,
} from '../../internal';

export class GroupObserver<DataType = any> extends Observer {
Expand All @@ -31,7 +32,10 @@ export class GroupObserver<DataType = any> extends Observer {
group: Group<DataType>,
config: CreateObserverConfigInterface = {}
) {
super(group.agileInstance(), { ...config, ...{ value: group._output } });
super(
group.agileInstance(),
defineConfig(config, { value: group._output })
);
this.group = () => group;
this.nextGroupOutput = copy(group._output);
}
Expand Down Expand Up @@ -67,7 +71,7 @@ export class GroupObserver<DataType = any> extends Observer {
config: GroupIngestConfigInterface = {}
): void {
const group = this.group();
config = {
config = defineConfig(config, {
perform: true,
background: false,
sideEffects: {
Expand All @@ -76,8 +80,7 @@ export class GroupObserver<DataType = any> extends Observer {
},
force: false,
maxTriesToUpdate: 3,
...config,
};
});

// Force overwriting the Group value if it is a placeholder.
// After assigning a value to the Group, the Group is supposed to be no placeholder anymore.
Expand Down
Loading