-
Notifications
You must be signed in to change notification settings - Fork 60
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
Helpers refactoring w/ stateful buffers, soon to contain live binding hookup #12
Conversation
vash.helpers was turned into the vash.Helpers class to separate running state of templates: Each vash.Helpers instance internally instantiates and maintains a separate Buffer.
Now passes the new layout unit tests for our alternative take on vash.Helpers
Conflicts: build/vash-runtime-all.js build/vash-runtime-all.min.js build/vash-runtime.js build/vash-runtime.min.js build/vash.js build/vash.min.js package.json src/vcompiler.js src/vruntime.js
Pulled in remaining changes from vcompiler.js by manual picking. (Too many changes with the internal rewrite to perform a merge without messing things up.) Now passes all unit tests.
@@ -131,7 +145,7 @@ | |||
var curr = i + start + 1; | |||
|
|||
return (curr === lineno ? ' > ' : ' ') | |||
+ (curr < 10 ? curr + ' ' : curr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe I specifically added this line relatively recently. Is there a reason for its removal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I added it was to correctly format error reporting for line numbers less than 10 (e.g. make them line up)
Awesome, I think this is on a great path. Once I hear back on the comment in Regarding Is this necessary? I understand the attempt at preventing mucking about, but given that there is still a direct route in via Regarding compile + link I think the primary goal should be "least surprise". What about:
Regardless, if we can always assume closures are valid, then I think your
The vash cli helper would be modified to either wrap each I did wonder if it would ever be beneficial to pass in an instance of Regarding compiler rewrite note I actually didn't do it for speed, but primarily to make it easier to understand what was going on as a developer. Dealing switching back and forth between quotes, static strings vs dynamic properties, etc. was just getting really messy. In previous jsperf tests (there's a link in the pre-rewrite code) I agree though that the names are potentially dangerous without control characters. Again, I was trying to keep it readable and easy to type, as the templates got complicated with helper additions. Alternatives could be Which part feels like I'm sacrificing correctness? I think we need to break out these discussions into more than one issue 8) |
Regarding The Regarding compile + link Regarding compiler rewrite note
I meant the 'hackiness' of using string substitution for placeholder tokens, which may cause problems if you have a token format that is too general. You have to consider that users may use string formatting functions inside the template (formatting could be a presentation/view concern and not a model concern) and thus all the general formatting masks such as %FOO%, #FOO, {FOO} etc. are all susceptible to being mangled. Imho you should probably be looking for something more exotic that a user is never expected to enter as a regular part of a template. (E.g. stuff like the zero width joiner character.) |
Alright cool, Regarding compiler strings I hadn't considered string formatting functions and the possibility for them to either mangle or be mangled. In that case, I wonder if just a straight string actually makes more sense, similar to what's used now: Do you have an example of how something could mangle or destroy a template? I could see this being bad (imagine documentation): And the string <a href="">VVMODNAMEVV</a> is used to ease code readability So I guess I'm looking for an example that is not Vash documentation. I'd want to avoid something super exotic, because typing a bunch of unicode escape sequences isn't any easier to do or read than before :) but it's an interesting idea... maybe something like Or I suppose I could just use an array again. |
Helpers refactoring w/ stateful buffers, soon to contain live binding hookup
I'll fix the reportError code myself just to expedite the other planned additions. We can keep discussing compiler stuff here for now, but live binding should be a separate pull request if it requires vash changes. |
I was already planning on having it as separate delivered pull requests. Hence the early commit of the stateful The Regarding the string substitution: you could always go back to using string concat for the template body code and only use the replacement technique with the head/foot boilerplate before inserting the template body. That would avert any potential problems with user-inserted comment matching a replacer token. The boilerplate was the biggest eye-sore with the concatenation anyway. |
Ha, I had basically the exact same idea! I'll get to work on it. |
An initial version of our refactoring to support live binding, as disussed in #8.
Stateful rendering context using Helpers calss
We've t turned the static
vash.helpers
into thevash.Helpers
class. This passes all current unit tests, does away with the live/die hack and internalizes buffer instances to theHelpers
instances.The latter effectively creates rendering contexts with separate state. Check out the changes to the helpers.layout.js file as well; we've solved the composition problem simply by using the nested compiled templates as actual nested templates (which now have proper nested, separate buffers).
The actual
vash.Helpers
class constructor is currently internal. Only the prototype is exposed, so users can add additional members to it. We did this to prevent users from mucking about with the internal state of the template engine (Ofcourse,Foo.prototype.constructor == Foo
so you can never really make it internal ...)Live binding hookup
This is coming; still working on it.
Known issues
For the moment, the above changes break the capability to have pre-compiled templates. (There is no way to pass in a valid
Helpers
instance as the second parameter to a template, and since the helpers now carry the isolated buffer, you really must have them and have them constructed in a controlled manner.)Probably it will be necessary to expose three functions if you want pre-compiled templates:
vash.compile
-- to compile the functionvash.link
-- to link the compiled function with a helpers instance (see `linkedFunc in the compiler)vash.build
-- compiles the function, links it with the helpers and returns it ready for use.This way, you can just do
vash.compile( <template> ).toString()
to get the pre-compiled function. Before it is used, you have to runvash.link( <func> )
to get the linked function (which you can run).vash.build
would be a shorthand that, when offered a string, would runcompile
beforelink
and would just runlink
otherwise.Ofcourse, we could also just roll this into the existing
vash.compile
function, but it feels better to use the actual correct terms. (compile + link == build)What is your opinion on this?
A note on the compiler rewrite to use regex token replacements
It's a nice idea to increase readability, however you may want to reconsider the actual token strings. I'd atleast make them a bit more robust by adding a control character you are unlikely to have have inside regular template body text. (Honestly, it feels like you're sacrificing correctness / stability for speed though. While Vash's origins are Razor, you may not want it to live that close to the edge...)