@@ -3,19 +3,20 @@ import { template, transform } from '@vaadin/hilla-generator-utils/ast.js';
3
3
import createSourceFile from '@vaadin/hilla-generator-utils/createSourceFile.js' ;
4
4
import DependencyManager from '@vaadin/hilla-generator-utils/dependencies/DependencyManager.js' ;
5
5
import PathManager from '@vaadin/hilla-generator-utils/dependencies/PathManager.js' ;
6
- import ts , { type CallExpression , type FunctionDeclaration , type ReturnStatement , type SourceFile } from 'typescript' ;
6
+ import ts , { type FunctionDeclaration , type SourceFile } from 'typescript' ;
7
7
8
8
export type MethodInfo = Readonly < {
9
9
name : string ;
10
10
signalType : string ;
11
11
} > ;
12
12
13
- const ENDPOINT_CALL_EXPRESSION = '$ENDPOINT_CALL_EXPRESSION$' ;
14
- const NUMBER_SIGNAL_QUEUE = '$NUMBER_SIGNAL_QUEUE$' ;
15
- const SIGNALS_HANDLER = '$SIGNALS_HANDLER$' ;
16
- const CONNECT_CLIENT = '$CONNECT_CLIENT$' ;
17
13
const HILLA_REACT_SIGNALS = '@vaadin/hilla-react-signals' ;
18
- const ENDPOINTS = 'Frontend/generated/endpoints.js' ;
14
+
15
+ const NUMBER_SIGNAL_CHANNEL = '$NUMBER_SIGNAL_CHANNEL$' ;
16
+ const CONNECT_CLIENT = '$CONNECT_CLIENT$' ;
17
+ const ENDPOINT_AND_METHOD_NAME = '$ENDPOINT_AND_METHOD_NAME$' ;
18
+
19
+ const signalImportPaths = [ 'com/vaadin/hilla/signals/NumberSignal' ] ;
19
20
20
21
export default class SignalProcessor {
21
22
readonly #dependencyManager: DependencyManager ;
@@ -36,55 +37,59 @@ export default class SignalProcessor {
36
37
process ( ) : SourceFile {
37
38
this . #owner. logger . debug ( `Processing signals: ${ this . #service} ` ) ;
38
39
const { imports } = this . #dependencyManager;
39
- const numberSignalQueueId = imports . named . add ( HILLA_REACT_SIGNALS , 'NumberSignalQueue' ) ;
40
- const signalHandlerId = imports . named . add ( ENDPOINTS , 'SignalsHandler' ) ;
40
+ const numberSignalChannelId = imports . named . add ( HILLA_REACT_SIGNALS , 'NumberSignalChannel' ) ;
41
41
42
42
const [ _p , _isType , connectClientId ] = imports . default . find ( ( p ) => p . includes ( 'connect-client' ) ) ! ;
43
43
44
- this . #processNumberSignalImport ( 'com/vaadin/hilla/signals/NumberSignal' ) ;
44
+ this . #processSignalImports ( signalImportPaths ) ;
45
45
46
46
const [ file ] = ts . transform < SourceFile > ( this . #sourceFile, [
47
47
...this . #methods. map ( ( method ) =>
48
- transform < SourceFile > ( ( node ) => {
49
- if ( ts . isFunctionDeclaration ( node ) && node . name ?. text === method . name ) {
50
- const callExpression = ( node . body ?. statements [ 0 ] as ReturnStatement ) . expression as CallExpression ;
48
+ transform < SourceFile > ( ( tsNode ) => {
49
+ if ( ts . isFunctionDeclaration ( tsNode ) && tsNode . name ?. text === method . name ) {
50
+ const endpointAndMethodName = ts . factory . createStringLiteral ( ` ${ this . #service } . ${ method . name } ` ) ;
51
51
const body = template (
52
52
`
53
53
function dummy() {
54
- const sharedSignal = await ${ ENDPOINT_CALL_EXPRESSION } ;
55
- const queueDescriptor = {
56
- id: sharedSignal.id,
57
- subscribe: ${ SIGNALS_HANDLER } .subscribe,
58
- publish: ${ SIGNALS_HANDLER } .update,
59
- };
60
- const valueLog = new ${ NUMBER_SIGNAL_QUEUE } (queueDescriptor, ${ CONNECT_CLIENT } );
61
- return valueLog.getRoot();
54
+ return new ${ NUMBER_SIGNAL_CHANNEL } (${ ENDPOINT_AND_METHOD_NAME } , ${ CONNECT_CLIENT } ).signal;
62
55
}` ,
63
56
( statements ) => ( statements [ 0 ] as FunctionDeclaration ) . body ?. statements ,
64
57
[
65
58
transform ( ( node ) =>
66
- ts . isIdentifier ( node ) && node . text === ENDPOINT_CALL_EXPRESSION ? callExpression : node ,
59
+ ts . isIdentifier ( node ) && node . text === ENDPOINT_AND_METHOD_NAME ? endpointAndMethodName : node ,
67
60
) ,
68
61
transform ( ( node ) =>
69
- ts . isIdentifier ( node ) && node . text === NUMBER_SIGNAL_QUEUE ? numberSignalQueueId : node ,
62
+ ts . isIdentifier ( node ) && node . text === NUMBER_SIGNAL_CHANNEL ? numberSignalChannelId : node ,
70
63
) ,
71
- transform ( ( node ) => ( ts . isIdentifier ( node ) && node . text === SIGNALS_HANDLER ? signalHandlerId : node ) ) ,
72
64
transform ( ( node ) => ( ts . isIdentifier ( node ) && node . text === CONNECT_CLIENT ? connectClientId : node ) ) ,
73
65
] ,
74
66
) ;
75
67
68
+ let returnType = tsNode . type ;
69
+ if (
70
+ returnType &&
71
+ ts . isTypeReferenceNode ( returnType ) &&
72
+ 'text' in returnType . typeName &&
73
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
74
+ returnType . typeName . escapedText === 'Promise'
75
+ ) {
76
+ if ( returnType . typeArguments && returnType . typeArguments . length > 0 ) {
77
+ returnType = returnType . typeArguments [ 0 ] ;
78
+ }
79
+ }
80
+
76
81
return ts . factory . createFunctionDeclaration (
77
- node . modifiers ,
78
- node . asteriskToken ,
79
- node . name ,
80
- node . typeParameters ,
81
- node . parameters ,
82
- node . type ,
83
- ts . factory . createBlock ( body ?? [ ] , true ) ,
82
+ tsNode . modifiers ?. filter ( ( modifier ) => modifier . kind !== ts . SyntaxKind . AsyncKeyword ) ,
83
+ tsNode . asteriskToken ,
84
+ tsNode . name ,
85
+ tsNode . typeParameters ,
86
+ [ ] ,
87
+ returnType ,
88
+ ts . factory . createBlock ( body ?? [ ] , false ) ,
84
89
) ;
85
90
}
86
91
87
- return node ;
92
+ return tsNode ;
88
93
} ) ,
89
94
) ,
90
95
] ) . transformed ;
@@ -98,15 +103,17 @@ function dummy() {
98
103
) ;
99
104
}
100
105
101
- #processNumberSignalImport ( path : string ) {
106
+ #processSignalImports ( signalImports : string [ ] ) {
102
107
const { imports } = this . #dependencyManager;
103
108
104
- const result = imports . default . find ( ( p ) => p . includes ( path ) ) ;
109
+ signalImports . forEach ( ( signalImport ) => {
110
+ const result = imports . default . find ( ( p ) => p . includes ( signalImport ) ) ;
105
111
106
- if ( result ) {
107
- const [ path , _ , id ] = result ;
108
- imports . default . remove ( path ) ;
109
- imports . named . add ( HILLA_REACT_SIGNALS , id . text , true , id ) ;
110
- }
112
+ if ( result ) {
113
+ const [ path , _ , id ] = result ;
114
+ imports . default . remove ( path ) ;
115
+ imports . named . add ( HILLA_REACT_SIGNALS , id . text , true , id ) ;
116
+ }
117
+ } ) ;
111
118
}
112
119
}
0 commit comments