         if el.closed and el is el.parent.pending[0]
             release.call el
 
-    tpl.on 'data', (el, data) ->
-        delay.call el, ->
-            write data
+    ondata: (el, data) ->
+        delay.call el, =>
+            @write data
 
-    tpl.on 'text', (el, text) ->
-        delay.call el, ->
-            write prettify el, text
+    ontext: (el, text) ->
+        delay.call el, =>
+            @write prettify el, text
 
-    tpl.on 'raw', (el, html) ->
-        delay.call el, ->
-            write html
+    onraw: (el, html) ->
+        delay.call el, =>
+            @write html
 
-    tpl.on 'attr', (el, key, value) ->
+    onattr: (el, key, value) ->
         return unless el.isempty
         console.warn "attributes of #{el.toString()} don't change anymore"
 
-    tpl.on 'end', ->
-        stream.emit 'end'
+    onend: () ->
+        @stream.emit 'end'
 
-    tpl.register 'ready', (tag, next) ->
-        if tag._stream_ready is yes
-            next(tag)
-        else
-            tag._stream_ready = ->
-                next(tag)
 
+streamify = (tpl, opts) ->
+    new StreamAdapter(tpl, opts)
     return tpl
 
 # exports
 
+streamify.Adapter = StreamAdapter
 module.exports = streamify
 
 # browser support

{ Stream } = require 'stream'
{ delay, release, prettify, attrStr } = require './util'

# TODO drainage

#return true if @hidden # dont emit data when this tag is hidden # FIXME
streamify = (tpl) ->
    stream = new Stream
    stream.readable = on

    write = (data) ->
        stream.emit('data', data) if data

    tpl.stream = stream
    builder = tpl.xml ? tpl
    builder._streamed = yes

    tpl.on 'add', (parent, el) ->
        # insert into parent
        delay.call el, ->
            if el.closed is 'self'
                write prettify el, "<#{el.name}#{attrStr el.attrs}/>"
            else
                write prettify el, "<#{el.name}#{attrStr el.attrs}>"

        delay.call parent, ->
            return unless el.closed


        release.call parent if el is parent.pending[0]

    onclose: (el) ->
        delay.call el, =>
            unless el.closed is 'self'
                @write prettify el, "</#{el.name}>"
            el._stream_ready?()
            el._stream_ready = yes

