atom.io 0.46.10 → 0.46.12

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.
@@ -1,52 +1,44 @@
1
- import * as AtomIO from "atom.io"
2
- import {
3
- type RoomKey,
4
- type SocketKey,
5
- type UserKey,
6
- usersInRooms,
7
- } from "atom.io/realtime"
1
+ import type { ReadonlyPureSelectorToken, RegularAtomToken } from "atom.io"
2
+ import { atom, getInternalRelations, selector } from "atom.io"
3
+ import type { RoomKey, SocketKey, UserKey } from "atom.io/realtime"
4
+ import { usersInRooms } from "atom.io/realtime"
8
5
 
9
- export const mySocketKeyAtom: AtomIO.RegularAtomToken<SocketKey | undefined> =
10
- AtomIO.atom<SocketKey | undefined>({
11
- key: `mySocketKey`,
12
- default: undefined,
13
- })
6
+ export const mySocketKeyAtom: RegularAtomToken<SocketKey | undefined> = atom({
7
+ key: `mySocketKey`,
8
+ default: undefined,
9
+ })
14
10
 
15
- export const myUserKeyAtom: AtomIO.RegularAtomToken<UserKey | null> =
16
- AtomIO.atom<UserKey | null>({
17
- key: `myUserKey`,
18
- default: null,
19
- effects: [
20
- (userKey) => {
21
- if (typeof window !== `undefined`) {
22
- void import(`atom.io/web`).then(({ storageSync }) => {
23
- storageSync(globalThis.localStorage, JSON, `myUserKey`)(userKey)
24
- })
25
- }
26
- },
27
- ],
28
- })
11
+ export const myUserKeyAtom: RegularAtomToken<UserKey | null> = atom({
12
+ key: `myUserKey`,
13
+ default: null,
14
+ effects: [
15
+ (userKey) => {
16
+ if (typeof window !== `undefined`) {
17
+ void import(`atom.io/web`).then(({ storageSync }) => {
18
+ storageSync(globalThis.localStorage, JSON, `myUserKey`)(userKey)
19
+ })
20
+ }
21
+ },
22
+ ],
23
+ })
29
24
 
30
- export const myRoomKeySelector: AtomIO.ReadonlyPureSelectorToken<RoomKey | null> =
31
- AtomIO.selector<RoomKey | null>({
25
+ export const myRoomKeySelector: ReadonlyPureSelectorToken<RoomKey | null> =
26
+ selector({
32
27
  key: `myRoomKey`,
33
28
  get: ({ get }) => {
34
29
  if (
35
- `env` in globalThis &&
36
- `REALTIME_ROOM_KEY` in (globalThis as any).env
30
+ `process` in globalThis &&
31
+ `env` in process &&
32
+ `REALTIME_ROOM_KEY` in process.env
37
33
  ) {
38
34
  // if a room running server-side wants its own key, this is where it lives
39
- return (globalThis as any).env[`REALTIME_ROOM_KEY`]
35
+ return process.env[`REALTIME_ROOM_KEY`] as RoomKey
40
36
  }
41
37
  const myUserKey = get(myUserKeyAtom)
42
38
  if (!myUserKey) return null
43
- const [, usersInRoomsAtoms] = AtomIO.getInternalRelations(
44
- usersInRooms,
45
- `split`,
46
- )
39
+ const [, usersInRoomsAtoms] = getInternalRelations(usersInRooms, `split`)
47
40
  const roomKeys = get(usersInRoomsAtoms, myUserKey)
48
- for (const roomKey of roomKeys) {
49
- return roomKey
50
- }
41
+ for (const roomKey of roomKeys) return roomKey
42
+ return null
51
43
  },
52
44
  })
@@ -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) => {