66from ..client import Client
77from ..client .protocol import Proto
88from ..util .is_name import is_name
9+ from ..util .cnscope import cnscope
10+ from ..util .fcscope import fcscope
911if TYPE_CHECKING :
1012 from ..client .package import Package
1113
@@ -47,11 +49,13 @@ def __init__(
4749 """
4850 self ._client : Client | None = None
4951 self ._id = room
50- self ._scope = scope
52+ self ._key = None
53+ self ._scope = \
54+ None if scope is None else f'@collection:{ cnscope (scope )} '
5155 self ._wait_join : bool | asyncio .Future [None ] | None = False
5256
5357 @property
54- def id (self ):
58+ def id (self ) -> int | None :
5559 return self ._id if isinstance (self ._id , int ) else None
5660
5761 @property
@@ -72,7 +76,8 @@ async def no_join(self, client: Client):
7276 """
7377 async with client ._rooms_lock :
7478 if self ._scope is None :
75- self ._scope = client .get_default_scope ()
79+ scope = client .get_default_scope ()
80+ self ._scope = f'@collection:{ cnscope (scope )} '
7681 self ._client = client
7782
7883 if isinstance (self ._id , str ):
@@ -96,6 +101,7 @@ async def no_join(self, client: Client):
96101 '!is_err(try(room(id)));' , id = id , scope = self ._scope )
97102 if not is_room :
98103 raise TypeError (f'Id `{ id } ` is not a room' )
104+ assert isinstance (id , int )
99105 self ._id = id
100106
101107 async def join (self , client : Client , wait : float | None = 60.0 ):
@@ -116,7 +122,9 @@ async def join(self, client: Client, wait: float | None = 60.0):
116122 # is created inside the dict *before* the on_join is called.
117123 async with client ._rooms_lock :
118124 if self ._scope is None :
119- self ._scope = client .get_default_scope ()
125+ scope = client .get_default_scope ()
126+ self ._scope = f'@collection:{ cnscope (scope )} '
127+
120128 self ._client = client
121129
122130 if isinstance (self ._id , str ):
@@ -141,19 +149,23 @@ async def join(self, client: Client, wait: float | None = 60.0):
141149 f'the room Id has been returned using the ThingsDB '
142150 f'code `{ code } ` using scope `{ self ._scope } `' )
143151 self ._id = id
152+ assert isinstance (self ._id , int )
144153 else :
145154 assert isinstance (self ._id , int )
146155 res = await client ._join (self ._id , scope = self ._scope )
147156 if res [0 ] is None :
148157 raise LookupError (f'room with Id { self ._id } not found' )
149158
150- if self ._id in client ._rooms :
151- prev = client ._rooms [self ._id ]
159+ self ._scope = fcscope (self ._scope )
160+ try :
161+ prev = client ._rooms [self ._scope ][self ._id ]
152162 logging .warning (
153- f'Room Id { self ._id } is previously registered by { prev } '
163+ f'Room Id { self ._key } is previously registered by { prev } '
154164 f'and will be overwritten with { self } ' )
165+ except KeyError :
166+ pass
155167
156- client ._rooms [self ._id ] = self
168+ client ._rooms [self ._scope ][ self . _id ] = self
157169 self .on_init ()
158170 if wait :
159171 self ._wait_join = asyncio .Future ()
@@ -255,12 +267,16 @@ def _on_join(self, _data: Any) -> asyncio.Task[None] | None:
255267 asyncio .ensure_future (self .on_join (), loop = loop )
256268
257269 def _on_stop (self , func : Callable [[], None ]):
258- try :
259- assert self ._client
260- if isinstance (self ._id , int ):
261- del self ._client ._rooms [self ._id ]
262- except KeyError :
263- pass
270+ assert self ._client
271+ if isinstance (self ._id , int ) and isinstance (self ._scope , str ):
272+ rooms = self ._client ._rooms .get (self ._scope )
273+ if rooms is not None :
274+ try :
275+ del rooms [self ._id ]
276+ except KeyError :
277+ pass
278+ if not rooms :
279+ del self ._client ._rooms [self ._scope ]
264280 func ()
265281
266282 def _emit_handler (self , data : _TEvent ):
0 commit comments