Gozala / reducers

Library for higher-order manipulation of collections
MIT License
181 stars 8 forks source link

Multiple folds + reduced #33

Closed Raynos closed 11 years ago

Raynos commented 11 years ago

What happens if this is done:

var r = Reducible()

fold(r, function (d) {
  if (d === 5) {
    return reduced()
  }
})

fold(r, function (d) {
  if (d === 6) {
    return reduced()
  }
})

Here there are two folds which both send a signal on back channel saying "I dont need any more data cleanup"

However the second one does it later and he never get's the value 6 although he wanted that value.

Gozala commented 11 years ago

Each fold will initiate completely new read so there are no state connection. For stateful reducers there is hub that manages folders and only actually closes source on last consumer sending back reduced

On Thursday, January 10, 2013, Raynos wrote:

What happens if this is done:

var r = Reducible() fold(r, function (d) { if (d === 5) { return reduced() }}) fold(r, function (d) { if (d === 6) { return reduced() }})

Here there are two folds which both send a signal on back channel saying "I dont need any more data cleanup"

However the second one does it later and he never get's the value 6 although he wanted that value.

— Reply to this email directly or view it on GitHubhttps://github.com/Gozala/reducers/issues/33.

Typed on tiny virtual keyboard

Gozala commented 11 years ago

BTW you create reducible by passing function to it. Each fold will cause that function to be executed unless wrapped in something like hub that intentionally alters this behavior

On Thursday, January 10, 2013, Irakli Gozalishvili wrote:

Each fold will initiate completely new read so there are no state connection. For stateful reducers there is hub that manages folders and only actually closes source on last consumer sending back reduced

On Thursday, January 10, 2013, Raynos wrote:

What happens if this is done:

var r = Reducible() fold(r, function (d) { if (d === 5) { return reduced() }}) fold(r, function (d) { if (d === 6) { return reduced() }})

Here there are two folds which both send a signal on back channel saying "I dont need any more data cleanup"

However the second one does it later and he never get's the value 6 although he wanted that value.

— Reply to this email directly or view it on GitHubhttps://github.com/Gozala/reducers/issues/33.

Typed on tiny virtual keyboard

Typed on tiny virtual keyboard

Raynos commented 11 years ago

@Gozala oh each fold does a new read! that's genius, that makes sense.

I was assuming it's hub() by default because pipe handles multiple writers with one reader.

how does hub() handle the case of multiple reduced signals?

Gozala commented 11 years ago

how does hub() handle the case of multiple reduced signals?

It just increments count of readers when they fold and decrements when reduced is returned. Once count is down to 0 it passes reduced back to source to close it too.

Gozala commented 11 years ago

In fact that's only thing hub does it just keeps list of open reducers and dispatches yield value across them. If no one else is left to dispatch to it closes source.

Gozala commented 11 years ago

It does not looks like anything needs fixing here.