Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add watch to valtio/utils #157

Merged
merged 6 commits into from
May 15, 2021
Merged

Add watch to valtio/utils #157

merged 6 commits into from
May 15, 2021

Conversation

lxsmnsyc
Copy link
Contributor

Resolves #149

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 15, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit bab8b5b:

Sandbox Source
React Configuration
React Typescript Configuration

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work. Overall it looks good.
I hadn't thought about nested watches. Would you like to add tests that depend currentCleanups?

src/utils.ts Outdated Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
@lxsmnsyc
Copy link
Contributor Author

I hadn't thought about nested watches. Would you like to add tests that depend currentCleanups?

I have added a test for this, however it isn't that verbose 😅

tests/watch.test.tsx Outdated Show resolved Hide resolved
@dai-shi
Copy link
Member

dai-shi commented May 15, 2021

Thanks for the improvement. I'm still not sure the recursive use case, but it's fine for now.

@lxsmnsyc
Copy link
Contributor Author

I could probably remove it as the nested watch and the cleanup use-cases are already covered, no?

@dai-shi
Copy link
Member

dai-shi commented May 15, 2021

That looks okay.

My (non-blocking) comment is about the use case of nested watch.

const stop = watch((get) => {
  // do something
  watch((get) => {
    // do something else
    return cleanup1
  })
  return cleanup2
})

When do you need something like this?

@lxsmnsyc
Copy link
Contributor Author

lxsmnsyc commented May 15, 2021

There may be a use-case wherein inside a watch there's a localized proxy object, in which you can watch it from the inside.

watch(() => {
  const localProxy = proxy({ count: 0 });
  
  watch((get) => {
    console.log(get(localProxy).count);
  });
});

Another use-case is if you want to compose cleanups. Adding cleanups through the cleanup return may be inconvenient. There's also the compositional watch so if ever you wanted to partially track individual proxies while sharing the same watch scope, this may be convenient.

watch(() => {
  const signal = new AbortSignal();
  
  watch(() => () => {
    signal.abort();
  });
  
  // Do other stuffs
});

Also, there's nothing that's going to stop users from doing nested watch calls, so adding an auto-cleanup may be convenient.

@dai-shi
Copy link
Member

dai-shi commented May 15, 2021

Thanks for the clarification.

Also, there's nothing that's going to stop users from doing nested watch calls, so adding an auto-cleanup may be convenient.

Again, I'm fine with your proposal. But, if I were to design it, I would require users to do manual cleanup instead of the auto-cleanup.

const stop = watch(() => {
  const signal = new AbortSignal();
  
  const innerStop = watch(() => () => {
    signal.abort();
  });
  
  // Do other stuffs

  return () => {
    // Do other cleanups

    innerStop();
  };
});

I think it's just a design choice.

I prefer manual one, because you never know if you are in watch or not.

const fn = () => {
  watch(() => {
    return cleanup
  }
}

// expecting this usage
const stop = watch(() => {
  fn()
})

// but someone else could use it directly
fn()

@dai-shi
Copy link
Member

dai-shi commented May 15, 2021

I was trying to resolve conflicts, but github warns me about the branch. I guess it should be fine, but I'd leave it (= resolving conflicts) to you.

@dai-shi
Copy link
Member

dai-shi commented May 15, 2021

Let me merge this and cut a release.

@dai-shi dai-shi merged commit 37fd535 into pmndrs:master May 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Watch
2 participants