-
Notifications
You must be signed in to change notification settings - Fork 396
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
Changes from 7 commits
37af6d9
841af0d
25a745d
e8945b8
6bdbc57
cb6c651
e666d91
81227ed
8a39806
0a23993
ebd4c02
64f89a0
afb44cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
}); | ||
} | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _get ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks strange. Is there a way to avoid this? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
There was a problem hiding this comment.
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 observablestore
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);
There was a problem hiding this comment.
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