atom.io 0.46.12 → 0.46.14

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,24 +1,21 @@
1
- import type { EventsMap, Socket, TypedSocket } from "atom.io/realtime"
2
- import type { Events } from "atom.io/realtime-server"
1
+ import type { Json } from "atom.io/json"
2
+ import type { EventsMap, GuardedSocket, Socket } from "atom.io/realtime"
3
3
 
4
4
  export function employSocket<I extends EventsMap, K extends string & keyof I>(
5
- socket: TypedSocket<I, any>,
5
+ socket: GuardedSocket<I>,
6
6
  event: K,
7
7
  handleEvent: (...data: Parameters<I[K]>) => void,
8
8
  ): () => void
9
- export function employSocket<I extends Events, K extends string & keyof I>(
9
+ export function employSocket(
10
10
  socket: Socket,
11
- event: K,
12
- handleEvent: (...data: I[K]) => void,
11
+ event: string,
12
+ handleEvent: (...data: Json.Serializable[]) => void,
13
13
  ): () => void
14
- export function employSocket<I extends Events, K extends string & keyof I>(
15
- socket: Socket | TypedSocket<any, any>,
16
- event: K,
17
- handleEvent: (...data: I[K]) => void,
14
+ export function employSocket(
15
+ socket: GuardedSocket<any> | Socket,
16
+ event: string,
17
+ handleEvent: (...data: Json.Serializable[]) => void,
18
18
  ): () => void {
19
19
  socket.on(event, handleEvent)
20
- const retireSocket = () => {
21
- socket.off(event, handleEvent)
22
- }
23
- return retireSocket
20
+ return socket.off.bind(socket, event, handleEvent)
24
21
  }
@@ -1,14 +1,14 @@
1
1
  import type { Loadable } from "atom.io"
2
2
  import type { Json } from "atom.io/json"
3
3
 
4
- import type { EventsMap, Socket, TypedSocket } from "./socket-interface"
4
+ import type { EventsMap, GuardedSocket, Socket } from "./socket-interface"
5
5
  import type { StandardSchemaV1 } from "./standard-schema"
6
6
 
7
- export type SocketListeners<T extends TypedSocket> =
8
- T extends TypedSocket<infer ListenEvents> ? ListenEvents : never
9
-
10
- export type SocketGuard<L extends EventsMap> = {
11
- [K in keyof L]: StandardSchemaV1<Json.Array, Parameters<L[K]>>
7
+ export type SocketGuard<ListenEvents extends EventsMap> = {
8
+ [K in keyof ListenEvents]: StandardSchemaV1<
9
+ Json.Array,
10
+ Parameters<ListenEvents[K]>
11
+ >
12
12
  }
13
13
 
14
14
  export type Loaded<L extends Loadable<any>> =
@@ -25,13 +25,13 @@ function onLoad<L extends Loadable<any>>(
25
25
  }
26
26
  }
27
27
 
28
- export function castSocket<T extends TypedSocket>(
28
+ export function guardSocket<ListenEvents extends EventsMap>(
29
29
  socket: Socket,
30
- guard: SocketGuard<SocketListeners<T>> | `TRUST`,
30
+ guard: SocketGuard<ListenEvents> | `TRUST`,
31
31
  logError?: (error: unknown) => void,
32
- ): T {
32
+ ): GuardedSocket<ListenEvents> {
33
33
  if (guard === `TRUST`) {
34
- return socket as T
34
+ return socket as GuardedSocket<ListenEvents>
35
35
  }
36
36
  const guardedSocket: Socket = {
37
37
  id: socket.id,
@@ -66,5 +66,5 @@ export function castSocket<T extends TypedSocket>(
66
66
  offAny: socket.offAny.bind(socket),
67
67
  emit: socket.emit.bind(socket),
68
68
  }
69
- return guardedSocket as T
69
+ return guardedSocket as GuardedSocket<ListenEvents>
70
70
  }
@@ -1,5 +1,5 @@
1
- export * from "./cast-socket"
2
1
  export * from "./employ-socket"
2
+ export * from "./guard-socket"
3
3
  export * from "./mutex-store"
4
4
  export * from "./realtime-continuity"
5
5
  export * from "./realtime-key-types"
@@ -87,7 +87,7 @@ export const mutualUsersSelector: ReadonlyPureSelectorFamilyToken<
87
87
  const users = get(usersOfRoomsAtoms, room)
88
88
  return [...users]
89
89
  }
90
- return []
90
+ return [userKey]
91
91
  },
92
92
  })
93
93
 
@@ -26,17 +26,30 @@ export type EventEmitter<EmitEvents extends EventsMap = EventsMap> = <
26
26
  ...args: Parameters<EmitEvents[E]>
27
27
  ) => void
28
28
 
29
- export type TypedSocket<
30
- ListenEvents extends EventsMap = EventsMap,
31
- EmitEvents extends EventsMap = never,
32
- > = {
29
+ export interface GuardedSocket<ListenEvents extends EventsMap> extends Socket {
33
30
  id: string | undefined
34
- on: ParticularEventListener<ListenEvents>
35
- onAny: (listener: AllEventsListener<ListenEvents>) => void
36
- onAnyOutgoing: (listener: AllEventsListener<EmitEvents>) => void
37
- off: ParticularEventListener<ListenEvents>
38
- offAny: (listener: AllEventsListener<ListenEvents>) => void
39
- emit: EventEmitter<EmitEvents>
31
+ on: <E extends string & keyof ListenEvents>(
32
+ event: E,
33
+ listener: ListenEvents[E],
34
+ ) => void
35
+ onAny: (
36
+ listener: <E extends string & keyof ListenEvents>(
37
+ event: E,
38
+ ...args: Parameters<ListenEvents[E]>
39
+ ) => void,
40
+ ) => void
41
+ onAnyOutgoing: (listener: AllEventsListener<EventsMap>) => void
42
+ off: <E extends string & keyof ListenEvents>(
43
+ event: E,
44
+ listener?: ListenEvents[E],
45
+ ) => void
46
+ offAny: (
47
+ listener?: <E extends string & keyof ListenEvents>(
48
+ event: E,
49
+ ...args: Parameters<ListenEvents[E]>
50
+ ) => void,
51
+ ) => void
52
+ emit: EventEmitter<EventsMap>
40
53
  }
41
54
 
42
55
  export type Socket = {
@@ -15,16 +15,16 @@ import type { Json } from "atom.io/json"
15
15
  import type {
16
16
  AllEventsListener,
17
17
  EventsMap,
18
+ GuardedSocket,
18
19
  RoomKey,
19
20
  RoomSocketInterface,
20
21
  Socket,
21
22
  SocketGuard,
22
23
  StandardSchemaV1,
23
- TypedSocket,
24
24
  UserKey,
25
25
  } from "atom.io/realtime"
26
26
  import {
27
- castSocket,
27
+ guardSocket,
28
28
  isRoomKey,
29
29
  ownersOfRooms,
30
30
  roomKeysAtom,
@@ -129,7 +129,7 @@ export function spawnRoom<RoomNames extends string>({
129
129
  export type ProvideEnterAndExitConfig = {
130
130
  store: RootStore
131
131
  socket: Socket
132
- roomSocket: TypedSocket<RoomSocketInterface<any>, any>
132
+ roomSocket: GuardedSocket<RoomSocketInterface<string>>
133
133
  userKey: UserKey
134
134
  }
135
135
  export function provideEnterAndExit({
@@ -283,9 +283,10 @@ export function provideRooms<RoomNames extends string>({
283
283
  roomNames,
284
284
  userKey,
285
285
  }: ProvideRoomsConfig<RoomNames>): () => void {
286
- const roomSocket = castSocket<
287
- TypedSocket<RoomSocketInterface<RoomNames>, never>
288
- >(socket, createRoomSocketGuard(roomNames))
286
+ const roomSocket = guardSocket<RoomSocketInterface<RoomNames>>(
287
+ socket,
288
+ createRoomSocketGuard(roomNames),
289
+ )
289
290
  const exposeMutable = realtimeMutableProvider({
290
291
  socket,
291
292
  store,
@@ -72,6 +72,13 @@ export function realtime(
72
72
  .use(async (socket, next) => {
73
73
  const result = await auth(socket.handshake)
74
74
  if (result instanceof Error) {
75
+ store.logger.error(
76
+ `📡`,
77
+ `socket`,
78
+ socket.id,
79
+ `failed to authenticate`,
80
+ result.message,
81
+ )
75
82
  next(result)
76
83
  return
77
84
  }