yoshuawuyts / barracks

:mountain_railway: action dispatcher for unidirectional data flows
MIT License
177 stars 22 forks source link

Expose `createSend` on the store object? #43

Closed anandthakker closed 8 years ago

anandthakker commented 8 years ago

@yoshuawuyts What do you think about exposing createSend on the store object upon the first call to start? (Before the first call, it could be either undefined or a placeholder function that just throws an error.)

yoshuawuyts commented 8 years ago

@anandthakker what's the use case?

anandthakker commented 8 years ago

I'm working on a router-less setup where I need to mount multiple views to different elements on the page. I thought I'd use a model to track the element-view mapping, and possibly other lifecycle-related state that we end up needing down the road. So, my mount-view module currently looks something like:

const store = require('./store')

store.model({ /* ... set up the model for tracking the state of the element-to-view mapping ... */)

module.exports = function (send) {
  return function mountView (...) {
    /* do some stuff */
    send('views:mount', {...})
  }
}

You can see here that there's a bit of dependency injection happening: currently I wait until after doing store.start() to create the mountView function using a created send function. However, I need to export mountView to the folks using our code, so I'd really prefer to have it look something like:

const store = require('./store')

store.model({ /* ... set up the model for tracking the state of the element-to-view mapping ... */)

var send
module.exports = function mountView (...) {
  if (!send) { send = store.createSend('mount-view', true) }
  /* do some stuff */
  send('views:mount', {...})
}

OR, maybe even better, if the store had a default send function attached, I wouldn't even need to createSend...

Definitely in early stages of thinking through the architecture here, so I'm definitely open to push back on this :)

yoshuawuyts commented 8 years ago

@anandthakker I believe we talked about this on Slack - is it still an issue? Any way I can assist?

anandthakker commented 8 years ago

Ah yeah -- no longer an issue, I think. Closing.