2012-12-20

Images embedding into HTML

Currently known image use in HTML do not have a support of reusing:
  • IMG tag do not refetch the file, but still issues http request
  • CSS background-image keeps the image in backround. Still it is the most powerful since it allows to reuse the image, pick the tiles, associate with CSS rule switch animation.
  • SVG embedding allow to reuse the image only by reusing its source text. Each image will have own instance in browser.
JS has a powerful gears for image reusing. The cloning image node will keep the same image reference. Unfortunately this optimization did not sneaked into JS libraries I am using.

Q. How to make image reusing convenient and still efficient on browser side?

Will be the preprocessor of HTML template the proper answer?
  1. It could strip off the URLs from IMG tags. Say by renaming src attribute.
  2. Load the image say in hidden node, keep it in global url to img map
  3. Upon image load and when widget dom is ready, roll over all IMG-es in DOM and swap with clone of cached one; apply all original attributes and CSS.
This method should work as compiler layer + runtime patch to DTK templated widgets;

PS. This note is just a response on multiple attempts around on image use optimization. As plain idea it is not worth anything, so bug me if you see the need for code or help there.

2012-12-05

ASSERT in JS

Usually asserts are not destructive. I.e. even if condition false-d, the code still working. Your evaluations throw-ing exceptions and changing the logic. Also there is a some additional logic resides in assert call:
  • in release build the code is disabled, ideally removed by compiler
  • log output with caller function
  • log evaluated value
  • log comment string
  • breakpoint call
Extra things I do in C++ and could potentially port to JS:
  • modal dialog with assert info(above)
  • ability to ignore further( do not display dialog), retry(repeat the condition call)
  • counter for particular assert failures
  • log not just to comsole but also to external target like FS or server.
There are some related patterns applied in debug mode
  • automatic parameters and return value check against type and permitted values. That should have the type definition and values/validators be available in run-time. Used Annotation for that. The call traps need enumeration of object's methods and wrapping by aspect.
  • Profiler( call counts+timing, call graph, etc) is not a hot topic anymore since most of browsers have them implemented. But in many cases like mobile, it worth to have the profiler be implemented by JS and hooked into server-side logger.
Final assert call I envision as such:

while( assert && assert( isDomNode(param1), "should be a valid dom node") );

or simpler form:

assert && assert( isDomNode(param1), "should be a valid dom node");

assert && assert(...) gives ability to disable assert for release build on AMD module switch level. It also could be used for individual module tuning.
while( assert(...) ) needed for "Retry" option. Handy for debugging. When problem found, the same condition could be reevaluated for step-through debugging.

Annotations and aspects are the gears for such functionality. 25% of above I have done earlier but not made a shippable tool. It is too big effort to be justified by projects I was working on at the time. If anyone interested we could talk further.