@@ -128,13 +128,16 @@ async def subscribe(
128128 durable : Optional [str ] = None ,
129129 stream : Optional [str ] = None ,
130130 config : Optional [api .ConsumerConfig ] = None ,
131- manual_ack : Optional [ bool ] = False ,
132- ordered_consumer : Optional [ bool ] = False ,
131+ manual_ack : bool = False ,
132+ ordered_consumer : bool = False ,
133133 idle_heartbeat : Optional [float ] = None ,
134- flow_control : Optional [ bool ] = False ,
134+ flow_control : bool = False ,
135135 ) -> Subscription :
136- """
137- subscribe returns a `Subscription` that is bound to a push based consumer.
136+ """Create consumer if needed and push-subscribe to it.
137+
138+ 1. Check if consumer exists.
139+ 2. Creates consumer if needed.
140+ 3. Calls `subscribe_bind`.
138141
139142 :param subject: Subject from a stream from JetStream.
140143 :param queue: Deliver group name from a set a of queue subscribers.
@@ -271,17 +274,36 @@ async def cb(msg):
271274 consumer_info = await self ._jsm .add_consumer (stream , config = config )
272275 consumer = consumer_info .name
273276
277+ if consumer is None :
278+ raise TypeError ("cannot detect consumer" )
279+ if config is None :
280+ raise TypeError ("config is required for existing durable consumer" )
281+ return await self .subscribe_bind (
282+ cb = cb ,
283+ stream = stream ,
284+ config = config ,
285+ manual_ack = manual_ack ,
286+ ordered_consumer = ordered_consumer ,
287+ consumer = consumer ,
288+ )
289+
290+ async def subscribe_bind (
291+ self ,
292+ stream : str ,
293+ config : api .ConsumerConfig ,
294+ consumer : str ,
295+ cb : Optional [Callback ] = None ,
296+ manual_ack : bool = False ,
297+ ordered_consumer : bool = False ,
298+ ) -> Subscription :
299+ """Push-subscribe to an existing consumer.
300+ """
274301 # By default, async subscribers wrap the original callback and
275302 # auto ack the messages as they are delivered.
276303 if cb and not manual_ack :
277304 cb = self ._auto_ack_callback (cb )
278-
279- # TODO (@orsinium): too many assumptions, refactor the code above to ensure they hold true.
280- assert config is not None
281305 if config .deliver_subject is None :
282- raise TypeError ("config.deliver_subject is required" )
283- if consumer is None :
284- raise TypeError ("cannot detect consumer" )
306+ config .deliver_subject = self ._nc .new_inbox ()
285307 sub = await self ._nc .subscribe (
286308 subject = config .deliver_subject ,
287309 queue = config .deliver_group or "" ,
0 commit comments