@@ -35,9 +35,18 @@ def get_operation(self, op_id):
3535
3636 def remove_operation (self , op_id ):
3737 try :
38- del self .operations [ op_id ]
38+ return self .operations . pop ( op_id )
3939 except KeyError :
40- pass
40+ return
41+
42+ def unsubscribe (self , op_id ):
43+ async_iterator = self .remove_operation (op_id )
44+ if hasattr (async_iterator , 'dispose' ):
45+ async_iterator .dispose ()
46+
47+ def unsubscribe_all (self ):
48+ for op_id in list (self .operations ):
49+ self .unsubscribe (op_id )
4150
4251 def receive (self ):
4352 raise NotImplementedError ("receive method not implemented" )
@@ -76,12 +85,6 @@ def process_message(self, connection_context, parsed_message):
7685
7786 elif op_type == GQL_START :
7887 assert isinstance (payload , dict ), "The payload must be a dict"
79-
80- # If we already have a subscription with this id, unsubscribe from
81- # it first
82- if connection_context .has_operation (op_id ):
83- self .unsubscribe (connection_context , op_id )
84-
8588 params = self .get_graphql_params (connection_context , payload )
8689 return self .on_start (connection_context , op_id , params )
8790
@@ -116,7 +119,10 @@ def on_open(self, connection_context):
116119 raise NotImplementedError ("on_open method not implemented" )
117120
118121 def on_stop (self , connection_context , op_id ):
119- raise NotImplementedError ("on_stop method not implemented" )
122+ return connection_context .unsubscribe (op_id )
123+
124+ def on_close (self , connection_context ):
125+ return connection_context .unsubscribe_all ()
120126
121127 def send_message (self , connection_context , op_id = None , op_type = None , payload = None ):
122128 message = self .build_message (op_id , op_type , payload )
@@ -171,11 +177,3 @@ def on_message(self, connection_context, message):
171177 return self .send_error (connection_context , None , e )
172178
173179 return self .process_message (connection_context , parsed_message )
174-
175- def unsubscribe (self , connection_context , op_id ):
176- if connection_context .has_operation (op_id ):
177- # Close async iterator
178- connection_context .get_operation (op_id ).dispose ()
179- # Close operation
180- connection_context .remove_operation (op_id )
181- return self .on_operation_complete (connection_context , op_id )
0 commit comments