Skip to content

Commit

Permalink
add debounce for search, increase the timer for fulltext updates (hce…
Browse files Browse the repository at this point in the history
…ngineering#5844)

Signed-off-by: Vyacheslav Tumanov <[email protected]>
  • Loading branch information
ThetaDR authored Jun 18, 2024
1 parent c526365 commit ece0504
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 68 deletions.
125 changes: 60 additions & 65 deletions packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1287,89 +1287,84 @@ export class LiveQuery implements WithTx, Client {
return result
}

triggerCounter = 0
searchTriggerTimer: any
triggerInProgress = true

private async checkUpdateEvents (evt: TxWorkspaceEvent, trigger = true): Promise<void> {
clearTimeout(this.searchTriggerTimer)

// We need to add trigger once more, since elastic could have a huge lag with document availability.
if (trigger || this.triggerCounter > 0) {
if (trigger) {
this.triggerCounter = 2 // Schedule 2 refreshes on every 10 seconds.
if (this.triggerInProgress) {
this.triggerInProgress = false
const h = this.client.getHierarchy()
function hasClass (q: Query, classes: Ref<Class<Doc>>[]): boolean {
return (
classes.includes(q._class) || classes.some((it) => h.isDerived(q._class, it) || h.isDerived(it, q._class))
)
}
setTimeout(() => {
this.triggerCounter--
void this.checkUpdateEvents(evt, false)
}, 10000)
}

const h = this.client.getHierarchy()
function hasClass (q: Query, classes: Ref<Class<Doc>>[]): boolean {
return classes.includes(q._class) || classes.some((it) => h.isDerived(q._class, it) || h.isDerived(it, q._class))
}
if (evt.event === WorkspaceEvent.IndexingUpdate) {
const indexingParam = evt.params as IndexingUpdateEvent
for (const q of [...this.queue]) {
if (hasClass(q, indexingParam._class) && q.query.$search !== undefined) {
if (!this.removeFromQueue(q)) {
try {
await this.refresh(q, true)
} catch (err: any) {
Analytics.handleError(err)
console.error(err)
}
} else {
const queries = this.queries.get(q._class)
const pos = queries?.indexOf(q) ?? -1
if (pos >= 0 && queries !== undefined) {
queries.splice(pos, 1)
if (queries?.length === 0) {
this.queries.delete(q._class)
if (evt.event === WorkspaceEvent.IndexingUpdate) {
const indexingParam = evt.params as IndexingUpdateEvent
for (const q of [...this.queue]) {
if (hasClass(q, indexingParam._class) && q.query.$search !== undefined) {
if (!this.removeFromQueue(q)) {
try {
await this.refresh(q, true)
} catch (err: any) {
Analytics.handleError(err)
console.error(err)
}
} else {
const queries = this.queries.get(q._class)
const pos = queries?.indexOf(q) ?? -1
if (pos >= 0 && queries !== undefined) {
queries.splice(pos, 1)
if (queries?.length === 0) {
this.queries.delete(q._class)
}
}
}
}
}
}
for (const v of this.queries.values()) {
for (const q of v) {
if (hasClass(q, indexingParam._class) && q.query.$search !== undefined) {
try {
await this.refresh(q, true)
} catch (err: any) {
Analytics.handleError(err)
console.error(err)
for (const v of this.queries.values()) {
for (const q of v) {
if (hasClass(q, indexingParam._class) && q.query.$search !== undefined) {
try {
await this.refresh(q, true)
} catch (err: any) {
Analytics.handleError(err)
console.error(err)
}
}
}
}
}
}
if (evt.event === WorkspaceEvent.BulkUpdate) {
const params = evt.params as BulkUpdateEvent
for (const q of [...this.queue]) {
if (hasClass(q, params._class)) {
if (!this.removeFromQueue(q)) {
try {
await this.refresh(q, true)
} catch (err: any) {
Analytics.handleError(err)
console.error(err)
if (evt.event === WorkspaceEvent.BulkUpdate) {
const params = evt.params as BulkUpdateEvent
for (const q of [...this.queue]) {
if (hasClass(q, params._class)) {
if (!this.removeFromQueue(q)) {
try {
await this.refresh(q, true)
} catch (err: any) {
Analytics.handleError(err)
console.error(err)
}
}
}
}
}
for (const v of this.queries.values()) {
for (const q of v) {
if (hasClass(q, params._class)) {
try {
await this.refresh(q, true)
} catch (err: any) {
Analytics.handleError(err)
console.error(err)
for (const v of this.queries.values()) {
for (const q of v) {
if (hasClass(q, params._class)) {
try {
await this.refresh(q, true)
} catch (err: any) {
Analytics.handleError(err)
console.error(err)
}
}
}
}
}
setTimeout(() => {
this.triggerInProgress = true
void this.checkUpdateEvents(evt, false)
}, 20000)
}
}

Expand Down
19 changes: 16 additions & 3 deletions plugins/view-resources/src/components/ActionsPopup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@
$: void translate(view.string.ActionPlaceholder, {}).then((res) => {
phTraslate = res
})
let timer: any
$: _search = search
function restartTimer (): void {
clearTimeout(timer)
timer = setTimeout(() => {
search = _search
dispatch('change', _search)
}, 500)
}
</script>

<ActionContext
Expand Down Expand Up @@ -364,10 +373,14 @@
class="actionsInput"
bind:this={textHTML}
type="text"
bind:value={search}
bind:value={_search}
placeholder={phTraslate}
on:change
on:input
on:change={() => {
restartTimer()
}}
on:input={() => {
restartTimer()
}}
on:keydown
/>
</div>
Expand Down

0 comments on commit ece0504

Please sign in to comment.