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