| ▲ | bastawhiz 16 hours ago | ||||||||||||||||||||||
I like this, but unfortunately it doesn't solve one annoying problem: lexical scope doesn't work and it will fail in an unexpected way. If you reference something lexically, your code fails at runtime. Want to use an import? You have to use import() inside the closure you pass to spawn(). Typescript doesn't know this. Your language server doesn't know this. Access a variable that shadows a built in global? Now you're accessing the built in global. The only way this could even be addressed is by having a full on parser. Even then you can't guarantee things will work. I think the only "fix" is for JS to introduce a new syntax to have a function that can't access lexical scope, returning a value that either extends a subclass of Function or has a cheeky symbol set on it. At least then, it'll fail at compile time. | |||||||||||||||||||||||
| ▲ | Etheryte 5 hours ago | parent | next [-] | ||||||||||||||||||||||
There is a simple solution to this problem, but it's not very popular: do the same thing Workers do, require using a separate file. All the tooling works out of the box, you have no issues with lexical scoping, etc. The only downside is it's (currently) clunky to work with, but that can be fixed with better interfaces. | |||||||||||||||||||||||
| ▲ | pwdisswordfishy 3 hours ago | parent | prev | next [-] | ||||||||||||||||||||||
https://github.com/tc39/proposal-module-expressions is basically that fix. | |||||||||||||||||||||||
| ▲ | buu700 9 hours ago | parent | prev | next [-] | ||||||||||||||||||||||
I've been using a functionally identical implementation of this since I wrote it in my startup's codebase a decade ago. It's really handy, but definitely not without edge case issues. I've occasionally had to put in workarounds for false positive TypeScript/lint errors or a tool in the bundling pipeline trying to be too clever and breaking the output. Overall it's great, and I'm glad to see a generic implementation of it which will hopefully become a thriving open source project, but ultimately it's a kludge. What's really needed is for JS to introduce a native standardized version of this construct which TypeScript and the rest of the ecosystem have to play nice with. | |||||||||||||||||||||||
| ▲ | kretaceous 14 hours ago | parent | prev | next [-] | ||||||||||||||||||||||
A linter rule provided by the library could be helpful here. I know it's just a workaround but probably easier than going for a solution that does compile time checks. | |||||||||||||||||||||||
| ▲ | austin-cheney 14 hours ago | parent | prev | next [-] | ||||||||||||||||||||||
This should be the expected behavior when multithreading. It is the expected behavior when executing a child process, such as node’s child_process.fork. | |||||||||||||||||||||||
| |||||||||||||||||||||||
| ▲ | christophilus 15 hours ago | parent | prev [-] | ||||||||||||||||||||||
I’d love a way to be able to specify that sort of thing. I wrote a little server-side JSX rendering layer, and event handlers were serialized to strings, and so they had similar restrictions. | |||||||||||||||||||||||