Re: Gio.InputStream (and friends)
- From: Sam Jansen <sam jansen starleaf com>
- To: Andrea Giammarchi <andrea giammarchi gmail com>
- Cc: javascript-list <javascript-list gnome org>
- Subject: Re: Gio.InputStream (and friends)
- Date: Tue, 7 Nov 2017 21:46:36 +0000
Hi Andrea,
I've come up with something that... almost does what you're looking at here. Perhaps it's useful as a guide of what one can do with the Gio interface.
I think you may hit some awkward problems with modelling Node-style streams with GLib ones. But perhaps it is possible and this helps. I don't really know the Node stream semantics so I have assumed various things in my Readable implementation as you'll see... Note that MemoryInputStream really is just a byte stream; so there is no guarantee you'll receive the exact block of bytes that was written -- for example, when I run this I get a first chunk of "12", followed by "3", "4", etc.
let Gio = imports.gi.Gio;
let byteArray = imports.byteArray;
let mainloop = imports.mainloop;
class Readable {
constructor() {
this._mio = Gio.MemoryInputStream.new();
this._callbacks = {};
this._startNext();
}
_startNext() {
// Enqueue an async read; and re-enqueue when it finishes, so we're
// always waiting for data...
this._mio.read_bytes_async(4096, 1, null, (source, res) => {
this._onData(this._mio.read_bytes_finish(res));
this._startNext();
});
}
_onData(bytes) {
if (this._callbacks['data']) {
let ba = byteArray.fromGBytes(bytes);
this._callbacks['data'](ba);
this._read();
}
}
push(bytes) {
if (bytes == null) {
mainloop.quit('main');
return;
}
this._mio.add_bytes(bytes);
}
on(name, callback) {
this._callbacks[name] = callback;
if (name === 'data') {
this._read();
}
}
}
class Counter extends Readable {
constructor(opt) {
super(opt);
this._max = 1000;
this._index = 1;
}
_read() {
const i = this._index++;
if (i > this._max)
this.push(null);
else {
const str = '' + i;
const buf = byteArray.fromString(str); // Buffer.from(str, 'ascii');
this.push(buf);
}
}
}
(new Counter).on('data', (str) => {
print("data", str.toString());
});
mainloop.run('main');
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]