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

Waiters composition #12

Open
pwlmaciejewski opened this issue Feb 20, 2020 · 1 comment
Open

Waiters composition #12

pwlmaciejewski opened this issue Feb 20, 2020 · 1 comment

Comments

@pwlmaciejewski
Copy link

I've been using the library for a while and I noticed that I could simplify my code with something like composing of the waiters. For example, given that I have two waiters bar and baz, I would like to have foo waiter that is waiting if one of bar or baz is waiting.

Motivation

Now, first I was thinking about adding array argument support to isWaiting so that I could call isWaiting(["foo", "bar"]) or a bit more explicit convenience function isAnyWaiting(["foo", "bar"]).

But after looking into my code I realized it's not enough. In my case, I have some big component
Foo rendering smaller components Bar and Baz that wait on each other. That works fine with current API. It gets more convoluted when you would like to have some other component Qux that doesn't exactly want to know about the lower-level ones but needs to know if Foo or any of it's children waits. So currently in Qux you would need to write

const isFooWaiting = isWaiting("baz") && isWaiting("baz")

That's not ideal because Qux needs to know too much about Foo internals.

Proposal

Create a separate method that will create a new waiter if one of the child waiters is waiting. It could be similar in usage to createWaitingContext:

const { compose } = useWait();
const { isWaiting } = compose("Foo", ["Bar", "Baz"])
  • Foo is a waiter, you can check if it's waiting with isWaiting("Foo")
  • Foo is always waiting if Bar or Baz is waiting.
  • the open question is should you be able to manually start and end a composed waiter?

Anternative idea

Maybe instead adding a compose method, we could compose Waiter providers? It's just an idea, I don't know if it's possible or makes sense but provier could have an optional name and be a child of another warapper like this?

<Waiter>
  <Waiter name="foo">
    // ...
  </Waiter>
</Waiter>

Then if the inner's anyWaiting() is true, foo is waiting in the outer waiter?
Again, it's just an idea.

@pwlmaciejewski
Copy link
Author

On second thought, maybe adding support for wildcards to isWaiting would be enough and a lot simpler solution.

With wildcards, you could have your own waiters naming convention, eg. foo_a, foo_b, etc. and then call isWaiting("foo_*") to check if any of them is waiting?

anyWaiting() could be then written as isWaiting("*").

I think I like this one the most, @f ?

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

No branches or pull requests

1 participant