@pluv/io 0.17.3 → 0.18.0

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.
package/src/types.ts CHANGED
@@ -1,16 +1,15 @@
1
1
  import type { AbstractCrdtDoc } from "@pluv/crdt";
2
2
  import type {
3
+ BaseIOAuthorize,
3
4
  EventRecord,
5
+ IOAuthorize,
4
6
  Id,
5
- InputZodLike,
7
+ InferIOAuthorizeUser,
6
8
  JsonObject,
9
+ Maybe,
7
10
  MaybePromise,
8
11
  } from "@pluv/types";
9
- import {
10
- AbstractPlatform,
11
- InferPlatformEventContextType,
12
- InferPlatformRoomContextType,
13
- } from "./AbstractPlatform";
12
+ import type { AbstractPlatform, InferPlatformRoomContextType } from "./AbstractPlatform";
14
13
  import type { AbstractWebSocket } from "./AbstractWebSocket";
15
14
 
16
15
  declare global {
@@ -19,54 +18,17 @@ declare global {
19
18
  };
20
19
  }
21
20
 
22
- export interface EventConfig<
23
- TPlatform extends AbstractPlatform = AbstractPlatform,
24
- TContext extends Record<string, any> = {},
25
- TData extends JsonObject = {},
26
- TResultBroadcast extends EventRecord<string, any> = {},
27
- TResultSelf extends EventRecord<string, any> = {},
28
- TResultSync extends EventRecord<string, any> = {},
29
- > {
30
- input?: InputZodLike<TData>;
31
- resolver:
32
- | EventResolver<
33
- TContext &
34
- InferPlatformRoomContextType<TPlatform> &
35
- InferPlatformEventContextType<TPlatform>,
36
- TData,
37
- TResultBroadcast
38
- >
39
- | EventResolverObject<
40
- TPlatform,
41
- TContext,
42
- TData,
43
- TResultBroadcast,
44
- TResultSelf,
45
- TResultSync
46
- >;
47
- }
48
-
49
- export type EventConfigType = "broadcast" | "self" | "sync";
50
-
51
- export type SyncEventResolver<
52
- TContext extends Record<string, any> = {},
53
- TData extends JsonObject = {},
54
- TResultBroadcast extends EventRecord<string, any> = {},
55
- > = (
56
- data: TData,
57
- context: EventResolverContext<TContext>,
58
- ) => MaybePromise<TResultBroadcast | void>;
59
-
60
21
  export type EventResolver<
22
+ TPlatform extends AbstractPlatform = AbstractPlatform,
23
+ TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize,
61
24
  TContext extends Record<string, any> = {},
62
- TData extends JsonObject = {},
63
- TResultBroadcast extends EventRecord<string, any> = {},
64
- > = (
65
- data: TData,
66
- context: EventResolverContext<TContext>,
67
- ) => MaybePromise<TResultBroadcast | void>;
25
+ TInput extends JsonObject = {},
26
+ TOutput extends EventRecord<string, any> = {},
27
+ > = (data: TInput, context: EventResolverContext<TPlatform, TAuthorize, TContext>) => MaybePromise<TOutput | void>;
68
28
 
69
29
  export interface EventResolverContext<
30
+ TPlatform extends AbstractPlatform = AbstractPlatform,
31
+ TAuthorize extends IOAuthorize<any, any, InferPlatformRoomContextType<TPlatform>> = BaseIOAuthorize,
70
32
  TContext extends Record<string, any> = {},
71
33
  > {
72
34
  context: TContext;
@@ -84,57 +46,10 @@ export interface EventResolverContext<
84
46
  * @author leedavidcs
85
47
  * @date December 25, 2022
86
48
  */
87
- session: WebSocketSession | null;
88
- sessions: Map<string, WebSocketSession>;
49
+ session: WebSocketSession<TAuthorize> | null;
50
+ sessions: Map<string, WebSocketSession<TAuthorize>>;
89
51
  }
90
52
 
91
- export type EventResolverObject<
92
- TPlatform extends AbstractPlatform = AbstractPlatform,
93
- TContext extends Record<string, any> = {},
94
- TData extends JsonObject = {},
95
- TResultBroadcast extends EventRecord<string, any> = {},
96
- TResultSelf extends EventRecord<string, any> = {},
97
- TResultSync extends EventRecord<string, any> = {},
98
- > = {
99
- broadcast?: EventResolver<
100
- TContext &
101
- InferPlatformRoomContextType<TPlatform> &
102
- InferPlatformEventContextType<TPlatform>,
103
- TData,
104
- TResultBroadcast
105
- >;
106
- self?: EventResolver<
107
- TContext &
108
- InferPlatformRoomContextType<TPlatform> &
109
- InferPlatformEventContextType<TPlatform>,
110
- TData,
111
- TResultSelf
112
- >;
113
- sync?: SyncEventResolver<TContext, TData, TResultSync>;
114
- };
115
-
116
- export type InferEventConfig<
117
- TPlatform extends AbstractPlatform = AbstractPlatform,
118
- TContext extends Record<string, any> = {},
119
- TInput extends EventRecord<string, any> = {},
120
- TOutputBroadcast extends EventRecord<string, any> = {},
121
- TOutputSelf extends EventRecord<string, any> = {},
122
- TOutputSync extends EventRecord<string, any> = {},
123
- > = {
124
- [P in keyof TInput]: P extends string
125
- ? Id<
126
- EventConfig<
127
- TPlatform,
128
- TContext,
129
- TInput[P],
130
- TOutputBroadcast,
131
- TOutputSelf,
132
- TOutputSync
133
- >
134
- >
135
- : never;
136
- };
137
-
138
53
  export type SendMessageOptions =
139
54
  | { type?: "broadcast"; sessionIds?: readonly string[] }
140
55
  | { type: "self" }
@@ -144,12 +59,45 @@ export interface WebSocketSessionTimers {
144
59
  ping: number;
145
60
  }
146
61
 
147
- export interface WebSocketSession {
62
+ export interface WebSocketSession<TAuthorize extends IOAuthorize<any, any, any> = BaseIOAuthorize> {
148
63
  id: string;
149
64
  presence: JsonObject | null;
150
65
  quit: boolean;
151
66
  room: string;
152
67
  timers: WebSocketSessionTimers;
153
68
  webSocket: AbstractWebSocket;
154
- user: JsonObject | null;
69
+ user: InferIOAuthorizeUser<TAuthorize>;
70
+ }
71
+
72
+ export type MergeEventRecords<
73
+ TEventRecords extends EventRecord<string, any>[],
74
+ TRoot extends EventRecord<string, any> = {},
75
+ > = TEventRecords extends [
76
+ infer IHead extends EventRecord<string, any>,
77
+ ...infer ITail extends EventRecord<string, any>[],
78
+ ]
79
+ ? MergeEventRecords<
80
+ ITail,
81
+ Omit<TRoot, keyof IHead> & {
82
+ [P in keyof IHead]: TRoot extends Record<P, any> ? TRoot[P] | IHead[P] : IHead[P];
83
+ }
84
+ >
85
+ : Id<TRoot>;
86
+
87
+ type GetInitialStorageEvent<TPlatform extends AbstractPlatform> = {
88
+ room: string;
89
+ } & InferPlatformRoomContextType<TPlatform>;
90
+
91
+ export type GetInitialStorageFn<TPlatform extends AbstractPlatform> = (
92
+ event: GetInitialStorageEvent<TPlatform>,
93
+ ) => MaybePromise<Maybe<string>>;
94
+
95
+ export interface PluvIOListeners<TPlatform extends AbstractPlatform> {
96
+ onRoomDeleted: (event: IORoomListenerEvent<TPlatform>) => void;
97
+ onStorageUpdated: (event: IORoomListenerEvent<TPlatform>) => void;
155
98
  }
99
+
100
+ export type IORoomListenerEvent<TPlatform extends AbstractPlatform> = {
101
+ room: string;
102
+ encodedState: string;
103
+ } & InferPlatformRoomContextType<TPlatform>;
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '0.17.3'
2
+ export const version = '0.18.0'