@@ -51,7 +51,7 @@ define([
5151
5252 CommManager . prototype . register_comm = function ( comm ) {
5353 // Register a comm in the mapping
54- this . comms [ comm . comm_id ] = comm ;
54+ this . comms [ comm . comm_id ] = Promise . resolve ( comm ) ;
5555 comm . kernel = this . kernel ;
5656 return comm . comm_id ;
5757 } ;
@@ -66,67 +66,62 @@ define([
6666 CommManager . prototype . comm_open = function ( msg ) {
6767 var content = msg . content ;
6868 var that = this ;
69-
70- var instantiate_comm = function ( target ) {
71- var comm = new Comm ( content . target_name , content . comm_id ) ;
72- that . register_comm ( comm ) ;
69+ var comm_id = content . comm_id ;
70+
71+ this . comms [ comm_id ] = utils . load_class ( content . target_name , content . target_module ,
72+ this . targets ) . then ( function ( target ) {
73+
74+ var comm = new Comm ( content . target_name , comm_id ) ;
75+ comm . kernel = that . kernel ;
7376 try {
74- target ( comm , msg ) ;
77+ var response = target ( comm , msg ) ;
78+ if ( response instanceof Promise ) {
79+ return response . then ( function ( ) { return Promise . resolve ( comm ) ; } ) ;
80+ }
7581 } catch ( e ) {
76- console . log ( "Exception opening new comm:" , e , e . stack , msg ) ;
7782 comm . close ( ) ;
7883 that . unregister_comm ( comm ) ;
84+ var wrapped_error = new utils . WrappedError ( "Exception opening new comm" , e ) ;
85+ console . error ( wrapped_error ) ;
86+ return Promise . reject ( wrapped_error ) ;
7987 }
80- } ;
81-
82- if ( content . target_module ) {
83- // Load requirejs module for comm target
84- require ( [ content . target_module ] , function ( mod ) {
85- var target = mod [ content . target_name ] ;
86- if ( target !== undefined ) {
87- instantiate_comm ( target )
88- } else {
89- console . log ( "Comm target " + content . target_name +
90- " not found in module " + content . target_module ) ;
91- }
92- } , function ( err ) { console . log ( err ) ; } ) ;
93- } else {
94- // No requirejs module specified: look for target in registry
95- var f = this . targets [ content . target_name ] ;
96- if ( f === undefined ) {
97- console . log ( "No such target registered: " , content . target_name ) ;
98- console . log ( "Available targets are: " , this . targets ) ;
99- return ;
100- }
101- instantiate_comm ( f )
102- }
88+ return Promise . resolve ( comm ) ;
89+ } , utils . reject ( 'Could not open comm' , true ) ) ;
90+ return this . comms [ comm_id ] ;
10391 } ;
10492
105- CommManager . prototype . comm_close = function ( msg ) {
93+ CommManager . prototype . comm_close = function ( msg ) {
10694 var content = msg . content ;
107- var comm = this . comms [ content . comm_id ] ;
108- if ( comm === undefined ) {
95+ if ( this . comms [ content . comm_id ] === undefined ) {
96+ console . error ( 'Comm promise not found for comm id ' + content . comm_id ) ;
10997 return ;
11098 }
111- this . unregister_comm ( comm ) ;
112- try {
113- comm . handle_close ( msg ) ;
114- } catch ( e ) {
115- console . log ( "Exception closing comm: " , e , e . stack , msg ) ;
116- }
99+
100+ this . comms [ content . comm_id ] = this . comms [ content . comm_id ] . then ( function ( comm ) {
101+ this . unregister_comm ( comm ) ;
102+ try {
103+ comm . handle_close ( msg ) ;
104+ } catch ( e ) {
105+ console . log ( "Exception closing comm: " , e , e . stack , msg ) ;
106+ }
107+ } ) ;
117108 } ;
118109
119- CommManager . prototype . comm_msg = function ( msg ) {
110+ CommManager . prototype . comm_msg = function ( msg ) {
120111 var content = msg . content ;
121- var comm = this . comms [ content . comm_id ] ;
122- if ( comm === undefined ) {
112+ if ( this . comms [ content . comm_id ] === undefined ) {
113+ console . error ( 'Comm promise not found for comm id ' + content . comm_id ) ;
123114 return ;
124115 }
125- try {
126- comm . handle_msg ( msg ) ;
127- } catch ( e ) {
128- console . log ( "Exception handling comm msg: " , e , e . stack , msg ) ;
129- }
116+
117+ this . comms [ content . comm_id ] = this . comms [ content . comm_id ] . then ( function ( comm ) {
118+ try {
119+ comm . handle_msg ( msg ) ;
120+ } catch ( e ) {
121+ console . log ( "Exception handling comm msg: " , e , e . stack , msg ) ;
122+ }
123+ return Promise . resolve ( comm ) ;
124+ } ) ;
130125 } ;
131126
132127 //-----------------------------------------------------------------------
@@ -180,7 +175,7 @@ define([
180175
181176 // methods for handling incoming messages
182177
183- Comm . prototype . _maybe_callback = function ( key , msg ) {
178+ Comm . prototype . _callback = function ( key , msg ) {
184179 var callback = this [ '_' + key + '_callback' ] ;
185180 if ( callback ) {
186181 try {
@@ -192,11 +187,11 @@ define([
192187 } ;
193188
194189 Comm . prototype . handle_msg = function ( msg ) {
195- this . _maybe_callback ( 'msg' , msg ) ;
190+ this . _callback ( 'msg' , msg ) ;
196191 } ;
197192
198193 Comm . prototype . handle_close = function ( msg ) {
199- this . _maybe_callback ( 'close' , msg ) ;
194+ this . _callback ( 'close' , msg ) ;
200195 } ;
201196
202197 // For backwards compatability.
0 commit comments