-
Notifications
You must be signed in to change notification settings - Fork 24
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
Should support for functions be removed? #12
Comments
We do want to share functions between client and server somehow, though, right? At the very least, with what we do with templates and internationalization, those functions are wrapped up into their modules, and accessed through a registry to be used. If we do remove this from |
@clarle, in principle, Also, keep in mind that this is a very low level component, with a low level api. It doesn't, and shouldn't, care about templates, loader registry or i18n registry, it cares about data that carry the current state of the app per request. That been said, we are using So, I vote for keeping support for it, but as @ericf said, adding the proper docs explaining in details what are the implications about serializing functions, and adding few guidelines on how and when to use them. |
@caridy good points. I think you explained the pros/cons and primary use cases well. What I'll do, for now, is add docs and guidance about only serializing dependency-free, monadic functions that are for advanced configuration (like YUI config feature tests or Express route param formatters). @clarle my main worry is that people will use @caridy the reason I wanted to add support for functions to begin with is to support monadic config functions, like feature tests in YUI config or formatters for Express route params. I think code-inlining is a different problem that we want to solve with another package because it will involve modules, loaders, dependency resolution, and could simply deal with the string contents of a files to concat into the response body. |
Closing this. We are keeping support for functions. |
So this is something I´ve been thinking about for a couple of weeks.
Let's use the right terms: we can safely share functions with the client as long as they don't access anything in an outer scope. Remarkably, this is easy to check statically. I've been playing around with this small tool that Gozala wrote exactly for this use case: https://github.com/gozala/isoscope. Isoscope takes the AST for a function and gives you back a list of all the variables that function is closing over. For example: var esprima = require("esprima")
var enclosed = require("isoscope/enclosed")
// Parse some code
var form = esprima.parse(String(function fn(a, b) {
console.log(String(a) + b)
}))
// Get a function form we'll be analyzing
var fn = form.body[0]
fn.id
// => { type: "Identifier", name: "fn" }
// Get names of enclosed references
enclosed(fn)
// => [ "console", "String" ] This means |
Sounds like a cool idea. If we do this, we'd only want to do in dev-mode and not in prod because of the runtime cost. |
that means |
Yeah true, and the function will throw on the client because it will try to access something in the outer scope which doesn't exist. |
@ericf you don't know when it'll throw, which is dangerous part. |
How about this… someone could use these tools or some Making this feature exist outside of express-state to avoid @caridy's concerns. |
Yes, if I want to, I can check for myself. I just thought it could be a nice addition to prevent errors. I think it'd be nice if JS itself had a reflection API for this BTW. |
Serializing functions is tricky business that is basically not possible to get 100% correct without doing insane things. It also encourages bad practices of shipping app code over the wire by serializing it in the HTML. If this foot-gun is left it, it needs big scary docs about how this feature ought to be used!
The text was updated successfully, but these errors were encountered: