Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Last active April 19, 2023 17:38
Show Gist options
  • Save cowboyd/384b456e7093462ac0b1c7b33d6213d7 to your computer and use it in GitHub Desktop.
Save cowboyd/384b456e7093462ac0b1c7b33d6213d7 to your computer and use it in GitHub Desktop.
Piping, buffering, filtering, mapping, and general hypothetical combinations API.
import { buffer, on, flow, filter } from "effection";
let results = flow(
on('message'),
buffer({ size: 100 }),
map(message => JSON.parse(message.data)),
filter(data => data.type === "result"),
);
let websocket = new WebSocket('wss://localhost:9000');
// in this example, forEach is not a combinator, just a regular function
yield* forEach(results(websocket), function*(result) {
console.log('received result from websocket', result)
});
import { buffer, on, pipe, filter } from "effection";
yield* pipe(
new WebSocket('wss://localhost:9000'),
on('message'),
buffer({ size: 100 }),
map(message => JSON.parse(message.data)),
filter(data => data.type === "result"),
forEach(function*() {
console.log('received result from websocket', result)
})
);
import { buffer, on, flow, filter } from "effection";
let results = flow(
on('message'),
buffer({ size: 100 }),
map(message => JSON.parse(message.data)),
filter(data => data.type === "result"),
);
let websocket = new WebSocket('wss://localhost:9000');
yield* pipe(websocket, results, forEach(function*(result) {
console.log('received result from websocket', result)
}));
import { on, pipe } from "effection";
let websocket = new WebSocket('wss://localhost:9000');
yield* pipe(websocket, on("message"), forEach(function*(message) {
console.log('received message from websocket', message)
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment