crucialfelix/supercolliderjs

inconsistent output from stream evaluation

htor opened this issue · 5 comments

htor commented

evaluating this block several times results in inconsistent, sometimes partial output emitted on stdout:

(
a = Pseq([1, 2, 3, 4]);
b = a.asStream;
4.do({ b.next.postln; });
nil;
)

results in:

nil       // eval result, but no output emitted before
nil
->       // an arrow (generally emitted now and then)
nil
1          // here the correct output is emitted
2
3
4
nil
1     // partial result emitted
2
nil
nil
nil
1
nil
...

expected output would be:

1
2
3
4
nil
1
2
3
4
nil
...

my setup is this:

  sclang = await sc.lang.boot({
    stdin: false,
    echo: false,
    debug: false
  })
  sclang.on('stdout', console.log)
  sclang.on('stderr', console.log)

why could this be? my guess is this has something to do with the way stdout from sclang is handled by scjs, as you mentioned in #45 (comment), but i'm really not sure.

htor commented

it works if i wrap the 4.do() inside a fork?

(
a = Pseq([1, 2, 3, 4]);
b = a.asStream;
fork {
  4.do({ b.next.postln; });
};
nil
)
1
2
3
4
nil
1
2
3
4
...

I don't think it's related to #45 That would only be an issue when you are posting massive amounts of text.

I better get this lerna PR merged so that I can accept new PRs (should you discover what the issue is)

Try turning off stderr => console.log Just to be certain what you are looking at. AFAI remember sclang doesn't post much of anything to stderr. It doesn't do it in the standard fashion anyway. But just to be sure you are looking at the right output.

If you are working on your IDE (as you mentioned to me in slack) then look at https://github.com/crucialfelix/atom-supercollider

https://github.com/crucialfelix/atom-supercollider/blob/00b5f5cbf2b0b78a0d4e5931c30cabc8bbee3287/lib/repl.coffee#L201-L229

As I said on slack, if I did this now I would rebuild the whole rendering system in something modern. What are you using for your mini-ide?

htor commented

tried turning off stderr, but the output stays the same (nil as result + sometimes postln messages emitted on stdout after eval).

anyways, i'm trying this in my new ide/editor, much the same way as you do in atom-sc. i'm using electron.js and vanilla js. if you are interested in having a look:

htor commented

okay it was an issue in my code, i think with electron.js. something about sclang instance being an EventEmitter and sharing that instance across render thread (browser) and main thread (nodejs) messes up stdout. (see htor/hadron-editor@000501d#diff-9ce6183be2ee46c4fb3c33c437179466L6). moving sclang = await sc.lang.boot() and sclang.on('...') calls from nodejs land to browser land (renderer) solves the issue, basically.