|
| 1 | +import { Task } from "@lit/task"; |
| 2 | +import { type ReactiveController } from "lit"; |
| 3 | + |
| 4 | +import { type WorkflowsList } from "@/pages/org/workflows-list"; |
| 5 | + |
| 6 | +export class ClockController implements ReactiveController { |
| 7 | + host: WorkflowsList; |
| 8 | + |
| 9 | + timeout: number; |
| 10 | + private _timerID?: number; |
| 11 | + |
| 12 | + readonly task; |
| 13 | + |
| 14 | + constructor(host: WorkflowsList, timeout = 1000, INITIAL_PAGE_SIZE = 10) { |
| 15 | + (this.host = host).addController(this); |
| 16 | + this.timeout = timeout; |
| 17 | + this.task = new Task(this.host, { |
| 18 | + task: async ( |
| 19 | + [showRunningFirst, filterBy, orderBy, page, filterByCurrentUser], |
| 20 | + { signal }, |
| 21 | + ) => { |
| 22 | + if (!showRunningFirst) { |
| 23 | + return; |
| 24 | + } |
| 25 | + const queryParams = { |
| 26 | + ...filterBy, |
| 27 | + page: page || 1, |
| 28 | + pageSize: this.task.value?.pageSize || INITIAL_PAGE_SIZE, |
| 29 | + userid: filterByCurrentUser ? this.host.userInfo?.id : undefined, |
| 30 | + sortBy: orderBy.field, |
| 31 | + sortDirection: orderBy.direction === "desc" ? -1 : 1, |
| 32 | + running: true, |
| 33 | + }; |
| 34 | + |
| 35 | + const query = stringifyQuery(queryParams); |
| 36 | + |
| 37 | + const workflows = await this.api.fetch<APIPaginatedList<Workflow>>( |
| 38 | + `/orgs/${this.orgId}/crawlconfigs?${query}`, |
| 39 | + { |
| 40 | + signal: signal, |
| 41 | + }, |
| 42 | + ); |
| 43 | + |
| 44 | + signal.addEventListener("abort", () => { |
| 45 | + clearTimeout(this.runningIntervalId); |
| 46 | + this.runningIntervalId = undefined; |
| 47 | + }); |
| 48 | + |
| 49 | + clearTimeout(this.allIntervalId); |
| 50 | + |
| 51 | + this.runningIntervalId = window.setTimeout(() => { |
| 52 | + void this.runningWorkflowsTask.run(); |
| 53 | + }, 1000 * POLL_INTERVAL_SECONDS); |
| 54 | + |
| 55 | + return workflows; |
| 56 | + }, |
| 57 | + args: () => |
| 58 | + [ |
| 59 | + this.showRunningFirst, |
| 60 | + this.filterBy, |
| 61 | + this.orderBy, |
| 62 | + this.page[WorkflowGroup.RUNNING], |
| 63 | + this.filterByCurrentUser, |
| 64 | + ] as const, |
| 65 | + }); |
| 66 | + } |
| 67 | + hostConnected() { |
| 68 | + // Start a timer when the host is connected |
| 69 | + this._timerID = setInterval(() => { |
| 70 | + this.value = new Date(); |
| 71 | + // Update the host with new value |
| 72 | + this.host.requestUpdate(); |
| 73 | + }, this.timeout); |
| 74 | + } |
| 75 | + hostDisconnected() { |
| 76 | + // Clear the timer when the host is disconnected |
| 77 | + clearInterval(this._timerID); |
| 78 | + this._timerID = undefined; |
| 79 | + } |
| 80 | +} |
0 commit comments