fix: CB-2503 cached resources optimization

This commit is contained in:
Wroud
2022-09-07 16:47:44 +03:00
parent 322baa4197
commit a6cbbc8a47
6 changed files with 46 additions and 33 deletions
@@ -276,6 +276,7 @@ export function useMapResource<
preloaded: computed,
exception: observable.ref,
loading: observable.ref,
resource: observable.ref,
}, {
exceptionObserved: false,
resource,
@@ -6,7 +6,7 @@
* you may not use this file except in compliance with the License.
*/
import { computed, makeObservable } from 'mobx';
import { computed, makeObservable, runInAction } from 'mobx';
import { injectable } from '@cloudbeaver/core-di';
import { EPermission, SessionPermissionsResource, ServerConfigResource } from '@cloudbeaver/core-root';
@@ -72,16 +72,19 @@ export class DBDriverResource extends CachedMapResource<string, DBDriver, Driver
...this.getIncludesMap(driverId, includes),
});
if (all) {
this.resetIncludes();
this.data.clear();
}
if (driverId && !drivers.some(driver => driver.id === driverId)) {
throw new Error('Driver is not found');
}
this.updateDriver(...drivers);
runInAction(() => {
if (all) {
this.resetIncludes();
this.data.clear();
}
this.updateDriver(...drivers);
});
});
return this.data;
+26 -24
View File
@@ -6,10 +6,12 @@
* you may not use this file except in compliance with the License.
*/
import { flat } from '@cloudbeaver/core-utils';
import { Bootstrap } from './Bootstrap';
import { Dependency } from './Dependency';
import type { DIContainer } from './DIContainer';
import type { IServiceCollection, IServiceInjector } from './IApp';
import type { IServiceCollection, IServiceConstructor, IServiceInjector } from './IApp';
import { IDiWrapper, inversifyWrapper } from './inversifyWrapper';
import type { PluginManifest } from './PluginManifest';
@@ -24,6 +26,14 @@ export class App {
this.getServiceCollection().addServiceByClass(App, this);
}
getPlugins(): PluginManifest[] {
return [...this.plugins];
}
getServices(): IServiceConstructor<any>[] {
return flat(this.plugins.map(plugin => plugin.providers));
}
registerChildContainer(container: DIContainer): void {
this.diWrapper.registerChildContainer(container);
}
@@ -42,41 +52,33 @@ export class App {
// first phase register all dependencies
registerServices(): void {
for (const plugin of this.plugins) {
if (plugin.providers.length) {
plugin.providers.forEach(provider => {
// console.log('provider', provider.name);
this.diWrapper.collection.addServiceByClass(provider);
});
}
for (const service of this.getServices()) {
// console.log('provider', provider.name);
this.diWrapper.collection.addServiceByClass(service);
}
}
async initializeServices(): Promise<void> {
for (const plugin of this.plugins) {
for (const service of plugin.providers) {
if (service.prototype instanceof Bootstrap) {
const serviceInstance = this.diWrapper.injector.getServiceByClass<Bootstrap>(service);
for (const service of this.getServices()) {
if (service.prototype instanceof Bootstrap) {
const serviceInstance = this.diWrapper.injector.getServiceByClass<Bootstrap>(service);
if ('register' in serviceInstance) {
await serviceInstance.register();
}
} else if (service.prototype instanceof Dependency) {
this.diWrapper.injector.getServiceByClass<Bootstrap>(service);
if ('register' in serviceInstance) {
await serviceInstance.register();
}
} else if (service.prototype instanceof Dependency) {
this.diWrapper.injector.getServiceByClass<Bootstrap>(service);
}
}
}
async loadServices(): Promise<void> {
for (const plugin of this.plugins) {
for (const service of plugin.providers) {
if (service.prototype instanceof Bootstrap) {
const serviceInstance = this.diWrapper.injector.getServiceByClass<Bootstrap>(service);
for (const service of this.getServices()) {
if (service.prototype instanceof Bootstrap) {
const serviceInstance = this.diWrapper.injector.getServiceByClass<Bootstrap>(service);
if ('load' in serviceInstance) {
await serviceInstance.load();
}
if ('load' in serviceInstance) {
await serviceInstance.load();
}
}
}
+1 -1
View File
@@ -8,7 +8,7 @@
import type { ISyncContextLoader } from '@cloudbeaver/core-executor';
import type { App } from './App';
import { App } from './App';
import type { IServiceConstructor, IServiceInjector } from './IApp';
import { injectable } from './injectable';
@@ -454,6 +454,7 @@ export abstract class CachedMapResource<
if (key === undefined) {
key = ResourceKeyUtils.join(resourceKeyList(this.keys), ...this.loadedKeys.map(key => this.transformParam(key)));
this.loadedKeys = [];
this.resetIncludes();
} else {
if (this.isAlias(key)) {
const index = this.loadedKeys.findIndex(loadedKey => this.isAliasEqual(key!, loadedKey));
@@ -98,7 +98,10 @@ export abstract class CachedResource<
this.spy(this.onDataError, 'onDataError');
}
makeObservable<CachedResource<TData, TParam, TKey, TContext>, 'loader' | 'loadedKeys'>(this, {
makeObservable<
CachedResource<TData, TParam, TKey, TContext>,
'loader' | 'loadedKeys' | 'commitIncludes' | 'resetIncludes' | 'markOutdatedSync'
>(this, {
loadedKeys: observable,
data: observable,
loader: action,
@@ -107,6 +110,9 @@ export abstract class CachedResource<
markDataError: action,
markOutdated: action,
markUpdated: action,
commitIncludes: action,
markOutdatedSync: action,
resetIncludes: action,
});
}