-
Notifications
You must be signed in to change notification settings - Fork 9
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
simpler implementation while still avoid stack overflow #5
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,16 @@ | ||
const fromIter = iter => (start, sink) => { | ||
if (start !== 0) return; | ||
const iterator = | ||
typeof Symbol !== 'undefined' && iter[Symbol.iterator] | ||
? iter[Symbol.iterator]() | ||
: iter; | ||
let inloop = false; | ||
let got1 = false; | ||
let completed = false; | ||
let res; | ||
function loop() { | ||
inloop = true; | ||
while (got1 && !completed) { | ||
got1 = false; | ||
res = iterator.next(); | ||
if (res.done) { | ||
sink(2); | ||
break; | ||
} | ||
else sink(1, res.value); | ||
} | ||
inloop = false; | ||
} | ||
const isIterable = typeof Symbol !== 'undefined' && iter[Symbol.iterator]; | ||
const iterator = isIterable ? iter[Symbol.iterator]() : iter; | ||
let disposed = false; | ||
let result = {}; | ||
sink(0, t => { | ||
if (completed) return | ||
|
||
if (t === 1) { | ||
got1 = true; | ||
if (!inloop && !(res && res.done)) loop(); | ||
} else if (t === 2) { | ||
completed = true; | ||
} | ||
if (disposed) return; | ||
if (t === 1) result = iterator.next(); | ||
if (t === 2) disposed = true; | ||
}); | ||
while (!result.done && !disposed) sink(1, result.value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to be a correct pullable implementation. A pullable source should only push once for every pull (in a 1:1 ratio), not push everything at once. |
||
if (!disposed) sink(2); | ||
}; | ||
|
||
module.exports = fromIter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might cause infinie loop. U assume in this implementation that u always receive pull immediately in response to pushing the value down
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you give an example?
EDIT: you mean a sink calls the source with zero and then doesn't talk back with 1, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this example works normally. What you mentioned can occur just if for some reason a sink calls the source with 0 and doesn't immediately talkback with 1 or 2. That's true. I just wonder if that's a real scenario, I mean forEach, iterate, subscribe, all of them call
source(0, (t, d) => { })
and in case oft === 0 || t === 1
there is an immediatetalkback(1)
. Is that in compliance with the spec? I believe delays should happen through operators and not in the sink by not calling tb(1).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented sinks (like forEach, subscribe etc) indeed pull automatically - but this is not something that spec assumes. You could also implement
backpressurePulls
operator that could delay pulls sent upwards and this implementation wouldnt handle it well.Also from what I see this implementation doesnt handle my delay example (I havent tested it though and I might be wrong). I think it will hang in the while loop, because it will try to send down the same value, rather than next ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it, it works fine if working with delay operators without patching the sinks we know.
In any case, I believe the original version that is implemented by Andre now should include these test cases you mentioned in test.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm gonna clone the PR because it really doesnt support that a lazy sink that talks back after a delay, thanks @Andarist for the keen eye