Skip to content

Instantly share code, notes, and snippets.

@JozefFlakus
Created February 27, 2019 19:29
Show Gist options
  • Save JozefFlakus/c4eacdce42ca0f3a1dc480080f518e74 to your computer and use it in GitHub Desktop.
Save JozefFlakus/c4eacdce42ca0f3a1dc480080f518e74 to your computer and use it in GitHub Desktop.
import { use, matchEvent } from '@marblejs/core';
import { WsEffect } from '@marblejs/websockets';
import { t, eventValidator$ } from '@marblejs/middleware-io';
export const sum$: WsEffect = event$ =>
event$.pipe(
matchEvent('SUM')
);
export const add$: WsEffect = (event$, ...args) =>
event$.pipe(
matchEvent('ADD'),
use(eventValidator$(t.number)),
buffer(sum$(event$, ...args)),
map(addEvents => addEvents.reduce((a, e) => e.payload + a, 0)),
map(payload => ({ type: 'SUM_RESULT', payload })),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment