-
-
Notifications
You must be signed in to change notification settings - Fork 18
be interactive #4
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
Comments
For that snippetify may help which will try to detect executable JS snippets in a given string. |
great suggestion! i'm assuming snippetify will help with raw streaming data as chunk boundaries likely won't line up with line endings. i'll give it a shot! |
@thlorenz @juliangruber just used snippetify to make a streaming snippet parser, https://github.com/mafintosh/snippet-stream then its as simple as var snippets = require('snippet-stream')
// create a snippet stream
var stream = snippets()
var scope = {}
// write some js to it
stream.write('var a = 1\n')
stream.write('function foo () {\n')
stream.write(' return a + 1\n')
stream.write('}\n')
stream.write('console.log(foo())')
stream.end()
stream.on('data', function (data) {
eval.call(scope, data)
}) |
@mafintosh ok that's really sweet. Ok I might need your guys' help again: In the current design I boot an http server that serves the script inlined into a script tag: https://github.com/juliangruber/electron-stream/blob/master/index.js#L77. I'm doing this because otherwise window.onerror will only get "Script Error." instead of a full error with stack trace. Could you think of a way to serve javascript via a snippet stream, like using electron's ipc module, while still retaining sourcemap and full window.onerror support? |
If you use an HTTP server you can use the response stream and pipe data into it
IIRC the browser will evaluate the JS as it's piped into the script tag. Might need to do something about a keepalive, though. |
I did a test on this and unfortunately no it doesn't. So maybe we introduce this as a second mode simply, you lose sourcemaps and window.onerror support but gain interactive execution. |
Some javascript will even break by this execution technique, imagine for example those two snippets arriving in separate chunks console.log(typeof foo) var foo = 'bar' The output is going to be Therefore interactive mode is strictly to be seen as interactive / repl mode, it's not a way of improving performance for regular scripts. It needs to work with copy / pasting regular scripts though too, as do the nodejs / chrome repl, so this needs to be coupled to IO. I'm thinking this should then be its own module, wrapping electron-stream. |
@juliangruber Just to confirm, in your example it wouldn't be "string". Variable hosting does not hoist the assignment. It would still be undefined until the line when the value gets assigned to the variable. console.log(typeof foo);
var foo = 'bar' Turns into var foo
console.log(typeof foo)
foo = 'bar' What it wouldn't work with is function hoisting since the value does get hoisted to the top in that case. |
yeah you're right about that, thanks for the clarification. Anyway, the important part is that interactive mode can break javascript if not done correctly. |
would be nice if this could execute the js as you stream it to it instead of buffering the entire source.
it could probably be executed like this
The text was updated successfully, but these errors were encountered: