jlongster / js-csp

CSP channels for Javascript (like Clojurescript's core.async, or Go) THIS IS AN UPSTREAM FORK
282 stars 9 forks source link

csp promises and async react components #6

Open ThomasDeutsch opened 9 years ago

ThomasDeutsch commented 9 years ago

I used js-csp in my last project to replace promises (ajax) and frp (search logic). With webpack + bundle-loader i am able to load components only when needed. webpack makes this very easy.

<AsyncComp data={dataCh} bundle={require('bundle?lazy!./file.js')};

This component will load the data and the bundle only when needed. Complete example here.

If more components are using data from dataCh or if the same component gets re-mounted to the DOM, the csp solution will fail, because the value will be taken and the AsyncComponent will wait for this never-arriving data. At this point, csp-promises would be a nice solution.

I ended up loading all data to the immutable datastructure. I am not happy with this solution because i think that loaded data should not be part of the app-state (in most cases). But i was able to utilize immutable-cursors.

I think that a csp-promise solution would be a competitor for immutable-cursors. If a channel would return always the last value on every take, and if you could renew the resolved promise value (unlike real promises that could only be resolved once) - would this make immutable-cursors obsolete?

getify commented 9 years ago

I personally think you're asking for observables, not channel-promises.

ThomasDeutsch commented 9 years ago

For the first part, promise-channels would be a nice solution for multiple components that will read from a single channel.

Maybe i am asking for observables, when it comes to "renewable" promise-channels. Because of "first value written wins", it would only make sense to use promise-channels if the receiver would wait for a new promise after the first resolved value is taken.