<?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:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[Ideas Into Software]]></title><description><![CDATA[Musings on writing code]]></description><link>http://ideasintosoftware.com/</link><generator>Ghost 0.7</generator><lastBuildDate>Sun, 19 Sep 2021 11:46:41 GMT</lastBuildDate><atom:link href="http://ideasintosoftware.com/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Exhaustive Switches in TypeScript]]></title><description><![CDATA[<p>Let's say we all love ice cream. So we define the following enum in TypeScript:  </p>

<pre><code>enum IceCreamTaste {  
  awesome,
  meh,
  dunnoYet
}
</code></pre>

<p>And now we want a switch that returns an answer to your friend's question "How does your ice cream taste?". Like so:</p>

<pre><code>function getAnswer(taste: IceCreamTaste) {  
    switch (taste) { 
        case IceCreamTaste.</code></pre>]]></description><link>http://ideasintosoftware.com/exhaustive-switch-in-typescript/</link><guid isPermaLink="false">640834e4-c21e-475f-8ec2-4a9957a30234</guid><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Tue, 17 Jul 2018 12:06:38 GMT</pubDate><content:encoded><![CDATA[<p>Let's say we all love ice cream. So we define the following enum in TypeScript:  </p>

<pre><code>enum IceCreamTaste {  
  awesome,
  meh,
  dunnoYet
}
</code></pre>

<p>And now we want a switch that returns an answer to your friend's question "How does your ice cream taste?". Like so:</p>

<pre><code>function getAnswer(taste: IceCreamTaste) {  
    switch (taste) { 
        case IceCreamTaste.awesome:
            return `It's like a party in my mouth!`;
        case IceCreamTaste.meh:
            return 'Umm I think someone has eaten this before me.';
         case IceCreamTaste.dunnoYet:
            return 'Lemme try it first, ok?';
        default:
            throw new Error('unreachable case');
    } 
}
</code></pre>

<p>Now, it sure would be nice if TypeScript could hand-hold us a little and <strong>verify that we've covered all the possible values</strong> of the  <code>IceCreamTaste</code> enum, right? Otherwise if I add, say, a new <code>chocolate</code> taste (which is obviously above <code>awesome</code>), I would have to remember to update all the switches that reference the enum and...oh hey, welcome, bugs!</p>

<h4 id="unreachability">Unreachability</h4>

<p>Fortunately TypeScript 2+ sports a useful type called <code>never</code> (sometime referred to as the "bottom" type) which represents a value that can never occur.</p>

<p>OK, so how is that useful, you ask? Well, because our <code>IceCreamTaste</code> switch is exactly such a case - we want to tell the TypeScript compiler that the <code>default</code> branch of the switch statement is one of those "code execution can never get there" blocks.</p>

<p>Let's define an <code>UnreachableCaseError</code>:  </p>

<pre><code>class UnreachableCaseError extends Error {  
    constructor(val: never) { 
        super(`Unreachable case: ${val}`);
    }
}
</code></pre>

<p>Now if we plug this error into our ice cream switch, we get this type safe bliss:</p>

<pre><code>function getAnswer(taste: IceCreamTaste) {  
    switch (taste) { 
        case IceCreamTaste.awesome:
            return `It's like a party in my mouth!`;
        case IceCreamTaste.meh:
            return 'Umm I think someone has eaten this before me.';
         case IceCreamTaste.dunnoYet:
            return 'Lemme try it first, ok?';
        default:
            throw new UnreachableCaseError(taste);
    } 
}
</code></pre>

<p>Try commenting out one of the cases and TypeScript will report an <code>Argument of type 'IceCreamTaste.dunnoYet' is not assignable to parameter of type 'never'</code> error.</p>

<p>Happy exhaustive switching! I'm <a href="https://twitter.com/tomas_brambora">@tomasbrambora</a></p>]]></content:encoded></item><item><title><![CDATA[TypeScript Module Declaration Vs Augmentation]]></title><description><![CDATA[<p>Just a quick writeup on a simple but perhaps not an entirely obvious <a href="https://www.typescriptlang.org/docs/handbook/declaration-merging.html">feature</a> of TypeScript.</p>

<p>TypeScript modules are written in one of two modes: <code>declaration</code> mode or <code>augmentation</code> mode. The difference is that in <em>declaration</em> mode, you can declare new top-level declarations - wow, that's a mouthful - but</p>]]></description><link>http://ideasintosoftware.com/typescript-module-augmentation-vs-declaration/</link><guid isPermaLink="false">1e2d2bfc-1093-4231-836b-7c8030e7012b</guid><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Tue, 17 Jul 2018 11:33:44 GMT</pubDate><content:encoded><![CDATA[<p>Just a quick writeup on a simple but perhaps not an entirely obvious <a href="https://www.typescriptlang.org/docs/handbook/declaration-merging.html">feature</a> of TypeScript.</p>

<p>TypeScript modules are written in one of two modes: <code>declaration</code> mode or <code>augmentation</code> mode. The difference is that in <em>declaration</em> mode, you can declare new top-level declarations - wow, that's a mouthful - but you can't update existing ones whereas in <em>augmentation</em> mode it's vice versa.</p>

<p>Real-life example is <a href="https://github.com/Microsoft/TypeScript/issues/14325">extending React to support onTouchTap attribute</a>.</p>

<p>If you have a, say, <code>on-touch-tap-react.d.ts</code> declaration file that looks like this</p>

<pre><code class="language-typescript">declare module 'react' {  
    interface HTMLProps&lt;T&gt; {
        onTouchTap?: React.EventHandler&lt;React.TouchEvent&lt;T&gt;&gt;;
    }
}
</code></pre>

<p>You'll get a flurry of errors from <code>tsc</code> because, whoa, you've just overwritten <code>react</code>.</p>

<p>What you need to do instead is:</p>

<pre><code class="language-typescript">import React from 'react';

declare module 'react' {  
  interface HTMLProps&lt;T&gt; {
    onTouchTap?: React.EventHandler&lt;React.TouchEvent&lt;T&gt;&gt;;
  }
}
</code></pre>

<p>The only difference is the <code>import</code> statement at the top of the file which switches the module to the <em>augmentation</em> mode, hence updating <code>react</code>rather than overwriting it.</p>

<p>Well, good to know.</p>

<p>Happy augmenting! I'm <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>.</p>]]></content:encoded></item><item><title><![CDATA[Typescript: Omit And Projection Types]]></title><description><![CDATA[<p>Typescript 2 introduced a number of useful mapped types such as <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html"><code>Pick</code></a> or <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html"><code>Partial</code></a>. However sometimes the predefined ones just don't cut it. Here's two new types you can use to make your life easier.</p>

<h3 id="omit">Omit</h3>

<p><code>Pick</code> is handy when you need to create a new type from an existing</p>]]></description><link>http://ideasintosoftware.com/typescript-advanced-tricks/</link><guid isPermaLink="false">24448d0f-f741-4872-bf24-77018da78a4a</guid><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Sun, 09 Jul 2017 12:18:44 GMT</pubDate><content:encoded><![CDATA[<p>Typescript 2 introduced a number of useful mapped types such as <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html"><code>Pick</code></a> or <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html"><code>Partial</code></a>. However sometimes the predefined ones just don't cut it. Here's two new types you can use to make your life easier.</p>

<h3 id="omit">Omit</h3>

<p><code>Pick</code> is handy when you need to create a new type from an existing interface with only the specified keys. Which is great but sometimes you need just the opposite. Like when you're defining the return type for your API endpoint where you want to include everything except for one or two private fields.</p>

<p>Using <code>Pick</code> for that gets awkward very fast. You can use <code>Partial</code> but that throws typechecking out of the window.</p>

<p>So what you actually need is <code>Omit</code>. Here it is:</p>

<pre><code class="language-typescript">type Diff&lt;T extends string, U extends string&gt; = ({[P in T]: P } &amp; {[P in U]: never } &amp; { [x: string]: never })[T];  
type Omit&lt;T, K extends keyof T&gt; = Pick&lt;T, Diff&lt;keyof T, K&gt;&gt;;  
</code></pre>

<p>(Update: Thanks to Andrew Bradley for his comment below about homomorphic transformation; snippet updated)</p>

<p>Now you can do things like:</p>

<pre><code>type TCleanedUser = Omit&lt;IUser, 'privateField1' | 'privateField2'&gt;;  
</code></pre>

<p>Note: The original author of this is <a href="https://github.com/tycho01">@tycho01</a> and the code was taken from <a href="https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-308052919">this GitHub issue</a>.</p>

<h3 id="projectionietypesafemongoprojectionspecifier">Projection (i.e., typesafe Mongo projection specifier)</h3>

<p>If you use MongoDB it's pretty useful to be able to typecheck your projection specifiers, i.e., which fields should - or should not - be returned in the result. To do that, you can use this mapped type:</p>

<pre><code>type Projection&lt;T&gt; = Partial&lt;Record&lt;keyof T, 0 | 1&gt;&gt;;  
</code></pre>

<p>Now you can write code like</p>

<pre><code>MyCollection.find({}, {importantField: 1: anotherImportantField: 1} as Projection&lt;IMyDoc&gt;);  
</code></pre>

<p>Well at least till typed projection gets into the typings for your favourite Mongo driver by when you can just forget about this. :-)</p>

<p>Happy typechecking! I'm <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>.</p>]]></content:encoded></item><item><title><![CDATA[Which JS Framework Should I Use For My 2017 Web App: A Flowchart]]></title><description><![CDATA[Which framework should I use when building a webapp in 2017?]]></description><link>http://ideasintosoftware.com/which-framework-should-i-use-for-my-2017-web-app/</link><guid isPermaLink="false">e2a18912-9d0b-478b-b76e-3bb39c5fdb4b</guid><category><![CDATA[javascript]]></category><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Wed, 29 Mar 2017 08:43:25 GMT</pubDate><media:content url="http://ideasintosoftware.com/content/images/2017/03/Which-Major-JS-Framework-Should-I-Use--4.png" medium="image"/><content:encoded><![CDATA[<img src="http://ideasintosoftware.com/content/images/2017/03/Which-Major-JS-Framework-Should-I-Use--4.png" alt="Which JS Framework Should I Use For My 2017 Web App: A Flowchart"><p><img src="http://ideasintosoftware.com/content/images/2017/03/Which-Major-JS-Framework-Should-I-Use--3.png" alt="Which JS Framework Should I Use For My 2017 Web App: A Flowchart"></p>]]></content:encoded></item><item><title><![CDATA[Routing Emails Through Meteor Server]]></title><description><![CDATA[How to use custom replyTo email addresses and route incoming email messages to your Meteor or Node.js server when using Mailgun as the email provider. ]]></description><link>http://ideasintosoftware.com/routing-emails-through-meteor-server/</link><guid isPermaLink="false">4f4d7eca-eca0-4379-a8ec-5f426ed156a0</guid><category><![CDATA[meteor]]></category><category><![CDATA[javascript]]></category><category><![CDATA[email]]></category><category><![CDATA[nodejs]]></category><category><![CDATA[mailgun]]></category><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Fri, 24 Mar 2017 07:02:17 GMT</pubDate><content:encoded><![CDATA[<p>Some time ago I was adding email messaging to <a href="https://sharewaste.com">sharewaste.com</a> - a waste-reducing app that connects people with kitchen scraps to their composting neighbours. It turned out to be a tad bit trickier than originally expected.</p>

<p>You see, I started with the simplest thing: a <code>"click here to reply"</code> link in the emails. Naturally, it turned a lot of our users were trying to reply directly to the emails rather than following the link. Because people want convenience. And it makes much sense, right? After all, who has to time to follow reply links in email.</p>

<p style="text-align: center; margin: 2rem 0;">  
<img src="http://ideasintosoftware.com/content/images/2017/03/aint-nobody-got-time-fo-that-following-email-reply-links-aint-nobody-got-time-for-that-1.jpg" alt="">
</p>

<p>So I decided to implement email routing for ShareWaste so that users can reply to emails directly. And for posterity, here's how I went about it.</p>

<h3 id="quickoverview">Quick Overview</h3>

<p>We'll be using <a href="http://ideasintosoftware.com/routing-emails-through-meteor-server/mailgun.com">Mailgun</a> as the email provider in this example. That said, the approach would be very much the same for any provider that offers webhooks for incoming emails (e.g. Sendgrid).</p>

<p>Here's what we'll do:</p>

<ol>
<li>First, we'll set up a Mailgun "route" (an incoming traffic webhook with configurable rules).</li>
<li>Then we'll implement a routine in our app that sends out emails with a specific <code>replyTo</code> header value (so that we can match incoming replies with their respective "parent" messages)</li>
<li>Finally, we'll create an API endpoint that handles the incoming emails (<code>POST</code> requests from Mailgun).</li>
</ol>

<p>We'll be using <code>mg.sharewaste.com</code> for the domain (but obviously you'd use your own app's domain instead).</p>

<p>Let's look at these steps in a little more detail.</p>

<h3 id="mailgunconfig">Mailgun Config</h3>

<p>After you've signed up with Mailgun and created the domain (<code>mg.sharewaste.com</code> in our case), <a href="https://app.mailgun.com/app/routes">go to the <code>routes</code> tab</a> and create a new route. Use the following settings:</p>

<ol>
<li><code>Expression type</code>: <code>Match recipient</code></li>
<li><code>Recipient</code>: <code>.*_reply@mg.sharewaste.com</code></li>
<li><code>Forward actions</code>: add <code>https://sharewaste.com/api/email/reply</code></li>
<li>Fill in the route description (e.g. "email reply handler")</li>
<li>Hit <code>Create route</code>.</li>
</ol>

<p>Now any email that goes to <code>.*_reply@mg.sharewaste.com</code> will trigger this route which means Mailgun will send an HTTP <code>POST</code> request to <code>https://sharewaste.com/api/email/reply</code> (which we specified in <code>Forward actions</code>) with the parsed email data in payload. Sweet as!</p>

<p>Next, we need two things: <em>a</em>) consume Mailgun's payload; and <em>b</em>) figure out which message is being replied to. Let's start with the latter.</p>

<h3 id="emailsendinglogic">Email sending logic</h3>

<p>In order to match the incoming messages with their thread, we'll use a Mongo collection to store the mapping between the incoming email and the thread it belongs to. Let's call the collection <code>mailRepliesMapping</code>.</p>

<p>Here's what the documents will look like:</p>

<pre><code class="language-typescript">interface IMailData {  
  _id: string;

  // Id of the thread the email reply belongs to.
  threadId: string;

  // Id of the recipient of the email (i.e., of
  // the user who will send the reply).
  replySenderId: string;

  // Created at date, 'nuff said.
  createdAt: Date; 
}
</code></pre>

<p>And here's a code snippet of what we'll do when a new message is added to a thread. (With Meteor, sending emails is pretty easy - just use the <a href="http://docs.meteor.com/api/email.html#Email-send"><code>Email.send</code></a> function. But with vanilla Node it's really not much harder.)</p>

<pre><code class="language-typescript">function sendEmailForPost(post) {  
  // Get thread, get recipients, prepare email data.
  // ...


  const emailMappingDocId = MailDataCollection.insert({
    threadId: thread._id,
    replySenderId: emailRecipient._id,
    createdAt: new Date()
  });

  Email.send({
    // ...
  })
}
</code></pre>

<p>The idea is that every time Mailgun tells us we have a new reply, we'll find out the relevant <code>mailRepliesMapping</code> and we'll fetch all the metadata we need in order to add the message where it belongs.</p>

<p>To make things easily configurable, we'll add a <code>replyEmailSuffix</code> field to our Meteor <code>settings</code> with the value <code>_reply@mg.sharewaste.com</code>. We'll use this constant to easily parse out our internal id from an incoming email and lookup the relevant <code>mailRepliesMapping</code> document.</p>

<p>When we send our email out, we'll use the following <code>replyTo</code> header value:  </p>

<pre><code>const replyEmailSuffix = Meteor.settings['replyEmailSuffix'];  
const replyTo = `${emailMappingDocId}${replyEmailSuffix}`  
</code></pre>

<p>Now when the recipient replies, the email will be sent to an email address that looks like this: <code>&lt;my_document_id&gt;_reply@mg.sharewaste.com</code>. That way we're able to easily parse out the <code>&lt;my_document_id&gt;</code> section (as we describe in the next section).</p>

<h3 id="serverendpointforincomingemails">Server Endpoint For Incoming Emails</h3>

<p>The last bit is the "incoming email" handler. No curve ball there. Create a module file (e.g. <code>imports/server/incoming-mail-handler.js</code>) that looks like this:</p>

<pre><code>import express from 'express';  
import bodyParser from 'body-parser';  
import { WebApp } from 'meteor/webapp';  
import { parseOneAddress } from 'email-addresses';  
import ThreadsApi from '../../api/threads';

// We need `multer` middleware because sending an email
// with attachments will produce a multipart message.
import multer from 'multer';

import { MailDataCollection } from '../../api/mailData';

// Create and set up Express app.
// Note: You would just use Express `Router` in
// production and create the Express App object in another module.
export const app = express();  
export const router = express.Router();  
const upload = multer();

// The incoming email handler, deals with Mailgun webhook payload.
router.post('/api/email/reply', upload.any(), Meteor.bindEnvironment((req, res) =&gt; {  
  console.log('MailHandler: processing email');

  // Parse out the email mapping document id from the email address.
  const replyEmailSuffix = Meteor.settings['replyEmailSuffix'];
  const parsedTo = parseOneAddress(req.body.To || req.body.to);
  const dataKey = parsedTo.address.replace(replyEmailSuffix, '');
  const data = MailDataCollection.findOne({ _id: dataKey });

  if (!data) {
    console.error(`Mailhandler: invalid mail reply key ${dataKey}`);
    return res
      .status(400)
      .send(`Invalid mail reply key ${dataKey}`);
  }

  const emailText = `${req.body['stripped-text']}\n\n${req.body['stripped-signature']}`;
  const threadId = data.threadId;
  if (!threadId) {
    console.error(`Mailhandler: invalid thread id ${threadId}`);
    return res
      .status(400)
      .send(`Invalid thread id ${threadId}`);
  }

  const sender = Meteor.users.findOne({ _id: data.replySenderId });
  if (!sender) {
    console.error(`Mailhandler: invalid sender ${data.replySenderId}`);
    return res
      .status(400)
      .send(`Invalid sender ${data.replySenderId}`);
  }

  const post = ThreadsApi.insertPost(emailText, threadId, sender);

  // Don't forget to send an email notification to the actual recipient.
  ThreadsApi.sendEmailForPost(post._id, sender);

  // In case we want to track which messages have been replied to via email.
  MailDataCollection.update({ _id: dataKey }, { $set: { emailReplyRouted: true } });

  // All done!
  res.send('ok');
}));

// Again, in production app, you'd use Express Router
// and create the app in another module.
app.use(bodyParser.urlencoded({  
  extended: true
}));
app.use(router);

// Wire up Meteor.
WebApp.connectHandlers.use(app);  
</code></pre>

<p>And that's it! Now every time any of our users replies to a message directly from their email client, we'll route the reply through Mailgun to our server which will append the message to the right thread and will also send an email notification to the recipient.</p>

<p>A cool feature of Mailgun is that if your server is down, Mailgun will try to re-deliver the payload a number of times (with semi-exponential backoff) in the next eight hours before giving up.</p>

<h5 id="attachments">Attachments</h5>

<p>Please note that in our example we don't handle attachments - they will simply be ignored. Inline attachments will just show as string ids in the email body. Parsing attachments wouldn't be very hard though (although you'd need to upload them somewhere); check out <a href="https://documentation.mailgun.com/user_manual.html#routes">Mailgun's Documentation</a> to see how to get them from the payload.</p>

<h3 id="summary">Summary</h3>

<p>We've learnt to route incoming emails to our Meteor app via Mailgun. Here's what we covered:</p>

<ul>
<li>When we send a message from our app, we use a special <code>replyTo</code> header value with a unique identifier for each email.</li>
<li>When a user replies to an email directly from the email client, the message will be routed to our server via Mailgun</li>
<li>When we detect an incoming email reply, we parse the email address to get the email's unique identifier. Then we look up the relevant metadata in our database and we figure out who sent the message and which thread the message should be appended to. </li>
<li>If our server is down momentarily, Mailgun will re-send the notification a couple times (so we don't lose messages).</li>
</ul>

<p>And that's all there is to it. Not that hard, right?</p>

<p>Happy routing! I'm <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>.</p>

<p>PS: If you're into composting or if you simply think that composting scraps - and producing soil - is better than just chucking them into garbage, be sure check out <a href="https://sharewaste.com">ShareWaste</a>. And spread the word, the more, the merrier! :-)</p>]]></content:encoded></item><item><title><![CDATA[TypeScript Conditional Imports And Reference Elision]]></title><description><![CDATA[How to use reference elision to achieve type-safe conditional module imports in TypeScript.]]></description><link>http://ideasintosoftware.com/typescript-conditional-imports/</link><guid isPermaLink="false">97a380d9-5a24-4e87-b2e5-fae70336b22e</guid><category><![CDATA[TypeScript]]></category><category><![CDATA[modules]]></category><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Sat, 04 Mar 2017 02:10:11 GMT</pubDate><content:encoded><![CDATA[<p>ES6 modules specification explicitly states <code>import</code>/<code>export</code> statements <a href="http://exploringjs.com/es6/ch_modules.html#_imports-and-exports-must-be-at-the-top-level">may only be used at the top level</a> of files.</p>

<p>Static imports like this bring about a <a href="http://calculist.org/blog/2012/06/29/static-module-resolution/">number of benefits</a> such as first-class support for circular dependencies between modules and (potential) support for macros but it also means <strong>you cannot conditionally import modules</strong>. Which can be a fairly big issue if you're using e.g. Meteor, as code-sharing between server and client is one of its <a href="https://guide.meteor.com/structure.html#meteor-structure">core propositions</a>.</p>

<p>Now, this is not reallly a problem when using vanilla JS as you can simply switch to raw <code>require</code>. However, if you're using TypeScript (and some smart people <a href="http://staltz.com/all-js-libraries-should-be-authored-in-typescript.html">say you should</a>), this doesn't really cut it because, <em>hey, where did my types go</em>?</p>

<p>So that's the question - with <code>require</code> which is just a regular function, how do I tell compiler the <em>type</em> of what I'm requiring? </p>

<h4 id="referenceelision">Reference Elision</h4>

<p>Enter <em>reference elision</em>. See, in TypeScript, when you import a module, the compiler only emits code into the JavaScript output if you use the import in a <strong>value position</strong> in the body of the module. In other words, if you import a module but never use any of the imported values other than as TypeScript types, the resulting javascript file will look as if you never imported the module at all.</p>

<p>Which means these two files will output the same JS code:</p>

<p><em>b.ts</em></p>

<pre><code class="language-TypeScript">import * as ModuleA from './a';  
const modA: typeof ModuleA = require('./a');  
console.log('modA.foo', modA.foo);  
</code></pre>

<p><em>c.ts</em></p>

<pre><code class="language-TypeScript">const modA = require('./a');  
console.log('modA.foo', modA.foo);  
</code></pre>

<h4 id="conditionalimports">Conditional Imports</h4>

<p>This is incredibly useful because if we combine the <code>typeof</code> keyword (which returns the <em>type</em> of its argument) with what we know of reference elision, we can achieve conditional imports in TypeScript while maintaining type safety:</p>

<pre><code class="language-TypeScript">import * as ServerOnly from './top-secret';

let serverMod: typeof ServerOnly;  
if (Meteor.isServer) {  
  serverMod = require('./top-secret');
}

if (Meteor.isServer) {  
  console.log('topSecret:', serverMod.topSecret);
}
</code></pre>

<p>Note that this won't prevent you from accidentally calling the (undefined) module from your client code; that would require something like <a href="https://github.com/Microsoft/TypeScript/issues/4691">preprocessor directives</a> which TS doesn't currently support. But at least you'll have types when calling it from the server.</p>

<p>Happy importing! I'm <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>.</p>

<hr>

<p><em>Sidenote</em>: There was <a href="https://github.com/Microsoft/TypeScript/issues/2812">a proposal</a> to make reference elision explicit via the <code>type</code> keyword but it was rejected.</p>

<p><em>Sidenote2</em>: Once TypeScript supports the ES <a href="https://github.com/Microsoft/TypeScript/issues/12364">dynamic <code>import</code></a> proposal this will be a non-issue.</p>

<p><em>Sidenote3</em>: Kudos to <a href="https://www.typescriptlang.org/docs/handbook/modules.html">TypeScript handbook</a> which is where I've found info on reference elision and optional imports.</p>

<p><em>Sidenote4</em>: I've submitted <a href="https://github.com/meteor/guide/pull/613">a pull-request</a> to the Meteor guide.</p>]]></content:encoded></item><item><title><![CDATA[On delayed tasks]]></title><description><![CDATA[<p>Scheduling tasks to be executed later is a pretty common scenario in backend development. Say you want to send a welcome followup email to a new user 24 hours after they sign up for your service. There’s a couple ways to approach this.</p>

<h3 id="badapproachusingatimeout">Bad approach - using a timeout</h3>]]></description><link>http://ideasintosoftware.com/on-delayed-tasks/</link><guid isPermaLink="false">4703a043-8c79-4bda-859c-d497dd2c4971</guid><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Sun, 12 Feb 2017 02:14:14 GMT</pubDate><content:encoded><![CDATA[<p>Scheduling tasks to be executed later is a pretty common scenario in backend development. Say you want to send a welcome followup email to a new user 24 hours after they sign up for your service. There’s a couple ways to approach this.</p>

<h3 id="badapproachusingatimeout">Bad approach - using a timeout</h3>

<p>Pretty simple:  </p>

<pre><code>User.onSignup((user) =&gt; {  
  setTimeout(() =&gt; {
    sendFollowupEmailToUser(user);
  }, FOLLOWUP_EMAIL_DELAY_MS);
})
</code></pre>

<p>Also: wrong.</p>

<p>Don’t do this. If the server restarts, the delayed task is lost; and since we’re in the age of clouds, you should expect your server to restart anytime and surprisingly often (see e.g. <a href="https://devcenter.heroku.com/articles/dynos#restarting">Heroku dyno cycling</a>). It’s wrong if you wait just for a couple seconds, but it’s even wronger (read: pretty much useless) if you wait twenty four hours or longer.</p>

<h3 id="betterapproachperiodicpolling">Better approach - periodic polling</h3>

<p>Periodically fetch for new users and send them the email. Here's the pseudocode:  </p>

<pre><code>setInterval(() =&gt; {  
  const dayAgo = moment().subtract(24, ‘h’).toDate(); 
  const users = Users.find({
    createdAt: { $gt: dayAgo},
    welcomeEmailSent: { $exists: false }
  }).fetch();
  users.each((user) =&gt; {
    try {
      sendFollowupEmailToUser(user);
      Users.update({ _id: user._id}, { $set: { welcomeEmailSent: true } });
    } catch (e) {
      logger.error(‘Failed to send welcome followup email’, e);
    }
  })
}, POLLING_INTERVAL_MS);
</code></pre>

<p>This approach is better in that it is robust with regards to server restarting. But it’s quite wordy. And you have to manually keep track of which users have been sent the welcome email which is slightly annoying and it pollutes the user document (or becomes more annoying if you decide to keep the flag outside of the user document).</p>

<h3 id="bestapproachdelayedtaskinataskqueue">Best approach - delayed task in a task queue</h3>

<p>Here we go:  </p>

<pre><code>const WELCOME_FOLLOWUP_EMAIL_TASK_KEY = ‘sendWelcomeFollowupEmail’;

User.onSignup((user) =&gt; {  
  const delay = moment.duration(24, ‘h’).asMilliseconds();
  const payload = { userId: user._id };
  jobQueue.addJob(WELCOME_FOLLOWUP_EMAIL_TASK_KEY, {
    delay,
    maxRetries: 3,
    backoff: ‘exponential’
  }, payload);
});

jobQueue.processTask(WELCOME_FOLLOWUP_EMAIL_TASK_KEY, (data) =&gt; {  
  const { userId } = data;
  const user = Users.findOne({ _id: userId });
  sendFollowupEmailToUser(user);
});
</code></pre>

<p>I left out the necessary ceremonies to set up the task queue. Which can be very straightforward (or not - depends on the queue you’re using).</p>

<p>This approach is robust w/r to server restarting and we’ve “outsourced” the flag-keeping to the job queue itself, i.e., existing pending task means we haven’t sent the email yet. Also - depending on the task queue - we get (limited) retries, possibly with exponential back-off, for free.</p>

<p>You can choose from a number of task queues, here's just a few: <a href="https://github.com/Automattic/kue">Kue</a>, <a href="https://github.com/rschmukler/agenda">Agenda</a>, <a href="https://www.iron.io/platform/ironmq/">IronMQ</a>, <a href="https://github.com/vsivsi/meteor-job-collection">JobCollection</a> - if you're into <a href="https://www.meteor.com/">Meteor</a> - or <a href="https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/">RabbitMQ</a>)</p>

<h3 id="whattolookoutforwithdelayedjobs">What to look out for with delayed jobs</h3>

<p>Mainly two things:</p>

<ol>
<li><p>Persisted delayed jobs introduce <strong>backwards compatibility issues</strong> into your system (your server code might have changed since the job was scheduled).</p></li>
<li><p>If the delayed action is conditional, you need to make sure to <strong>check abort-condition when processing the task</strong> (e.g. sending invitation reminder only makes sense if the invited user hasn’t joined yet)</p></li>
</ol>

<p>With regards to problem #1, to minimise the compatibility issues, you should always pass as little information in the payload as possible and fetch whatever you need when the job executes; e.g., pass document identifiers instead of whole documents. And if you must make breaking changes, start versioning the jobs, either by adding <code>version</code> field to the payload or by modifying the job key itself. Then just wait out till all old-style jobs are gone and clean up your code.</p>

<p>Problem #2 is fairly self-explanatory. If you use a delayed job, you schedule the job for everyone and then you need to check if the job still makes sense when it actually executes. So, e.g., in <code>sendReminder</code> job you need to make sure it still makes sense to actually remind the user at the time the job runs.</p>

<p>Happy scheduling! I’m <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a></p>]]></content:encoded></item><item><title><![CDATA[6 Tips To Becoming a Better (Web) Developer]]></title><description><![CDATA[6 tips to becoming a better web developer]]></description><link>http://ideasintosoftware.com/tips-to-become-a-better-web-developer/</link><guid isPermaLink="false">7c11c3c3-bf45-4281-b415-27a5ac19f988</guid><category><![CDATA[programming]]></category><category><![CDATA[javascript]]></category><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Sat, 23 Jul 2016 09:05:16 GMT</pubDate><content:encoded><![CDATA[<p>Much like football is not just about kicking the ball, programming is not just about writing code. Below I've summarized six things I've learned over the last ten years as a professional programmer that will help you become a better developer not just in terms of the code you write but all around.</p>

<h3 id="1makeyourselfcomfortable">1. Make. Yourself. Comfortable.</h3>

<p>I can't stress this enough. Don't suffer in silence. Or, you know, loudly. Make it your number one priority to stay focused when programming.</p>

<p>Your job is hard. And it is demanding. And there's only so much willpower you can muster each day (and you can only really replenish it by sleeping). <strong>Don't allow yourself to be frustrated by things you can fix.</strong></p>

<p>If your build process takes too long, make it a priority to fix that. Use Google! Ask on Stack Overflow. Use <a href="https://travis-ci.org/">Travis</a> or <a href="https://circleci.com">CircleCI</a> to make it trivial to deploy your app. Deploy to <a href="http://heroku.com/">Heroku</a>. Make your life easier.</p>

<p>Get a second monitor, a big one with good resolution (I use <a href="https://www.amazon.com/gp/product/B009C3M7H0/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B009C3M7H0&amp;linkCode=as2&amp;tag=ideintsof-20&amp;linkId=4834a2b676a50450565bf2824115e4e3">ASUS PB278Q</a><img src="//ir-na.amazon-adsystem.com/e/ir?t=ideintsof-20&amp;l=am2&amp;o=1&amp;a=B009C3M7H0" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">) and - please! - get yourself a good keyboard (IMO nothing beats <a href="https://www.amazon.com/gp/product/B000LVJ9W8/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B000LVJ9W8&amp;linkCode=as2&amp;tag=ideintsof-20&amp;linkId=d81bf9fc55a8eaf64df0a5607e2b2231">Kinesis Advantage</a><img src="//ir-na.amazon-adsystem.com/e/ir?t=ideintsof-20&amp;l=am2&amp;o=1&amp;a=B000LVJ9W8" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;">). Your eyes and wrists will thank you.</p>

<div>  
  <img src="https://i.imgur.com/R6qrD.gif" style="display: block; margin-left: auto; margin-right: auto; margin-top: 2rem; margin-bottom: 2rem;">
</div>

<p>And last but not least - you spend a lot of time sitting. So get a good chair. No, wait, scratch that. Get a <em>great chair</em>! I can recommend <a href="http://www.spinalischairs.co.uk/back-pain-chair/spinalis-hacker-office-chair-4">Spinalis Hacker</a> - this chair fixed my lower back problems. True story.</p>

<h3 id="2learntostaycalm">2. Learn to stay calm</h3>

<p>Seriously. Just calm down. You're writing a web app. People are not going to die if you don't fix it within the next fifteen minutes. All your restless leg syndrome and quiet "fuck fuck fuck" muttering does is stress out your colleagues. Breathe deep.</p>

<p>There's more to life than coding. Make sure you get some perspective. Read books. Travel. Travel anywhere. Travel to a developing country and have a chat with someone who lives on $5 a day. Have a baby. Go skydiving. Anything.</p>

<p>Be passionate about what you do - you should be because, well, programming rocks! - but don't get all OCD about it. It's a marathon, not a sprint.</p>

<p>Or, as Goethe put it: <em>die ganze Arbeit ist Ruhig sein</em> (all the effort consists of staying calm). </p>

<h3 id="3readoutofthetarpithttpshaffneruscspaperstarpitpdf">3. Read <a href="http://shaffner.us/cs/papers/tarpit.pdf">Out of the Tar Pit</a></h3>

<p>This paper is pure gold. Get it here: <a href="http://shaffner.us/cs/papers/tarpit.pdf">http://shaffner.us/cs/papers/tarpit.pdf</a> . Then go brew yourself a big mug of tea, sit down and read. And re-read. And then think about how you program and how you deal with state.</p>

<div>  
  <img src="https://i.imgur.com/UmpOi.gif" style="display: block; margin-left: auto; margin-right: auto; margin-top: 2rem;">
</div>

<h3 id="4learnfunctionalprogrammingtechniques">4. Learn functional programming techniques</h3>

<p>You don't need to drop everything and start using Clojure or Haskell. FP is not a silver bullet; nothing is, really. But what FP will do for you is it will teach you to reason about state and that explicit is usually better than implicit. And that there is beauty in pure functions. And that in most cases, immutable is better than mutable. </p>

<p>Let's face it - web development is kinda like shovelling dirt. You get a bunch of data and you move it around (plus you map it to a different representation that works better for your problem domain). Learn to use <code>map</code>, <code>filter</code> and <code>reduce</code>. Check out <a href="https://lodash.com/">lodash</a>, you will most likely want to use it. That said, don't be too cryptic - the goal is to use FP techniques to make your code clear and easier to follow. So don't overdo it. Especially with <code>reduce</code> which is usually the trickiest to understand.</p>

<h3 id="5learntomanagecodedebt">5. Learn to manage code debt</h3>

<p>There's nothing wrong with code debt. Yeah, I said it. You see, code debt is pretty much like any other debt - you can use it to your advantage or you can ignore it till it crushes you. You accrue code debt when you need to take a shortcut and move faster than you normally would (for which there are often very much legit reasons). And then eventually you have to slow down and pay back what you've borrowed.</p>

<p>When code debt becomes problematic is when it's not <em>managed</em>. Either because you don't know about it (which is arguably the worst case) or because you were sitting on it for too long and now all you can do is pay the interest. And put in extra time to cut down the debt on top of that.</p>

<p><em>Every</em> coding project has code debt. It's a fact of programming life. And there's nothing wrong with that. Just be aware of it and do your best to keep it in check.</p>

<p>Incidentally, this is one area where you can really tell apart senior and junior developers. The former are better at judging and managing code debt. It's one of the things that only comes with experience.</p>

<h3 id="6usetypescript">6. Use Typescript</h3>

<p>Yep. Types are helpful. Refactoring vanilla Javascript is a nightmare (even with unit/integration tests). There's virtually no disadvantage to using Typescript; it's a superset of JS and it's maintained by Microsoft and used in Angular 2 so it's here to stay for the foreseeable future.</p>

<p>That said, Typescript is not perfect; the types are only as good as you - or whoever wrote the definition file you're using - make them and it's just too easy to use the <code>any</code> type (which is essentially telling the compiler to give up). And - at least till Typescript 2.0 - all types are implicitly nullable (yuck!). But anything that helps us write code with less errors counts.</p>

<div style="padding-bottom: 2rem;"></div>  

<p>If you have any thoughts, please do leave a comment. I hope you liked my tips and - enjoy coding, guys! It's a great craft.</p>

<p>I'm <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>!</p>

<div style="padding-bottom: 2rem;"></div>]]></content:encoded></item><item><title><![CDATA[Git Tips And Tricks]]></title><description><![CDATA[Git VCS time saving tips and tricks]]></description><link>http://ideasintosoftware.com/git-tips-and-tricks/</link><guid isPermaLink="false">d008eb82-4927-4cff-aa31-acb8232f75f4</guid><category><![CDATA[git]]></category><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Sun, 10 Apr 2016 09:53:16 GMT</pubDate><content:encoded><![CDATA[<p>Git is probably the greatest thing that happened to the world of programming since the invention of sliced bread (<a href="https://en.wikipedia.org/wiki/Sliced_bread">1928</a>...wait, seriously?). Anyway, as I'm sure we all know sometimes it can also be a huge pain in the butt. So here's a handful of quick tips and tricks that make it less so.</p>

<h4 id="listofcommitsthathavealreadybeencherrypicked">> List of commits that have already been cherry picked</h4>

<p>I'm a fan of <a href="http://paulhammant.com/2013/04/05/what-is-trunk-based-development/">trunk based development</a>, I find it to be a nice way to handle complexity in configuration management.</p>

<p>But it also means I cherry-pick a lot. And so it's pretty useful to know what commits I've already copied onto the target branch.</p>

<p>To do just that, run <code>git cherry -v &lt;target_branch&gt; &lt;source_branch&gt;</code>. Each line beginning with the <code>+</code> sign is a commit that hasn't been picked into target branch (while <code>-</code> means it's there).</p>

<h4 id="somuchnicercommitgraph">> (So Much) Nicer commit graph</h4>

<p><code>git log</code> is boring. And verbose. And uses too much unnecessary whitespace. And I can't see what's where with regards to branches. We can do better. Much better.</p>

<p>Like <code>git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&lt;%an&gt;%Creset' --abbrev -commit --date=relative</code> better.</p>

<p>Seriously, try it and you'll never use <code>git log</code> ever again.</p>

<h4 id="storedmycodeinabranchandforgotwhichone">> Stored-my-code-in-a-branch-and-forgot-which-one</h4>

<p>Now, this happens to me regularly: I implement a new feature in a branch and forget the name immediately. Or definitely the next day. And since there's twenty other (mostly legacy) branches in my git which I haven't merged in and haven't cleaned up because, well, I'm a lazy bum, it's hard to find it again.</p>

<p>This command will list your branches sorted by the last commit's date (starting with the newest one).</p>

<p><code>git for-each-ref --sort=-committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:    reset))' | less -R</code></p>

<p>Kudos <a href="http://stackoverflow.com/a/5188364/148450">Jakub Narębski</a>!</p>

<h4 id="lastminutecheckbeforecommiting">> Last minute check before commiting</h4>

<p>Super simple but super useful one. Shows the diff at the bottom of the commit message (don't worry, git will strip it for you before storing it). You wouldn't believe how often you can catch a bug just by glancing at your patch right before you commit.</p>

<p><code>git commit -v</code></p>

<p>Happy gitting, folks! I'm <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>.</p>]]></content:encoded></item><item><title><![CDATA[Ten-fold Query Execution Speedup For Larger Datasets In Meteor]]></title><description><![CDATA[Make your Meteor reactive queries up to ten times faster for large client-side datasets.]]></description><link>http://ideasintosoftware.com/ten-fold-query-execution-speedup-for-larger-datasets-in-meteor/</link><guid isPermaLink="false">a224e345-ed67-4aa7-9f04-f63617aafa3b</guid><category><![CDATA[meteor]]></category><category><![CDATA[javascript]]></category><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Sat, 16 Jan 2016 12:02:39 GMT</pubDate><content:encoded><![CDATA[<p>Blaze's reactivity is an incredibly nifty feature of Meteor. It does away with a huge amount of boilerplate code and is mostly really nice to work with.</p>

<p>However, nothing really comes for free, does it. The problem with reactivity is that unless you do some relatively awkward things, you may easily lose a lot of control over what gets called when in your own code: yes, I'm talking about Blaze's helpers.</p>

<p>Helper functions are reactive by design which means if you reference any reactive data source (such as minimongo cursor) in them, the helpers will be rerun anytime the underlying collection data changes to determine whether the UI should be updated. Speaking from experience, the helpers can run very often. Now, <em>usually</em> this is not a problem because computers have become pretty darn fast lately and the helper queries run in milliseconds so everything works just fine and UI is smooth.</p>

<p>Except sometimes it's not.  </p>

<p>If you have a lot of data - and/or big documents - in your minimongo collection, re-runing the helpers can become a massive bottleneck because the minimongo query <em>runs synchronously</em>. Yes, "blocks CPU until finished" synchronously. How bad can it get? Well, let's see an example.</p>

<h4 id="slowquery">Slow Query</h4>

<p>In <a href="https://gist.github.com/realyze/f430042e9c4605634ac8">this gist</a> (the "slow query" section)  I'm inserting 1000 documents (sample doc taken from <a href="https://www.reddit.com/.json">https://www.reddit.com/.json</a>) into an anonymous minimongo collection and then I run a simple query, sort the result using the <code>sort</code> operator and limit the result set to 50. Which is similar to what one could do for example when you want to show a list of posts for a user. Pretty usual stuff.</p>

<p>The results are pretty horrifying.</p>

<p>On my 2015 Macbook Pro the query runs on average in 150 ms. That's CPU <em>blocked for over one eight of a second for a single run of the (hypotetical) helper</em>. Which gets run every time you insert a new document into the collection. Not great, right? How can we do better?</p>

<h4 id="optimisingminimongoquery">Optimising Minimongo Query</h4>

<p>After some experimenting it turns out that the slowest parts of the query is:</p>

<ol>
<li><p>Sorting the results (which is by Meteor's own admission <a href="https://github.com/meteor/meteor/blob/f0404b6e0d50ce80949f896d346d5b2557da5494/packages/minimongo/minimongo.js#L867-L871">"laughably inefficient"</a>).</p></li>
<li><p>Retrieving the actual documents as JS objects (looks like minimongo does a lot of EJSON operations on the docs before returning them to you).</p></li>
</ol>

<p>Fortunately, we can work around both of those: see the <code>fetch optimised</code> section of the <a href="https://gist.github.com/realyze/f430042e9c4605634ac8">gist</a>. The key to making the query faster is a combination of:</p>

<ol>
<li><p>Avoiding minimongo's sort implementation in favor of our own (in this case <a href="http://underscorejs.org/#sortBy">underscore's <code>sortBy</code></a>).</p></li>
<li><p>Using minimongo <a href="http://ideasintosoftware.com/fine-grained-reactivity-via-mongo-projections/">projection</a> to limit what's returned in the result set.</p></li>
</ol>

<p>Those two simple changes result in roughly <em>ten-fold speedup</em> in the query execution time (150 ms vs 16 ms on average).</p>

<p>Of course, if we use projection, the result set will not contain the full documents. However, minimongo has an <a href="https://www.meteor.com/mini-databases">implicit "index" on the <code>_id</code> field</a> (which we already have) and therefore fetching the full document in a helper in a child template will be fast enough (cca 0.2 ms on average which means 0.2 * 50 = 10ms to fetch 50 documents one by one).</p>

<p>Overall, we've just made a speedup from 150 ms to 16 + 10 = 26 ms per query run. Pretty nice, right?</p>

<p>Happy reactive meteoring! I'm <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>.</p>]]></content:encoded></item><item><title><![CDATA[Harmful Timeouts]]></title><description><![CDATA[<p>Whenever I see code that goes like this:</p>

<pre><code class="language-javascript ">// Make sure foo is closed. 
setTimeout(function () {  
  openNewFoo();
}, 150);
</code></pre>

<p>my spidey sense starts tingling. Why? Because coding that way too often leads to "followup" code that looks like this:</p>

<pre><code>// Make sure foo is closed. 
setTimeout(function () {  
  openNewFoo();
}, 150);

// Make sure new foo</code></pre>]]></description><link>http://ideasintosoftware.com/harmful-timeouts/</link><guid isPermaLink="false">d08d9e32-8ca5-4921-abf6-8611459ffcd6</guid><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Sun, 01 Nov 2015 12:09:17 GMT</pubDate><content:encoded><![CDATA[<p>Whenever I see code that goes like this:</p>

<pre><code class="language-javascript ">// Make sure foo is closed. 
setTimeout(function () {  
  openNewFoo();
}, 150);
</code></pre>

<p>my spidey sense starts tingling. Why? Because coding that way too often leads to "followup" code that looks like this:</p>

<pre><code>// Make sure foo is closed. 
setTimeout(function () {  
  openNewFoo();
}, 150);

// Make sure new foo is open
setTimeout(function () {  
  doBar();
}, 170);
</code></pre>

<p>Which is a sure one-way ticket to world full of a pain and a gordian knot of code where timeouts need to run after timeouts that need to run after timeouts. It's like an asynchronous <code>goto</code>.</p>

<p>As a programmer you should always be aware of the order of actions you need to execute. Your aim is to reduce complexity and allow fellow programmers (including future yourself) to reason about your code. This means you should prefer synchronous execution model whenever possible (simply because it's easier to follow).</p>

<p>Timeouts and debounces are an easy way out when you're in trouble because you need to carry out an action after another one or you want to prevent a function from being called too often (e.g. because it's called from two different places) - but more often than not it's the wrong solution. Like treating a symptom instead of the illness.</p>

<p>It's almost always possible to do away with <code>setTimeout</code> and <code>_.debounce</code>. If your code calls the same function from two places in quick succession perhaps those two places need to be more tightly coupled and made aware of each other. If you need to wait until something is rendered on the screen, Meteor offers the <a href="http://docs.meteor.com/#/full/tracker_afterflush"><code>Tracker.afterFlush</code></a> function.</p>

<h4 id="settimeout">setTimeout</h4>

<p><code>setTimeout</code> really should be reserved for operations that are explicitly time-dependent - such as when you want to run something every five minutes and you don't want to use <code>setInterval</code> (there are many legit cases where <code>setInterval</code> doesn't cut it).</p>

<h4 id="debouncing">Debouncing</h4>

<p>Important point here: your app logic must work without debouncing. Yes, it may be unbearably slow but when you remove the <code>_.debounce</code> calls it must still run correctly. Debouncing is an optimisation, not a fix.</p>

<p>Typical usecases for debouncing are things like handling window  resize events or saving editor content when user is typing - generally speaking you use it anywhere where you have to handle lots of events and you can safely ignore some of them plus your app doesn't need to react immediately. If you use <code>_.debounce</code> because for some reason your function is getting called more times than you expect, you're simply doing it wrong.</p>

<p>Happy coding! I'm <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>.</p>]]></content:encoded></item><item><title><![CDATA[Git Tips: Ignore Changes To a Local File]]></title><description><![CDATA[What is "git update-index --skip-worktree" and why is it useful.]]></description><link>http://ideasintosoftware.com/git-tips-ignore-changes-to-a-local-file/</link><guid isPermaLink="false">e46e88c6-d9bd-4e20-8e2c-b9b456910dd2</guid><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Sun, 18 Oct 2015 06:51:15 GMT</pubDate><content:encoded><![CDATA[<p>You know how sometimes when you need to change a flag in a versioned config file, it is just <em>so</em> hard to remember not to do <code>git add .</code>, commit, accidentally push the change and break everyone else's toys?</p>

<p>Turns out the authors of Git are a forgetful (and smart) bunch too. Which is why there's <code>git update-index</code> and why it has the super-handy <code>--skip-worktree</code> flag. What it does is that it that tells Git "hey, Git, let's pretend I didn't do any changes to this file and also don't track any of its further changes either, OK?" Kind of like a transient <code>.gitignore</code>. You can run it on your config file and stop worrying about accidentally commiting changes that should stay local. </p>

<p>Now, one can immediately see how this can be pretty useful, right? Two obvious questions pop up though:</p>

<ol>
<li>How do I turn this thing off?</li>
<li>I forgot what I told Git to forget; how do I tell which files are skipped?</li>
</ol>

<p>Luckily for us, both have easy enough answers:</p>

<ol>
<li><code>git update-index --no-skip-worktree &lt;file&gt;</code> will make all the untracked changes appear in Git as modifications again;</li>
<li><code>git ls-files -v | grep '^S'</code> will tell you all the files that have been marked as skipped.</li>
</ol>

<h4 id="differenceswithupdateindexassumeunchanged">Differences with <code>update index --assume-unchanged</code></h4>

<p>To make things complicated, there are actually two flags to <code>update-index</code> whose task is to prevent Git from tracking changes to a file: <code>--skip-worktree</code> and <code>--assume-unchanged</code>. And people usually seem to know about <code>--assume-unchanged</code> only. What's the difference?</p>

<p>Well, turns out <code>--assume-unchanged</code> has been designed with a different usecase in mind: as a performance optimisation for files that are expensive to calculate diff of (think SDKs). On the other hand <code>--skip-worktree</code> goes the extra mile to prevent you from losing your changes such as when you do a <code>git reset --hard</code> (which can obviously save you from potentially a lot of trouble).  You can find a great comparison <a href="http://fallengamer.livejournal.com/93321.html">here</a>. </p>

<p>Happy gitting! I'm <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>.</p>]]></content:encoded></item><item><title><![CDATA[Fine-Grained Reactivity Via Mongo Projections]]></title><description><![CDATA[Using projections to achieve fine grained reactivity]]></description><link>http://ideasintosoftware.com/fine-grained-reactivity-via-mongo-projections/</link><guid isPermaLink="false">8b9fd019-d9ca-43a9-809d-d5966dd7f868</guid><category><![CDATA[meteor]]></category><category><![CDATA[javascript]]></category><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Tue, 08 Sep 2015 22:28:44 GMT</pubDate><content:encoded><![CDATA[<p>MongoDB's <a href="http://docs.mongodb.org/manual/reference/method/db.collection.find">find</a> method allows us to specify a <a href="http://docs.mongodb.org/manual/reference/method/db.collection.find/#projections">projection</a>, i.e., what fields should be included in or omitted from the result set. Meteor, connected to a database on both the server side and client side, can benefit from using projection in two major and different ways.</p>

<h3 id="publishedcursorprojectionserverside">Published Cursor Projection (Server-side)</h3>

<p>The benefits of using projection on a published cursor are fairly straightforward; two main use cases here:</p>

<ul>
<li><em>bandwidth optimisation</em>: send less data down the wire</li>
<li><em>security</em>: prevent sensitive data from being sent to the client</li>
</ul>

<p>Nothing to surprise you here.</p>

<h3 id="minimongoprojectionclientside">Minimongo Projection (Client-side)</h3>

<p>This is where things get a little more interesting. Obviously, since the data has already been loaded on the client side, we don't have to concern ourselves with network traffic or bandwidth. Similarly, if server is sending any sensitive data, now it's too late to do anything about that. What good can projecting fields do us on the client side then?</p>

<p>A lot, in fact! You see, in my experience Meteor's minimongo makes it quite easy to forget about any optimisations and just fetch whole documents in template helpers. And this is actually fine and convenient in the beginning but it may lead to severe performance problems if your template helpers run expensive queries (because every update of the document will trigger the query to be reactively re-run).</p>

<p>Projection gives us a big win in that if we exclude fields in a cursor in a template helper (or generally in any <code>Tracker.autorun</code> context), <strong>Meteor's reactivity will not trigger when the excluded fields change</strong>.</p>

<pre><code class="language-javascript">var coll = new Mongo.Collection(null);  
coll.insert({who: 'Darth Vader', what: 'father'});

// Set up a reactive callback (projection via `fields` option).
Tracker.autorun(function() {  
  console.log('reactively called');
  coll.find({}, {fields: {who: 1}}).fetch();
});

// Update excluded field.
var item = coll.find().fetch()[0];  
coll.update(item._id, {$set: {what: 'son'}});  
// no log output

// Update included field.
coll.update(item._id, {$set: {who: 'Luke Skywalker'}});  
// "reactively called"
</code></pre>

<p>Which means we can only project the fields we actually need in a template helper and we can rest assured our helper will be called when any of <em>those</em> fields are updated but not when other fields change. In other words, we can <strong>move reactivity from document level to field level</strong>! </p>

<p>In certain - and actually quite common - cases, projecting only a subset of document's fields in reactive contexts will help your app's client-side performance <em>a lot</em>.</p>

<p>Happy reacting! I'm <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>.</p>]]></content:encoded></item><item><title><![CDATA[How To Crash Your Meteor Server: Unlimited Cursors]]></title><description><![CDATA[Preventing out of memory in meteor by limiting published cursors.]]></description><link>http://ideasintosoftware.com/how-to-crash-a-meteor-server/</link><guid isPermaLink="false">55ceef0e-0a83-481b-bed3-42f099e88f5e</guid><category><![CDATA[meteor]]></category><category><![CDATA[javascript]]></category><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Sun, 30 Aug 2015 01:48:47 GMT</pubDate><content:encoded><![CDATA[<p>When it comes to determining what goes where from the server, <a href="https://www.meteor.com/">Meteor</a> does some pretty cool optimisations to be fast and effective. Yet, I’ve found in certain special cases - especially if you’re deploying to RAM-tight environments such as <a href="https://www.heroku.com/">Heroku</a> or <a href="https://www.digitalocean.com/?refcode=c6e24a8db522">DigitalOcean</a> - it can be <em>surprisingly easy</em> to kill your server if you’re not a bit careful.</p>

<h4 id="usecasesubscribingtoalistofchanges">Use case: subscribing to a list of changes</h4>

<p>Imagine you’re implementing an offline-enabled app and have a collection which contains a list of changes since the client last connected. When the client connects, it subscribes to the collection and you start inserting documents which the client drains. Every time it gets a document, it removes it from local minimongo so that the removal is propagated to the database on the server.</p>

<p>Now, say you have a couple hundred documents in your <code>changes</code> collection (for example because the client disconnected before it managed to pull all the changes, so there are some leftovers from the last sync) and you subscribe using something like this:</p>

<pre><code class="language-javacript">return changes.find({userId: ‘&lt;myUserId&gt;’});  
</code></pre>

<p>Now, I've found when you start generating the documents and pushing them into the changes collection, Meteors CPU and <strong>memory usage just skyrockets</strong>. I've been monitoring the server with <a href="https://kadira.io">kadira</a> and the memory usage grows from 150MB to 1500MB within a couple of <em>minutes</em> taking the heroku server - which usually runs with 0.5 or 1 GB RAM - down.</p>

<p>Here's a snapshot of my console (<a href="https://github.com/lloyd/node-memwatch">memwatch</a> logs): <br>
<img src="http://i.imgur.com/zaX0bBg.png" style="width: 80%; margin: 2rem auto 2rem auto; display: block;"></p>

<p>What happens? Well, hard to say for sure as Meteor's code dealing with data synchronisation is quite complex but my theory is if you are subscribed to a collection and your cursor doesn’t have a <code>limit</code> (meaning <em>everything</em> matching the query is to be sent down the wire), every time you add a new document to a collection, Meteor stores a snapshot of the cursor's contents. Then, when you add a new document, Meteor stores a snapshot of the new set. When you add another one, again. And again and again. With each document added, another chunk of RAM is used. Mind you - this is not a memory leak <em>per se</em>: the memory is eventually freed; there’s just a good chance your server will die before it happens.</p>

<p>How to prevent this? Super easy:</p>

<pre><code class="language-javascript">return changes.find({userId: ‘&lt;myUserId&gt;’}, {limit: 10});  
</code></pre>

<p>That way, Meteor will only snapshot the first 10 documents in the collection and as the client drains the data, the cursor “window” will move. Memory impact: negligible.</p>

<h4 id="moralofthetale">Moral of the tale</h4>

<p>Simple enough: <em>always remember to limit your published cursors</em>. Or even better, use <a href="https://atmospherejs.com/packages/Pagination">some sort of pagination</a> that handles that for you. And use some monitoring tool - such as <a href="https://kadira.io">kadira</a> - to see when the figurative shit hits the fan (pardon my French).</p>

<p>Happy meteoring! I’m <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>.</p>]]></content:encoded></item><item><title><![CDATA[Opting Out of Contenteditable Reactivity in Meteor]]></title><description><![CDATA[Fixing broken contenteditable behaviour in meteor and Blaze.]]></description><link>http://ideasintosoftware.com/opting-out-of-contenteditable-reactivity/</link><guid isPermaLink="false">0f8588b3-c944-45c4-8091-13c74dd4e3e9</guid><category><![CDATA[meteor]]></category><category><![CDATA[javascript]]></category><dc:creator><![CDATA[Tomas Brambora]]></dc:creator><pubDate>Sun, 09 Aug 2015 01:41:21 GMT</pubDate><content:encoded><![CDATA[<p>It's a sad fact that <a href="https://www.meteor.com/blaze">Blaze</a> (Meteor's reactive UI library) <a href="https://github.com/meteor/meteor/issues/1964">doesn't play particularly well with contenteditable elements</a>. The corresponding <a href="https://github.com/meteor/meteor/issues/1964">ticket</a> in GitHub is marked as closed but the issue itself is unfortunately far from resolved.</p>

<p>This makes implementing e.g. autosave for contenteditable elements quite annoying - say you're throttling the <code>keypress</code> event and saving every two or three seconds. However, when you save the document to Mongo, reactivity kicks in and the text in contenteditable randomly gets duplicated and/or the caret jumps <em>somewhere</em> (read: the contenteditable is pretty much rendered unusable). Incredibly frustrating.</p>

<p>There's a couple workarounds suggested in the GitHub issue comments but none of them works particularly great - in general they are either too complicated or they have issues with <a href="https://github.com/meteor/meteor/issues/1964#issuecomment-87444235">focus getting lost</a>.</p>

<p>Which is why my colleague, <a href="https://twitter.com/alonnovy">Alon</a>, came up with a workaround that is actually quite simple and preserves focus and caret position.</p>

<p>What we need to do is to essentially <strong>opt-out from Blaze's reactivity for the contenteditable element</strong>. When we save the content, we don't want Blaze to re-render it based on the stored version (which, in case we're throttling the <code>keypress</code> event, will often be behind the actual content). So how do we do that?</p>

<p>The idea is based on the fact that Blaze is trying to do as little work as it can when re-rendering the DOM (because <a href="http://blog.letitialew.com/post/30425074101/repaints-and-reflows-manipulating-the-dom">reflows/repaints are expensive</a>). This means that when we use a template helper and its value does not change, Blaze knows it does not need to re-render the corresponding DOM tree...and so it won't.</p>

<p>Now, what we want to do is to persuade Blaze the value of the helper that affects the contenteditable element never changes after it's rendered, i.e., we want to turn it into a <em>constant function</em>. In order to do this, we need to somehow tap into the lifecycle of the relevant Blaze template - lucky for us, Blaze provides a <code>created</code> callback which gets called before anything gets rendered and before any helpers are called.</p>

<p>Which in other words means we can do this:</p>

<pre><code class="language-javascript">MyTemplate.created = function () {  
  this.data.__values_override__ = {
    editableContent: this.data.editableContent
  }
};

MyTemplate.helpers({  
  myEditableHelperContent: function () {
    var instance = Template.instance();
    return instance.__values_override__.editableContent;
  }
});
</code></pre>

<p>Now we can be sure Blaze will load the contents of the editor from Mongo when the relevant DOM tree is first rendered and that it will then not mutate the content under our hands afterwards (because we've turned the helper into a constant function so Blaze will never try to re-render it). </p>

<p>Pretty? Not particularly. Functional? Very. :-)</p>

<p>Happy meteoring! I'm <a href="https://twitter.com/tomas_brambora">@tomas_brambora</a>.</p>]]></content:encoded></item></channel></rss>