2021-08-06 08:31:17 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 Hardcore Engineering Inc.
|
2021-12-06 16:57:35 +00:00
|
|
|
//
|
2021-08-06 08:31:17 +00:00
|
|
|
// 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
|
2021-12-06 16:57:35 +00:00
|
|
|
//
|
2021-08-06 08:31:17 +00:00
|
|
|
// 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.
|
2021-12-06 16:57:35 +00:00
|
|
|
//
|
2021-08-06 08:31:17 +00:00
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2021-12-15 09:15:43 +00:00
|
|
|
import core, {
|
2022-04-29 05:27:17 +00:00
|
|
|
AnyAttribute,
|
|
|
|
ArrOf,
|
|
|
|
AttachedDoc,
|
|
|
|
Class,
|
|
|
|
Client,
|
|
|
|
Collection,
|
|
|
|
Doc,
|
|
|
|
DocumentQuery,
|
|
|
|
FindOptions,
|
|
|
|
FindResult,
|
|
|
|
getCurrentAccount,
|
2022-06-08 13:55:01 +00:00
|
|
|
Hierarchy,
|
2022-04-29 05:27:17 +00:00
|
|
|
Ref,
|
|
|
|
RefTo,
|
|
|
|
Tx,
|
|
|
|
TxOperations,
|
|
|
|
TxResult
|
2021-12-15 09:15:43 +00:00
|
|
|
} from '@anticrm/core'
|
2021-10-24 10:14:33 +00:00
|
|
|
import login from '@anticrm/login'
|
2022-01-13 09:06:50 +00:00
|
|
|
import { getMetadata } from '@anticrm/platform'
|
|
|
|
import { LiveQuery as LQ } from '@anticrm/query'
|
|
|
|
import { onDestroy } from 'svelte'
|
2022-04-27 17:01:10 +00:00
|
|
|
import { deepEqual } from 'fast-equals'
|
2021-08-06 08:31:17 +00:00
|
|
|
|
2021-08-06 10:18:50 +00:00
|
|
|
let liveQuery: LQ
|
2022-01-13 09:06:50 +00:00
|
|
|
let client: TxOperations
|
2021-08-07 17:21:52 +00:00
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
const txListeners: Array<(tx: Tx) => void> = []
|
2022-04-19 09:38:31 +00:00
|
|
|
|
|
|
|
export function addTxListener (l: (tx: Tx) => void): void {
|
|
|
|
txListeners.push(l)
|
|
|
|
}
|
|
|
|
|
2021-08-07 17:21:52 +00:00
|
|
|
class UIClient extends TxOperations implements Client {
|
2022-01-13 09:06:50 +00:00
|
|
|
constructor (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
|
|
|
}
|
|
|
|
|
2022-01-13 09:06:50 +00:00
|
|
|
override async tx (tx: Tx): Promise<TxResult> {
|
2021-12-06 16:57:35 +00:00
|
|
|
return await super.tx(tx)
|
|
|
|
}
|
2021-08-07 17:21:52 +00:00
|
|
|
}
|
2021-08-06 10:18:50 +00:00
|
|
|
|
2022-01-13 09:06:50 +00:00
|
|
|
export function getClient (): TxOperations {
|
2021-08-07 17:21:52 +00:00
|
|
|
return client
|
2021-08-06 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 16:57:35 +00:00
|
|
|
export function setClient (_client: Client): void {
|
2021-08-07 17:21:52 +00:00
|
|
|
liveQuery = new LQ(_client)
|
2021-08-08 21:04:39 +00:00
|
|
|
client = new UIClient(_client, liveQuery)
|
|
|
|
_client.notify = (tx: Tx) => {
|
2021-12-15 09:15:43 +00:00
|
|
|
liveQuery.tx(tx).catch((err) => console.log(err))
|
2022-04-19 09:38:31 +00:00
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
txListeners.forEach((it) => it(tx))
|
2021-08-08 21:04:39 +00:00
|
|
|
}
|
2021-08-06 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 11:07:44 +00:00
|
|
|
export class LiveQuery {
|
2022-04-27 17:01:10 +00:00
|
|
|
private oldClass: Ref<Class<Doc>> | undefined
|
|
|
|
private oldQuery: DocumentQuery<Doc> | undefined
|
|
|
|
private oldOptions: FindOptions<Doc> | undefined
|
|
|
|
private oldCallback: ((result: FindResult<any>) => void) | undefined
|
2021-12-23 09:05:50 +00:00
|
|
|
unsubscribe = () => {}
|
2021-08-06 08:31:17 +00:00
|
|
|
|
2022-05-21 17:17:10 +00:00
|
|
|
constructor (dontDestroy: boolean = false) {
|
|
|
|
if (!dontDestroy) {
|
|
|
|
onDestroy(() => {
|
|
|
|
this.unsubscribe()
|
|
|
|
})
|
|
|
|
}
|
2021-08-06 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 09:15:43 +00:00
|
|
|
query<T extends Doc>(
|
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
query: DocumentQuery<T>,
|
2022-04-14 16:55:56 +00:00
|
|
|
callback: (result: FindResult<T>) => void,
|
2021-12-15 09:15:43 +00:00
|
|
|
options?: FindOptions<T>
|
2022-04-27 17:01:10 +00:00
|
|
|
): boolean {
|
|
|
|
if (!this.needUpdate(_class, query, callback, options)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
this.oldCallback = callback
|
|
|
|
this.oldClass = _class
|
|
|
|
this.oldOptions = options
|
|
|
|
this.oldQuery = query
|
2021-08-06 08:31:17 +00:00
|
|
|
this.unsubscribe()
|
2021-12-23 09:05:50 +00:00
|
|
|
const unsub = liveQuery.query(_class, query, callback, options)
|
|
|
|
this.unsubscribe = () => {
|
|
|
|
unsub()
|
|
|
|
this.unsubscribe = () => {}
|
|
|
|
}
|
2022-04-27 17:01:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
private needUpdate<T extends Doc>(
|
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
query: DocumentQuery<T>,
|
|
|
|
callback: (result: FindResult<T>) => void,
|
|
|
|
options?: FindOptions<T>
|
|
|
|
): boolean {
|
|
|
|
if (!deepEqual(_class, this.oldClass)) return true
|
|
|
|
if (!deepEqual(query, this.oldQuery)) return true
|
|
|
|
if (!deepEqual(callback.toString(), this.oldCallback?.toString())) return true
|
|
|
|
if (!deepEqual(options, this.oldOptions)) return true
|
|
|
|
return false
|
2021-08-06 08:31:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 17:17:10 +00:00
|
|
|
export function createQuery (dontDestroy?: boolean): LiveQuery {
|
|
|
|
return new LiveQuery(dontDestroy)
|
2021-12-15 09:15:43 +00:00
|
|
|
}
|
2021-10-24 10:14:33 +00:00
|
|
|
|
2021-12-06 16:57:35 +00:00
|
|
|
export function getFileUrl (file: string): string {
|
2021-10-24 10:14:33 +00:00
|
|
|
const uploadUrl = getMetadata(login.metadata.UploadUrl)
|
|
|
|
const token = getMetadata(login.metadata.LoginToken)
|
2021-12-06 16:57:35 +00:00
|
|
|
const url = `${uploadUrl as string}?file=${file}&token=${token as string}`
|
2021-10-24 10:14:33 +00:00
|
|
|
return url
|
|
|
|
}
|
2021-12-06 15:15:53 +00:00
|
|
|
|
2022-01-12 09:52:22 +00:00
|
|
|
export async function getBlobURL (blob: Blob): Promise<string> {
|
|
|
|
return await new Promise((resolve) => {
|
|
|
|
const reader = new FileReader()
|
|
|
|
|
|
|
|
reader.addEventListener('load', () => resolve(reader.result as string), false)
|
|
|
|
reader.readAsDataURL(blob)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-08 13:55:01 +00:00
|
|
|
export type AttributeCategory = 'attribute' | 'collection' | 'array'
|
2021-12-06 15:15:53 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2022-06-08 13:55:01 +00:00
|
|
|
export function getAttributePresenterClass (
|
|
|
|
hierarchy: Hierarchy,
|
|
|
|
attribute: AnyAttribute
|
|
|
|
): { attrClass: Ref<Class<Doc>>, category: AttributeCategory } {
|
2021-12-06 15:15:53 +00:00
|
|
|
let attrClass = attribute.type._class
|
2022-06-08 13:55:01 +00:00
|
|
|
let category: AttributeCategory = 'attribute'
|
|
|
|
if (hierarchy.isDerived(attrClass, core.class.RefTo)) {
|
2021-12-06 15:15:53 +00:00
|
|
|
attrClass = (attribute.type as RefTo<Doc>).to
|
2022-06-08 13:55:01 +00:00
|
|
|
category = 'attribute'
|
2021-12-06 15:15:53 +00:00
|
|
|
}
|
2022-06-08 13:55:01 +00:00
|
|
|
if (hierarchy.isDerived(attrClass, core.class.Collection)) {
|
2021-12-15 09:15:43 +00:00
|
|
|
attrClass = (attribute.type as Collection<AttachedDoc>).of
|
2022-06-08 13:55:01 +00:00
|
|
|
category = 'collection'
|
2021-12-15 09:15:43 +00:00
|
|
|
}
|
2022-06-08 13:55:01 +00:00
|
|
|
if (hierarchy.isDerived(attrClass, core.class.ArrOf)) {
|
2022-05-13 17:30:06 +00:00
|
|
|
const of = (attribute.type as ArrOf<AttachedDoc>).of
|
|
|
|
attrClass = of._class === core.class.RefTo ? (of as RefTo<Doc>).to : of._class
|
2022-06-08 13:55:01 +00:00
|
|
|
category = 'array'
|
2021-12-20 09:06:31 +00:00
|
|
|
}
|
2022-06-08 13:55:01 +00:00
|
|
|
return { attrClass, category }
|
2021-12-06 16:57:35 +00:00
|
|
|
}
|