2021-08-06 08:31:17 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
import { onDestroy } from 'svelte'
|
2021-08-06 08:31:17 +00:00
|
|
|
|
2021-10-13 16:46:48 +00:00
|
|
|
import { Doc, Ref, Class, DocumentQuery, FindOptions, Client, Hierarchy, Tx, getCurrentAccount, ModelDb, TxResult } from '@anticrm/core'
|
2021-08-07 17:21:52 +00:00
|
|
|
import { TxOperations } from '@anticrm/core'
|
2021-08-06 10:18:50 +00:00
|
|
|
import { LiveQuery as LQ } from '@anticrm/query'
|
2021-10-24 10:14:33 +00:00
|
|
|
import { getMetadata } from '@anticrm/platform'
|
|
|
|
|
|
|
|
import login from '@anticrm/login'
|
2021-08-06 08:31:17 +00:00
|
|
|
|
2021-08-06 10:18:50 +00:00
|
|
|
let liveQuery: LQ
|
2021-08-07 17:21:52 +00:00
|
|
|
let client: Client & TxOperations
|
|
|
|
|
|
|
|
class UIClient extends TxOperations implements Client {
|
|
|
|
|
2021-08-08 21:04:39 +00:00
|
|
|
constructor (private readonly client: Client, private readonly liveQuery: LQ) {
|
2021-09-10 15:58:02 +00:00
|
|
|
super(client, getCurrentAccount()._id)
|
2021-08-07 17:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getHierarchy (): Hierarchy {
|
|
|
|
return this.client.getHierarchy()
|
2021-08-08 21:04:39 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 09:06:55 +00:00
|
|
|
getModel (): ModelDb {
|
|
|
|
return this.client.getModel()
|
|
|
|
}
|
|
|
|
|
2021-10-13 16:46:48 +00:00
|
|
|
tx(tx: Tx): Promise<TxResult> {
|
2021-10-07 20:38:05 +00:00
|
|
|
// return Promise.all([super.tx(tx), this.liveQuery.tx(tx)]) as unknown as Promise<void>
|
|
|
|
return super.tx(tx)
|
2021-08-08 21:04:39 +00:00
|
|
|
}
|
2021-08-07 17:21:52 +00:00
|
|
|
}
|
2021-08-06 10:18:50 +00:00
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
export function getClient(): Client & TxOperations {
|
|
|
|
return client
|
2021-08-06 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
export function setClient(_client: Client) {
|
|
|
|
liveQuery = new LQ(_client)
|
2021-08-08 21:04:39 +00:00
|
|
|
client = new UIClient(_client, liveQuery)
|
|
|
|
_client.notify = (tx: Tx) => {
|
|
|
|
liveQuery.tx(tx)
|
|
|
|
}
|
2021-08-06 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 11:07:44 +00:00
|
|
|
export class LiveQuery {
|
2021-08-06 08:31:17 +00:00
|
|
|
private unsubscribe = () => {}
|
|
|
|
|
|
|
|
constructor() {
|
2021-09-22 08:41:03 +00:00
|
|
|
onDestroy(() => { console.log('onDestroy query'); this.unsubscribe() })
|
2021-08-06 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
query<T extends Doc>(_class: Ref<Class<T>>, query: DocumentQuery<T>, callback: (result: T[]) => void, options?: FindOptions<T>) {
|
|
|
|
this.unsubscribe()
|
2021-08-06 10:18:50 +00:00
|
|
|
this.unsubscribe = liveQuery.query(_class, query, callback, options)
|
2021-08-06 08:31:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createQuery() { return new LiveQuery() }
|
2021-10-24 10:14:33 +00:00
|
|
|
|
|
|
|
export function getFileUrl(file: string): string {
|
|
|
|
const uploadUrl = getMetadata(login.metadata.UploadUrl)
|
|
|
|
const token = getMetadata(login.metadata.LoginToken)
|
|
|
|
const url = `${uploadUrl}?file=${file}&token=${token}`
|
|
|
|
return url
|
|
|
|
}
|