Skip to content

Instantly share code, notes, and snippets.

View Odonno's full-sized avatar
🏠
Working from home

David Bottiau Odonno

🏠
Working from home
View GitHub Profile
@Odonno
Odonno / hooks.context.ts
Created June 1, 2020 09:30
State management comparison - pure React hooks
export type StoreContextState = {
todos: Todo[];
loadTodos: () => Promise<void>;
createTodo: (content: string) => Promise<void>;
changeContent: (todo: Todo, content: string) => void;
updateTodo: (id: number, content: string) => Promise<void>;
deleteTodo: (id: number) => Promise<void>;
};
const StoreContext = createContext<StoreContextState>({} as StoreContextState);
@Odonno
Odonno / ServerToClient.ToBufferedStream.cs
Created May 4, 2020 19:05
ServerToClient.ToBufferedStream.cs
public ChannelReader<Weather> RealtimeWeather()
{
return _realtimeValuesService.Observe()
.ToBufferedStream(Context.ConnectionAborted);
}
@Odonno
Odonno / ServerToClient.ToNewestValueStream.cs
Created May 4, 2020 19:04
ServerToClient.ToNewestValueStream.cs
public ChannelReader<Weather> RealtimeWeather()
{
return _realtimeValuesService.Observe()
.ToNewestValueStream(Context.ConnectionAborted);
}
@Odonno
Odonno / fromStream.ts
Created May 4, 2020 19:04
fromStream.ts
// listen to realtime event
fromStream<number>(connection, "realtimeWeather")
.subscribe({
next: value => setWeather(value),
error: err => displayUserError(err),
complete: () => console.log('No more weather?')
});
@Odonno
Odonno / sendStream.ts
Created May 4, 2020 19:03
sendStream.ts
const count$ = combineLatest(increment$, decrement$).pipe(
scan((acc, curr) => acc + curr, 0),
startWith(0)
); // basic counter with increment and decrement features
// send realtime event
sendStream("setCount", count$);
@Odonno
Odonno / ClientToServer.cs
Created May 4, 2020 19:03
ClientToServer.cs
// Read the client-to-server stream
private async Task SetCount(IAsyncEnumerable<int> stream)
{
await foreach (int item in stream.WithCancellation(Context.ConnectionAborted))
{
Console.WriteLine(item);
}
}
@Odonno
Odonno / ServerToClient.Basic.cs
Created May 4, 2020 19:02
ServerToClient.Basic.cs
// Basic implementation of server-to-client
public ChannelReader<Weather> RealtimeWeather()
{
var channel = Channel.CreateUnbounded<Weather>();
_ = WriteItemsAsync(channel.Writer, count, delay);
return channel.Reader;
}
@Odonno
Odonno / connectionState.ts
Created July 27, 2019 15:55
ngrx/signalr - connectionState$
const offline$ = fromEvent(window, 'offline').pipe(map(_ => false));
const online$ = fromEvent(window, 'online').pipe(map(_ => true));
const hasInternetConnection$ = merge(
of(navigator.onLine),
offline$,
online$
);
const hubsStatuses$ = this.store.pipe(
select(selectHubsStatuses)
@Odonno
Odonno / state.definition.ts
Created July 27, 2019 15:54
ngrx/signalr - root state definition
class BaseSignalRStoreState {
hubStatuses: SignalRHubStatus[];
}
@Odonno
Odonno / state.definition.ts
Created July 27, 2019 15:53
ngrx/signalr - part state definition
type SignalRHubState =
| typeof unstarted
| typeof connected
| typeof disconnected;
type SignalRHubStatus = {
hubName: string;
url: string;
state: SignalRHubState;
};