Skip to content

Instantly share code, notes, and snippets.

@reaktivo
Created October 29, 2018 09:04
Show Gist options
  • Save reaktivo/df30d8ba66e5912d96fb3a23c0649c4f to your computer and use it in GitHub Desktop.
Save reaktivo/df30d8ba66e5912d96fb3a23c0649c4f to your computer and use it in GitHub Desktop.
Playing around with React Hooks + Callbags
import React, { useState, useEffect } from "react";
import { interval, map, pipe, merge } from "callbag-basics";
import subscribe from "callbag-subscribe";
import of from "callbag-of";
const usePipeStream = fns => {
const [value, setValue] = useState(null);
// maybe should use useCallback on the pipe and then run it
useEffect(
() =>
pipe(
...fns,
subscribe(setValue)
),
fns
);
return value;
};
const intervalImmediate = intervalMs => {
let count = 0;
return pipe(
merge(of(0), interval(intervalMs)),
map(() => count++)
);
};
const ticker = intervalImmediate(1000);
// const ticker = interval(1000);
const mapper = map(x => `${x} count`);
export default function Timer() {
const count = usePipeStream([ticker, mapper]);
console.log({ count });
return <h1>{count}</h1>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment