Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor cb-4018 data context api #2708

Merged
merged 13 commits into from
Jun 18, 2024
Merged
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case where data was already defined it misses an if statement above where you set new context to the shallow observable store

this means that mobx wont be triggered if value changed in data, right? should we just move this line to the end of the function?

this.store.set(context, data);

Copy link
Member Author

@Wroud Wroud Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, here is all fine, to access data mobx will track both Maps and when we change value all subscribers to that value will be notified

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_get ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't like this prefixes

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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks strange. Is there a way to avoid this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know any way to keep the generic type without using it in the object


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
Loading