This repository was archived by the owner on Oct 18, 2024. It is now read-only.
File tree 2 files changed +17
-19
lines changed
2 files changed +17
-19
lines changed Original file line number Diff line number Diff line change @@ -55,13 +55,13 @@ describe('realtime component', () => {
55
55
} ) ;
56
56
57
57
describe ( 'connect' , ( ) => {
58
- test ( 'should log an error when trying to create a channel before start' , ( ) => {
58
+ test ( 'should return a promise when trying to create a channel before start' , ( ) => {
59
+ RealtimeComponentInstance [ 'start' ] ( ) ;
59
60
RealtimeComponentInstance [ 'state' ] = RealtimeComponentState . STOPPED ;
60
61
61
- const spy = jest . spyOn ( RealtimeComponentInstance [ 'logger' ] , 'log' ) ;
62
- RealtimeComponentInstance . connect ( 'test' ) ;
62
+ const channel = RealtimeComponentInstance . connect ( 'test' ) ;
63
63
64
- expect ( spy ) . toHaveBeenCalled ( ) ;
64
+ expect ( channel instanceof Promise ) . toBe ( true ) ;
65
65
} ) ;
66
66
67
67
test ( 'should create a new channel' , ( ) => {
Original file line number Diff line number Diff line change @@ -41,25 +41,23 @@ export class Realtime extends BaseComponent {
41
41
* @param name - channel name
42
42
* @returns {Channel }
43
43
*/
44
- public connect ( name : string ) : Channel {
45
- if ( this . state !== RealtimeComponentState . STARTED ) {
46
- const message =
47
- "[SuperViz] Realtime component is not started yet. You can't connect to a channel before start" ;
48
-
49
- this . logger . log ( message ) ;
50
- console . warn ( message ) ;
51
- return ;
52
- }
53
-
44
+ public connect ( name : string ) : Promise < Channel > {
54
45
let channel : Channel = this . channels . get ( name ) ;
55
-
56
- if ( channel ) return channel ;
46
+ if ( channel ) return channel as unknown as Promise < Channel > ;
57
47
58
48
channel = new Channel ( name , this . ioc , this . localParticipant , this . connectionLimit ) ;
59
-
60
49
this . channels . set ( name , channel ) ;
61
50
62
- return channel ;
51
+ if ( this . state === RealtimeComponentState . STARTED ) {
52
+ return channel as unknown as Promise < Channel > ;
53
+ }
54
+
55
+ return new Promise ( ( resolve ) => {
56
+ this . channel . subscribe ( RealtimeComponentEvent . REALTIME_STATE_CHANGED , ( state ) => {
57
+ if ( state !== RealtimeComponentState . STARTED ) return ;
58
+ resolve ( channel ) ;
59
+ } ) ;
60
+ } ) ;
63
61
}
64
62
65
63
/**
@@ -75,7 +73,7 @@ export class Realtime extends BaseComponent {
75
73
return ;
76
74
}
77
75
78
- this . channel ? .subscribe ( event , callback ) ;
76
+ this . channel . subscribe ( event , callback ) ;
79
77
} ;
80
78
81
79
/**
You can’t perform that action at this time.
0 commit comments