npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

t-hooks

v1.1.0

Published

T-Hooks provides a set of react hooks built upon T-Tasks library. Using task hooks instead of conventional lifecycle hooks alows easier usage of asynchronous operations withing hooks and provides automatic operation cancelation in case of hook unmounting

Downloads

239

Readme

CI

T-Hooks task-based React hook library

T-Hooks provides a set of react hooks built upon T-Tasks library. Using task hooks instead of conventional lifecycle hooks alows easier usage of asynchronous operations withing hooks and provides automatic operation cancelation in case of hook unmounting or re-render.

Task-based useEffect

In this scenario, every time arg updates an asynchronous operation is started, resulting in a side effect. If hook is unmonted before the operation finishes no side effect is performed, preventing modification of state of unmounted component. Moreover, if arg changes before the operation finishes another operation in started instead, canceling the previous one.

const taskCreator = (arg: string) => Task.fromPromise(someAsyncOperation(arg)).tap((result) => {
  setSomething(result); // side effects
});

useTaskEffect(() => taskCreator(arg), [arg]);

Generator-based useEffect

In this scenario a generator syntax is used to achieve the same result. Generator syntax allows for more natural task chaining via yield syntax. Note that despite the fact that a task may be yielded directly as yield someTask the result of such expression may be unknown in case of multiple yield statements, so using yield* someTask.generator() is recommended.

useGeneratorEffect(function*() {
  const result = yield* Task.fromPromise(someAsyncOperation(arg)).generator();

  setSomething(result); // side effects
}, [arg]);

Chaining multiple effects

In this scenario generator syntax is used to chain two consequtive async operations with two side-effects. In case of unmounting or re-render during execution of the first operation, the second one would not be started and both side effects would not be performed. In case of unmounting or re-render during execution of the second operation, only the second side-effect would be prevented.

useGeneratorEffect(function*() {
  const result = yield* Task.fromPromise(someAsyncOperation(arg)).generator();

  setSomething(result1); // side effect 1

  const result2 = yield* Task.fromPromise(otherAsyncOperation(resilt1)).generator();

  setSomethingElse(result2); // side effect 2
}, [arg]);

Task-based useMemo

In this scenario, every time arg updates an asynchronous operation is started, updating userData asynchronously. In case of hook unmounting or updating arg again before the previous operation finishes, another operation in started instead, canceling the previous one.

const userData = useTaskMemo(null, () => Task.fromPromise(getUserDataAsync(userId)), [userId]);

Generator-based useMemo

In this scenario a generator syntax is used to achieve the same result. Generator syntax allows for more natural task chaining via yield syntax. Note that despite the fact that a task may be yielded directly as yield someTask the result of such expression may be unknown in case of multiple yield statements, so using yield* someTask.generator() is recommended.

const userData = useGeneratorMemo(null, function*() {
  return yield* Task.fromPromise(getUserDataAsync(userId)).generator();
}, [userId]);

Chaining multiple transformations

In this scenario a generator syntax is used to chain two consequtive async operations in a single task memo.

const usageStats = useGeneratorMemo(null, function*() {
  const userData = yield* Task.fromPromise(getUserDataAsync(userId)).generator();

  yield* timeoutTask(100).generator(); // one may add delays easily

  return yield* Task.fromPromise(getUsageStats(userData)).generator();
}, [userId]);

Task-based useCallback

In this scenario an asynchrounous callback is created, performing side-effects only if the hook is not unmounted. Callback is also interrupted if invoked again

const taskCreator = (userId: string) => liftResult(getUserDataAsync(arg)).tap((userData) => {
  setUserData(userData); // side effects
});

const onClick = useTaskCallback(() => taskCreator(userId)), [userId]);

return <button onClick={onClick}>Click me</button>

Generator-based useCallback

In this scenario an asynchrounous callback is created via generator syntax

const onClick = useGeneratorCallback(function*() {
  const userData = yield* Task.fromPromise(getUserDataAsync(userId)).generator();

  yield* timeoutTask(100).generator(); // one may add delays easily

  setUserData(userData); // side-effects
}), [userId]);

return <button onClick={onClick}>Click me</button>