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

Channels #5

Open
jaetask opened this issue Sep 23, 2019 · 4 comments
Open

Channels #5

jaetask opened this issue Sep 23, 2019 · 4 comments

Comments

@jaetask
Copy link

jaetask commented Sep 23, 2019

HI, I really like where you are going with this idea. Would you consider extending the Map to a Map of Maps to enable channels?

Instead of strings

useBus(
    '@@ui/ADD_ITERATION',
    () => setIterations(iterations + 1),
    [iterations],
  )

You could pass the channel which would reducer the listeners affected in a large system.

const useBus = (channel, type, callback, deps = [])

This would enable messages to be sent on a single channel

useBus(
    'ui',
    'ADD_ITERATION',
    () => setIterations(iterations + 1),
    [iterations],
  )

This is how apps used to communicate in systems like backbone, enabling nesting of comms to avoid global chatter.

Just an idea.

@fabienjuif
Copy link
Owner

Hi @jaetask 👋

I think this is a great idea.

I have some things in mind base on that:

1. Compatibility

It would be better if we can keep both API, so someone that don't want to bother with channels can still use this lib as it is right now.

Maybe we can do this by testing if the second parameter is a function?

2. How about dispatch?

What do you have in mind for the dispatcher?

  1. We keep dispatch(type) as it is?
  2. And if you need channel you just use the object signature? dispatch({ channel, type, payload })

3. Register a function?

If we implement this, you can do whatever you can imagine: #3

const isMatching = (channel, type) => action => action.channel === channel && action.type === type

useBus(
  isMatching('ui', 'ADD_ITERATION'),
  () => setIterations(iterations + 1),
  [iterations],
)

dispatch({ channel: 'ui', type: 'ADD_ITERATION' })

// or if you want to do it "better"
const dispatchUi = action => ({ ...action, channel: 'ui' })
dispatchUi({ type: 'ADD_ITERATION' })

I have a preference for the 3rd version, we then can add some helper like (matchChannelAndType(channel)(type)), and more.

@jaetask
Copy link
Author

jaetask commented Sep 24, 2019

1. Compatibility

I agree, keeping both APIs is important.

2. dispatch

Yes, absolutely, I like the shorthand, longhand ability

3. Simplicity

The isMatching example is very clean and easy to understand, it would be easy to test

dispatchUi seems over eager, I would keep a single dispatch function and let the user pass in what they feel is appropriate.

@fabienjuif
Copy link
Owner

3rd point is just example of how can achieve channels and more with the resolution of the issue #3
If we do this we don't need to change the API so 1st and 2nd point will be obsoletes.

Do you want to take a look at #3 ?
If you don't want to code this then I'll take a look the 30/09 I think.

Thank you for your interest @jaetask :)

@fabienjuif
Copy link
Owner

@jaetask Hey 👋

With the last version (2.2.0 -or latest-) you can use your own functions.
As you can see here: https://github.com/fabienjuif/use-bus/blob/master/index.spec.js#L66

You can do something like this:

const isMatching = (channel, type) => (event) => event.channel === channel && event.type === type

useBus(
  isMatching('ui', 'ADD_ITERATION'),
  () => setIterations(iterations + 1),
  [iterations],
)

dispatch({
  channel: 'ui',
  type: 'ADD_ITERATION',
})

You can use lodash/isMatch too:

import { isMatch } from 'lodash-es'

useBus(
  isMatch({ channel: 'ui', type: 'ADD_ITERATION' }),
  () => setIterations(iterations + 1),
  [iterations],
)

dispatch({
  channel: 'ui',
  type: 'ADD_ITERATION',
})

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

2 participants