1
1
import type { StreamId } from "$lib/lens/types" ;
2
- import { queriesStore } from "$lib/stores/queries.svelte" ;
2
+ import { queriesStore , type QueryStoreEntry } from "$lib/stores/queries.svelte" ;
3
3
import type { QueryStream } from "$lib/stores/QueryStream.svelte" ;
4
4
5
5
export type SplitDirection = 'vertical' | 'horizontal' ;
6
+ export const DEFAULT_QUERY_TITLE : string = 'Unnamed' ;
6
7
7
- type PaneInfo = {
8
- get query ( ) : string ,
9
- set query ( val : string ) ,
8
+ class QueryPane {
9
+ query = $state ( '' ) ;
10
+ title = $state ( '' ) ;
11
+ stream = $state < QueryStream | undefined > ( undefined ) ;
10
12
11
- stream ?: QueryStream ,
12
- } ;
13
+ streamId ?: StreamId ;
13
14
14
- function usePane ( queryString ?: string ) : PaneInfo {
15
- let query = $state ( queryString ?? '' ) ;
15
+ constructor ( queryString ?: string , title : string = DEFAULT_QUERY_TITLE ) {
16
+ this . query = queryString ?? '' ;
17
+ this . title = title ;
18
+ }
19
+
20
+ clear ( ) {
21
+ this . query = '' ;
22
+ this . title = DEFAULT_QUERY_TITLE ;
23
+
24
+ this . streamId = undefined ;
25
+ this . stream = undefined ;
26
+ }
27
+
28
+ renew ( entry : QueryStoreEntry ) {
29
+ const { title, stream } = entry ;
16
30
17
- return {
18
- get query ( ) : string { return query } ,
19
- set query ( val : string ) { query = val } ,
31
+ this . title = title ;
20
32
21
- stream : undefined ,
22
- } ;
33
+ if ( stream . kind === 'partial' ) {
34
+ this . query = stream . query ;
35
+ this . streamId = stream . id ;
36
+ this . stream = undefined ;
37
+ } else if ( stream . kind === 'full' ) {
38
+ const { query, streamId } = stream . stream ;
39
+
40
+ this . query = query ;
41
+ this . streamId = streamId ;
42
+ this . stream = stream . stream ;
43
+ }
44
+ }
23
45
}
24
46
25
47
export class QueryPaneGroup {
26
48
direction = $state < SplitDirection | undefined > ( undefined ) ;
27
- panes = $state < PaneInfo [ ] > ( [ usePane ( undefined ) ] ) ;
49
+ panes = $state < QueryPane [ ] > ( [ new QueryPane ( '' ) ] ) ;
28
50
overlayVisible = $state ( false ) ;
29
51
30
52
constructor ( ) {
@@ -34,7 +56,7 @@ export class QueryPaneGroup {
34
56
this . direction = direction ;
35
57
36
58
if ( this . panes . length == 1 ) {
37
- this . panes . push ( usePane ( undefined ) )
59
+ this . panes . push ( new QueryPane ( '' ) )
38
60
}
39
61
}
40
62
@@ -49,21 +71,11 @@ export class QueryPaneGroup {
49
71
if ( paneId >= this . panes . length )
50
72
throw new Error ( `invalid pane ${ paneId } ` ) ;
51
73
52
- const stream = queriesStore . get ( streamId ) ;
53
- if ( ! stream )
54
- throw new Error ( `Could not find stream for id ${ streamId } ` ) ;
74
+ const entry = queriesStore . get ( streamId ) ;
75
+ if ( ! entry )
76
+ throw new Error ( `Could not find query for stream ${ streamId } ` ) ;
55
77
56
- if ( stream . kind === 'partial' ) {
57
- this . panes [ paneId ] = {
58
- query : stream . query ,
59
- stream : undefined
60
- }
61
- } else if ( stream . kind === 'full' ) {
62
- this . panes [ paneId ] = {
63
- query : stream . stream . query ,
64
- stream : stream . stream
65
- }
66
- }
78
+ this . panes [ paneId ] . renew ( entry ) ;
67
79
}
68
80
69
81
save ( paneId : number ) {
@@ -75,25 +87,33 @@ export class QueryPaneGroup {
75
87
queriesStore . save ( stream ) ;
76
88
}
77
89
90
+ setTitle ( paneId : number , title : string ) {
91
+ if ( paneId >= this . panes . length )
92
+ throw new Error ( `invalid pane ${ paneId } ` ) ;
93
+
94
+ const streamId = this . panes [ paneId ] ?. streamId ;
95
+ if ( streamId )
96
+ queriesStore . setTitle ( streamId , title ) ;
97
+
98
+ }
99
+
78
100
clear ( paneId : number ) {
79
101
if ( paneId >= this . panes . length )
80
102
throw new Error ( `invalid pane ${ paneId } ` ) ;
81
103
82
- this . panes [ paneId ] . stream = undefined ;
83
- this . panes [ paneId ] . query = '' ;
104
+ this . panes [ paneId ] . clear ( ) ;
84
105
}
85
106
86
- async run ( paneId : number ) : Promise < QueryStream > {
107
+ async run ( paneId : number , title : string ) : Promise < QueryStream > {
87
108
if ( paneId >= this . panes . length )
88
109
throw new Error ( `invalid pane ${ paneId } ` ) ;
89
110
90
111
const query = this . panes [ paneId ] . query ;
91
- const stream = await queriesStore . run ( query ) ;
112
+ const stream = await queriesStore . run ( query , title ) ;
113
+
114
+ this . panes [ paneId ] . streamId = stream . streamId ;
115
+ this . panes [ paneId ] . stream = stream ;
92
116
93
- this . panes [ paneId ] = {
94
- query,
95
- stream
96
- }
97
117
return stream ;
98
118
}
99
119
0 commit comments