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

UBERF-9032: Fix proper query initializers #7563

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/presentation/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import core, {
TxProcessor,
getCurrentAccount,
reduceCalls,
type Account,
type AnyAttribute,
type ArrOf,
type AttachedDoc,
Expand Down Expand Up @@ -253,6 +254,18 @@ export function getClient (): TxOperations & Client {
return clientProxy
}

export type OnClientListener = (client: Client, account: Account) => void
const onClientListeners: OnClientListener[] = []

export function onClient (l: OnClientListener): void {
onClientListeners.push(l)
if (client !== undefined) {
setTimeout(() => {
l(client, getCurrentAccount())
})
}
}

let txQueue: Tx[] = []

export type RefreshListener = () => void
Expand Down Expand Up @@ -306,6 +319,10 @@ export async function setClient (_client: Client): Promise<void> {
if (needRefresh || globalQueries.length > 0) {
await refreshClient(true)
}
const acc = getCurrentAccount()
onClientListeners.forEach((l) => {
l(_client, acc)
})
}
/**
* @public
Expand Down
32 changes: 11 additions & 21 deletions plugins/activity-resources/src/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,21 @@
//
import activity, { type ActivityMessage, type SavedMessage } from '@hcengineering/activity'
import core, { type Ref, SortingOrder, type WithLookup } from '@hcengineering/core'
import { createQuery, onClient } from '@hcengineering/presentation'
import { writable } from 'svelte/store'
import { createQuery, getClient } from '@hcengineering/presentation'

export const savedMessagesStore = writable<Array<WithLookup<SavedMessage>>>([])
export const messageInFocus = writable<Ref<ActivityMessage> | undefined>(undefined)

const savedMessagesQuery = createQuery(true)

export function loadSavedMessages (): void {
const client = getClient()

if (client !== undefined) {
savedMessagesQuery.query(
activity.class.SavedMessage,
{ space: core.space.Workspace },
(res) => {
savedMessagesStore.set(res.filter(({ $lookup }) => $lookup?.attachedTo !== undefined))
},
{ lookup: { attachedTo: activity.class.ActivityMessage }, sort: { modifiedOn: SortingOrder.Descending } }
)
} else {
setTimeout(() => {
loadSavedMessages()
}, 50)
}
}

loadSavedMessages()
onClient(() => {
savedMessagesQuery.query(
activity.class.SavedMessage,
{ space: core.space.Workspace },
(res) => {
savedMessagesStore.set(res.filter(({ $lookup }) => $lookup?.attachedTo !== undefined))
},
{ lookup: { attachedTo: activity.class.ActivityMessage }, sort: { modifiedOn: SortingOrder.Descending } }
)
})
34 changes: 12 additions & 22 deletions plugins/calendar-resources/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
type ReccuringInstance,
generateEventId
} from '@hcengineering/calendar'
import { type IdMap, type Timestamp, getCurrentAccount, toIdMap, type DocumentUpdate } from '@hcengineering/core'
import { createQuery, getClient } from '@hcengineering/presentation'
import { showPopup, closePopup, DAY } from '@hcengineering/ui'
import { type DocumentUpdate, type IdMap, type Timestamp, getCurrentAccount, toIdMap } from '@hcengineering/core'
import { createQuery, getClient, onClient } from '@hcengineering/presentation'
import { closePopup, DAY, showPopup } from '@hcengineering/ui'
import { writable } from 'svelte/store'
import calendar from './plugin'
import UpdateRecInstancePopup from './components/UpdateRecInstancePopup.svelte'
import calendar from './plugin'

export function saveUTC (date: Timestamp): Timestamp {
const utcdate = new Date(date)
Expand Down Expand Up @@ -74,24 +74,14 @@ export const calendarByIdStore = writable<IdMap<Calendar>>(new Map())
export const calendarStore = writable<Calendar[]>([])
export const visibleCalendarStore = writable<Calendar[]>([])

function fillStores (): void {
const client = getClient()

if (client !== undefined) {
const query = createQuery(true)
query.query(calendar.class.Calendar, {}, (res) => {
calendarStore.set(res)
visibleCalendarStore.set(res.filter((p) => !p.hidden))
calendarByIdStore.set(toIdMap(res))
})
} else {
setTimeout(() => {
fillStores()
}, 50)
}
}

fillStores()
const query = createQuery(true)
onClient((client, account) => {
query.query(calendar.class.Calendar, {}, (res) => {
calendarStore.set(res)
visibleCalendarStore.set(res.filter((p) => !p.hidden))
calendarByIdStore.set(toIdMap(res))
})
})

export async function updatePast (ops: DocumentUpdate<Event>, object: ReccuringInstance): Promise<void> {
const client = getClient()
Expand Down
15 changes: 4 additions & 11 deletions plugins/chunter-resources/src/components/chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import core, {
} from '@hcengineering/core'
import notification, { type DocNotifyContext } from '@hcengineering/notification'
import { InboxNotificationsClientImpl } from '@hcengineering/notification-resources'
import { createQuery, getClient, MessageBox } from '@hcengineering/presentation'
import { createQuery, getClient, MessageBox, onClient } from '@hcengineering/presentation'
import { type Action, showPopup } from '@hcengineering/ui'
import view from '@hcengineering/view'
import workbench, { type SpecialNavModel } from '@hcengineering/workbench'
Expand Down Expand Up @@ -364,12 +364,9 @@ function archiveActivityChannels (contexts: DocNotifyContext[]): void {
)
}

const savedAttachmentsQuery = createQuery(true)
export function loadSavedAttachments (): void {
const client = getClient()

if (client !== undefined) {
const savedAttachmentsQuery = createQuery(true)

onClient(() => {
savedAttachmentsQuery.query(
attachment.class.SavedAttachments,
{ space: core.space.Workspace },
Expand All @@ -378,11 +375,7 @@ export function loadSavedAttachments (): void {
},
{ lookup: { attachedTo: attachment.class.Attachment }, sort: { modifiedOn: SortingOrder.Descending } }
)
} else {
setTimeout(() => {
loadSavedAttachments()
}, 50)
}
})
}

export async function hideActivityChannels (contexts: DocNotifyContext[]): Promise<void> {
Expand Down
52 changes: 21 additions & 31 deletions plugins/contact-resources/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import core, {
} from '@hcengineering/core'
import notification, { type DocNotifyContext, type InboxNotification } from '@hcengineering/notification'
import { type IntlString, getEmbeddedLabel, getResource, translate } from '@hcengineering/platform'
import { createQuery, getClient } from '@hcengineering/presentation'
import { createQuery, getClient, onClient } from '@hcengineering/presentation'
import { type TemplateDataProvider } from '@hcengineering/templates'
import {
getCurrentResolvedLocation,
Expand Down Expand Up @@ -323,42 +323,32 @@ export const personByIdStore = derived([personAccountPersonByIdStore, employeeBy
return new Map([...m1, ...m2])
})

function fillStores (): void {
const client = getClient()

if (client !== undefined) {
const accountPersonQuery = createQuery(true)

const query = createQuery(true)
query.query(contact.mixin.Employee, { active: { $in: [true, false] } }, (res) => {
employeesStore.set(res)
employeeByIdStore.set(toIdMap(res))
})
const query = createQuery(true)
const accountQ = createQuery(true)
const accountPersonQuery = createQuery(true)
const providerQuery = createQuery(true)

const accountQ = createQuery(true)
accountQ.query(contact.class.PersonAccount, {}, (res) => {
personAccountByIdStore.set(toIdMap(res))
onClient(() => {
query.query(contact.mixin.Employee, { active: { $in: [true, false] } }, (res) => {
employeesStore.set(res)
employeeByIdStore.set(toIdMap(res))
})

const persons = res.map((it) => it.person)
accountQ.query(contact.class.PersonAccount, {}, (res) => {
personAccountByIdStore.set(toIdMap(res))

accountPersonQuery.query<Person>(contact.class.Person, { _id: { $in: persons } }, (res) => {
const personIn = toIdMap(res)
personAccountPersonByIdStore.set(personIn)
})
})
const persons = res.map((it) => it.person)

const providerQuery = createQuery(true)
providerQuery.query(contact.class.ChannelProvider, {}, (res) => {
channelProviders.set(res)
accountPersonQuery.query<Person>(contact.class.Person, { _id: { $in: persons } }, (res) => {
const personIn = toIdMap(res)
personAccountPersonByIdStore.set(personIn)
})
} else {
setTimeout(() => {
fillStores()
}, 50)
}
}
})

fillStores()
providerQuery.query(contact.class.ChannelProvider, {}, (res) => {
channelProviders.set(res)
})
})

const userStatusesQuery = createQuery(true)

Expand Down
132 changes: 62 additions & 70 deletions plugins/love-resources/src/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
type Room,
type MeetingMinutes
} from '@hcengineering/love'
import { createQuery, getClient } from '@hcengineering/presentation'
import { createQuery, onClient } from '@hcengineering/presentation'
import { derived, get, writable } from 'svelte/store'
import { personIdByAccountId } from '@hcengineering/contact-resources'
import aiBot from '@hcengineering/ai-bot'
Expand Down Expand Up @@ -79,77 +79,69 @@ function filterParticipantInfo (value: ParticipantInfo[]): ParticipantInfo[] {

export const storePromise = writable<Promise<void>>(new Promise((resolve) => {}))

function fillStores (): void {
const client = getClient()
if (client !== undefined || getCurrentAccount() != null) {
const query = createQuery(true)
const roomPromise = new Promise<void>((resolve) =>
query.query(love.class.Room, {}, (res) => {
rooms.set(res)
resolve()
})
)
const statusQuery = createQuery(true)
const infoPromise = new Promise<void>((resolve) =>
statusQuery.query(love.class.ParticipantInfo, {}, (res) => {
infos.set(filterParticipantInfo(res))
resolve()
})
)
const floorsQuery = createQuery(true)
const floorPromise = new Promise<void>((resolve) =>
floorsQuery.query(love.class.Floor, {}, (res) => {
floors.set(res)
const query = createQuery(true)
const statusQuery = createQuery(true)
const floorsQuery = createQuery(true)
const requestsQuery = createQuery(true)
const preferencesQuery = createQuery(true)
const invitesQuery = createQuery(true)

onClient(() => {
const roomPromise = new Promise<void>((resolve) =>
query.query(love.class.Room, {}, (res) => {
rooms.set(res)
resolve()
})
)
const infoPromise = new Promise<void>((resolve) =>
statusQuery.query(love.class.ParticipantInfo, {}, (res) => {
infos.set(filterParticipantInfo(res))
resolve()
})
)
const floorPromise = new Promise<void>((resolve) =>
floorsQuery.query(love.class.Floor, {}, (res) => {
floors.set(res)
resolve()
})
)
const requestPromise = new Promise<void>((resolve) =>
requestsQuery.query(
love.class.JoinRequest,
{ person: (getCurrentAccount() as PersonAccount).person, status: RequestStatus.Pending },
(res) => {
myRequests.set(res)
resolve()
})
)
const requestsQuery = createQuery(true)
const requestPromise = new Promise<void>((resolve) =>
requestsQuery.query(
love.class.JoinRequest,
{ person: (getCurrentAccount() as PersonAccount).person, status: RequestStatus.Pending },
(res) => {
myRequests.set(res)
resolve()
}
)
}
)
const preferencesQuery = createQuery(true)
const preferencePromise = new Promise<void>((resolve) =>
preferencesQuery.query(love.class.DevicesPreference, {}, (res) => {
myPreferences.set(res[0])
$myPreferences = res[0]
resolve()
})
)
const invitesQuery = createQuery(true)
const invitesPromise = new Promise<void>((resolve) =>
invitesQuery.query(love.class.Invite, { status: RequestStatus.Pending }, (res) => {
invites.set(res)
)
const preferencePromise = new Promise<void>((resolve) =>
preferencesQuery.query(love.class.DevicesPreference, {}, (res) => {
myPreferences.set(res[0])
$myPreferences = res[0]
resolve()
})
)

const invitesPromise = new Promise<void>((resolve) =>
invitesQuery.query(love.class.Invite, { status: RequestStatus.Pending }, (res) => {
invites.set(res)
resolve()
})
)
storePromise.set(
new Promise((resolve) => {
void Promise.all([
roomPromise,
infoPromise,
floorPromise,
requestPromise,
preferencePromise,
invitesPromise
]).then(() => {
resolve()
})
)
storePromise.set(
new Promise((resolve) => {
void Promise.all([
roomPromise,
infoPromise,
floorPromise,
requestPromise,
preferencePromise,
invitesPromise
]).then(() => {
resolve()
})
})
)
} else {
setTimeout(() => {
fillStores()
}, 50)
}
}

fillStores()

})
)
})
export const lockedRoom = writable<string>('')
Loading
Loading