Last active
March 31, 2017 14:57
-
-
Save Maarten88/526e4a4c86face737b3d0551a13f6da6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CounterGrain : ReduxGrain<CounterState>, ICounterGrain | |
{ | |
IDisposable storeSubscription; | |
IAsyncStream<IAction> actionsToClientStream; | |
public CounterGrain(ReduxTableStorage<CounterState> storage) : base(CounterState.Reducer, storage) | |
{ | |
} | |
public override async Task OnActivateAsync() | |
{ | |
// Do this first, it initializes the Store! | |
await base.OnActivateAsync(); | |
// Get stream to client with corresponding session id | |
var streamProvider = this.GetStreamProvider("Default"); | |
this.actionsToClientStream = streamProvider.GetStream<IAction>(this.GetPrimaryKey(), "ActionsToClient"); | |
// Subscribe to state updates as they happen on the server, and publish them using the SyncCounterState action | |
this.storeSubscription = this.Store.Subscribe( | |
async (CounterState state) => { | |
if (state != null) | |
await this.actionsToClientStream.OnNextAsync(new SyncCounterStateAction { CounterState = state }); | |
}); | |
} | |
public override async Task OnDeactivateAsync() | |
{ | |
// clean up when grain goes away | |
this.storeSubscription.Dispose(); | |
await base.OnDeactivateAsync(); | |
} | |
public async Task IncrementCounter() | |
{ | |
await this.Dispatch(new IncrementCounterAction()); | |
await this.WriteStateAsync(); | |
} | |
// [ ... other methods ] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment