atom.io 0.46.9 → 0.46.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,56 +3,59 @@ import { findInStore, getInternalRelationsFromStore } from "atom.io/internal"
3
3
  import { StoreContext, useO } from "atom.io/react"
4
4
  import type { RoomKey, RoomSocketInterface, UserKey } from "atom.io/realtime"
5
5
  import { ownersOfRooms, roomKeysAtom, usersInRooms } from "atom.io/realtime"
6
+ import { myUserKeyAtom } from "atom.io/realtime-client"
6
7
  import type { UList } from "atom.io/transceivers/u-list"
7
8
  import * as React from "react"
8
9
  import type { Socket } from "socket.io-client"
9
10
 
10
11
  import { RealtimeContext } from "./realtime-context"
12
+ import { usePullAtom } from "./use-pull-atom"
11
13
  import { usePullMutable } from "./use-pull-mutable-atom"
12
14
  import { usePullMutableAtomFamilyMember } from "./use-pull-mutable-family-member"
13
15
 
14
16
  export type RealtimeRoomsTools = {
15
- socket: Socket<{}, RoomSocketInterface<string>>
17
+ roomSocket: Socket<{}, RoomSocketInterface<string>>
16
18
  myRoomKey: RoomKey | undefined
17
19
  myMutualsAtom: MutableAtomToken<UList<UserKey>>
18
20
  myOwnedRoomsAtom: MutableAtomToken<UList<RoomKey>>
19
21
  allRoomKeysAtom: MutableAtomToken<UList<RoomKey>>
20
22
  }
21
- export function useRealtimeRooms<RoomNames extends string>(
22
- userKey: UserKey,
23
- ): RealtimeRoomsTools {
23
+ export function useRealtimeRooms<
24
+ RoomNames extends string,
25
+ >(): RealtimeRoomsTools {
24
26
  const store = React.useContext(StoreContext)
25
27
  const { socket } = React.useContext(RealtimeContext)
26
28
  usePullMutable(roomKeysAtom)
29
+ const userKey = usePullAtom(myUserKeyAtom) ?? `user::$_NONE_$`
27
30
 
28
31
  const [userKeysFamily, roomKeysFamily] = getInternalRelationsFromStore(
29
32
  store,
30
33
  usersInRooms,
31
34
  `split`,
32
35
  )
33
-
34
36
  usePullMutableAtomFamilyMember(roomKeysFamily, userKey)
37
+
35
38
  const myJoinedRoomKeys = useO(roomKeysFamily, userKey)
36
39
  let myRoomKey: RoomKey | undefined
37
40
  for (const roomKey of myJoinedRoomKeys) {
38
41
  myRoomKey = roomKey
39
42
  break
40
43
  }
41
-
42
44
  const roomKey = myRoomKey ?? `room::$_NONE_$`
43
45
  const myMutualsAtom = findInStore(store, userKeysFamily, roomKey)
44
46
  usePullMutableAtomFamilyMember(userKeysFamily, roomKey)
45
47
 
46
- const [ownedRoomsFamily] = getInternalRelationsFromStore(
48
+ const [ownedRoomsFamily, roomOwnersFamily] = getInternalRelationsFromStore(
47
49
  store,
48
50
  ownersOfRooms,
49
51
  `split`,
50
52
  )
51
53
  const myOwnedRoomsAtom = findInStore(store, ownedRoomsFamily, userKey)
52
54
  usePullMutableAtomFamilyMember(ownedRoomsFamily, userKey)
55
+ usePullMutableAtomFamilyMember(roomOwnersFamily, roomKey)
53
56
 
54
57
  return {
55
- socket: socket as Socket<{}, RoomSocketInterface<RoomNames>>,
58
+ roomSocket: socket as Socket<{}, RoomSocketInterface<RoomNames>>,
56
59
  myRoomKey,
57
60
  allRoomKeysAtom: roomKeysAtom,
58
61
  myMutualsAtom,
@@ -19,7 +19,7 @@ export class SubjectSocket<
19
19
  public in: Subject<EventPayload<I>>
20
20
  public out: Subject<EventPayload<O>>
21
21
  public id = `no_id_retrieved`
22
- public disposalFunctions: (() => void)[] = []
22
+ public disposalEffects: (() => void)[] = []
23
23
 
24
24
  public constructor(id: string) {
25
25
  super((...args) => {
@@ -35,7 +35,7 @@ export class SubjectSocket<
35
35
  }
36
36
 
37
37
  public dispose(): void {
38
- for (const dispose of this.disposalFunctions) {
38
+ for (const dispose of this.disposalEffects) {
39
39
  dispose()
40
40
  }
41
41
  }
@@ -179,6 +179,19 @@ export class ParentSocket<
179
179
  this.logger.info(`👤`, userKey, `joined`)
180
180
  const existingRelay = this.relays.get(userKey)
181
181
  if (existingRelay) {
182
+ this.logger.info(`🔗`, `reattaching relay services for`, userKey)
183
+ const cleanupRelay = this.initRelay(existingRelay, userKey)
184
+ if (cleanupRelay) {
185
+ existingRelay.disposalEffects.push(cleanupRelay)
186
+ }
187
+ this.on(userKey, (...data) => {
188
+ relay.in.next(data)
189
+ })
190
+ existingRelay.disposalEffects.push(
191
+ existingRelay.out.subscribe(`socket`, (data) => {
192
+ this.emit(userKey, ...(data as any))
193
+ }),
194
+ )
182
195
  return
183
196
  }
184
197
  const relay = new SubjectSocket(userKey)
@@ -186,12 +199,12 @@ export class ParentSocket<
186
199
  this.logger.info(`🔗`, `attaching relay services for`, userKey)
187
200
  const cleanupRelay = this.initRelay(relay, userKey)
188
201
  if (cleanupRelay) {
189
- relay.disposalFunctions.push(cleanupRelay)
202
+ relay.disposalEffects.push(cleanupRelay)
190
203
  }
191
204
  this.on(userKey, (...data) => {
192
205
  relay.in.next(data)
193
206
  })
194
- relay.disposalFunctions.push(
207
+ relay.disposalEffects.push(
195
208
  relay.out.subscribe(`socket`, (data) => {
196
209
  this.emit(userKey, ...(data as any))
197
210
  }),
@@ -191,10 +191,17 @@ export function provideEnterAndExit({
191
191
  roomSocket.on(`joinRoom`, enterRoom)
192
192
  }
193
193
 
194
- editRelationsInStore(store, usersInRooms, (relations) => {
195
- relations.set({ room: roomKey, user: userKey })
196
- })
194
+ const userIsAlreadyInRoom = getFromStore(
195
+ store,
196
+ getInternalRelationsFromStore(store, usersInRooms),
197
+ roomKey,
198
+ ).has(userKey as any)
197
199
 
200
+ if (!userIsAlreadyInRoom) {
201
+ editRelationsInStore(store, usersInRooms, (relations) => {
202
+ relations.set({ room: roomKey, user: userKey })
203
+ })
204
+ }
198
205
  childSocket.emit(`user-joins`, userKey)
199
206
 
200
207
  toRoom = (payload) => {