fix client

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov 2021-08-07 19:21:52 +02:00
parent f67908b5b4
commit 54eca771d2
No known key found for this signature in database
GPG Key ID: C8787EFEB4B64AF0
3 changed files with 37 additions and 35 deletions

View File

@ -15,7 +15,6 @@
import type { Doc, Ref, Class } from './classes'
import type { Tx } from './tx'
import { TxOperations } from './tx'
import type { Storage, DocumentQuery, FindOptions, FindResult } from './storage'
import { Hierarchy } from './hierarchy'
@ -32,14 +31,16 @@ export type TxHander = (tx: Tx) => void
/**
* @public
*/
export interface Client extends TxOperations {
export interface Client extends Storage {
getHierarchy: () => Hierarchy
}
class ClientStorage implements Storage {
class ClientImpl implements Storage {
constructor (private readonly hierarchy: Hierarchy, private readonly model: ModelDb, private readonly conn: Storage) {
}
getHierarchy (): Hierarchy { return this.hierarchy }
async findAll<T extends Doc>(
_class: Ref<Class<T>>,
query: DocumentQuery<T>,
@ -57,14 +58,6 @@ class ClientStorage implements Storage {
}
}
class ClientImpl extends TxOperations {
constructor (private readonly hierarchy: Hierarchy, model: ModelDb, conn: Storage) {
super(new ClientStorage(hierarchy, model, conn), core.account.System)
}
getHierarchy (): Hierarchy { return this.hierarchy }
}
/**
* @public
*/

View File

@ -14,22 +14,34 @@
// limitations under the License.
//
import { getContext, setContext, onDestroy } from 'svelte'
import { onDestroy } from 'svelte'
import type { Doc, Ref, Class, DocumentQuery, FindOptions, Client } from '@anticrm/core'
import { Doc, Ref, Class, DocumentQuery, FindOptions, Client, Hierarchy } from '@anticrm/core'
import { TxOperations } from '@anticrm/core'
import { LiveQuery as LQ } from '@anticrm/query'
const CLIENT_CONEXT = 'workbench.context.Client'
import core from '@anticrm/core'
let liveQuery: LQ
let client: Client & TxOperations
export function getClient(): Client {
return getContext<Client>(CLIENT_CONEXT)
class UIClient extends TxOperations implements Client {
constructor (private readonly client: Client) {
super(client, core.account.System)
}
getHierarchy (): Hierarchy {
return this.client.getHierarchy()
}
}
export function setClient(client: Client) {
setContext(CLIENT_CONEXT, client)
liveQuery = new LQ(client)
export function getClient(): Client & TxOperations {
return client
}
export function setClient(_client: Client) {
liveQuery = new LQ(_client)
client = new UIClient(liveQuery)
}
class LiveQuery {

View File

@ -16,13 +16,10 @@
import {
Ref, Class, Doc, Tx, DocumentQuery, TxCreateDoc, TxRemoveDoc, Client,
FindOptions, TxUpdateDoc, _getOperator, TxProcessor, resultSort, SortingQuery,
FindResult, Hierarchy, Refs, WithLookup, LookupData, Storage
FindResult, Hierarchy, Refs, WithLookup, LookupData
} from '@anticrm/core'
/**
* @internal
*/
export interface _Query {
interface Query {
_class: Ref<Class<Doc>>
query: DocumentQuery<Doc>
result: Doc[] | Promise<Doc[]>
@ -33,9 +30,9 @@ export interface _Query {
/**
* @public
*/
export class LiveQuery extends TxProcessor implements Storage {
export class LiveQuery extends TxProcessor implements Client {
private readonly client: Client
private readonly queries: _Query[] = []
private readonly queries: Query[] = []
constructor (client: Client) {
super()
@ -46,7 +43,7 @@ export class LiveQuery extends TxProcessor implements Storage {
return this.client.getHierarchy()
}
private match (q: _Query, doc: Doc): boolean {
private match (q: Query, doc: Doc): boolean {
if (!this.getHierarchy().isDerived(doc._class, q._class)) {
return false
}
@ -65,7 +62,7 @@ export class LiveQuery extends TxProcessor implements Storage {
query<T extends Doc>(_class: Ref<Class<T>>, query: DocumentQuery<T>, callback: (result: T[]) => void, options?: FindOptions<T>): () => void {
const result = this.client.findAll(_class, query, options)
const q: _Query = {
const q: Query = {
_class,
query,
result,
@ -86,7 +83,7 @@ export class LiveQuery extends TxProcessor implements Storage {
}
}
async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {
protected async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {
for (const q of this.queries) {
if (q.result instanceof Promise) {
q.result = await q.result
@ -110,7 +107,7 @@ export class LiveQuery extends TxProcessor implements Storage {
(doc as WithLookup<Doc>).$lookup = result
}
async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
protected async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
for (const q of this.queries) {
const doc = TxProcessor.createDoc2Doc(tx)
if (this.match(q, doc)) {
@ -135,7 +132,7 @@ export class LiveQuery extends TxProcessor implements Storage {
}
}
async txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {
protected async txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {
for (const q of this.queries) {
if (q.result instanceof Promise) {
q.result = await q.result
@ -153,7 +150,7 @@ export class LiveQuery extends TxProcessor implements Storage {
await super.tx(tx)
}
async __updateDoc (updatedDoc: Doc, tx: TxUpdateDoc<Doc>): Promise<void> {
private async __updateDoc (updatedDoc: Doc, tx: TxUpdateDoc<Doc>): Promise<void> {
const ops = tx.operations as any
for (const key in ops) {
if (key.startsWith('$')) {
@ -167,7 +164,7 @@ export class LiveQuery extends TxProcessor implements Storage {
updatedDoc.modifiedOn = tx.modifiedOn
}
private sort (q: _Query, tx: TxUpdateDoc<Doc>): void {
private sort (q: Query, tx: TxUpdateDoc<Doc>): void {
const sort = q.options?.sort
if (sort === undefined) return
let needSort = sort.modifiedBy !== undefined || sort.modifiedOn !== undefined
@ -190,7 +187,7 @@ export class LiveQuery extends TxProcessor implements Storage {
return false
}
private async callback (updatedDoc: Doc, q: _Query): Promise<void> {
private async callback (updatedDoc: Doc, q: Query): Promise<void> {
q.result = q.result as Doc[]
if (q.options?.limit !== undefined && q.result.length > q.options.limit) {