<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: joel.</title>
    <description>The latest articles on DEV Community by joel. (@joelsaupe).</description>
    <link>https://dev.to/joelsaupe</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F91827%2F195ac534-75eb-44e4-99f2-ca00b60328a8.jpg</url>
      <title>DEV Community: joel.</title>
      <link>https://dev.to/joelsaupe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joelsaupe"/>
    <language>en</language>
    <item>
      <title>The Observer Pattern</title>
      <dc:creator>joel.</dc:creator>
      <pubDate>Fri, 22 Mar 2019 15:43:54 +0000</pubDate>
      <link>https://dev.to/joelsaupe/the-observer-pattern-1o6a</link>
      <guid>https://dev.to/joelsaupe/the-observer-pattern-1o6a</guid>
      <description>&lt;p&gt;The Observer Pattern is a design pattern in software engineering that allows communication between multiple places. In web development it can be a way to communicate complex changes to data, and to separate UI from business logic. But before we go into how you can start using this pattern, let's go over some real world examples to make sure we understand what it is.&lt;/p&gt;

&lt;p&gt;Let's say you find out about a new magazine that releases new issues every month. You want to keep up with all the new issues right when they come out. So you subscribe to the magazine and then every month when a new issue is released it shows up at your door. Great, right? Eventually you might get bored of the content, or you get sick of paying for it every month, so you unsubscribe from it. Then, the next time an issue is released it doesn't show up at your door.&lt;/p&gt;

&lt;p&gt;This is a real world example of the observer pattern. Whether you've known it or not, you probably run into this every single day. Things like email newsletters, RSS feeds or subscribing to tweets from your friends are other examples.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Observer Pattern
&lt;/h1&gt;

&lt;p&gt;There are two components to the observer pattern. The &lt;strong&gt;subject&lt;/strong&gt; and the &lt;strong&gt;observer&lt;/strong&gt;. Each subject can have many observers. When an observer subscribers to the subject, it will be informed when changes happen to the subject. The diagram below gives a quick overview of the structure, but we'll dig deeper in just a second.&lt;/p&gt;

&lt;p&gt;&lt;a href="/static/46761db22dd1694ef0b05d0daed5f3b8/6c283/observer-pattern-diagram.png"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwebdev.tips%2Fstatic%2F46761db22dd1694ef0b05d0daed5f3b8%2F6c283%2Fobserver-pattern-diagram.png" alt="Diagram of how changes affect the subject and the changes are passed down to the subscribed observers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Subject
&lt;/h2&gt;

&lt;p&gt;Look at the subject as the thing that's going to be observed. Every subject needs a method to subscribe and a method to unsubscribe. Internally this just adds and removes a function from an array.&lt;/p&gt;

&lt;p&gt;Once subscribing and unsubscribing from the subject has been setup, any time you want to inform subscribers of something, you just have to iterate over each if them and pass them the data they should know about.&lt;/p&gt;

&lt;p&gt;So depending on what the subject is, you might provide a method for others to update some local state, and every time that local state is updated you let all the subscribers know by passing them the updated state.&lt;/p&gt;

&lt;p&gt;Or maybe the subject periodically fetches new data from am API and any time the data changes it lets subscribers know.&lt;/p&gt;

&lt;p&gt;The possibilities are endless as long as you include a way to subscribe and unsubscribe, and make calls to each subscriber whenever something important happens.&lt;/p&gt;

&lt;p&gt;Here's an example of a super basic implementation of an observer subject.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Subject {
  subscribers = []

  subscribe = (functionToSubscribe) =&amp;gt; {
    this.subscribers.push(functionToSubscribe)
  }

  unsubscribe = (functionToUnsubscribe) =&amp;gt; {
    this.subscribers = this.subscribers
      .filter(func =&amp;gt; func !== functionToUnsubscribe)
  }

  action = (data) =&amp;gt; {
    this.subscribers
      .forEach(subscriber =&amp;gt; subscriber(data))
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Observer
&lt;/h2&gt;

&lt;p&gt;Once the subject is all setup and ready to accept subscribers, you just have to pass a function to the subscribe method to get started. This function you pass to subscribe is called an observer. You can have lots of observers watching a single subject. Observers can be added and removed whenever they want. An observer should expect some sort of data to be passed in related to the subject and should perform some sort of action because of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create a new subject to observe.
const subject = new Subject()

// Create a new function and subscribe to the subject.
const observerFunction = (data) =&amp;gt; console.log(data)
subject.subscribe(observerFunction)

// Stuff happens with subject
subject.action('foo')
subject.action('bar')

subject.unsubscribe(observerFunction)

// More stuff happens, but the `observerFunction` will never find out.
subject.action('baz')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Examples
&lt;/h1&gt;

&lt;p&gt;Now that we know all the components of the Observer pattern, let's take a look at a few examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Counter
&lt;/h2&gt;

&lt;p&gt;In this example the subject is a counter. It provides methods to subscribe, unsubscribe, and add to the count. Any time you click the +1 or -1 buttons the count will change.&lt;/p&gt;

&lt;p&gt;There are then three observers that store and display the last count they are informed of. Initially none are observing the count, but you can toggle this by pressing the subscribe/unsubscribe button.&lt;/p&gt;

&lt;p&gt;Play around with it for a while, change subscriptions and change the count to see which ones update and which ones don't and when.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/o48m8lm64y"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Toasts
&lt;/h2&gt;

&lt;p&gt;While the counter is fun to play with and gives a good visual of how the observer pattern works, it's not super practical. Toasts, on the other hand, are a bit more practical and are a much more real-world example of when this pattern is useful.&lt;/p&gt;

&lt;p&gt;If you've never heard the term "toast" before, it's a notification that pops up (you know, like toast out of a toaster) briefly when you're on a site. Usually it's used to provide feedback after the results come back from some previous action you performed.&lt;/p&gt;

&lt;p&gt;So in our example, our subject is toasts, and our observer is the UI that will display new toasts as they are received. So the toasts subject has methods to subscribe and unsubscribe from new toasts, and it also has methods for creating success toasts, warning toasts, and error toasts.&lt;/p&gt;

&lt;p&gt;Each time the success, warning, and error methods are called, the new toasts are sent to the subscribers. When our observer receives a new toast, it briefly renders it.&lt;/p&gt;

&lt;p&gt;By implementing our toasts in this way, we can have one subject and one observer and call the success, warning, and error methods from anywhere in our codebase to display a toast. This is really useful in providing a generic tool the rest of our codebase can use to render a toast while keeping the UI logic out of the business logic that needs to trigger it.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/1q73rv34q"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  That's It!
&lt;/h1&gt;

&lt;p&gt;Pretty simple, right? Now you're an expert on the observer pattern. This article is part of an ongoing series on web development design patterns, so now go check out the others or come back later for more.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>observer</category>
      <category>pattern</category>
    </item>
    <item>
      <title>How to Limit Dependency on 3rd Party Tools and Libraries</title>
      <dc:creator>joel.</dc:creator>
      <pubDate>Wed, 02 Jan 2019 22:27:04 +0000</pubDate>
      <link>https://dev.to/joelsaupe/how-to-limit-dependency-on-3rd-party-tools-and-libraries-59np</link>
      <guid>https://dev.to/joelsaupe/how-to-limit-dependency-on-3rd-party-tools-and-libraries-59np</guid>
      <description>&lt;p&gt;NPM and other package managers have made integrating 3rd party libraries easier than ever.&lt;/p&gt;

&lt;p&gt;Need to build a UI? There's a package for that.&lt;/p&gt;

&lt;p&gt;Need to format time? There's a package for that. &lt;/p&gt;

&lt;p&gt;Need to add authentication? There's a package for that.&lt;/p&gt;

&lt;p&gt;Nowadays there's open sourced code that does practically everything. Just &lt;code&gt;npm install&lt;/code&gt; it and you're on your way. Simple enough, right?&lt;/p&gt;

&lt;p&gt;But what happens when that package you installed stops being maintained or you find out has a security vulnerability? What do you do when a newer, faster, and smaller package is released?&lt;/p&gt;

&lt;p&gt;It's easy to allow 3rd party code to weave and integrate itself into your app, and it's only until you need to replace or remove it that you realize it's become more of a weed than a benefit.&lt;/p&gt;

&lt;p&gt;Refactoring and changing libraries is eventually inevitable in practically any long term project. So what can you do to make the migration process easier?&lt;/p&gt;

&lt;h1&gt;
  
  
  Reducing Dependency
&lt;/h1&gt;

&lt;p&gt;In order to reduce dependency on a 3rd party library, you can create a single entry point where the 3rd party library is accessed. Then, throughout the app, you can access the local entry point instead of accessing the package directly. The day you decide to migrate, all you have to do is migrate the entry point instead of every instance the package was used.&lt;/p&gt;

&lt;p&gt;Wrapping 3rd party libraries in this manner also makes it easier for you to add or modify functionality if something needs to be adjusted.&lt;/p&gt;

&lt;p&gt;If the newer library has a different API, or if you just aren't a fan of the API to begin with, this gives you the opportunity to create your own custom API while reaping all the rewards of using an existing library.&lt;/p&gt;

&lt;h2&gt;
  
  
  An Example
&lt;/h2&gt;

&lt;p&gt;Let's say there's a package we want to add called &lt;code&gt;text-upper&lt;/code&gt; that capitalizes text. &lt;/p&gt;

&lt;p&gt;Instead of accessing the package directly, first  we create an entry point in say &lt;code&gt;utils/caseChanger&lt;/code&gt;. Usually it's better to name it something generic or descriptive instead of just naming the entry point the same as the package. &lt;/p&gt;

&lt;p&gt;Inside the entry point can be something as simple as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;TextUpper&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text-upper&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;TextUpper&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then anytime we need to upper case text we just access the library through our entry point like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;caseChanger&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./utils/caseChanger&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nx"&gt;upperCase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;makeUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later on, we find an even better package called &lt;code&gt;textMcChanger&lt;/code&gt; that not only makes text uppercase, but also makes text lower case too. We &lt;em&gt;definitely&lt;/em&gt; want that.&lt;/p&gt;

&lt;p&gt;So now all we have to do is modify our entry point to use this new package. And even though the new package has a slightly different API for capitalizing text, we only have to modify our entry point to ensure everywhere we're already capitalizing text doesn't also need to be refactored.&lt;/p&gt;

&lt;p&gt;Our migration in the &lt;code&gt;utils/caseChanger&lt;/code&gt; might end up looking something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;betterPackage&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;textMcChanger&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Modify API for backwards compatibility&lt;/span&gt;
&lt;span class="nx"&gt;betterPackage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;makeUpperCase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;betterPackage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Modify API for consistency with old API&lt;/span&gt;
&lt;span class="nx"&gt;betterPackage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;makeLowerCase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;betterPackage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;betterPackage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy enough, right? All we had to do was change this one file and our app instantly reaps the benefits of the new package without requiring all the extra refactor.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Obviously it won't make sense to wrap every package like this. It would be silly to try to add a wrapper for React as your UI library and then later try to transform Vue to work like React. But I guess you could!&lt;/p&gt;

&lt;p&gt;For most other things though, it's a good practice to follow, and you'll thank yourself later once you find the perfect replacement and you don't have to dread the migration.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
