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

Channel refuses content #10

Open
flying-sheep opened this issue May 16, 2013 · 2 comments
Open

Channel refuses content #10

flying-sheep opened this issue May 16, 2013 · 2 comments

Comments

@flying-sheep
Copy link

If i write too much/take too long, i get

NS_BASE_STREAM_WOULD_BLOCK: Component returned failure code: 0x80470007 (NS_BASE_STREAM_WOULD_BLOCK) [nsIOutputStream.write]
undefined 36
@flying-sheep
Copy link
Author

initializing the pipe with nonBlockingInput=false, it works.

i just write to it like in the examples, how can i change this to write asynchronously? or is the Pipe not accepting blocking input a bug?

@flying-sheep
Copy link
Author

i found a nice workaround here

add to the header:

const {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm")
const ThreadManager = Cc['@mozilla.org/thread-manager;1'].
                      getService(Ci.nsIThreadManager);

add to Response.initialize:

this._asyncWait = stream.asyncWait.bind(stream)

and to Response itself:

writeFrom: function(data, end=false) {
  let self = this;
  function writeNextChunk() {
    self._asyncWait({
      QueryInterface: XPCOMUtils.generateQI([Ci.nsIOutputStreamCallback]),
      onOutputStreamReady: function() { try {
        var str = data.next()
        self.write(str, str.length)
        writeNextChunk()
      } catch (e if e instanceof StopIteration) {
        if(end) self.end()
      } }
    }, 0, 0, ThreadManager.currentThread)
  }
  writeNextChunk()
}

then just feed it data:

function myData() {
  yield 'this is so much, it’s blocking!'
}
exports.handler = protocol('much', {
  onRequest: function(request, response) {
    response.writeFrom(myData(), true)
  }
})

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