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

What is the purpose of DestroyableTransform? #43

Closed
ghost opened this issue Apr 23, 2015 · 4 comments
Closed

What is the purpose of DestroyableTransform? #43

ghost opened this issue Apr 23, 2015 · 4 comments

Comments

@ghost
Copy link

ghost commented Apr 23, 2015

I have implemented a through2-like transform stream function wrapper that uses a simpler interface than through2 here.

While in through2 you do

fs.createReadStream('ex.txt')
  .pipe(thru(function (chunk, enc, callback) {
    for (var i = 0; i < chunk.length; i++)
      if (chunk[i] == 97)
        chunk[i] = 122 // swap 'a' for 'z'

    this.push(chunk)

    callback()
   }))
  .pipe(fs.createWriteStream('out.txt'))

In mine you could:

fs.createReadStream("ex.txt")
  .pipe(thru(function (chunk) {
    return chunk.map(function (value) {
      return (value == 97) ? 122 : value
    })
   }))
  .pipe(fs.createWriteStream("out.txt"))

I find this syntax (not necessarily adding the custom .map() function to the chunk object), but the return style to pass data) simpler, but there is no notion of destroying anything in my implementation.

  1. Why do I need this?
  2. What do you think of yet another through version that accomodates for this syntax?
@juliangruber
Copy link

  1. Not all streams implement .destroy and they also don't have to, but if they do, it's useful because you can just tell the stream "destroy yourself, i don't want to hear from you ever again"
  2. The through2 syntax takes in account asynchronous operations, which makes it more generic. If all you do is sync though, for your purpose that module makes sense.

Also keep in mind that this is also valid with through2:

fs.createReadStream('ex.txt')
  .pipe(thru(function (chunk, enc, callback) {
    for (var i = 0; i < chunk.length; i++)
      if (chunk[i] == 97)
        chunk[i] = 122 // swap 'a' for 'z'
    callback(null, chunk)
   }))
  .pipe(fs.createWriteStream('out.txt'))

@ghost
Copy link
Author

ghost commented Apr 23, 2015

@juliangruber Thanks!

(1) I see, but could you name some / any use cases of having your stream destroy itself? I don't want to add this feature until I can fully understand this point.

(2) My through version also takes async operations into account via promises. If you return a promise it will handle it accordingly.

@juliangruber
Copy link

  1. if you don't need it, don't add it, simple as that. see also add .destroy method #25 for motivation in this repo
  2. if that works for you, that's great

@ghost
Copy link
Author

ghost commented Apr 23, 2015

@juliangruber Thanks! 😄

@ghost ghost closed this as completed Apr 23, 2015
This issue was closed.
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

1 participant