<?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"><channel><title><![CDATA[Aaron Arney's RSS Feed]]></title><description><![CDATA[A development blog, brought to you by Aaron Arney.]]></description><link>https://www.aaronarney.dev</link><generator>GatsbyJS</generator><lastBuildDate>Mon, 21 Nov 2022 17:31:04 GMT</lastBuildDate><item><title><![CDATA[Critical Path Rendering]]></title><description><![CDATA[Critical Rendering Path CRP or Critical Rendering Path is an algorithm of sorts that browsers use to display a webpage. When you use your…]]></description><link>https://www.aaronarney.dev/blog/critical-path-rendering/</link><guid isPermaLink="false">https://www.aaronarney.dev/blog/critical-path-rendering/</guid><pubDate>Fri, 14 Aug 2020 14:49:31 GMT</pubDate><content:encoded>&lt;h1&gt;Critical Rendering Path&lt;/h1&gt;
&lt;p&gt;CRP or Critical Rendering Path is an algorithm of sorts that browsers use to display a webpage. When you use your browser to navigate to a webpage, a series of steps have to occur before you will see anything populate the screen. You can think of CRP as a linear top-down process in which the browser reads HTML one line at a time and forms the webpage as it goes.&lt;/p&gt;
&lt;p&gt;The CRP process has several phases;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DOM (Document Object Model)&lt;/li&gt;
&lt;li&gt;CSSOM (Cascading Style Sheet Object Model)&lt;/li&gt;
&lt;li&gt;Render Tree &lt;/li&gt;
&lt;li&gt;Layout&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In each of these phases a different set of steps occur. Let&apos;s go over them individually.&lt;/p&gt;
&lt;h2&gt;Document Object Model (DOM)&lt;/h2&gt;
&lt;p&gt;The Document Object Model is a tree structure that represents the HTML on a webpage, by converting each HTML entity into a node object. When each HTML entity is encountered a &lt;code class=&quot;language-text&quot;&gt;startTag&lt;/code&gt; is generated. The parser then moves to the next token to see if there are any children of the entity. If there is a new node and &lt;code class=&quot;language-text&quot;&gt;startTag&lt;/code&gt; is generated, and so on and so forth until the end of the entity is found. At that point an &lt;code class=&quot;language-text&quot;&gt;endTag&lt;/code&gt; is created and that node is closed. These series of startTags and endTags form the tree that makes up the DOM.&lt;/p&gt;
&lt;p&gt;As a simplified example, it may help to visualize it.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;pre class=&quot;language-html&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;#&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
            Some Link
        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Could convert to something like the following. Note that this is a JavaScript flavor and probably not the best way or most accurate way to represent this information.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token function&quot;&gt;startTag&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;div&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;startTag&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;div&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;startTag&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;attribute&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;href&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;#&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Some Link&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;endTag&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;endTag&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;div&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;endTag&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;div&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The more HTML on a page the longer this step takes. However, don&apos;t be fooled into thinking that reducing your HTML by a few or even dozens of elements will make a difference! Browsers are really fast and performance gains from such optimizations are likely to not result in anything significant.&lt;/p&gt;
&lt;h2&gt;CSS Object Model (CSSOM)&lt;/h2&gt;
&lt;p&gt;The CSS object model works similar to the DOM except this time CSS is parsed and applied to nodes. When each CSS rule is encountered, the browser performs a lookup on the DOM to make a match to an element.&lt;/p&gt;
&lt;p&gt;Both HTML and CSS are render blocking resources. However, using through the use of media queries you can achieve non-render blocking CSS. This is because the CSS is ignored if the viewport size doesn&apos;t match the media query, essentially deferring the application of the CSS. &lt;/p&gt;
&lt;h2&gt;Render Tree&lt;/h2&gt;
&lt;p&gt;The Render Tree is the phase in which the DOM and CSSOM are combined. Each node in the DOM has it&apos;s position and size computed. This phase is when the layout is created. Therefore, any time you modify the DOM such as adding or removing nodes, this phase gets re-triggered.&lt;/p&gt;
&lt;p&gt;Here are some &lt;a href=&quot;https://docs.google.com/spreadsheets/u/0/d/1Hvi0nu2wG3oQ51XRHtMv-A_ZlidnwUYwgQsPQUg1R2s/pub?single=true&amp;#x26;gid=0&amp;#x26;output=html&quot;&gt;common properties that trigger layout&lt;/a&gt;.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;width&lt;/td&gt;
&lt;td&gt;height&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;padding&lt;/td&gt;
&lt;td&gt;margin&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;display&lt;/td&gt;
&lt;td&gt;border-width&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;border&lt;/td&gt;
&lt;td&gt;top&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;position&lt;/td&gt;
&lt;td&gt;font-size&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;float&lt;/td&gt;
&lt;td&gt;text-align&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;overflow-y&lt;/td&gt;
&lt;td&gt;font-weight&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;overflow&lt;/td&gt;
&lt;td&gt;left&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;font-family&lt;/td&gt;
&lt;td&gt;line-height&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;vertical-align&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;clear&lt;/td&gt;
&lt;td&gt;white-space&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bottom&lt;/td&gt;
&lt;td&gt;min-height&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2&gt;Painting&lt;/h2&gt;
&lt;p&gt;Finally at the end of the pipeline, the painting phase is when the render tree elements are converted into pixels on the screen.&lt;/p&gt;
&lt;p&gt;The complete layout is generated and painted. Then for each node, lays out the elements on the page with their given dimensions and positions. Lastly it applies the remaining CSS to the element.&lt;/p&gt;
&lt;p&gt;Just like with the layout there are CSS properties that can re-trigger paint.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.google.com/spreadsheets/u/0/d/1Hvi0nu2wG3oQ51XRHtMv-A_ZlidnwUYwgQsPQUg1R2s/pub?single=true&amp;#x26;gid=0&amp;#x26;output=html&quot;&gt;common properties that trigger a re-paint&lt;/a&gt;.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;color&lt;/td&gt;
&lt;td&gt;border-style&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;visibility&lt;/td&gt;
&lt;td&gt;background&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;text-decoration&lt;/td&gt;
&lt;td&gt;background-image&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;background-position&lt;/td&gt;
&lt;td&gt;background-repeat&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;outline-color&lt;/td&gt;
&lt;td&gt;outline&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;outline-style&lt;/td&gt;
&lt;td&gt;border-radius&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;outline-width&lt;/td&gt;
&lt;td&gt;box-shadow&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;background-size&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;As a sidenote, this is why with CSS animations it&apos;s fairly important you stick to the rules of only animating four things; position, scale, rotation and opacity. These operations allow the animation to run on the GPU instead of the browsers software rasterizer. For a more in-depth explanation of how this works, read the excellent post &lt;a href=&quot;https://www.html5rocks.com/en/tutorials/speed/high-performance-animations/&quot;&gt;&quot;High Performance Animations.&quot;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This is a nutshell version of the Critical Rendering Path. It is worth diving into how to optimize the CRP but I will save that for another post. &lt;/p&gt;</content:encoded></item><item><title><![CDATA[Node Concurrency]]></title><description><![CDATA[Node.js in of itself is a single-threaded application (if you want to call it that). However through the use of  and the underlying…]]></description><link>https://www.aaronarney.dev/blog/node-concurrency/</link><guid isPermaLink="false">https://www.aaronarney.dev/blog/node-concurrency/</guid><pubDate>Wed, 01 Jul 2020 20:51:01 GMT</pubDate><content:encoded>&lt;p&gt;Node.js in of itself is a single-threaded application (if you want to call it that). However through the use of &lt;code class=&quot;language-text&quot;&gt;libuv&lt;/code&gt; and the underlying operating system, we can enjoy asynchronous capabilities. The question I&apos;m going to hopefully answer, is how does this work?&lt;/p&gt;
&lt;h2&gt;Node.js Event Loop&lt;/h2&gt;
&lt;p&gt;First, we need to have an understanding of how the event loop works in Node. The event loop is essentially an infinite while loop that waits for an instruction, executes it, and returns to waiting unless there are no more tasks in which case Node will gracefully shut-down.&lt;/p&gt;
&lt;p&gt;There are a group of queue&apos;s which are called phases in Node nomenclature, each with a specific job.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Timers - Executes callbacks scheduled by &lt;code class=&quot;language-text&quot;&gt;setTimeout&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;setInterval&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Pending Callbacks - Executes I/O callbacks that have been deferred to the next loop iteration.&lt;/li&gt;
&lt;li&gt;Idle, Prepare - Used internally by Node.&lt;/li&gt;
&lt;li&gt;Poll - Get new I/O events and execute I/O callbacks&lt;/li&gt;
&lt;li&gt;Check - Executes callbacks registered with &lt;code class=&quot;language-text&quot;&gt;setImmediate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Close Callbacks - Close certain callbacks such as &lt;code class=&quot;language-text&quot;&gt;socket.on(&amp;#39;close&amp;#39;, ...)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;/843db086b852b49ccbd7bd51b1f356c0/event-loop.svg&quot; alt=&quot;Diagram visualizing the Node JS event loop&quot;&gt;&lt;/p&gt;
&lt;p&gt;I&apos;m not going to go into detail about each of these phases in this post, but perhaps in the future I will. Until then you can &lt;a href=&quot;https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#phases-in-detail&quot;&gt;read about each phase in-depth&lt;/a&gt; over at the Node docs.&lt;/p&gt;
&lt;p&gt;Each phase is an array/queue with a FIFO (first in, first out) list of callbacks. When Node enters a phase it will cycle through each queue and execute each callback until either the maximum number of callbacks is reached, or the queue length is zero.&lt;/p&gt;
&lt;p&gt;An important detail to realize is that during any of these phases, additional callbacks can be registered and in some cases those callbacks won&apos;t get executed until the next loop iteration. You can use &lt;code class=&quot;language-text&quot;&gt;setImmediate&lt;/code&gt; to register a callback that gets executed, well, immediately. However you need to know what you&apos;re doing as you can cause the application to hang indefinitely in a scenario where &lt;code class=&quot;language-text&quot;&gt;setImmediate&lt;/code&gt; gets called recursively.&lt;/p&gt;
&lt;h2&gt;Concurrency&lt;/h2&gt;
&lt;p&gt;The first thing to understand with Node.js and threads is that, as mentioned, Nod.js is single-threaded. It runs on one and only one thread for the duration of its lifetime. Write an infinite loop and you&apos;ll lock up your whole application. But concurrency or multi-threading does exist in this architecture.&lt;/p&gt;
&lt;p&gt;As explained earlier I noted that Node.js defers I/O operations to the operating system. Take the &lt;code class=&quot;language-text&quot;&gt;https&lt;/code&gt; module for example.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; https &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;https&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
https&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;https://aaronarney.dev&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you are familiar with jQuery&apos;s &lt;code class=&quot;language-text&quot;&gt;$.ajax&lt;/code&gt; function, JavaScripts &lt;code class=&quot;language-text&quot;&gt;xmlhttprequest&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;fetch&lt;/code&gt; then you might think you know how this snippet is executed. However recall that Node.js uses V8 as essentially a type translator not as the underlying engine itself. When Node.js uses &lt;code class=&quot;language-text&quot;&gt;https&lt;/code&gt; requests, that request is passed to &lt;code class=&quot;language-text&quot;&gt;libuv&lt;/code&gt; and then &lt;code class=&quot;language-text&quot;&gt;libuv&lt;/code&gt; defers it to the operating systems networking libraries. &lt;/p&gt;
&lt;p&gt;Once that request hits the OS layer, Node.js knows that it will get notified when the OS returns some data so it decides to free up it&apos;s thread and work on something else until that happens, giving us asynchronicity. &lt;/p&gt;
&lt;p&gt;When the OS returns the data, Node.js gets notified that it&apos;s ready to get back to work processing the &lt;code class=&quot;language-text&quot;&gt;https&lt;/code&gt; request and pushes that task into poll queue. When a thread becomes available to process the request the original callback assigned to &lt;code class=&quot;language-text&quot;&gt;https.request&lt;/code&gt; gets executed with the data returned from the OS.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/00be76a1a093e0d197456c0668929d7d/thread-queue.svg&quot; alt=&quot;Diagram demonstrating how tasks are routed through the thread pool, into the O.S thread scheduler and finally to the CPU&quot;&gt;&lt;/p&gt;
&lt;p&gt;Note that by default, libuv uses 4 threads which can be increased using the &lt;code class=&quot;language-text&quot;&gt;process.env.UV_THREADPOOL_SIZE&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But the question we should be asking ourselves at this point is which parts of Node.js use this thread pool? The answer isn&apos;t easy because different OS&apos;s might decide to do different things. However it is somewhat safe to assume any intense operation such as reading and writing files, working with databases or networking will utilize the thread pool.&lt;/p&gt;
&lt;p&gt;I never know how to end blog posts so I&apos;ll just leave it at that.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Brief Overview of Node Internals]]></title><description><![CDATA[Node.js is an architecture that wraps several APIs into one consistent interface. There are several libraries which are encapsulated within…]]></description><link>https://www.aaronarney.dev/node-internals/</link><guid isPermaLink="false">https://www.aaronarney.dev/node-internals/</guid><pubDate>Thu, 18 Jun 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Node.js is an architecture that wraps several APIs into one consistent interface. There are several libraries which are encapsulated within Node.js and I want to provide a brief overview of what they are and how they are used.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/9b79c830447558abb30b3db67037e2d5/node-internals.svg&quot; alt=&quot;Node Libraries&quot;&gt;&lt;/p&gt;
&lt;p&gt;In order to access the filesystem (I/O operations), asynchronous TCP/UDP sockets, child processes etc, Node.js utilizes the &lt;code class=&quot;language-text&quot;&gt;libuv&lt;/code&gt; library. It was originally developed to be used for Node.js but has since been adopted by several other platforms. This library is exposed through multiple modules such as the &lt;code class=&quot;language-text&quot;&gt;fs&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;path&lt;/code&gt; module. The &lt;code class=&quot;language-text&quot;&gt;libuv&lt;/code&gt; library is written in C/C++, this is important since it needs to be fast and cross-platform.&lt;/p&gt;
&lt;p&gt;Complementing &lt;code class=&quot;language-text&quot;&gt;libuv&lt;/code&gt; is the &lt;code class=&quot;language-text&quot;&gt;V8&lt;/code&gt; engine, Google&apos;s JavaScript implementation that is used in Chrome, Edge, and other browsers. V8 implements both ECMAScript and WebAssembly, but does not have a DOM as that functionality is provided by a browser. V8 is mostly written in C++ with some JavaScript mixed in at some places.&lt;/p&gt;
&lt;p&gt;There are a few more dependencies that Node.js has such as &lt;code class=&quot;language-text&quot;&gt;llhttp&lt;/code&gt;. This library extracts http message data such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Headers&lt;/li&gt;
&lt;li&gt;Content-Length&lt;/li&gt;
&lt;li&gt;Methods&lt;/li&gt;
&lt;li&gt;Status Codes&lt;/li&gt;
&lt;li&gt;Request URLs&lt;/li&gt;
&lt;li&gt;Message body&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you&apos;ve ever inspected a http request in your browsers networking debug tools, you&apos;ve likely encountered this kind of data. Since Node.js&apos;s bread and butter is acting as a some sort of web server, it&apos;s easy to see why this library would be so important.&lt;/p&gt;
&lt;p&gt;Next is &lt;code class=&quot;language-text&quot;&gt;c-ares&lt;/code&gt; which is a C library that Node.js utilizes to parse DNS requests asynchronously.&lt;/p&gt;
&lt;p&gt;OpenSSL is another dependency that is used mostly for cryptographic functions and modules in Node.js. This library is exposed primarily through the &lt;code class=&quot;language-text&quot;&gt;crypto&lt;/code&gt; module in Node.js.&lt;/p&gt;
&lt;p&gt;Lastly is &lt;code class=&quot;language-text&quot;&gt;zlib&lt;/code&gt; a really fast compression/decompression library. If you&apos;ve worked on websites before you understand the value in &lt;code class=&quot;language-text&quot;&gt;gzip&lt;/code&gt;, well that&apos;s what &lt;code class=&quot;language-text&quot;&gt;zlib&lt;/code&gt; provides. Gzip compresses data that is sent to a client so that no unnecessary bandwidth is eaten up by serving assets in their raw form. This helps both performance and speed.&lt;/p&gt;
&lt;h2&gt;C++ Bindings&lt;/h2&gt;
&lt;p&gt;Node.js exposes these underlying libraries through its own JavaScript API. When one API written in one language, ties into another API written in another language, it is often done so through what is known as a binding. If we take a look at the Node.js source code, there are two specific directories that I want to point out - &lt;code class=&quot;language-text&quot;&gt;src&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;lib&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;lib&lt;/code&gt; directory is where the JavaScript API is contained. This is where methods such as &lt;code class=&quot;language-text&quot;&gt;fs.readFileSync&lt;/code&gt; are contained. Bindings are bound through the &lt;code class=&quot;language-text&quot;&gt;process.binding()&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;src&lt;/code&gt; directory is the C++ bindings that the &lt;code class=&quot;language-text&quot;&gt;lib&lt;/code&gt; modules call into. This is where most of the magic happens. &lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/b64fcfd168196923b1f332cd03acc5fa/node-bindings.svg&quot; alt=&quot;Node Bindings&quot;&gt;&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;That is a &lt;em&gt;very&lt;/em&gt; brief overview of the internals of Node.js but there is so much more to get into. I only wanted to talk about how the libraries work and come together to make Node function the way it does.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The JavaScript Event Loop]]></title><description><![CDATA[JavaScript is an interesting language and part of that interestingness comes from it's event driven architecture. Everytime we need some…]]></description><link>https://www.aaronarney.dev/the-javascript-event-loop/</link><guid isPermaLink="false">https://www.aaronarney.dev/the-javascript-event-loop/</guid><pubDate>Thu, 11 Jun 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;JavaScript is an interesting language and part of that interestingness comes from it&apos;s event driven architecture. Everytime we need some code to execute we have to hook into an event of some sort. Whether it&apos;s when the page loads, a button is clicked or a message is broadcast.&lt;/p&gt;
&lt;p&gt;Think about how click handlers work. Typically you write something like this..&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;button&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;click&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; callback&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here &quot;click&quot; is an ID of an event that get&apos;s emitted by the JavaScript engine any time a button in the DOM is clicked or tapped. Using the &lt;code class=&quot;language-text&quot;&gt;addEventListner&lt;/code&gt; let&apos;s us hook into that event so we can do something interesting. This is event driven JavaScript.&lt;/p&gt;
&lt;h2&gt;The Event Loop&lt;/h2&gt;
&lt;p&gt;JavaScript utilizes what is known as The Event Loop™. If you have ever written a video game or used the Observer/Pub-sub design patter it is likely you already are familiar with the event loop concept.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;queue&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;waitForMessage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    queue&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;processNextMessage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You might think that when we add an event listener, like the &quot;click&quot; one above, that it get&apos;s pushed into this queue. This is in a way true, but not really.&lt;/p&gt;
&lt;p&gt;When a button is found in the DOM, all click events are handled by the browser automatically. It knows what all events an element supports and dispatches messages when those events are triggered.&lt;/p&gt;
&lt;p&gt;I&apos;m getting a little sidetracked here, but this is what a basic observer would look like in JavaScript.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; Observer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;/**
     * A list of registered observers. The intent is to store essentially key-&gt;value pairs
     * so that events like &quot;click&quot;, &quot;change&quot;, &quot;submit&quot; etc are stored as keys with an 
     * array of callbacks as their values.
     */&lt;/span&gt;
    observers&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;/**
     * Register a callback with a given event. If an event already has been initialized
     * with an array we use the spread operator to merge the existing array with the 
     * new observer.
     */&lt;/span&gt;
    &lt;span class=&quot;token function-variable function&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;eventID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; callback&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; queue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;callback&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;observers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;eventID&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isArray&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            queue&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;observers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;eventID&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;observers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;eventID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; queue&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;/**
     * Dispatch a message to all registered observers for a given event name.
     */&lt;/span&gt;
    &lt;span class=&quot;token function-variable function&quot;&gt;notifyListeners&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;eventID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;observers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;eventID&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;observer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;observer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;event&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now imagine that this is the implementation the browser uses (narrator: it isn&apos;t). When click events occur, the browser would then execute the &lt;code class=&quot;language-text&quot;&gt;notifyListeners&lt;/code&gt; passing &lt;code class=&quot;language-text&quot;&gt;click&lt;/code&gt; as the &lt;code class=&quot;language-text&quot;&gt;eventID&lt;/code&gt; and an object containing all of the event data we&apos;ve come to expect as the &lt;code class=&quot;language-text&quot;&gt;event&lt;/code&gt; argument.&lt;/p&gt;
&lt;p&gt;With an understanding of events/messages, there are three new terms we need to understand here.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The Stack&lt;/li&gt;
&lt;li&gt;The Queue&lt;/li&gt;
&lt;li&gt;The Heap&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;The Stack&lt;/h3&gt;
&lt;p&gt;The stack is a list of function calls that create a stack of frames. The frames are generated when the engine encounters a new function call. That frame contains all of the variables and arguments of the function. Frames are generated until there are no more function calls being made. Then as each frame returns some value either implecitely or explicitely, frames are popped off the stack until there are no more frames.&lt;/p&gt;
&lt;p&gt;The absolute easiest way to understand the stack is realizing this is the exact same stack you see when debugging or encountering errors in the console. The stack trace is a human readable form of this stack.&lt;/p&gt;
&lt;h3&gt;The Queue&lt;/h3&gt;
&lt;p&gt;A queue a linear wait-list. Harking back to our event listeners, when a button is clicked a new item is pushed into this queue to process that button click. Then if event listeners are found to be observing this click, additional messages are pushed into this queue. The queue works by processing messages in a FIFO (first in first out) manner.&lt;/p&gt;
&lt;h3&gt;The Heap&lt;/h3&gt;
&lt;p&gt;The heap is just a section in memory where the engine has reserved for use. When items in the stack have variables for instance, those values are stored in the heap.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;pre class=&quot;language-html&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;   STACK                                 HEAP
-----------
-  Frame  -
-----------                 [] Object
-----------        [] Object
-  Frame  -
-----------
-----------                         [] Object
-  Frame  -
-----------
-----------------------------------------------------
- Message -- Message -- Message -- Message --       - QUEUE
-----------------------------------------------------&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is how JavaScript does what it does. Wait, process, execute rinse and repeat over and over until the end of time.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Named Routes in Flutter]]></title><description><![CDATA[Flutter provides built-in functionality for routing in apps. You can use either pass the MaterialApp a Widget, or a named route. There are…]]></description><link>https://www.aaronarney.dev/named-routes-in-flutter/</link><guid isPermaLink="false">https://www.aaronarney.dev/named-routes-in-flutter/</guid><pubDate>Sat, 21 Dec 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Flutter provides built-in functionality for routing in apps. You can use either pass the MaterialApp a Widget, or a named route.&lt;/p&gt;
&lt;p&gt;There are two ways of going about named routes, you can either:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use URL style routes /main or /login etc.&lt;/li&gt;
&lt;li&gt;Use an arbitrary string login&lt;em&gt;screen, main&lt;/em&gt;screen etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When using URL style identifiers, you must provide a default route of / or the app will crash. Personally, I don’t like this style at the moment, but in the future when Flutter web support is stable this may make more sense to me.&lt;/p&gt;
&lt;p&gt;The issue with using strings as ID’s is that static code analysis won’t catch typos. Meaning if you have a login&lt;em&gt;screen route but then attempt to use that route later in your app, but accidentally type logn&lt;/em&gt;screen, the IDE won’t catch that.&lt;/p&gt;
&lt;p&gt;Instead, in each of your screen widgets, I think it’s a good idea to add a static id to the class, so that you can reference it wherever you need without having to worry about getting the string correct every time.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;dart&quot;&gt;&lt;pre class=&quot;language-dart&quot;&gt;&lt;code class=&quot;language-dart&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;LoginScreen&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;StatefulWidget&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; String id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;login_screen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token metadata symbol&quot;&gt;@override&lt;/span&gt;
  _LoginScreenState &lt;span class=&quot;token function&quot;&gt;createState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_LoginScreenState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When you create your named routes Map, pass the id’s as the key.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;dart&quot;&gt;&lt;pre class=&quot;language-dart&quot;&gt;&lt;code class=&quot;language-dart&quot;&gt;routes&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  LoginScreen&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;LoginScreen&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then whenever you want to navigate to that screen, you simply access that static id.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;dart&quot;&gt;&lt;pre class=&quot;language-dart&quot;&gt;&lt;code class=&quot;language-dart&quot;&gt;Navigator&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;pushNamed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; LoginScreen&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That’s it.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[WordPress Transients]]></title><description><![CDATA[WordPress has several great API’s for caching and retrieving data, transients being one of them. Transients are best used in situations that…]]></description><link>https://www.aaronarney.dev/wordpress-transients/</link><guid isPermaLink="false">https://www.aaronarney.dev/wordpress-transients/</guid><pubDate>Fri, 29 Nov 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;WordPress has several great API’s for caching and retrieving data, transients being one of them.&lt;/p&gt;
&lt;p&gt;Transients are best used in situations that you need to persist data through several request-response transactions. They can have either a set expiration time or default to “indefinite.”&lt;/p&gt;
&lt;p&gt;If you’ve used a custom WP_Query at any point in your theme or plugin, you might already have a great use-case for implementing the transient API.&lt;/p&gt;
&lt;p&gt;The best part of transients is just how ridiculously simple they are to use. There are only two functions you need, get&lt;em&gt;transient and set&lt;/em&gt;transient.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token function&quot;&gt;set_transient&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;name_of_key&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;some&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;values&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;60&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;MINUTE_IN_SECONDS&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This code snippet stores an array of data with an expiration time of 60 minutes. Note that this is not guaranteed by WordPress to remain viable for 60 minutes. The expiration time is simply the maximum time that the transient should potentially be present. Because of this you should always ensure that there is a fallback routine to refresh the data set should the cache not be there!&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_transient&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;key&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token variable&quot;&gt;$data&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WP_Query&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;post_type&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;cpt&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;set_transient&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;key&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;60&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;MINUTE_IN_SECONDS&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Something else you will need to consider is cache priming. Let’s say you have a transient that is storing an array of possible values for a custom select dropdown element on a page. This data is being populated through all the titles in a custom post type you’ve created.&lt;/p&gt;
&lt;p&gt;Now, imagine that a visitor hits your site and triggers a new transient cache. This transient is set to expire in 60 minutes, but only a few minutes later one of your editors adds a new post. This new post will not be present in the transient for possibly an hour!&lt;/p&gt;
&lt;p&gt;The way to account for this is to prime your cache by adding an action that triggers when your custom post types are modified. This way you can clear any transients and ensure that your data is as fresh as possible.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;prime_my_cache&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; void &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;delete_transient&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;key&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token variable&quot;&gt;$data&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WP_Query&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;post_type&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;cpt&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token function&quot;&gt;set_transient&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;key&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;60&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;MINUTE_IN_SECONDS&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;add_action&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;edit_post_custom_post_type&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;prime_my_cache&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Very cool and very easy.&lt;/p&gt;
&lt;h2&gt;Resources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://codex.wordpress.org/Transients_API&quot;&gt;WordPress Transients API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[Azure Devops Pipeline Build with Shopify]]></title><description><![CDATA[At Leap, we utilize Microsoft's Team Foundation for all of our version control efforts. Included in this platform is Azure DevOps, a service…]]></description><link>https://www.aaronarney.dev/azure-devops-pipeline-shopify/</link><guid isPermaLink="false">https://www.aaronarney.dev/azure-devops-pipeline-shopify/</guid><pubDate>Thu, 11 Apr 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;At Leap, we utilize Microsoft&apos;s Team Foundation for all of our version control efforts. Included in this platform is Azure DevOps, a service that allows users to set up continuous improvement, continuous delivery, and continuous deployment, for repositories.&lt;/p&gt;
&lt;p&gt;A client of ours, &lt;a href=&quot;https://derbypie.com&quot;&gt;Kern&apos;s Kitchen&lt;/a&gt;, is a Shopify hosted website that requires deployments from the command line in order to update theme code. I decided to set up an Azure Pipeline to automate this deployment, but ran into an issue. Shopify developed a node package called Slate that allows a dev to generate, develop, and deploy themes. This package also includes a very annoying &quot;feature&quot; that stopped my automated deployments dead in their tracks.&lt;/p&gt;
&lt;h2&gt;The Issue&lt;/h2&gt;
&lt;p&gt;Before I jump into the issue, I want to provide my pipeline and give a bit of context. I&apos;m not going to go over on how to build a pipeline out in Azure, but it&apos;s worth noting the tasks I needed to be accomplished for deployments.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Install Node v9.10.1&lt;/li&gt;
&lt;li&gt;Run &lt;code class=&quot;language-text&quot;&gt;npm install&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Run &lt;code class=&quot;language-text&quot;&gt;npm run-script gulp&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Run &lt;code class=&quot;language-text&quot;&gt;npm run-script deploy&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In my &lt;code class=&quot;language-text&quot;&gt;package.json&lt;/code&gt;, my scripts look something like the following.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;pre class=&quot;language-json&quot;&gt;&lt;code class=&quot;language-json&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;scripts&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token property&quot;&gt;&quot;gulp&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;gulp build&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token property&quot;&gt;&quot;deploy&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;slate-tools build &amp;amp;&amp;amp; slate-tools deploy --skipPrompts&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;All of my tasks would execute right up until it hit step number 4, my &lt;code class=&quot;language-text&quot;&gt;deploy&lt;/code&gt; command. The &quot;error&quot; I received from Azure was as helpful as...well...something. Disclaimer, this isn&apos;t verbatim as I&apos;m writing this from memory at the moment.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;Error: Exited with error code &lt;span class=&quot;token number&quot;&gt;130&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When I inspected the logs, there was no error to be found, just that the script halted execution as soon as slate-tools initialized. At this point I started brainstorming and attempting different fixes, among them...&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Changed the &lt;code class=&quot;language-text&quot;&gt;Agent Pool&lt;/code&gt; to a mac, then ubuntu, and VS2017...thinking maybe the script was choking when it hit the &lt;code class=&quot;language-text&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator in my script for some reason.&lt;/li&gt;
&lt;li&gt;Changed the node version.&lt;/li&gt;
&lt;li&gt;Wrote a bash script that executed all the commands instead of using individual tasks.&lt;/li&gt;
&lt;li&gt;Upgraded slate-tools to the latest beta version.&lt;/li&gt;
&lt;li&gt;Many others..&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Then it hit me like a ton of bricks. The reason I couldn&apos;t reproduce this error locally, was that I have used slate-tools a ton. When you first build a theme with the slate-tools, Shopify asks if they can collect data from you using a prompt. Once you answer that question, a &lt;code class=&quot;language-text&quot;&gt;.slaterc&lt;/code&gt; file generates and stored on the root of your OS. Since this file was present, I was never asked again - even though I cleared all global packages from my system, did &lt;code class=&quot;language-text&quot;&gt;yarn cache clean&lt;/code&gt;, among other things.&lt;/p&gt;
&lt;p&gt;Now it all made sense. Since a new container&apos;s spun up to run my scripts, it will never save a &lt;code class=&quot;language-text&quot;&gt;.slaterc&lt;/code&gt; file locally. Thus, the question will always be asked &quot;can we fucking collect analytics from you,&quot; with the task runner never being able to answer. Instead, it aborts the script and fails, even with the &lt;code class=&quot;language-text&quot;&gt;--skipPrompts&lt;/code&gt; flag!&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;The solution was simple. Create a &lt;code class=&quot;language-text&quot;&gt;.slaterc&lt;/code&gt; file in the root of any Shopify project, copy the contents of the one on my local machine, and commit that to the repo. I also had to set a configuration value in the &lt;code class=&quot;language-text&quot;&gt;slate.config.js&lt;/code&gt; file that tells Slate to look for this rc file in the root of my project, as opposed to OS.&lt;/p&gt;
&lt;p&gt;What happens? Spoiler alert, it fucking deploys with no problems.&lt;/p&gt;
&lt;h3&gt;In Conclusion&lt;/h3&gt;
&lt;p&gt;Maybe there is a super easy way to combat these types of issues in packages during CI/CD builds. I&apos;m fairly new to using these automated systems, but after searching around I couldn&apos;t find a simple answer.&lt;/p&gt;
&lt;p&gt;I think the take away here is that, in my humble opinion, you should not create these kinds of prompts without a more clear way of circumventing them. As of this writing, Shopify decided the Slate tool was not worth maintaining or improving, so it might die soon anyway, who knows.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Fathom Analytics]]></title><description><![CDATA[The following is not entirely a step-by-step tutorial on how to get up and running with Fathom. Instead, this is an overview of my setup and…]]></description><link>https://www.aaronarney.dev/fathom-analytics/</link><guid isPermaLink="false">https://www.aaronarney.dev/fathom-analytics/</guid><pubDate>Mon, 04 Feb 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The following is not entirely a step-by-step tutorial on how to get up and running with Fathom. Instead, this is an overview of my setup and how I went about getting a Fathom server up and capturing data.&lt;/p&gt;
&lt;h2&gt;Fathom&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/usefathom/fathom&quot;&gt;Fathom is an privacy focused data collection system&lt;/a&gt; that replaces the need for using intrusive and overreaching platforms like Google or Adobe analytics. Of course companies or individuals that require a great deal of data filtering, conversions, filters, etc, will probably not be able to replace their platforms of choice with Fathom. The greatest &quot;weakness&quot; of Fathom is also its greatest strength, simplicity. Instead of tracking tons of data that you really will never use, much less &lt;em&gt;how&lt;/em&gt; to use, Fathom just gives you the data you&apos;re really looking for - visitors and top pages.&lt;/p&gt;
&lt;p&gt;It took me a few hours to get the Fathom server up and running using an Amazon Web Services EC2 instance. This is mostly due to my inexperience with managing servers on AWS and less to do with Fathom as an application. I first attempted to run the Fathom docker image using Amazon&apos;s ECS platform, but those efforts proved futile. In the end I decided to say fuck it and just boot up an Ubuntu image and install Fathom manually.&lt;/p&gt;
&lt;p&gt;This is how I went about it.&lt;/p&gt;
&lt;h2&gt;Launching EC2 Instance&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Create an EC2 instance, with Ubuntu&lt;/li&gt;
&lt;li&gt;Create an Elastic IP and associate it with the EC2 instance&lt;/li&gt;
&lt;li&gt;SSH into the instance&lt;/li&gt;
&lt;li&gt;Run the following commands&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Download the release&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;curl&lt;/span&gt; -o fathom.tar.gz -L https://github.com/usefathom/fathom/releases/download/v1.2.1/fathom_1.2.1_linux_amd64.tar.gz

&lt;span class=&quot;token comment&quot;&gt;# Extract it&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;tar&lt;/span&gt; -C /usr/local/bin -xzf fathom.tar.gz

&lt;span class=&quot;token comment&quot;&gt;# Change the permissions&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;chmod&lt;/span&gt; +x /usr/local/bin/fathom

&lt;span class=&quot;token comment&quot;&gt;# Check that it installed correctly&lt;/span&gt;
fathom --version&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Configuring Fathom&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Create an &lt;code class=&quot;language-text&quot;&gt;.env&lt;/code&gt; file with the following&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token assign-left variable&quot;&gt;FATHOM_SERVER_ADDR&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8080&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;FATHOM_GZIP&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;true
&lt;span class=&quot;token assign-left variable&quot;&gt;FATHOM_DEBUG&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;true
&lt;span class=&quot;token assign-left variable&quot;&gt;FATHOM_DATABASE_DRIVER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;sqlite3&quot;&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;FATHOM_DATABASE_NAME&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;fathom.db&quot;&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;FATHOM_SECRET&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;random-secret-string-change-this&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Create a service file&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;touch&lt;/span&gt; /etc/systemd/system/fathom.service&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;Add the following to the file&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Unit&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;Description&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;Starts the fathom server
&lt;span class=&quot;token assign-left variable&quot;&gt;Requires&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;network.target
&lt;span class=&quot;token assign-left variable&quot;&gt;After&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;network.target

&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Service&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;simple
&lt;span class=&quot;token assign-left variable&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ubuntu
&lt;span class=&quot;token assign-left variable&quot;&gt;Restart&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;always
&lt;span class=&quot;token assign-left variable&quot;&gt;RestartSec&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;WorkingDirectory&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/home/ubuntu
&lt;span class=&quot;token assign-left variable&quot;&gt;ExecStart&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/usr/local/bin/fathom server

&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Install&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;WantedBy&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;multi-user.target&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;Reload the daemon, enable fathom, and start it.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; systemctl daemon-reload
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; systemctl &lt;span class=&quot;token builtin class-name&quot;&gt;enable&lt;/span&gt; fathom
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; systemctl start fathom&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Configuring a Custom Domain&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Create a new Hosted Zone in &lt;strong&gt;AWS Route 53&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Supply your domain name and leave the &lt;code class=&quot;language-text&quot;&gt;type&lt;/code&gt; as &lt;code class=&quot;language-text&quot;&gt;Public Hosted Zone&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Once created, make two new &lt;code class=&quot;language-text&quot;&gt;Record Set&lt;/code&gt; by clicking on the &lt;code class=&quot;language-text&quot;&gt;Create Record Set&lt;/code&gt; button&lt;/li&gt;
&lt;li&gt;Add &lt;code class=&quot;language-text&quot;&gt;A&lt;/code&gt; type with a blank &lt;code class=&quot;language-text&quot;&gt;Name&lt;/code&gt;. Paste the address of your &lt;strong&gt;Elastic IP&lt;/strong&gt; as the &lt;code class=&quot;language-text&quot;&gt;value&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Add &lt;code class=&quot;language-text&quot;&gt;A&lt;/code&gt; type with a name of &lt;code class=&quot;language-text&quot;&gt;www&lt;/code&gt;. Paste the &lt;code class=&quot;language-text&quot;&gt;Public DNS&lt;/code&gt; of the &lt;strong&gt;EC2&lt;/strong&gt; as the &lt;code class=&quot;language-text&quot;&gt;value&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;SSH back into the EC2 instance&lt;/li&gt;
&lt;li&gt;Enable &lt;code class=&quot;language-text&quot;&gt;mod_proxy&lt;/code&gt; via the commands&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; a2enmod proxy
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; a2enmod proxy_balancer
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; a2enmod proxy_http&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ol start=&quot;8&quot;&gt;
&lt;li&gt;Edit the &lt;code class=&quot;language-text&quot;&gt;Apache2.conf&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;vim&lt;/span&gt; /etc/apache2/apache2.conf

&lt;span class=&quot;token comment&quot;&gt;# Add the following to the file&lt;/span&gt;
ProxyPass &lt;span class=&quot;token string&quot;&gt;&quot;/&quot;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;http://keytar.co:8080/&quot;&lt;/span&gt;
ProxyPassReverse &lt;span class=&quot;token string&quot;&gt;&quot;/&quot;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;http://keytar.co:8080/&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ol start=&quot;9&quot;&gt;
&lt;li&gt;Restart Apache &lt;code class=&quot;language-text&quot;&gt;sudo systemctl restart apache2&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now the requests to &lt;code class=&quot;language-text&quot;&gt;http://keytar.co&lt;/code&gt; should route to &lt;code class=&quot;language-text&quot;&gt;http://keytar.co:8080&lt;/code&gt; behind the scenes. That way users don&apos;t have to know to use the &lt;code class=&quot;language-text&quot;&gt;:8080&lt;/code&gt; port and makes passing the URL as a configuration much easier.&lt;/p&gt;
&lt;h3&gt;Content Security Policy&lt;/h3&gt;
&lt;p&gt;Fathom uses embedded data in images as a way of capturing data instead of ajax. Because of this, my content security policy was preventing the images from being created, as it sets the source of the image to be my tracking url for Fathom, in my case &lt;code class=&quot;language-text&quot;&gt;https://keytar.co&lt;/code&gt; (a domain I had sitting around doing nothing).&lt;/p&gt;
&lt;p&gt;Modifying the CSP header in my &lt;code class=&quot;language-text&quot;&gt;gatsby-config.js&lt;/code&gt; was easy enough. I just had to pass &lt;code class=&quot;language-text&quot;&gt;https://keytar.co&lt;/code&gt; as an additional value.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;csp&quot;&gt;&lt;pre class=&quot;language-csp&quot;&gt;&lt;code class=&quot;language-csp&quot;&gt;Content-Security-Policy: &lt;span class=&quot;token directive keyword&quot;&gt;default-src &lt;/span&gt;&lt;span class=&quot;token safe selector&quot;&gt;&apos;self&apos;&lt;/span&gt;; &lt;span class=&quot;token directive keyword&quot;&gt;script-src &lt;/span&gt;&lt;span class=&quot;token safe selector&quot;&gt;&apos;self&apos;&lt;/span&gt; &lt;span class=&quot;token unsafe function&quot;&gt;&apos;unsafe-inline&apos;&lt;/span&gt; https://keytar.co; &lt;span class=&quot;token directive keyword&quot;&gt;connect-src &lt;/span&gt;&lt;span class=&quot;token safe selector&quot;&gt;&apos;self&apos;&lt;/span&gt;; &lt;span class=&quot;token directive keyword&quot;&gt;img-src &lt;/span&gt;https://&lt;span class=&quot;token unsafe function&quot;&gt;*&lt;/span&gt;.ocular-rhythm.io https://keytar.co data:; &lt;span class=&quot;token directive keyword&quot;&gt;style-src &lt;/span&gt;&lt;span class=&quot;token safe selector&quot;&gt;&apos;self&apos;&lt;/span&gt; &lt;span class=&quot;token unsafe function&quot;&gt;&apos;unsafe-inline&apos;&lt;/span&gt;;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The last step is to throw the tracking code Fathom gives you when you create a new site.&lt;/p&gt;
&lt;p&gt;Bye.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Importance of Perception]]></title><description><![CDATA[During my most recent performance review at work, my boss and I began discussing communication. For almost my entire life, I have had an…]]></description><link>https://www.aaronarney.dev/importance-of-perception/</link><guid isPermaLink="false">https://www.aaronarney.dev/importance-of-perception/</guid><pubDate>Fri, 01 Feb 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;During my most recent performance review at work, my boss and I began discussing communication. For almost my entire life, I have had an weakness when it comes to communicating with my co-workers. I can articulate my thoughts well, and enjoy documenting code and processes, but there is a recurring perception that I &quot;don&apos;t care&quot; about my job - but this couldn&apos;t be farther from the truth.&lt;/p&gt;
&lt;p&gt;It was brought up that I have excellent work ethic and my boss believes I am one of the hardest workers at my company. Yet, due to several reasons such as my sarcastic nature, the fact I don&apos;t often smile, and have a very even and flat emotional level, it&apos;s perceived to others that I lack drive and ambition. This is nothing new to me, I&apos;ve had this recurring problem everywhere I&apos;ve gone, including the Army.&lt;/p&gt;
&lt;p&gt;In a strange twist of fate, I so happened to be reading a book entitled &lt;a href=&quot;https://pragprog.com/book/mnee2/release-it-second-edition&quot;&gt;&quot;Release It!: Design and Deploy Production-Ready Software&quot;&lt;/a&gt; a few days ago. In it the author was describing a situation in which the airline company he worked for had a major outage. Part of the post-mortem, the author flew to another location to talk to the team them and assess what he could salvage from the server information. The reason for this was not for solving the problem itself, but also for managing the perception that his team was looking for answers. His direct quote:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&quot;...managing perception after a major incident can be as important as managing the incident itself.&quot; - Michael T. Nygard&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I&apos;m taking the quote a bit out of context here, but the lesson is roughly the same. Even though I &lt;strong&gt;do&lt;/strong&gt; care about my job a great deal and &lt;strong&gt;do&lt;/strong&gt; a lot behind the scenes, people don&apos;t see that. They only know what I &quot;give&quot; them in the form of enthusiasm, reaction, micro-interactions, etc..in other words - &lt;em&gt;the perception&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;My Plan&lt;/h2&gt;
&lt;p&gt;The goal is to manage this perception using a combination of factors.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Maintain eye contact with the person I am talking to.&lt;/li&gt;
&lt;li&gt;Don&apos;t isolate myself and be more social at work through small talk.&lt;/li&gt;
&lt;li&gt;Reduce the sarcasm when appropriate. If others are concerned or stressed out, don&apos;t try to cut the tension with a joke. Things are serious to my co-workers, so I need to act like they are serious even if I find them insignificant.&lt;/li&gt;
&lt;li&gt;Reduce or cut passive aggressiveness. We all suffer from this affliction.&lt;/li&gt;
&lt;li&gt;Be an advocate for my accomplishments. If I do something cool, share it with people. Don&apos;t assume anyone cares.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;That at least starts me out with giving me a good leg up on trying to improve my soft skills. It will be difficult for me to quantity or measure the results of my efforts, I will be able to tell a difference based on the attitudes afforded to me.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Writing the Ferret Plugin]]></title><description><![CDATA[Recently I decided to begin leveraging the Sentry service for some of the WordPress sites I manage. I wanted to use this on several sites…]]></description><link>https://www.aaronarney.dev/writing-ferret-plugin/</link><guid isPermaLink="false">https://www.aaronarney.dev/writing-ferret-plugin/</guid><pubDate>Fri, 11 Jan 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Recently I decided to begin leveraging the &lt;a href=&quot;https://sentry.io&quot;&gt;Sentry&lt;/a&gt; service for some of the WordPress sites I manage. I wanted to use this on several sites, and I noticed that Sentry has a PHP library but not an officially supported plugin for WordPress.&lt;/p&gt;
&lt;p&gt;There was one other plugin in the directory, but it essentially only acted as an entry point in which to interface with Sentrys SDK. I was looking for a solution where I could upload the plugin, set my API key (or DSN in Sentry nomenclature) and just catch any and all errors with no configuration. I decided to just quickly write my own plugin.&lt;/p&gt;
&lt;h2&gt;The Boilerplate&lt;/h2&gt;
&lt;p&gt;Usually, when I write a new plugin, I tend to head straight for the &lt;a href=&quot;https://wppb.me/&quot;&gt;WordPress plugin boilerplate generator&lt;/a&gt; to quickly create the plugin files.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/61851bc28607dfc5de5293e83ae2e235/wppb-screenshot.png&quot; alt=&quot;WPPB Screenshot&quot;&gt;&lt;/p&gt;
&lt;p&gt;I made a snap decision to just name the plugin &lt;strong&gt;Ferret&lt;/strong&gt; because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a) I love ferrets (used to own two!)&lt;/li&gt;
&lt;li&gt;b) Due to trademarks, I can&apos;t really use my original idea of &lt;code class=&quot;language-text&quot;&gt;WordPress Sentry&lt;/code&gt; since both of those (I believe) are trademarked&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;The SDK&lt;/h3&gt;
&lt;p&gt;The next step was to take a look at the integration documentation on Sentry&apos;s website. Luckily enough, they have a library for just about every language and framework you could realistically want in 2019. The PHP library is part of &lt;strong&gt;composer&lt;/strong&gt;, so including that in the project was very simple.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;composer require &lt;span class=&quot;token string&quot;&gt;&quot;sentry/sentry&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, I had to ensure the composer &lt;code class=&quot;language-text&quot;&gt;autoloader.php&lt;/code&gt; is called in my plugin. This would work perfect in the plugins entry point file - &lt;code class=&quot;language-text&quot;&gt;ferret/ferret.php&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;require_once&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;__DIR__&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;/vendor/autoload.php&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now was the time to initialize the Sentry client, which happens to be a class named &lt;code class=&quot;language-text&quot;&gt;Raven_Client&lt;/code&gt;. I put this logic in the base plugin class located in the &lt;code class=&quot;language-text&quot;&gt;includes&lt;/code&gt; directory - &lt;code class=&quot;language-text&quot;&gt;ferret/includes/class-ferret.php&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To follow the practice of single responsibility and to keep my functions small, I created a new function to initialize Sentry. Note that to keep my DSN and Project ID secret, I&apos;m just using dummy text in their place.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;init_sentry&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token variable&quot;&gt;$client&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Raven_Client&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;https://abc123@sentry.io/123&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token variable&quot;&gt;$error_handler&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Raven_ErrorHandler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$client&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token variable&quot;&gt;$error_handler&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;registerExceptionHandler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token variable&quot;&gt;$error_handler&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;registerErrorHandler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token variable&quot;&gt;$error_handler&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;registerShutdownFunction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;$error_handler&lt;/code&gt; here hooks into all errors that occur in PHP. This is desirable because I am wanting to just &quot;set and forget&quot; the client.&lt;/p&gt;
&lt;p&gt;The last thing to do was call this function. The best place is to just call it during the construction of the class, so I added it to the &lt;code class=&quot;language-text&quot;&gt;__constructor&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;init_sentry&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Plugin Options&lt;/h3&gt;
&lt;p&gt;Writing code to handle options has gotten better than it used to be (I think), but it&apos;s still a somewhat confusing process. The way I&apos;ve been accomplishing it lately is by creating a helper class to encapsulate my getters and setters for the native WordPress &lt;code class=&quot;language-text&quot;&gt;get_option&lt;/code&gt; function. Then passing an instance of that class to constructors of other classes, so they have a single access point for everything. This has reduced the need to arbitrarily pass around key strings and reduces the cognitive load.&lt;/p&gt;
&lt;p&gt;Since quite a bit is going on to create these options, I don&apos;t want to write everything I did in this post, but it was essentially what I just outlined above. If you want to take a look and dissect my approach, you may find the following files of interest...&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/leap-spark/ferret/blob/master/public/class-ferret-public.php&quot;&gt;Ferret_Public class&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/leap-spark/ferret/blob/master/includes/class-ferret-options.php&quot;&gt;Ferret_Options class&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/leap-spark/ferret/blob/master/public/partials/ferret-public-display.php&quot;&gt;Public Display Partial&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Submission to Directory&lt;/h4&gt;
&lt;p&gt;I had never submitted a plugin to the WordPress plugin directory before, so this was all new to me. Luckily, WordPress has some fairly thorough resources for the process and what I should be cognizant of when submitting a new plugin.&lt;/p&gt;
&lt;p&gt;Since this was a &lt;em&gt;very&lt;/em&gt; simple plugin, there wasn&apos;t too much to be worried about. There were only a few things I had to go &quot;out of my way&quot; to prepare for submission...&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Localization&lt;/strong&gt; - I had never generated this before but it was simple. I used the recommended app &lt;a href=&quot;https://poedit.net/&quot;&gt;Poedit&lt;/a&gt; to generate my english &lt;code class=&quot;language-text&quot;&gt;.pot&lt;/code&gt; file. For the uninitiated, you need to wrap all of your plain text that is visible to users with the native WordPress &lt;code class=&quot;language-text&quot;&gt;__()&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;_e()&lt;/code&gt; functions. This lets WordPress know that the strings are translatable.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;README.txt&lt;/strong&gt; - The readme is very important. This is where all of the critical information is placed, such as compatible versions, author data, descriptions, etc.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SVN&lt;/strong&gt; - Once the plugin is approved, you are required to commit your code into an svn repository. This is a bummer but not the end of the world. SVN is truly awful in my opinion, but most of that opinion stems from lack of extensive use and understanding. The &quot;svn way&quot; is quite different than the &quot;git way&quot;. When all was said and done, I think I had to publish around 7 versions just to rename a file.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Success!&lt;/h4&gt;
&lt;p&gt;My plugin was approved in just a day, which is surprising as they claim it can take up to two weeks for that to happen.&lt;/p&gt;
&lt;p&gt;Next, I hope to extend the plugin to allow a user to easily set up custom filters and events much like how Unity works when setting up event handlers in the UI.&lt;/p&gt;
&lt;p&gt;Thanks for reading!&lt;/p&gt;
&lt;h5&gt;Resources&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/leap-spark/ferret&quot;&gt;Plugin Git Repo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://wordpress.org/plugins/ferret/&quot;&gt;Plugin Homepage&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[Initial Thoughts on Flutter]]></title><description><![CDATA[While building an app in React Native, I accidentally stumbled on Flutter. I had heard of, but never used the Dart language and was…]]></description><link>https://www.aaronarney.dev/initial-thoughts-flutter/</link><guid isPermaLink="false">https://www.aaronarney.dev/initial-thoughts-flutter/</guid><pubDate>Wed, 19 Dec 2018 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;While building an app in React Native, I accidentally stumbled on Flutter. I had heard of, but never used the Dart language and was surprised to learn that Flutter existed as a React Native alternative. Glancing the docs got me excited to try it out, so I wrapped up as much as I could to make my other app &quot;complete&quot; and immediately moved into rebuilding it in Flutter.&lt;/p&gt;
&lt;p&gt;I decided to run through a Udemy course (which I highly recommend) called &lt;a href=&quot;https://www.udemy.com/flutter-dart-the-complete-flutter-app-development-course/&quot;&gt;&quot;The Complete Flutter App Development Course&quot;&lt;/a&gt; before I started hacking together an app on my own, just to make sure I was building it the &quot;Flutter&quot; way. After getting far enough through that course I started porting over the app. I&apos;m around 35% of the way through and decided to put together a sort of pros and cons list of my findings so far.&lt;/p&gt;
&lt;h2&gt;Pros:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Simple to start a new app.&lt;/li&gt;
&lt;li&gt;Very simple to debug with additional tools built into Android Studio (inspectors, performance, etc).&lt;/li&gt;
&lt;li&gt;Dart is basically TypeScript, so the adoption and learning process is smooth if you have experience with it.&lt;/li&gt;
&lt;li&gt;Testing framework is built-in an easy to get started. No installing additional dependencies and configurations.&lt;/li&gt;
&lt;li&gt;Laying out and styling the UI is substantially easier. Once you understand how to compose your Widgets, that is.&lt;/li&gt;
&lt;li&gt;The API documentation for both Dart and Flutter is fantastic.&lt;/li&gt;
&lt;li&gt;Google introduces a new architecture design pattern called BLoC which seems well suited to Flutter and apps in general.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Cons:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Not a lot of worldwide adoption so the ecosystem is much smaller than React Native.&lt;/li&gt;
&lt;li&gt;While the API docs are great, I found there to be lacking real-world examples here and there.&lt;/li&gt;
&lt;li&gt;The Dart linter takes direction from a remote server so can be difficult to configure. There was limited documentation on how to customize these lint rules. Other times I encountered errors which said one thing but meant another. Overall this was a very limited and tame issue.&lt;/li&gt;
&lt;li&gt;Could be a harder &quot;sell&quot; to development teams since it not only requires learning a new framework, but a new language as well. If teams are already using React Native and other JavaScript frameworks, I could see this being an uphill battle.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a fan of React, it &lt;em&gt;almost&lt;/em&gt; disappoints me to say that I found Flutter to be a much better tech stack to build apps with. The amount of configuring and third-party libraries and tools you need to get started in React Native and debugging, is just maddening. I absolutely love just having everything work and tie-in out of the box. Debugging apps on a mac with React Native Debugger sucks!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Testing WordPress Plugins with PHPUnit and WP Mock]]></title><description><![CDATA[Part 1: Up & Running Assumptions
This article assumes a basic understanding of WordPress Plugins and the PHP language. I'll also be using…]]></description><link>https://www.aaronarney.dev/testing-wordpress-plugins-phpunit/</link><guid isPermaLink="false">https://www.aaronarney.dev/testing-wordpress-plugins-phpunit/</guid><pubDate>Thu, 06 Dec 2018 00:00:00 GMT</pubDate><content:encoded>&lt;h2&gt;Part 1: Up &amp;#x26; Running&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Assumptions&lt;/strong&gt;
This article assumes a basic understanding of WordPress Plugins and the PHP language. I&apos;ll also be using the OSX/Linux flavor in terminal examples. If you are on Windows, you will need to use your own respective commands.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Testing code is important. It&apos;s the only way to know for certain that something you&apos;ve changed in your code behaves exactly as how you intended. Without automated testing there is a need to constantly run your code and check for every condition manually after every single change you make. This is far from ideal and a huge waste of your time. Granted tests do &lt;strong&gt;not&lt;/strong&gt; make your code bug free, but it certainly is a start to a healthy and maintainable codebase.&lt;/p&gt;
&lt;p&gt;As you begin writing tests you may find that your idea of &quot;simple&quot; and &quot;elegant&quot; code, may be turned upside down. For example, I discovered that even having several &lt;code class=&quot;language-text&quot;&gt;if&lt;/code&gt; statements in one function can be somewhat difficult to test every condition for which means that my function was too complicated. This is what testing code gifts us.&lt;/p&gt;
&lt;p&gt;The downside of tests are that they can be quite difficult to get started with and slow your development time substantially. That&apos;s not even factoring in the added complexity of integrating unit tests with WordPress, but that&apos;s something I hope to help remedy by the time you finish this article.&lt;/p&gt;
&lt;p&gt;For those who are new to testing, there are quite a few different types of tests you can write. For the sake of brevity, we&apos;re only going to cover two:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Unit Tests&lt;/li&gt;
&lt;li&gt;Integration Tests&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Unit Tests&lt;/h3&gt;
&lt;p&gt;Unit tests are ones that only test a single part, or unit, of your code. Consider the following class...&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Math&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; int a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; int b &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; int &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;divide&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; int a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; int b &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; int &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; int a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; int b &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; int &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With unit tests, we would write separate tests for each individual method, &lt;code class=&quot;language-text&quot;&gt;add&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;divide&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;multiply&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Integration Tests&lt;/h3&gt;
&lt;p&gt;On the other hand, integration tests are those that test an entire system or object. For example, if we had written a class that uses dependency injection to load another class in its constructor; with integration tests we would test that the classes work together appropriately.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FirstClass&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$secondClass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; SecondClass &lt;span class=&quot;token variable&quot;&gt;$instance&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token variable&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;secondClass&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$instance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You may be wondering why we wouldn&apos;t just test the constructor with a unit test in the following way:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$actual&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FirstClass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SecondClass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The reason is that when we write our unit test for the constructor, we&apos;ll be passing what is known as a &lt;code class=&quot;language-text&quot;&gt;stub&lt;/code&gt; instead of an actual &lt;code class=&quot;language-text&quot;&gt;SecondClass&lt;/code&gt; object. We&apos;ll get into that later on, but for now just know that unit tests should &lt;strong&gt;always&lt;/strong&gt; be done in isolation, meaning to have no external dependencies or reliance on code outside of its own body.&lt;/p&gt;
&lt;h3&gt;Get Dependencies&lt;/h3&gt;
&lt;p&gt;In order to even use WP Mock, you will need to have &lt;code class=&quot;language-text&quot;&gt;PHP v^7.0&lt;/code&gt; installed in your environment. You&apos;ll also need the plugin you are going to test installed into WordPress.&lt;/p&gt;
&lt;p&gt;Lastly, you&apos;ll need &lt;code class=&quot;language-text&quot;&gt;composer&lt;/code&gt; installed. If you don&apos;t know what composer is or how to use it, take a pause here and go read through the &lt;a href=&quot;https://getcomposer.org/doc/00-intro.md&quot;&gt;getting started&lt;/a&gt; guide on composers website.&lt;/p&gt;
&lt;h2&gt;Installing&lt;/h2&gt;
&lt;p&gt;Start by installing &lt;code class=&quot;language-text&quot;&gt;PHPUnit&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;WP Mock&lt;/code&gt; into your plugin. Open up a terminal and navigate to your plugins root directory and run the following.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;composer require --dev 10up/wp_mock:0.3.0&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As a side note, an issue I&apos;ve run into is that I have to use a downgraded version of PHPUnit in order for the tests to pass. At the time of this writing, there is a solution &lt;a href=&quot;https://github.com/10up/wp_mock/issues/117&quot;&gt;that can be found here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To mitigate, require PHPUnit version &lt;code class=&quot;language-text&quot;&gt;6.5.8&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;composer require --dev phpunit/phpunit:6.5.8&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Configuration&lt;/h2&gt;
&lt;p&gt;Next, a configuration file can optionally be created, though I highly recommend doing so. This configuration file accomplishes things such as telling PHPUnit where to look for tests, what directories to exclude, and if code coverage reports should be created.&lt;/p&gt;
&lt;p&gt;This file should be named &lt;code class=&quot;language-text&quot;&gt;phpunit.xml.dist&lt;/code&gt; and placed in your project root. To start, I&apos;ll specify..&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Test directory as &lt;code class=&quot;language-text&quot;&gt;./tests/&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Test file names all start with &lt;code class=&quot;language-text&quot;&gt;test-&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;File extensions with &lt;code class=&quot;language-text&quot;&gt;.php&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;xml&quot;&gt;&lt;pre class=&quot;language-xml&quot;&gt;&lt;code class=&quot;language-xml&quot;&gt;&lt;span class=&quot;token prolog&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot;?&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;phpunit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;testsuites&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;testsuite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;directory&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;test-&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;suffix&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;.php&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;./tests/&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;directory&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;testsuite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;testsuites&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;phpunit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I like to see my code coverage so I have a clear picture of exactly what code is getting hit throughout my tests, so I&apos;ll set that up as well. Code coverage reports are great visual helpers that allow you to quickly see and understand what classes and methods are lacking tests.&lt;/p&gt;
&lt;p&gt;This section gets added just beneath the &lt;code class=&quot;language-text&quot;&gt;&amp;lt;/testsuites&amp;gt;&lt;/code&gt; closing element.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;xml&quot;&gt;&lt;pre class=&quot;language-xml&quot;&gt;&lt;code class=&quot;language-xml&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;logging&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;log&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;coverage-html&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
         &lt;span class=&quot;token attr-name&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tests/coverage&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
         &lt;span class=&quot;token attr-name&quot;&gt;showUncoveredFiles&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;true&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
         &lt;span class=&quot;token attr-name&quot;&gt;charset&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;UTF-8&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
         &lt;span class=&quot;token attr-name&quot;&gt;highlight&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;true&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
         &lt;span class=&quot;token attr-name&quot;&gt;lowUpperBound&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;35&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
         &lt;span class=&quot;token attr-name&quot;&gt;highLowerBound&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;70&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;logging&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I&apos;m telling PHPUnit that I want:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The report to be output as HTML&lt;/li&gt;
&lt;li&gt;To output the report files into the &lt;code class=&quot;language-text&quot;&gt;tests/coverage&lt;/code&gt; directory&lt;/li&gt;
&lt;li&gt;To display files in the report that do not have associated tests (so we know where we should create tests)&lt;/li&gt;
&lt;li&gt;Highlight the coverage in either red (for uncovered) or green (for covered)&lt;/li&gt;
&lt;li&gt;Low upper bound is a percentage. This indicates of how uncovered a file can be before it is considered to be ”lowly&quot; covered.&lt;/li&gt;
&lt;li&gt;The high upper bound is just the opposite. How covered a file is before its considered &quot;highly&quot; covered.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Lastly, we need to add a section that specifies directories to exclude from being parsed. This is required for the code coverage to function. It wouldn&apos;t make sense to allow PHPUnit to crawl all files in the &lt;code class=&quot;language-text&quot;&gt;vendor&lt;/code&gt; directory since that isn&apos;t our code, and therefore shouldn&apos;t be our responsibility to unit test.&lt;/p&gt;
&lt;p&gt;Add this section directly under the &lt;code class=&quot;language-text&quot;&gt;&amp;lt;/logging&amp;gt;&lt;/code&gt; section, replacing the &lt;code class=&quot;language-text&quot;&gt;./your-source-files&lt;/code&gt; with the directory your plugin code is contained in.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;xml&quot;&gt;&lt;pre class=&quot;language-xml&quot;&gt;&lt;code class=&quot;language-xml&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;whitelist&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;processUncoveredFilesFromWhitelist&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;true&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;directory&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;suffix&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;.php&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;./your-source-files&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;directory&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;exclude&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;directory&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;suffix&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;.php&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;/tests/&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;directory&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;directory&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;suffix&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;.php&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;/vendor/&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;directory&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;exclude&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;whitelist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;The Bootstrap File&lt;/h2&gt;
&lt;p&gt;Next we need to write the &lt;code class=&quot;language-text&quot;&gt;bootstrap.php&lt;/code&gt; file which acts as the entry point into the tests. First create the file inside of your &lt;code class=&quot;language-text&quot;&gt;tests&lt;/code&gt; directory...&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;touch&lt;/span&gt; tests/bootstrap.php&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In order for PHPUnit to use this file, it will need to be defined in your &lt;code class=&quot;language-text&quot;&gt;phpunit.dist.xml&lt;/code&gt; file...&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;xml&quot;&gt;&lt;pre class=&quot;language-xml&quot;&gt;&lt;code class=&quot;language-xml&quot;&gt;&lt;span class=&quot;token prolog&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot;?&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;phpunit&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;bootstrap&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tests/bootstrap.php&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    ...
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;phpunit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The contents of this file will be fairly simple. We will need to define just a few things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Require the composer autoloader file so your dependencies can be loaded&lt;/li&gt;
&lt;li&gt;Your plugin entry file&lt;/li&gt;
&lt;li&gt;Any other dependencies you create (such as factories)&lt;/li&gt;
&lt;li&gt;Invoking the &lt;code class=&quot;language-text&quot;&gt;1WP_Mock::bootstrap()&lt;/code&gt; static method&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Your file may end up looking something like this (replace &lt;code class=&quot;language-text&quot;&gt;plugin-name&lt;/code&gt; with your plugin entry file).&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;require_once&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;dirname&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;dirname&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;__FILE__&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;/vendor/autoload.php&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;require_once&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;dirname&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;dirname&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;__FILE__&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token single-quoted-string string&quot;&gt;&apos;/plugin-name.php&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
WP_Mock&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;bootstrap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Save that file, and let&apos;s move on.&lt;/p&gt;
&lt;h2&gt;Writing a Test&lt;/h2&gt;
&lt;p&gt;Now is the time to write a simple passing test so you can ensure everything is wired up correctly.&lt;/p&gt;
&lt;p&gt;In your tests directory, create a directory called &lt;code class=&quot;language-text&quot;&gt;unit&lt;/code&gt;. Create a test file inside of that directory called &lt;code class=&quot;language-text&quot;&gt;test-example.php.&lt;/code&gt;&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; tests &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;mkdir&lt;/span&gt; unit
&lt;span class=&quot;token function&quot;&gt;touch&lt;/span&gt; unit/test-example.php&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Begin by defining your class and extending the &lt;code class=&quot;language-text&quot;&gt;WP_Mock&lt;/code&gt; test case class. In order for WP&lt;em&gt;Mock to work, you will have to implement the &lt;code class=&quot;language-text&quot;&gt;setUp&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;tearDown&lt;/code&gt; methods in every class that you use WP&lt;/em&gt;Mock on. These methods let you define logic that should be executed as the test is about to run, and then directly after. You would put things such as instantiating stubs or mock objects.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token php language-php&quot;&gt;&lt;span class=&quot;token delimiter important&quot;&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TestExample&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WP_Mock&lt;span class=&quot;token punctuation&quot;&gt;\&lt;/span&gt;Tools&lt;span class=&quot;token punctuation&quot;&gt;\&lt;/span&gt;TestCase&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

   &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;setUp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        WP_Mock&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setUp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

   &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;tearDown&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        WP_Mock&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;tearDown&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, add a method that just has a single assertion. Just to make things easy, let&apos;s assert that &lt;code class=&quot;language-text&quot;&gt;true === true&lt;/code&gt; since we know it will evaluate to, well, true...&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;pre class=&quot;language-php&quot;&gt;&lt;code class=&quot;language-php&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;test_arbitrary_value&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token variable&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;assertTrue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token boolean constant&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Open up your terminal and run the tests...&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;./vendor/bin/phpunit&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If all is well, you should see something to the following output...&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;Time: &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; seconds, Memory: &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;.00MB

OK &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; tests, &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; assertions&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If for some reason you have any errors or failures use the output and error messages to debug and problem solve.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Hopefully that gives you a taste of how to set up tests with your WordPress plugin, but there&apos;s still plenty more to cover and learn. Stay tuned for part 2.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Making the Switch to TypeScript]]></title><description><![CDATA[If you've been living under a rock, you might have not yet heard about Microsoft's TypeScript - "a typed superset of JavaScript that…]]></description><link>https://www.aaronarney.dev/making-switch-to-typescript/</link><guid isPermaLink="false">https://www.aaronarney.dev/making-switch-to-typescript/</guid><pubDate>Mon, 05 Nov 2018 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you&apos;ve been living under a rock, you might have not yet heard about &lt;a href=&quot;https://www.typescriptlang.org/index.html&quot;&gt;Microsoft&apos;s TypeScript&lt;/a&gt; - &quot;a typed superset of JavaScript that compiles to JavaScript.&quot; A typed superset in this context just means it combines plain old JavaScript syntax with a few new extras.&lt;/p&gt;
&lt;p&gt;It can often be overwhelming learning a new set of syntax and if you are new to JavaScript it may feel even more so. Luckily, learning TypeScript doesn&apos;t mean adopting it in its entirety. You can use as much or as little as you like, which makes getting started relatively painless.&lt;/p&gt;
&lt;h2&gt;What is TypeScript For?&lt;/h2&gt;
&lt;p&gt;To answer this question, it is first helpful to understand how JavaScript works. Since JavaScript is a loosely typed language, that means that we don&apos;t have to explicitly declare our types, instead the JavaScript &lt;a href=&quot;https://en.wikipedia.org/wiki/Just-in-time_compilation&quot;&gt;JIT (just in type) compiler&lt;/a&gt; that is built into our web browsers, simply infer the type at run time.&lt;/p&gt;
&lt;p&gt;That makes situations like this possible:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;string&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In strictly typed languages such as C# or C++, the above example would throw errors for several reasons.&lt;/p&gt;
&lt;p&gt;This is what TypeScript helps to mimic and achieve, a way to declare and enforce types in your JavaScript files. This helps to reduce bugs and increase collaboration. Not to mention it can reduce the sizes of your functions since you no longer must dedicate the first few lines performing the manual interrogation of your function arguments.&lt;/p&gt;
&lt;h3&gt;Getting Started&lt;/h3&gt;
&lt;p&gt;The first thing you need to do is install TypeScript. You can do these one of two ways:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;NPM - &lt;code class=&quot;language-text&quot;&gt;npm install -g typescript&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Yarn - &lt;code class=&quot;language-text&quot;&gt;yarn add global typescript&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To ensure its installed correctly, run this command in your terminal/console to print the package version:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;tsc &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Your output should be something along the lines of &lt;code class=&quot;language-text&quot;&gt;Version 2.7.1&lt;/code&gt;. If you see something similar &lt;code class=&quot;language-text&quot;&gt;command not found: tsc&lt;/code&gt;, then it means TypeScript wasn&apos;t installed. Try installing it again and then restarting your terminal/console.&lt;/p&gt;
&lt;p&gt;Now, create an empty file with the extension &lt;code class=&quot;language-text&quot;&gt;.ts&lt;/code&gt; - that stands for TypeScript. Inside we&apos;ll just create a simple function that represents a Vector2 to ensure our compiler works.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Vector2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Go back to your command line and run &lt;code class=&quot;language-text&quot;&gt;tsc name-of-file.ts&lt;/code&gt; and wait. If all goes well you will see no errors on the command line, but a new file will have been created with the same name as your TypeScript file - except with a &lt;code class=&quot;language-text&quot;&gt;.js&lt;/code&gt; extension. This is the compiled file TypeScript generated with your code. If you look, you&apos;ll notice it&apos;s the same, because we haven&apos;t written any TypeScript yet.&lt;/p&gt;
&lt;h3&gt;Writing Type Annotations&lt;/h3&gt;
&lt;p&gt;The next step is to use TypeScript. We&apos;re going to dive into type annotations, which essentially tell the TypeScript compiler what kind of variable a function should accept.&lt;/p&gt;
&lt;p&gt;In example, it should be easy enough to reason about that the function expects a number value for both x and y. However, since mistakes happen and to err is to human, we want to make sure that the compiler will throw a temper tantrum if either of the arguments are not numbers. This is where the type annotation comes into play.&lt;/p&gt;
&lt;p&gt;To write a type annotation, you simply follow the variable with &lt;code class=&quot;language-text&quot;&gt;:&lt;/code&gt; followed by the type. In our example, the outcome would be:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre class=&quot;language-typescript&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Vector2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Let&apos;s invoke the function with numbers&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Vector2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;49&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, compile the file and you should see no errors. Open your file back up and change one of your numbers to a string.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre class=&quot;language-typescript&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token function&quot;&gt;Vector2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;fail&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you run the compiler now, TypeScript is going to give you an error:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;thing.ts&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5,9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;: error TS2345: Argument of &lt;span class=&quot;token builtin class-name&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&quot;fail&quot;&apos;&lt;/span&gt; is not assignable to parameter of &lt;span class=&quot;token builtin class-name&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;number&apos;&lt;/span&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That&apos;s the beauty of TypeScript. Now you can always ensure that your functions are being called with the correct type. The best part of it is though is that this error is caught at compile time, not run time. Meaning it&apos;s not going to pop up as a surprise to you or your users.&lt;/p&gt;
&lt;h3&gt;Return Type&lt;/h3&gt;
&lt;p&gt;Not only can we set the type of arguments, we can also set the type that gets returned from the function. This is wonderful because anyone interacting with our code, knows without certainty that a function will always return something we expect, without side effects. Writing the return type is the same as before, except the “:” comes after the closing function parenthesis.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre class=&quot;language-typescript&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Vector2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; object &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, how can we test this with the tsc compiler? Let&apos;s create a variable to store the result of the function.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre class=&quot;language-typescript&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; result&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Vector2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Run the compiler and wait for the error. You should have gotten this message (or something like it):&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;thing.ts&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4,5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;: error TS2322: Type &lt;span class=&quot;token string&quot;&gt;&apos;object&apos;&lt;/span&gt; is not assignable to &lt;span class=&quot;token builtin class-name&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;number&apos;&lt;/span&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since you&apos;re smart you&apos;ve already guessed that we need to change the variables type from &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; to &lt;code class=&quot;language-text&quot;&gt;object&lt;/code&gt;. Go ahead and do so, then recompile.&lt;/p&gt;
&lt;p&gt;I am confident that this time, you ran into no issues.&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;There is a lot more to TypeScript than just type annotations, but this was meant to be a &quot;toe dipper&quot; into the library and not an extensive deep dive. Hopefully you have found that TypeScript is not as overwhelming and scary as you may have perceived it to be.&lt;/p&gt;
&lt;p&gt;TypeScript can be a very helpful development tool that can reduce the number of bugs in your code. It is not a magic wand however, and care should always be taken to write clean and maintainable code above all else.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Managing Levels in Phaser 2]]></title><description><![CDATA[When I first began to use Phaser 2, I had trouble wrapping my head around the way that "levels", stages", or "scenes" (like in Unity) were…]]></description><link>https://www.aaronarney.dev/managing-levels-in-phaser/</link><guid isPermaLink="false">https://www.aaronarney.dev/managing-levels-in-phaser/</guid><pubDate>Mon, 10 Sep 2018 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When I first began to use Phaser 2, I had trouble wrapping my head around the way that &quot;levels&quot;, stages&quot;, or &quot;scenes&quot; (like in Unity) were created and accessed. I was looking in the documentation for some kind of Level Manager or methods that would allow me to load said scenes but came up short.&lt;/p&gt;
&lt;p&gt;The reason I was getting tripped up, is due to the &lt;a href=&quot;https://phaser.io/tutorials/making-your-first-phaser-2-game&quot;&gt;official tutorials&lt;/a&gt; way of creating the &lt;code class=&quot;language-text&quot;&gt;Phaser.Game&lt;/code&gt; object.&lt;/p&gt;
&lt;p&gt;When the game configuration object is created, another object is passed into it containing three methods, the &lt;code class=&quot;language-text&quot;&gt;preload&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;create&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;update&lt;/code&gt; methods. These are essentially Phasers &quot;life cycle&quot; methods that tell the engine what should happen during each of these phases.&lt;/p&gt;
&lt;p&gt;In order to use more than one scene, just simply omit this object and instead add your scenes using the game state &lt;code class=&quot;language-text&quot;&gt;add&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;For example, if we want to have a start screen state, main game state, and end scene state, we would add these three states like so:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Some comment&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; game &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Phaser&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Game&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;config&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
game&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gameState&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; gameState&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
game&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;endState&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; endState&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
game&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;titleState&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; titleState&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Each state will have its own respective &lt;code class=&quot;language-text&quot;&gt;preload&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;create&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;update&lt;/code&gt; methods that will get executed automatically. Note that the order in which these states are added &lt;em&gt;does&lt;/em&gt; seem to matter. In order for you to change from the start state to the game state for example, you will need to add the game state &lt;em&gt;first&lt;/em&gt; or else Phaser won&apos;t know that it exists.&lt;/p&gt;
&lt;p&gt;To specify which state will start when the game is loaded, simply call the &lt;code class=&quot;language-text&quot;&gt;state.start&lt;/code&gt; method, passing the state you want to load as the only argument.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;game&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;titleState&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Lastly, when you want to load a new state you simply call the same method. For example if I want the game state to load after the user presses the &lt;code class=&quot;language-text&quot;&gt;spacebar&lt;/code&gt; key on the title screen, I would add this logic into the &lt;code class=&quot;language-text&quot;&gt;titleScene&lt;/code&gt; &lt;code class=&quot;language-text&quot;&gt;update&lt;/code&gt; method.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; game &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;game&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;keyboard&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;isDown&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Phaser&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;keyCode&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;SPACEBAR&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        game&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gameState&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There you have it, a simple explanation of how Phaser 2 handles game states.&lt;/p&gt;</content:encoded></item></channel></rss>