-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove Riptide hooks and move to recipes
- Loading branch information
Showing
8 changed files
with
141 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.