Skip to content

Commit

Permalink
Remove Riptide hooks and move to recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Jul 10, 2020
1 parent 043f853 commit f95ea43
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 237 deletions.
37 changes: 37 additions & 0 deletions docs/recipes/debounce.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
```ts
export default function useRiptideDebounce<T>(
riptide: RiptideObservable<T>,
cooldown: number,
): T | undefined {
const [state, setState] = useState<MutableRefObject<T | undefined>>({
current: undefined,
});

useEffect(() => {
let current: ReturnType<typeof setTimeout> | undefined;

const subscription = riptide.subscribe({
next(value) {
if (current) {
clearTimeout(current);
}
current = setTimeout(() => {
setState({
current: value,
});
current = undefined;
}, cooldown);
},
});

return () => {
if (current) {
clearTimeout(current);
}
subscription.cancel();
};
}, [riptide, cooldown]);

return state.current;
}
```
24 changes: 24 additions & 0 deletions docs/recipes/filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
```ts
export default function useRiptideFilter<T>(
riptide: RiptideObservable<T>,
filter: (value: T) => boolean,
): T | undefined {
const [state, setState] = useState<MutableRefObject<T | undefined>>({
current: undefined,
});

useEffect(() => {
const subscription = riptide.subscribe({
next(value) {
setState((current) => (filter(value) ? { current: value } : current));
},
});

return () => {
subscription.cancel();
};
}, [riptide, filter]);

return state.current;
}
```
25 changes: 25 additions & 0 deletions docs/recipes/map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

```ts
export default function useRiptideMap<T, R>(
riptide: RiptideObservable<T>,
mapper: (value: T) => R,
): R | undefined {
const [state, setState] = useState<MutableRefObject<R | undefined>>({
current: undefined,
});

useEffect(() => {
const subscription = riptide.subscribe({
next(value) {
setState(() => ({ current: mapper(value) }));
},
});

return () => {
subscription.cancel();
};
}, [riptide, mapper]);

return state.current;
}
```
55 changes: 55 additions & 0 deletions docs/recipes/throttle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
```ts
export default function useRiptideThrottle<T>(
riptide: RiptideObservable<T>,
cooldown: number,
): T | undefined {
/**
* Instead of directly writing the value to state,
* we can wrap it inside an object so that
* we can bypass useState's bail-out mechanism.
*/
const [state, setState] = useState<MutableRefObject<T | undefined>>({
current: undefined,
});

useEffect(() => {
/**
* Handles the timeout reference
*/
let current: ReturnType<typeof setTimeout> | undefined;

/**
* Begin subscription
*/
const subscription = riptide.subscribe({
next(value) {
/**
* If there is no timeout reference,
* emit the value
*/
if (!current) {
setState({
current: value,
});

/**
* Begin throttle
*/
current = setTimeout(() => {
current = undefined;
}, cooldown);
}
},
});

return () => {
if (current) {
clearTimeout(current);
}
subscription.cancel();
};
}, [riptide, cooldown]);

return state.current;
}
```
66 changes: 0 additions & 66 deletions src/hooks/useRiptideDebounce.ts

This file was deleted.

53 changes: 0 additions & 53 deletions src/hooks/useRiptideFilter.ts

This file was deleted.

53 changes: 0 additions & 53 deletions src/hooks/useRiptideMapper.ts

This file was deleted.

65 changes: 0 additions & 65 deletions src/hooks/useRiptideThrottle.ts

This file was deleted.

0 comments on commit f95ea43

Please sign in to comment.