Skip to content

Commit

Permalink
Refactor cb-4018 data context api (#2708)
Browse files Browse the repository at this point in the history
* CB-4018 refactor: data context api

* CB-4018 refactor: data context api

* CB-4018 refactor: form parts api

* CB-4018 fix: pr review

* CB-4018 fix: track mobx dependencies in useDataContextLink

* CB-4018 fix: unsubscribe behaviour

* CB-3510 feat: data editing actions based on view (#2710)

* CB-3510 feat: data editing actions based on view

* CB-3510 fix: pr review

* CB-3510 fix: table bottom bar styles

* CB-4018 refactor: respect presentation in data viewer

* CB-4018 fix: remove gap in data viewer toolbar

* CB-4018 fix: isolate contexts in data viewer
  • Loading branch information
Wroud authored Jun 18, 2024
1 parent e779a23 commit 9a471be
Show file tree
Hide file tree
Showing 132 changed files with 1,413 additions and 1,555 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
import { createDataContext } from '@cloudbeaver/core-data-context';
import type { UserInfo } from '@cloudbeaver/core-sdk';

export const DATA_CONTEXT_USER = createDataContext<UserInfo | null>('user-info', () => null);
export const DATA_CONTEXT_USER = createDataContext<UserInfo | null>('user-info');
162 changes: 75 additions & 87 deletions webapp/packages/core-data-context/src/DataContext/DataContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@
*/
import { action, makeObservable, observable } from 'mobx';

import { MetadataMap } from '@cloudbeaver/core-utils';

import type { DataContextGetter } from './DataContextGetter';
import type { DeleteVersionedContextCallback, IDataContext } from './IDataContext';
import type { IDataContext } from './IDataContext';
import type { IDataContextProvider } from './IDataContextProvider';

const NOT_FOUND = Symbol('not found');

export class DataContext implements IDataContext {
readonly map: Map<DataContextGetter<any>, any>;
private readonly versions: MetadataMap<DataContextGetter<any>, number>;
fallback?: IDataContextProvider;
private readonly store: Map<DataContextGetter<unknown>, Map<string, unknown>>;
private fallback?: IDataContextProvider;

constructor(fallback?: IDataContextProvider) {
this.map = new Map();
this.versions = new MetadataMap(() => 0);
this.store = new Map();
this.fallback = fallback;

makeObservable<this, 'map'>(this, {
makeObservable<this, 'store' | 'fallback'>(this, {
set: action,
delete: action,
clear: action,
map: observable.shallow,
deleteForId: action,
store: observable.shallow,
fallback: observable.ref,
});
}
Expand All @@ -37,119 +36,108 @@ export class DataContext implements IDataContext {
}

hasOwn(context: DataContextGetter<any>): boolean {
return this.map.has(context);
return this.store.has(context);
}

has(context: DataContextGetter<any>, nested = true): boolean {
if (this.hasOwn(context)) {
return true;
}

if (nested && this.fallback?.has(context)) {
return true;
}
has(context: DataContextGetter<any>): boolean {
return this.hasOwn(context) || this.fallback?.has(context) || false;
}

return false;
hasOwnValue<T>(context: DataContextGetter<T>, value: T): boolean {
return this.getOwn(context) === value;
}

hasValue<T>(context: DataContextGetter<T>, value: T, nested = true): boolean {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let provider: IDataContextProvider = this;
hasValue<T>(context: DataContextGetter<T>, value: T): boolean {
return this.hasOwnValue(context, value) || this.fallback?.hasOwnValue(context, value) || false;
}

while (true) {
if (provider.getOwn(context) === value) {
return true;
}
find<T>(context: DataContextGetter<T>, predicate: (value: T) => boolean): T | undefined {
const value = this.internalGet(context);

if (provider.fallback && nested) {
provider = provider.fallback;
} else {
return false;
}
if (value !== NOT_FOUND && predicate(value)) {
return value;
}
}

find<T>(context: DataContextGetter<T>, predicate: (value: T) => boolean): T | undefined {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let provider: IDataContextProvider = this;
if (this.fallback) {
return this.fallback.find(context, predicate);
}

while (true) {
if (provider.hasOwn(context)) {
const value = provider.getOwn(context)!;
return undefined;
}

if (predicate(value)) {
return value;
}
}
set<T>(context: DataContextGetter<T>, value: T, id: string): this {
let data = this.store.get(context);

if (provider.fallback) {
provider = provider.fallback;
} else {
return undefined;
}
if (!data) {
data = observable(new Map(), { deep: false });
this.store.set(context, data);
}

data.set(id, value);
return this;
}

set<T>(context: DataContextGetter<T>, value: T): DeleteVersionedContextCallback {
const data = this.getOwn(context);
let version = this.versions.get(context);
delete(context: DataContextGetter<any>, id?: string): this {
if (id) {
const data = this.store.get(context);
data?.delete(id);

if (data === value) {
return this.delete.bind(this, context, version);
if (data?.size) {
return this;
}
}
this.store.delete(context);

version++;
this.map.set(context, value);
this.versions.set(context, version);

return this.delete.bind(this, context, version);
return this;
}

delete(context: DataContextGetter<any>, version?: number): this {
if (version !== this.versions.get(context)) {
return this;
}
deleteForId(id: string): this {
for (const [context, data] of this.store) {
data.delete(id);

this.map.delete(context);
if (data.size === 0) {
this.store.delete(context);
}
}

return this;
}

getOwn<T>(context: DataContextGetter<T>): T | undefined {
return this.map.get(context);
}
const value = this.internalGet(context);

get<T>(context: DataContextGetter<T>): T {
if (!this.hasOwn(context)) {
const defaultValue = context(this);
if (value === NOT_FOUND) {
return undefined;
}

if (defaultValue !== undefined) {
this.set(context, defaultValue);
return defaultValue;
}
return value;
}

if (this.fallback) {
return this.fallback.get(context);
}
get<T>(context: DataContextGetter<T>): T | undefined {
const value = this.internalGet(context);

throw new Error("Context doesn't exists");
if (value === NOT_FOUND && this.fallback) {
return this.fallback.get(context);
}

return this.getOwn(context)!;
}

tryGet<T>(context: DataContextGetter<T>): T | undefined {
if (!this.map.has(context)) {
if (this.fallback) {
return this.fallback.tryGet(context);
}
if (value === NOT_FOUND) {
return undefined;
}

return this.getOwn(context);
return value;
}

clear(): void {
this.map.clear();
this.versions.clear();
this.store.clear();
}

private internalGet<T>(context: DataContextGetter<T>): T | typeof NOT_FOUND {
const data = this.store.get(context);

if (data?.size) {
return [...data.values()][data.size - 1] as T;
}

return NOT_FOUND;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import type { IDataContextProvider } from './IDataContextProvider';

const typescriptTypeLink = Symbol('typescript type link');

export type DataContextGetter<T> = {
(provider: IDataContextProvider): T;
id: string;
name: string;
[typescriptTypeLink]: T;
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import type { IDataContextProvider } from './IDataContextProvider';
export type DeleteVersionedContextCallback = () => void;

export interface IDataContext extends IDataContextProvider {
set: <T>(context: DataContextGetter<T>, value: T) => DeleteVersionedContextCallback;
delete: (context: DataContextGetter<any>, version?: number) => this;
set: <T>(context: DataContextGetter<T>, value: T, id: string) => this;
delete: (context: DataContextGetter<any>, id?: string) => this;
deleteForId: (id: string) => this;
clear: () => void;
setFallBack: (fallback?: IDataContextProvider) => void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import type { DataContextGetter } from './DataContextGetter';

export interface IDataContextProvider {
fallback?: IDataContextProvider;
readonly map: Map<DataContextGetter<any>, any>;
has: (context: DataContextGetter<any>, nested?: boolean) => boolean;
has: (context: DataContextGetter<any>) => boolean;
hasOwn: (context: DataContextGetter<any>) => boolean;
get: <T>(context: DataContextGetter<T>) => T;
get: <T>(context: DataContextGetter<T>) => T | undefined;
getOwn: <T>(context: DataContextGetter<T>) => T | undefined;
find: <T>(context: DataContextGetter<T>, predicate: (item: T) => boolean) => T | undefined;
hasValue: <T>(context: DataContextGetter<T>, value: T, nested?: boolean) => boolean;
tryGet: <T>(context: DataContextGetter<T>) => T | undefined;
hasOwnValue: <T>(context: DataContextGetter<T>, value: T) => boolean;
hasValue: <T>(context: DataContextGetter<T>, value: T) => boolean;
}
Loading

0 comments on commit 9a471be

Please sign in to comment.