@@ -25,30 +25,30 @@ internal MessageHandle()
25
25
26
26
public class UserHandle : RemoteTableHandle < EventContext , User >
27
27
{
28
- private static Dictionary < SpacetimeDB . Identity , User > Identity_Index = new ( 16 ) ;
29
28
30
29
public override void InternalInvokeValueInserted ( IDatabaseRow row )
31
30
{
32
31
var value = ( User ) row ;
33
- Identity_Index [ value . Identity ] = value ;
32
+ Identity . Cache [ value . Identity ] = value ;
34
33
}
35
34
36
35
public override void InternalInvokeValueDeleted ( IDatabaseRow row )
37
36
{
38
- Identity_Index . Remove ( ( ( User ) row ) . Identity ) ;
37
+ Identity . Cache . Remove ( ( ( User ) row ) . Identity ) ;
39
38
}
40
39
41
- public readonly ref struct IdentityUniqueIndex
40
+ public class IdentityUniqueIndex
42
41
{
42
+ internal readonly Dictionary < SpacetimeDB . Identity , User > Cache = new ( 16 ) ;
43
43
public User ? Find ( SpacetimeDB . Identity value )
44
44
{
45
- Identity_Index . TryGetValue ( value , out var r ) ;
45
+ Cache . TryGetValue ( value , out var r ) ;
46
46
return r ;
47
47
}
48
48
49
49
}
50
50
51
- public IdentityUniqueIndex Identity => new ( ) ;
51
+ public IdentityUniqueIndex Identity = new ( ) ;
52
52
53
53
internal UserHandle ( )
54
54
{
@@ -65,6 +65,54 @@ public sealed class RemoteReducers : RemoteBase<DbConnection>
65
65
{
66
66
internal RemoteReducers ( DbConnection conn , SetReducerFlags SetReducerFlags ) : base ( conn ) { this . SetCallReducerFlags = SetReducerFlags ; }
67
67
internal readonly SetReducerFlags SetCallReducerFlags ;
68
+ public delegate void IdentityConnectedHandler ( EventContext ctx ) ;
69
+ public event IdentityConnectedHandler ? OnIdentityConnected ;
70
+
71
+ public void IdentityConnected ( )
72
+ {
73
+ conn . InternalCallReducer ( new IdentityConnected { } , this . SetCallReducerFlags . IdentityConnectedFlags ) ;
74
+ }
75
+
76
+ public bool InvokeIdentityConnected ( EventContext ctx , IdentityConnected args )
77
+ {
78
+ if ( OnIdentityConnected == null ) return false ;
79
+ OnIdentityConnected (
80
+ ctx
81
+ ) ;
82
+ return true ;
83
+ }
84
+ public delegate void IdentityDisconnectedHandler ( EventContext ctx ) ;
85
+ public event IdentityDisconnectedHandler ? OnIdentityDisconnected ;
86
+
87
+ public void IdentityDisconnected ( )
88
+ {
89
+ conn . InternalCallReducer ( new IdentityDisconnected { } , this . SetCallReducerFlags . IdentityDisconnectedFlags ) ;
90
+ }
91
+
92
+ public bool InvokeIdentityDisconnected ( EventContext ctx , IdentityDisconnected args )
93
+ {
94
+ if ( OnIdentityDisconnected == null ) return false ;
95
+ OnIdentityDisconnected (
96
+ ctx
97
+ ) ;
98
+ return true ;
99
+ }
100
+ public delegate void InitHandler ( EventContext ctx ) ;
101
+ public event InitHandler ? OnInit ;
102
+
103
+ public void Init ( )
104
+ {
105
+ conn . InternalCallReducer ( new Init { } , this . SetCallReducerFlags . InitFlags ) ;
106
+ }
107
+
108
+ public bool InvokeInit ( EventContext ctx , Init args )
109
+ {
110
+ if ( OnInit == null ) return false ;
111
+ OnInit (
112
+ ctx
113
+ ) ;
114
+ return true ;
115
+ }
68
116
public delegate void SendMessageHandler ( EventContext ctx , string text ) ;
69
117
public event SendMessageHandler ? OnSendMessage ;
70
118
@@ -104,6 +152,12 @@ public bool InvokeSetName(EventContext ctx, SetName args)
104
152
public sealed class SetReducerFlags
105
153
{
106
154
internal SetReducerFlags ( ) { }
155
+ internal CallReducerFlags IdentityConnectedFlags ;
156
+ public void IdentityConnected ( CallReducerFlags flags ) { this . IdentityConnectedFlags = flags ; }
157
+ internal CallReducerFlags IdentityDisconnectedFlags ;
158
+ public void IdentityDisconnected ( CallReducerFlags flags ) { this . IdentityDisconnectedFlags = flags ; }
159
+ internal CallReducerFlags InitFlags ;
160
+ public void Init ( CallReducerFlags flags ) { this . InitFlags = flags ; }
107
161
internal CallReducerFlags SendMessageFlags ;
108
162
public void SendMessage ( CallReducerFlags flags ) { this . SendMessageFlags = flags ; }
109
163
internal CallReducerFlags SetNameFlags ;
@@ -126,11 +180,12 @@ internal EventContext(DbConnection conn, Event<Reducer> reducerEvent) : base(con
126
180
127
181
[ Type ]
128
182
public partial record Reducer : TaggedEnum < (
183
+ IdentityConnected IdentityConnected ,
184
+ IdentityDisconnected IdentityDisconnected ,
185
+ Init Init ,
129
186
SendMessage SendMessage ,
130
187
SetName SetName ,
131
- Unit StdbNone ,
132
- Unit StdbIdentityConnected ,
133
- Unit StdbIdentityDisconnected
188
+ Unit StdbNone
134
189
) > ;
135
190
public class DbConnection : DbConnectionBase < DbConnection , Reducer >
136
191
{
@@ -147,17 +202,16 @@ public DbConnection()
147
202
clientDB . AddTable < User > ( "user" , Db . User ) ;
148
203
}
149
204
150
- protected override Reducer ToReducer ( TransactionUpdate update )
205
+ protected override Reducer ToReducer ( string reducerName , TransactionUpdate update )
151
206
{
152
207
var encodedArgs = update . ReducerCall . Args ;
153
- return update . ReducerCall . ReducerName switch
154
- {
208
+ return reducerName switch {
209
+ "__identity_connected__" => new Reducer . IdentityConnected ( BSATNHelpers . Decode < IdentityConnected > ( encodedArgs ) ) ,
210
+ "__identity_disconnected__" => new Reducer . IdentityDisconnected ( BSATNHelpers . Decode < IdentityDisconnected > ( encodedArgs ) ) ,
211
+ "__init__" => new Reducer . Init ( BSATNHelpers . Decode < Init > ( encodedArgs ) ) ,
155
212
"send_message" => new Reducer . SendMessage ( BSATNHelpers . Decode < SendMessage > ( encodedArgs ) ) ,
156
213
"set_name" => new Reducer . SetName ( BSATNHelpers . Decode < SetName > ( encodedArgs ) ) ,
157
214
"<none>" => new Reducer . StdbNone ( default ) ,
158
- "__identity_connected__" => new Reducer . StdbIdentityConnected ( default ) ,
159
- "__identity_disconnected__" => new Reducer . StdbIdentityDisconnected ( default ) ,
160
- "" => new Reducer . StdbNone ( default ) ,
161
215
var reducer => throw new ArgumentOutOfRangeException ( "Reducer" , $ "Unknown reducer { reducer } ")
162
216
} ;
163
217
}
@@ -168,13 +222,13 @@ protected override IEventContext ToEventContext(Event<Reducer> reducerEvent) =>
168
222
protected override bool Dispatch ( IEventContext context , Reducer reducer )
169
223
{
170
224
var eventContext = ( EventContext ) context ;
171
- return reducer switch
172
- {
225
+ return reducer switch {
226
+ Reducer . IdentityConnected ( var args ) => Reducers . InvokeIdentityConnected ( eventContext , args ) ,
227
+ Reducer . IdentityDisconnected ( var args ) => Reducers . InvokeIdentityDisconnected ( eventContext , args ) ,
228
+ Reducer . Init ( var args ) => Reducers . InvokeInit ( eventContext , args ) ,
173
229
Reducer . SendMessage ( var args ) => Reducers . InvokeSendMessage ( eventContext , args ) ,
174
230
Reducer . SetName ( var args ) => Reducers . InvokeSetName ( eventContext , args ) ,
175
- Reducer . StdbNone or
176
- Reducer . StdbIdentityConnected or
177
- Reducer . StdbIdentityDisconnected => true ,
231
+ Reducer . StdbNone => true ,
178
232
_ => throw new ArgumentOutOfRangeException ( "Reducer" , $ "Unknown reducer { reducer } ")
179
233
} ;
180
234
}
0 commit comments