ysmood / nokit

A light weight set of handy tools for real world program.
51 stars 6 forks source link

Every drive deserves an onEnd callback #3

Closed dracupid closed 9 years ago

dracupid commented 9 years ago

Is it a good idea to let every drive has a onEnd callback, not only writers. For instance, what if I want to count lines of code? Or is there another way to achieve this?

countLine = ->
    total = 0
    kit._.assign ->
        total += @contents.split('\n').length
    , onEnd: ->
        console.log "Total:  #{total} lines。"
ysmood commented 9 years ago
counter = ->
  count = 0
  {
    drive: -> count++
    report: -> console.log count
  }

c = counter()

kit.warp '*.js'
.load c.drive
.run('dist')
.then ->
   c.report()

You need to think things from another aspect. Maybe the user want to control the report by themselves.

dracupid commented 9 years ago

Yes, this is a solution for this example, which is similar with what I'm using now. However, in comparison with Stream, a Transform can always know the end of the stream. Hmm.., I can't put forward a more powerful use case for no yet. Closure and then create more possibilities.

ysmood commented 9 years ago

I've added 3 lines of code. You can pull it and try it yourself first, if everything is OK, I may add it to the next version. 9ccb26e2bbf75f5

dracupid commented 9 years ago

Passed. Well done!