@@ -225,6 +225,46 @@ func (m *MultiStateMachineManager) SendEvent(ctx context.Context,
225
225
return nil
226
226
}
227
227
228
+ // SendEventSync sends an event to the state machine and waits for it to be
229
+ // processed and written to disk. This method provides synchronous confirmation
230
+ // that the event has been durably persisted. If the event doesn't support
231
+ // synchronous processing (i.e., it's not a SupplyUpdateEvent), this method will
232
+ // return an error.
233
+ func (m * MultiStateMachineManager ) SendEventSync (ctx context.Context ,
234
+ assetSpec asset.Specifier , event SyncSupplyUpdateEvent ) error {
235
+
236
+ // Only SupplyUpdateEvents can be processed synchronously.
237
+ supplyEvent , ok := event .(SupplyUpdateEvent )
238
+ if ! ok {
239
+ return fmt .Errorf ("event type %T does not support " +
240
+ "synchronous processing" , event )
241
+ }
242
+
243
+ // We'll use this channel to signal when the event has been processed.
244
+ done := make (chan error , 1 )
245
+
246
+ // As the event has already been created, we'll set the done channel
247
+ // directly.
248
+ switch e := supplyEvent .(type ) {
249
+ case * NewMintEvent :
250
+ e .Done = done
251
+ case * NewBurnEvent :
252
+ e .Done = done
253
+ case * NewIgnoreEvent :
254
+ e .Done = done
255
+ default :
256
+ return fmt .Errorf ("unexpected supply update event type: %T" ,
257
+ supplyEvent )
258
+ }
259
+
260
+ // Send the event to the state machine, the pause and wait until it
261
+ // signals that the event has been fully processed.
262
+ if err := m .SendEvent (ctx , assetSpec , supplyEvent ); err != nil {
263
+ return err
264
+ }
265
+ return event .WaitForDone (ctx )
266
+ }
267
+
228
268
// CanHandle determines if the state machine associated with the given asset
229
269
// specifier can handle the given message. If a state machine for the asset
230
270
// group does not exist, it will be created and started.
0 commit comments