From a6cbbc8a472dcbd063c3876c2700d30a1509fc9c Mon Sep 17 00:00:00 2001 From: Wroud Date: Wed, 7 Sep 2022 16:47:44 +0300 Subject: [PATCH] fix: CB-2503 cached resources optimization --- .../core-blocks/src/useMapResource.ts | 1 + .../core-connections/src/DBDriverResource.ts | 17 ++++--- webapp/packages/core-di/src/App.ts | 50 ++++++++++--------- webapp/packages/core-di/src/DIService.ts | 2 +- .../src/Resource/CachedMapResource.ts | 1 + .../core-sdk/src/Resource/CachedResource.ts | 8 ++- 6 files changed, 46 insertions(+), 33 deletions(-) diff --git a/webapp/packages/core-blocks/src/useMapResource.ts b/webapp/packages/core-blocks/src/useMapResource.ts index 2a3c55b40d..ae9f4cfd7d 100644 --- a/webapp/packages/core-blocks/src/useMapResource.ts +++ b/webapp/packages/core-blocks/src/useMapResource.ts @@ -276,6 +276,7 @@ export function useMapResource< preloaded: computed, exception: observable.ref, loading: observable.ref, + resource: observable.ref, }, { exceptionObserved: false, resource, diff --git a/webapp/packages/core-connections/src/DBDriverResource.ts b/webapp/packages/core-connections/src/DBDriverResource.ts index a853b9e832..d8a2c5ab1e 100644 --- a/webapp/packages/core-connections/src/DBDriverResource.ts +++ b/webapp/packages/core-connections/src/DBDriverResource.ts @@ -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 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; diff --git a/webapp/packages/core-di/src/App.ts b/webapp/packages/core-di/src/App.ts index 2c3edc1b5c..88feb6db4a 100644 --- a/webapp/packages/core-di/src/App.ts +++ b/webapp/packages/core-di/src/App.ts @@ -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[] { + 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 { - for (const plugin of this.plugins) { - for (const service of plugin.providers) { - if (service.prototype instanceof Bootstrap) { - const serviceInstance = this.diWrapper.injector.getServiceByClass(service); + for (const service of this.getServices()) { + if (service.prototype instanceof Bootstrap) { + const serviceInstance = this.diWrapper.injector.getServiceByClass(service); - if ('register' in serviceInstance) { - await serviceInstance.register(); - } - } else if (service.prototype instanceof Dependency) { - this.diWrapper.injector.getServiceByClass(service); + if ('register' in serviceInstance) { + await serviceInstance.register(); } + } else if (service.prototype instanceof Dependency) { + this.diWrapper.injector.getServiceByClass(service); } } } async loadServices(): Promise { - for (const plugin of this.plugins) { - for (const service of plugin.providers) { - if (service.prototype instanceof Bootstrap) { - const serviceInstance = this.diWrapper.injector.getServiceByClass(service); + for (const service of this.getServices()) { + if (service.prototype instanceof Bootstrap) { + const serviceInstance = this.diWrapper.injector.getServiceByClass(service); - if ('load' in serviceInstance) { - await serviceInstance.load(); - } + if ('load' in serviceInstance) { + await serviceInstance.load(); } } } diff --git a/webapp/packages/core-di/src/DIService.ts b/webapp/packages/core-di/src/DIService.ts index d9e12f8672..ee64426027 100644 --- a/webapp/packages/core-di/src/DIService.ts +++ b/webapp/packages/core-di/src/DIService.ts @@ -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'; diff --git a/webapp/packages/core-sdk/src/Resource/CachedMapResource.ts b/webapp/packages/core-sdk/src/Resource/CachedMapResource.ts index 4a38af4b34..c43a0bfe68 100644 --- a/webapp/packages/core-sdk/src/Resource/CachedMapResource.ts +++ b/webapp/packages/core-sdk/src/Resource/CachedMapResource.ts @@ -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)); diff --git a/webapp/packages/core-sdk/src/Resource/CachedResource.ts b/webapp/packages/core-sdk/src/Resource/CachedResource.ts index 80fe862dab..4e751b2682 100644 --- a/webapp/packages/core-sdk/src/Resource/CachedResource.ts +++ b/webapp/packages/core-sdk/src/Resource/CachedResource.ts @@ -98,7 +98,10 @@ export abstract class CachedResource< this.spy(this.onDataError, 'onDataError'); } - makeObservable, 'loader' | 'loadedKeys'>(this, { + makeObservable< + CachedResource, + '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, }); }