<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alexey Komov on Medium]]></title>
        <description><![CDATA[Stories by Alexey Komov on Medium]]></description>
        <link>https://medium.com/@alexey_komov?source=rss-3656edc3b58c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*AYPUiV_zqw2CMt0nSrxlFw.jpeg</url>
            <title>Stories by Alexey Komov on Medium</title>
            <link>https://medium.com/@alexey_komov?source=rss-3656edc3b58c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 07:31:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alexey_komov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Game of life in simplest terms]]></title>
            <link>https://medium.com/reflecting-on-bits/game-of-life-in-simplest-terms-ada16bbce2fb?source=rss-3656edc3b58c------2</link>
            <guid isPermaLink="false">https://medium.com/p/ada16bbce2fb</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[design]]></category>
            <dc:creator><![CDATA[Alexey Komov]]></dc:creator>
            <pubDate>Sun, 08 Jan 2017 18:48:21 GMT</pubDate>
            <atom:updated>2017-01-09T09:15:57.601Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi there!</p><p>I was always compelled to implement <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway’s game of life</a>. What a blunt start of the story 😆.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*pHDCnPbIi4IMxgiDG2H1RQ.gif" /><figcaption><strong>© goes to Wikipedia</strong></figcaption></figure><p>But yeah, I’ve always was astonished of how quite simple starting conditions and equally simple set of rules can lead to the ever growing complexity and vivid fluctuations of something… living. Or as close to living as squares can resemble 🤖🤖🤖.</p><p>Unlike some of my <a href="https://medium.com/reflecting-on-bits/hardest-bug-to-find-so-far-c80824bbc828#.faoqba27n">other</a> <a href="https://medium.com/reflecting-on-bits/physics-of-momentum-scrolling-3c1dd8ad18f4#.oxls9ufyp">stories</a>, this one would not be of a challenge persistently overcome. It was just three hours of coding and one of debugging, and yes, one more implementation of <a href="http://alexeykomov.me/game-of-life/">Game of life</a> was born. The world definitely needs more of that 😂.</p><p>What I really needed to consider is:</p><ol><li>With what technology to implement it.</li><li>What basic algorithm to choose.</li></ol><p>Well, being web developer slightly tilted to front-end, it’s no wonder that canvas I chose for the implementation, was, well… &lt;canvas&gt;. No pun was intended, of course.</p><p>So now to basic algo. It can be described as this: state1 =&gt; state2. And each point from state1 is transformed to point in state2 by following set of rules (© goes to Wikipedia):</p><ol><li>Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.</li><li>Any live cell with two or three live neighbours lives on to the next generation.</li><li>Any live cell with more than three live neighbours dies, as if by overpopulation.</li><li>Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.</li></ol><p>And to convince you further that exercise was more fun that challenging, here’s some essential pieces of code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7619b8ee38a150928a2c403e0c4f7766/href">https://medium.com/media/7619b8ee38a150928a2c403e0c4f7766/href</a></iframe><p>Here you can see how each cell modifies it’s future state based on 8 surrounding cells, walked clock-wise.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/116aa73cf6c97d03357e65a29859bb4d/href">https://medium.com/media/116aa73cf6c97d03357e65a29859bb4d/href</a></iframe><p>And in here, final state is drawn point by point, resulting in good old <em>O(nm)</em> complexity, where m = 8, but imagine three-dimensional field, where m would equal to 26, and n-dimensional... mmm… I can feel a new challenge… 🚀.</p><p>Anyways, here’s a few screenshots (too lazy for a GIF)…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*6peMckrDllKeE8PgpZB9oA.png" /><figcaption>Game of life in the beggining</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*-8jyrX3G8wliyn6VgqCpRQ.png" /><figcaption>And in the end</figcaption></figure><p>…and <a href="http://alexeykomov.me/game-of-life/">the link to fancy 404 page with game of life running</a>. And, of course, <a href="https://github.com/alexeykomov/game-of-life">the source code</a>.</p><p>I’m Alexey Komov — front-end developer and web enthusiast, interested in core and definitive Web things. Please share your thoughts about article. And you may contact me <a href="http://alexeykomov.me/">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ada16bbce2fb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/reflecting-on-bits/game-of-life-in-simplest-terms-ada16bbce2fb">Game of life in simplest terms</a> was originally published in <a href="https://medium.com/reflecting-on-bits">Reflecting on bits</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fun exercise: sum of numbers through infinite currying in JS.]]></title>
            <link>https://medium.com/reflecting-on-bits/fun-fact-sum-of-numbers-through-infinite-currying-in-js-a5c229765a18?source=rss-3656edc3b58c------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5c229765a18</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[job-interview]]></category>
            <dc:creator><![CDATA[Alexey Komov]]></dc:creator>
            <pubDate>Wed, 07 Sep 2016 15:15:34 GMT</pubDate>
            <atom:updated>2016-09-21T14:59:52.438Z</atom:updated>
            <content:encoded><![CDATA[<p>There’s a fun question (depending on you definition of fun, of course 😆). How do you write function <strong>sum</strong>, so that the following is true?</p><pre>sum(1) == 1;//true<br>sum(1)(2) == 3;//true<br>sum(1)(4)(1) == 6;//true</pre><pre>/*And works on any number of curried calls, adding numbers from each call.*/</pre><p>Comment here with your solutions. :) If you’re not sure (like was I when was first asked this 🤔) or curious, read on.</p><p>Notice two important parts here: how we check equality of function result (with ==) and how every result of <strong>sum</strong> call is callable itself. From point two it’s obvious that every result of sum’s call is function itself.</p><p>From ECMAScript standard we know that when operator == is performed on values of two different types, both are coerced to number. Right hand is already a number and left hand (which is function) will obviously be <strong>NaN</strong>. You can check it with explicit convertion to number:</p><pre>+function(){};//NaN </pre><p>So, tests like this are equivalent</p><pre>sum(1) == 1;//true<br>sum(1)(2) == 3;//true<br>sum(1)(4)(1) == 6;//true</pre><pre>+sum(1);//1<br>+sum(1)(2);//3<br>+sum(1)(4)(1);//6</pre><p>And how do we override this “to NaN” behavior and return whatever value we want? By implementing our own <strong>valueOf</strong>. So now, at least this part is clear:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a82a9ebb3fb8ba849e30218c830e0108/href">https://medium.com/media/a82a9ebb3fb8ba849e30218c830e0108/href</a></iframe><p>Okay, now it works with</p><pre>sum(1) == 1; </pre><p>condition at least. And how do we implement <strong>newFunction</strong> from above?</p><p>To chain call we would like to return <strong>sum</strong> itself. But if we did that</p><pre>const newFunction = sum;</pre><p>, result of every chained call will be argument <strong>firstArgument</strong> returned, not sum.</p><p>So lets wrap <strong>sum</strong> with function that takes argument and calls sum with this new argument + old argument <strong>firstArgument </strong>from main sum function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0de10b798451b9ff485463d5559cad99/href">https://medium.com/media/0de10b798451b9ff485463d5559cad99/href</a></iframe><p>Okay, let’s test this implementation.</p><pre>+sum(1)<br>1<br>+sum(1)(2)<br>3<br>+sum(1)(2)(3)<br>6</pre><p>Looks OK!</p><p>Those of you who eager to get a solution could stick to this one ✋. But if you want to elaborate even further, read on.</p><p>Okay, to be honest, I don’t like previous solution that much. How about this one?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51f15500faa5e7fb7edea3f9ca3949c4/href">https://medium.com/media/51f15500faa5e7fb7edea3f9ca3949c4/href</a></iframe><p>Here, we pass the stack of all accumulated arguments down to recursive call. And in <strong>valueOf</strong> call, stack is transformed to a number via reduce just in time. This gives us more flexibility, imo, if we wanted to change chained sum to chained multiplication or whatever else — we just plug in different reduce function.</p><p>Okay, this time that’s all for certain 😂. Cya.</p><p>I’m Alexey Komov — front-end developer and web enthusiast, interested in core and definitive Web things. Please share your thoughts about article. And you may contact me <a href="http://alexeykomov.me/">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5c229765a18" width="1" height="1" alt=""><hr><p><a href="https://medium.com/reflecting-on-bits/fun-fact-sum-of-numbers-through-infinite-currying-in-js-a5c229765a18">Fun exercise: sum of numbers through infinite currying in JS.</a> was originally published in <a href="https://medium.com/reflecting-on-bits">Reflecting on bits</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Internals of momentum scrolling in GMail iOS web app are pretty interesting!]]></title>
            <link>https://medium.com/reflecting-on-bits/physics-of-momentum-scrolling-3c1dd8ad18f4?source=rss-3656edc3b58c------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c1dd8ad18f4</guid>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Alexey Komov]]></dc:creator>
            <pubDate>Thu, 02 Jun 2016 16:26:31 GMT</pubDate>
            <atom:updated>2016-09-07T15:26:42.612Z</atom:updated>
            <content:encoded><![CDATA[<p>Why would anyone want to re-implement scrolling in browser? The quick answer would be: in modern times — no-one. Isn’t it obvious? Well, it turns out, not quite.</p><p>When developing one of my projects — <a href="http://reflectcal.com/">Reflect calendar,</a> I’ve encountered the issue in iOS Safari ‘s scrolling— whenever you update/create scrollable element via setting innerHTML of its parent, scrollable field doesn’t get its “scrollablity” immediately. Sometimes you drag the whole screen instead of dragging only scrollable area’s content.</p><p>This problem also manifests itself when you’re dragging the whole screen by some element outside of scrollable area, and then trying to scroll inside that area. You should wait until the whole screen “settles down” and approximately one more second to do that. Looks like this problem has a name — “overscroll”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*cQqmLnDFVyRN0JwbKpQQYQ.gif" /></figure><p>Now, is it really worth a concern doing something about it? After all, native is native and we won’t get any better option, right?</p><p>Here’s where we fall to this <a href="http://blog.codinghorror.com/avoiding-the-uncanny-valley-of-user-interface/">“uncanny valley” of UI</a>. Say, you want your web-application to feel “native” as much as it reasonable in mobile web browser. But, with things like overscroll happening, a user will notice that it’s not nearly as native as iOS gives you through SDK.</p><p>Sure, if you wanted native, you’d go with React Native and all, but why not push it to the limits when it’s possible? And in modern web — it’s totally possible. Moreover — it has been done! By Ryan Fioravanti, for Gmail web version for iOS (which means two separate versions for iPhone/iPod and iPad). Here’s the <a href="https://web.archive.org/web/20141001100814/https://developers.google.com/mobile/articles/webapp_fixed_ui#bg">article</a> by Ryan. I suggest you read it — it’s really good. It was done for another reason — Safari didn’t have position:fixed back then, and overflow-scrolling: touch had yet to come.</p><p>Anyway, let’s dive into the article and see whether it’s possible to implement momentum scrolling in JS as close to the native as possible.</p><p>We’re discovering immediately that Ryan’s solution is based on transitions — the simplest possible animations. And another good part that these particular transitions are hardware accelerated — the ones that animate opacity, translation, rotation and scaling. So, basically, making scrolling through transitions comes down to prepare right end state, commit it to the element which will be scrolled and then give it proper transition flavor, so that element will move its way gracefully and up to a user’s expectations.</p><p>First, let’s show what momentum scrolling will look in the end.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*PG1jOvS3KnQhpS_bxDGt2A.gif" /></figure><p>Ryan’s article describes basis DOM setup, drag’n’drop part via touch events and translate3d transform property. This should be fairly intuitive. Now, the heart of logic is this method — doing momentum whenever user releases touch with some velocity prior to that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20726786624cdd2cb849c8a50df5e8f1/href">https://medium.com/media/20726786624cdd2cb849c8a50df5e8f1/href</a></iframe><p>As you can see, we’re doing here: getting end velocity of user’s touch (<a href="https://gist.github.com/alexeykomov/d27ebc94b1f302d03baf5ba7957d199b#file-do-momentum-js-L2">line #2</a>), calculating displacement(<a href="https://gist.github.com/alexeykomov/d27ebc94b1f302d03baf5ba7957d199b#file-do-momentum-js-L7">line #7</a>) and time (<a href="https://gist.github.com/alexeykomov/d27ebc94b1f302d03baf5ba7957d199b#file-do-momentum-js-L8">line #8</a>). The we’re updating element with new position (<a href="https://gist.github.com/alexeykomov/d27ebc94b1f302d03baf5ba7957d199b#file-do-momentum-js-L28">line #28</a>) via translation. And, finally, in order for movement to look natural, we’re applying proper transition (<a href="https://gist.github.com/alexeykomov/d27ebc94b1f302d03baf5ba7957d199b#file-do-momentum-js-L21">line #21</a>) to scrollable element. This transition function is <strong>cubic-bezier(0.33, 0.66, 0.66, 1)</strong> and is kindly given by Ryan.</p><p>Let’s take a moment and reconsider this more carefully.</p><p>First, some theory and formulae for accelerated movement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*3ZYRa-ZdSHX1zJpBqOiLbg.png" /><figcaption>Fig 1. Velocity</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*3lo-InaOjkgAwatiq-mONw.png" /><figcaption>Fig 2. Displacement</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*sPUX1kKNiaSQz9GGcE2wSQ.png" /><figcaption>Fig 3. Time</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*1mb37V5aKbp7-OtcaNmrXQ.png" /><figcaption>Fig4. Displacement</figcaption></figure><p>Last formula (Fig. 4) is derived from formula on Fig. 2 by moving x minus x0 to the left and replacing time by expression in Fig. 3. So we’re using these formulae to get displacement and time in the <strong>doMomentum</strong> function, nothing particularly special.</p><p>Now that we know how far and how long our movement will take, it’s time to apply proper transition to this movement. Fortunately, Ryan gives us one — cubic bezier for slow deceleration ending in full stop (velocity is zero).</p><p>So now we have basic implementation of how content will behave should it decelerate to zero velocity.</p><p>Also, quite simple is the returning content from out of bounds position to frame. Here’s how it looks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*eYS2F9_GMHWH7Yao4kr4Pg.gif" /></figure><p>Implementation of this method is here.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b83460c5ab1cdbfe1da94ad911e0f36/href">https://medium.com/media/0b83460c5ab1cdbfe1da94ad911e0f36/href</a></iframe><p>As you can see, it it simply detection of whether we’re out of bounds and then applying transition to return content into frame. Transition function is simple ease-out and time is constant 500ms.</p><p>Now the most complex case is left — when deceleration takes content out of bounds. In this case, we have three stages with separate transition each:</p><ol><li>Transition that takes content exactly to bounds.</li><li>Transition that takes content from the edge to zero velocity.</li><li>Transition that returns content to bounds (snapToBounds).</li></ol><p>To better illustrate this, here’s visualization.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*MrsLEa86DbUcE7jYAOc_pw.gif" /></figure><p>Actually, part 2 is simple deceleration to zero velocity, which we’ve already implemented.</p><p>The interesting part is 1 — we need to translate content with non-zero velocity left in the end. This requires us different bezier function, but which one? Ryan writes obscurely that this case is outside of article’s scope.</p><p>This is apex point of my research in this problem and it brought me incredible satisfaction to finally find the answer! The feeling which, I believe, each engineer is striving for.</p><p>Turns out, the look of function will be different for each end velocity. Let us illustrate it by this pictures.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*veDWBM_Qd1iW-diPjoH7wg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*deYf-34nZ5FlqVEvqSuxNw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*hWo1JLT-TAdMSLjZDCz2iA.png" /><figcaption>Different bezier deceleration functions.</figcaption></figure><ol><li>On first picture, there’s an edge case — deceleration takes content to zero end velocity.</li><li>On second, end velocity is bigger than zero.</li><li>On third, end velocity is the same as start velocity.</li></ol><p>So you see relation here — the less end velocity will differ from start velocity, the more flat cubic bezier function will be, up to the limit of simple line (<a href="http://cubic-bezier.com/#.33,.33,.66,.66"><strong>cubic-bezier(.33,.33,.66,.66)</strong></a><strong> </strong>or <strong>linear</strong>). The maximal difference between start and end velocity is when end velocity is zero. In this case function will be <strong>cubic-bezier(0.33, 0.66, 0.66, 1)</strong>. And in any case in between, the function will look like <strong>cubic-bezier(0.33, 0.66-x, 0.66, 1-x)</strong> where <strong>x = abs(endVelocity/startVelocity)</strong>.</p><p>So we’re finally here! The demo of working implementation could be seen <a href="http://alexeykomov.me/momentum-scroller-demo">http://alexeykomov.me/momentum-scroller-demo</a>.</p><p>Thanks you for attention.</p><p>I’m Alexey Komov — front-end developer and web enthusiast, interested in core and definitive Web things. Please share your thoughts about article. And you may contact me <a href="http://alexeykomov.me/">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c1dd8ad18f4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/reflecting-on-bits/physics-of-momentum-scrolling-3c1dd8ad18f4">Internals of momentum scrolling in GMail iOS web app are pretty interesting!</a> was originally published in <a href="https://medium.com/reflecting-on-bits">Reflecting on bits</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to solve ‘missing %output%’ error when using Closure Compiler.]]></title>
            <link>https://medium.com/reflecting-on-bits/how-to-solve-missing-output-error-when-using-closure-compiler-7de6eac29776?source=rss-3656edc3b58c------2</link>
            <guid isPermaLink="false">https://medium.com/p/7de6eac29776</guid>
            <category><![CDATA[closure]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Alexey Komov]]></dc:creator>
            <pubDate>Thu, 28 Apr 2016 13:37:10 GMT</pubDate>
            <atom:updated>2016-09-07T20:51:20.144Z</atom:updated>
            <content:encoded><![CDATA[<pre>TLDR; Use --output_wrapper_file option.</pre><p>If you’re, like me, using <strong>closurebuilder.py</strong> to launch Closure Compiler compilation using <strong>output_wrapper</strong> option, you might be encountering this error:</p><pre>ERROR — invalid output_wrapper specified. Missing ‘%output%’.</pre><p>Though the output wrapper in my case had %output% included and was the following:</p><pre><em>//UMD bundling closure code inside.<br></em>;(<strong>function </strong>(root, factory) {<br>    <strong>if </strong>(<strong>typeof </strong>define === <strong>&#39;function&#39; </strong>&amp;&amp; define.amd) {<br>        define([], factory);<br>    } <strong>else if </strong>(<strong>typeof </strong>module === <strong>&#39;object&#39; </strong>&amp;&amp; module.exports) {<br>        module.exports = factory();<br>    } <strong>else </strong>{<br>        root.MomentumScroller = factory();<br>  }<br>}(<strong>this</strong>, <strong>function </strong>() {<br>  %output%<br>  <strong>return </strong>MomentumScroller;<br>}));</pre><p>What did the trick here was putting output wrapper in a separate file, say, output-wrapper.js and using it with <strong>output-wrapper-file</strong> option.</p><p>Hope it helps. Cheers!</p><p>I’m Alexey Komov — front-end developer and web enthusiast, interested in core and definitive Web things. Please share your thoughts about article. And you may contact me <a href="http://alexeykomov.me/">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7de6eac29776" width="1" height="1" alt=""><hr><p><a href="https://medium.com/reflecting-on-bits/how-to-solve-missing-output-error-when-using-closure-compiler-7de6eac29776">How to solve ‘missing %output%’ error when using Closure Compiler.</a> was originally published in <a href="https://medium.com/reflecting-on-bits">Reflecting on bits</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to export closure-compiled code in npm module.]]></title>
            <link>https://medium.com/reflecting-on-bits/how-to-publish-npm-module-with-code-produced-by-closure-compiler-in-it-f8d766205f6f?source=rss-3656edc3b58c------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8d766205f6f</guid>
            <category><![CDATA[closure]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[Alexey Komov]]></dc:creator>
            <pubDate>Wed, 06 Apr 2016 16:01:39 GMT</pubDate>
            <atom:updated>2016-09-07T20:48:16.238Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, I’ve discovered that it’s somewhat non-trivial to add block of closure-compiled code to npm as a module.</p><p>In order for it to be discoverable by Node.js module system, I did the following:</p><ol><li>Export non-obfuscated name of public API as a global variable (or module-scoped variable in case of Node.js).</li><li>Wrap compiled code with some of the <a href="https://github.com/umdjs/umd/blob/master/templates/returnExports.js">UMD</a> patterns.</li></ol><p>Let’s explain it by example. Say, I have some class called <strong>rflect.ui.MomentumScroller</strong> and I want it to be exported by name <strong>MomentumScroller</strong>.</p><pre>/* Closure-compiled code which defines rflect.ui.MomentumScroller is here. */</pre><pre>goog.exportSymbol(&#39;MomentumScroller&#39;,       rflect.ui.MomentumScroller);</pre><p>By this point, I could’ve already used it in browser by global name <strong>MomentumScroller</strong>. But, in order for it to be used as npm module, let’s wrap it with:</p><pre>(<strong>function </strong>(root, factory) {<br>    <strong>if </strong>(<strong>typeof </strong>define === <strong>&#39;function&#39; </strong>&amp;&amp; define.amd) {<br>        define([], factory);<br>    } <strong>else if </strong>(<strong>typeof </strong>module === <strong>&#39;object&#39; </strong>&amp;&amp; module.exports) {<br>        module.exports = factory();<br>    } <strong>else </strong>{<br>        root.MomentumScroller = factory();<br>  }<br>}(<strong>this</strong>, <strong>function </strong>() {<br>  <em>%output%</em><br>  <strong>return </strong>MomentumScroller;<br>}));</pre><p>…where <em>%output%</em> is placeholder for your closure-compiled code. Actually, it’s one step from final compilation command:</p><pre><strong>python <br>src/closure/closure/bin/build/closurebuilder.py <br>-n rflect.ui.MomentumScroller <br>--root=src/closure <br>--root=src/reflect <br>--root=src/momentum-scroller <br>-o compiled <br>--output_file=build/momentum-scroller.js --compiler_jar=bin/compiler.jar <br>--compiler_flags=\&quot;--charset=utf-8\&quot; <br>--compiler_flags=\&quot;--compilation_level=ADVANCED_OPTIMIZATIONS\&quot; --compiler_flags=\&quot;--summary_detail_level=3\&quot; <br>--compiler_flags=\&quot;--warning_level=VERBOSE\&quot; <br>--compiler_flags=\&quot;--js=src/closure/closure/goog/deps.js\&quot; --compiler_flags=\&quot;--debug=false\&quot; <br>--compiler_flags=\&quot;--source_map_format=V3\&quot; <br>--compiler_flags=\&quot;--closure_entry_point=rflect.ui.MomentumScroller\&quot; --compiler_flags=\&quot;--language_in=ECMASCRIPT6\&quot; <br>--compiler_flags=\&quot;--language_out=ECMASCRIPT5\&quot; --compiler_flags=\&quot;--externs=src/externs.js\&quot; <br>--compiler_flags=\&quot;--define=&#39;goog.DEBUG=false&#39;\&quot; --compiler_flags=\&quot;--js=src/momentum-scroller/api.js\&quot; --compiler_flags=\&quot;--process_common_js_modules\&quot; </strong></pre><pre><strong><em>--compiler_flags=\&quot;--output_wrapper=;(function (root, factory) {\n    if (typeof define === &#39;function&#39; &amp;&amp; define.amd) {\n        define([], factory);\n    } else if (typeof module === &#39;object&#39; &amp;&amp; module.exports) {\n        module.exports = factory();\n    } else {\n        root.MomentumScroller = factory();\n  }\n}(this, function () {\n  %output%\n  return MomentumScroller;\n}));\&quot;</em></strong></pre><p>Important part of this scary command is in italic, we’ve just used “<strong>/n</strong>”s instead of newlines.</p><p><em>So, in order to export closure-compiled code in npm module, we could use Closure Compiler’s </em><strong><em>— output_wrapper</em></strong><em> option and exported public API variable.</em></p><p>If you’ve found more effective ways to export closure-compiled code in Node.js, <a href="/service/mailto:alexeykcontact@gmail.com"><strong>please let me know</strong></a>.</p><p>Cheers!</p><p>I’m Alexey Komov — front-end developer and web enthusiast, interested in core and definitive Web things. Please share your thoughts about article. And you may contact me <a href="http://alexeykomov.me/">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8d766205f6f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/reflecting-on-bits/how-to-publish-npm-module-with-code-produced-by-closure-compiler-in-it-f8d766205f6f">How to export closure-compiled code in npm module.</a> was originally published in <a href="https://medium.com/reflecting-on-bits">Reflecting on bits</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go chromeless. Or don’t?]]></title>
            <link>https://medium.com/@alexey_komov/go-chromeless-or-dont-57f6b633b8f6?source=rss-3656edc3b58c------2</link>
            <guid isPermaLink="false">https://medium.com/p/57f6b633b8f6</guid>
            <dc:creator><![CDATA[Alexey Komov]]></dc:creator>
            <pubDate>Fri, 24 Oct 2014 17:48:03 GMT</pubDate>
            <atom:updated>2016-09-05T11:02:17.680Z</atom:updated>
            <content:encoded><![CDATA[<p>Should you banish browser UI or take your best from it?</p><p>Debates between web and native is a whole story of itself. A lot have been said from both sides, good example being this video from Google I/O.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F4f2Zky_YyyQ%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D4f2Zky_YyyQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F4f2Zky_YyyQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/aadfd7c2b8d9ba3b72e6ad226af57b8e/href">https://medium.com/media/aadfd7c2b8d9ba3b72e6ad226af57b8e/href</a></iframe><p>I’d like to focus more on web apps, which are presented as native-like apps, being saved with “Save to home screen” functionality which is present in iOS. Initially, when I saved application like this, it created fancy icon on home screen, but I thought, like, ah, it’s good to have app completely chromeless, so no browser UI is shown, just controls that you own.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C0XSA4uMsG13gWQFuoG5IA.png" /></figure><p>And there are apps that perform just that — see <a href="https://vk.com/">https://vk.com</a> web app, for example. So of course, this is possible, with &lt;meta name=“apple-mobile-web-app-capable” content=”yes”&gt; meta tag (<a href="https://developer.apple.com/library/safari/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html">https://developer.apple.com/library/safari/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*qwykBLZ08eT6HQUf9H-yfw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*SZmFzO5alc3opLAXK17UVg.png" /></figure><p>But when I saved my app to home screen, chromeless, some doubt appeared. You see, we have some truly fundamental browser UI that we are so used to, so we are hardly noticing them:</p><ol><li>Back button.</li><li>URL bar.</li><li>Reload button.</li></ol><p>Back button could be used with HTML5 history enabled application, you may navigate screen back with it, for example. Worth to keep? Likely so.</p><p>URL gets more and more abstracted from end user, starting from iOS 7 and later, with Chrome. Besides, if we saved app as chromeless, we already navigated to it by accessing shortcut, which did navigation function for us instead of typing URL and pressing Enter. URL also helps to track where we are in structure of UI, if application is HTML5 history enabled, again. But, it’s also very important to share state of application, to be able send it easily somewhere. Worth to keep? Likely so.</p><p>Reload button. Web application may fail, throw an exception or not perform async task in time and leave “waiting” spinner visible forever. Even GMail, being shining example of modern complex web-app, crashed occasionally. But guess what? Where in native world you have to go to processes and kill one that belongs to app (which can be ambiguous), in web world you can just reload page, <strong>effectively resetting state</strong>. Worth keeping? Yes, and pretty important, too. And it’s here where my main argument resides.</p><p>My point goes as that: for complex web app it’s better to leave browser UI as is. What’s your opinion on it?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57f6b633b8f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hardest bug to find. So far…]]></title>
            <link>https://medium.com/reflecting-on-bits/hardest-bug-to-find-so-far-c80824bbc828?source=rss-3656edc3b58c------2</link>
            <guid isPermaLink="false">https://medium.com/p/c80824bbc828</guid>
            <dc:creator><![CDATA[Alexey Komov]]></dc:creator>
            <pubDate>Thu, 10 Oct 2013 12:21:40 GMT</pubDate>
            <atom:updated>2016-09-07T20:52:11.047Z</atom:updated>
            <content:encoded><![CDATA[<h4>Be wary… Closure compiler related post. And, by accident, my first post on medium. So be wary even more. 😆</h4><p>For a months I was designing and developing my own app, which undoubtedly could be considered as large javascript application. The story of developing and being enlightened with ideas is most surely a story of itself, so maybe it will be told some time in the future. For now it’ll be enough to say that choice of <a href="https://code.google.com/p/closure-library/">closure library</a> and <a href="https://developers.google.com/closure/compiler/">closure compiler</a> was only logical for an app of such scale.</p><p>Most development time was spent working with code in uncompiled form, though I did some compilation runs just to see what size compiled code would have. It was actually fun to see how code grew to 50k, then to 70k and then to dreadful — for that point — 100k. Then I added gzip ant task on top of compile task, just to easy myself that ungzipped size is not that important, but gzipped size is.</p><p>So it was finally time to compile whole application and to see if compilation broke any functionality.</p><p>And it did.</p><blockquote>Uncaught TypeError: boolean is not a function</blockquote><p>What was considered a boolean, mapped through source map in Chrome to perfectly looking function:</p><blockquote>this.mainPaneBuilder_ = new rflect.cal.ui.MainPaneBuilder(this.viewManager_, …)</blockquote><p>That said, compiler at that stage was generating 109 warnings, most of them were of private properties being overridden and class methods being incorrectly overridden, too. So at first, I tried to remove every and each warning, just to be sure that compiler do not complain. It was one strange thing though: —debug (dash-dash-debug) option was producing error-less compiled code.</p><p>So I removed each warning except one, coming from loader class:</p><blockquote>WARNING — Bad type annotation. Unknown type rflect.cal.Loader * @this {rflect.cal.Loader} ^</blockquote><p>You may notice @this directive here. That’s because earlier compiler was complaining that my non-constructor, plain object has methods that reference “this” in them. And I didn’t want to get rid of “this” inside those methods. You see, Loader is just plain object here.</p><pre>goog.provide(&#39;rflect.cal.Loader&#39;);</pre><pre>goog.require(&#39;goog.events&#39;);<br>goog.require(&#39;rflect.Debug&#39;);<br>goog.require(&#39;rflect.cal.Main&#39;);</pre><pre><br>/**<br> * Binds creation of cal instance to load events.<br> * <a href="http://twitter.com/this">@this</a> {rflect.cal.Loader}<br> */<br>rflect.cal.Loader.main = function() {<br> goog.events.listenOnce(window, &#39;load&#39;, function(aEvent) {<br> // Load event will fire later than dom ready.<br> if (!this.documentLoaded_)<br> this.calInstance_ = new rflect.cal.Main();<br> }, false, this);</pre><pre> goog.events.listenOnce(window, &#39;DOMContentLoaded&#39;, function(aEvent) {<br> this.documentLoaded_ = true;<br> this.calInstance_ = new rflect.cal.Main();<br> }, false, this);</pre><pre> goog.events.listenOnce(window, &#39;unload&#39;, function(aEvent) {<br> if (this.calInstance_)<br> this.calInstance_.dispose();<br> }, false, this);<br>};</pre><p>But warning was about unknown type, isn’t it? I recalled that previous such cases were solved by adding <em>deps.js</em> file to compilation, as suggested <a href="http://code.google.com/p/closure-library/wiki/FrequentlyAskedQuestions#When_I_compile_with_type-checking_on,_I_get_warnings_about_%22">here</a>. But for this particular case it didn’t help, so I forgot about it for a while.</p><p>So, I was at the point when compiler emitted one warning ignored by me and erroneous compiled code. Hm…</p><p>Next thing I remembered that with —debug directive code was working. So in compiler source code I opened method which added options to compilation.</p><pre>public void setDebugOptionsForCompilationLevel(CompilerOptions     options) { <br>  options.anonymousFunctionNaming =      AnonymousFunctionNamingPolicy.UNMAPPED;<br>  options.generatePseudoNames = true;<br>  options.removeClosureAsserts = false;<br>  // Don’t shadow variables as it is too confusing.<br>  options.shadowVariables = false;<br>}</pre><p>So there are 4 possibilities which could be responsible for broken code. By rebuilding compiler with different options on/off, I tracked one that was responsible: essential one — variable renaming (options.generatePseudoNames being false).</p><p>Then I tried to search variable name (“Rk” most of the time, but there were different namings) without source map, in compiled code. And… voila, I spotted such a piece (by luck, just because it was at the very end of compiled file):</p><pre>function Vl() {<br>  Ub(window, “load”, function() {<br>    this.Rk || (this.Vg = new Tl)<br> }, !1, this);<br>  Ub(window, “DOMContentLoaded”, function() {<br>    this.Rk = !0;<br>    this.Vg = new Tl<br> }, !1, this);<br> Ub(window, “unload”, function() {<br>    this.Vg &amp;&amp; this.Vg.K()<br> }, !1, this)<br>} </pre><p>Do you see any familiarity here? :) It was compiled code for loader, which, here, used “this” to assign property “Rk”. Now what we see:</p><ol><li>Initially, in uncompiled file “this” inside of plain object was ok, because object existed.</li><li>After compilation, object was dissolved, and its method became global function, which assigned “Rk” to global space, which, by turn, clobbered some another “Rk” property (in my case, constructor function) which became boolean, true one!</li><li>Compilation with —debug statement worked because names won’t collide, being derived from original ones, like “$rflect$cal$Loader$main$$”.</li><li>When I redesigned loader as class with constructor and prototype, not only it removed error, but also strange warning of unknown type.</li></ol><p>So morals here: do not use “this” inside of plain objects, but only in constructors and prototypes. This I knew and it was told <a href="https://developers.google.com/closure/compiler/docs/limitations">here</a>. But what I didn’t know is that @this directive won’t help to preserve object to which “this” is pointing! It’ll just silence compiler. So my version of this rule is — do not ever use “this” outside of constructors and prototype methods AND do not attempt to silence compiler by @this directive.</p><p>Hoped you were enjoying the reading. Thanks for attention. See ya.</p><p>I’m Alexey Komov — front-end developer and web enthusiast, interested in core and definitive Web things. Please share your thoughts about article. And you may contact me <a href="http://alexeykomov.me/">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c80824bbc828" width="1" height="1" alt=""><hr><p><a href="https://medium.com/reflecting-on-bits/hardest-bug-to-find-so-far-c80824bbc828">Hardest bug to find. So far…</a> was originally published in <a href="https://medium.com/reflecting-on-bits">Reflecting on bits</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>