@@ -11,22 +11,42 @@ function start_process(module, args){
1111 let [ ok , state ] = module . init . apply ( null , [ args ] ) ;
1212 yield self . scheduler . put ( "state" , state ) ;
1313
14- while ( true ) {
15- yield self . scheduler . receive ( function ( args ) {
16- if ( args [ 0 ] === "call" ) {
17- let result = module . handle_call ( args [ 1 ] , args [ 2 ] , self . scheduler . get ( "state" ) ) ;
18- self . scheduler . put ( "state" , result [ 2 ] ) ;
14+ try {
15+ while ( true ) {
16+ yield self . scheduler . receive ( function ( args ) {
17+ let command = args [ 0 ] ;
1918
20- self . scheduler . send ( args [ 2 ] , result [ 1 ] ) ;
19+ switch ( command ) {
20+ case "call" :
21+ var request = args [ 1 ] ;
22+ var sender = args [ 2 ] ;
2123
22- } else if ( args [ 0 ] === "cast" ) {
23- let result = module . handle_cast ( args [ 1 ] , self . scheduler . get ( "state" ) ) ;
24- self . scheduler . put ( "state" , result [ 1 ] ) ;
24+ var [ reply , response , new_state ] = module . handle_call ( request , sender , self . scheduler . get ( "state" ) ) ;
25+ self . scheduler . put ( "state" , new_state ) ;
2526
26- self . scheduler . send ( args [ 2 ] , Symbol . for ( "ok" ) ) ;
27+ self . scheduler . send ( sender , response ) ;
28+ break ;
2729
28- }
29- } ) ;
30+ case "cast" :
31+ var request = args [ 1 ] ;
32+ var sender = args [ 2 ] ;
33+
34+ var [ reply , new_state ] = module . handle_cast ( request , self . scheduler . get ( "state" ) ) ;
35+
36+ self . scheduler . put ( "state" , new_state ) ;
37+ self . scheduler . send ( args [ 2 ] , Symbol . for ( "ok" ) ) ;
38+
39+ break ;
40+
41+ case "stop" :
42+ throw "stop" ;
43+ }
44+ } ) ;
45+ }
46+ } catch ( e ) {
47+ if ( e !== "stop" ) {
48+ throw e ;
49+ }
3050 }
3151 }
3252}
@@ -47,4 +67,8 @@ function* cast(server, request){
4767 } ) ;
4868}
4969
50- export default { start, start_link, call, cast } ;
70+ function stop ( server , request ) {
71+ self . scheduler . send ( server , [ "stop" ] ) ;
72+ }
73+
74+ export default { start, start_link, call, cast, stop } ;
0 commit comments