avatarMark Ellis

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

8604

Abstract

<span class="hljs-keyword">const</span> boundFn = fn.<span class="hljs-title function_">bind</span>(<span class="hljs-variable language_">this</span>); <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperty</span>(<span class="hljs-variable language_">this</span>, key, { <span class="hljs-attr">value</span>: boundFn, <span class="hljs-attr">configurable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">writable</span>: <span class="hljs-literal">true</span>, }); <span class="hljs-keyword">return</span> boundFn; }, }; }

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyComponent</span> { <span class="hljs-title function_">constructor</span>(<span class="hljs-params">props</span>) { <span class="hljs-variable language_">this</span>.<span class="hljs-property">props</span> = props; } @autobind <span class="hljs-title function_">handleClick</span>(<span class="hljs-params"></span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">props</span>); } }</pre></div><p id="0013">In this example, the decorator is used to decorate the function so that it automatically binds this when called and returns a new function. This way, after instantiating , you don’t need to manually bind this when calling the function.<code>autobindhandleClickMyComponentthis.handleClick()</code></p><h1 id="3ffe">Logging</h1><p id="7a28">Decorators can be used to record logs, including printing information such as function calls, function execution times, etc.</p><div id="1a70"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">log</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">Function <span class="hljs-subst">${name}</span> called with <span class="hljs-subst">${args}</span></span>); <span class="hljs-keyword">const</span> start = performance.<span class="hljs-title function_">now</span>(); <span class="hljs-keyword">const</span> result = originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); <span class="hljs-keyword">const</span> duration = performance.<span class="hljs-title function_">now</span>() - start; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">Function <span class="hljs-subst">${name}</span> completed in <span class="hljs-subst">${duration}</span>ms</span>); <span class="hljs-keyword">return</span> result; }; <span class="hljs-keyword">return</span> descriptor } <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyClass</span> { @log <span class="hljs-title function_">myMethod</span>(<span class="hljs-params">arg1, arg2</span>) { <span class="hljs-keyword">return</span> arg1 + arg2; } }

<span class="hljs-keyword">const</span> obj = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyClass</span>(); obj.<span class="hljs-title function_">myMethod</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); <span class="hljs-comment">// Output: </span> <span class="hljs-comment">// Function myMethod called with 1,2</span> <span class="hljs-comment">// Function myMethod completed in 0.013614237010165215ms</span></pre></div><h1 id="aff7">Authentication authentication</h1><p id="bbff">Decorators can also be used to check a user’s authentication status and permissions to prevent unauthorized users from accessing sensitive data or performing actions on a regular basis.</p><div id="be1a"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">authorization</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">...args</span>) { <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">isAuthenticated</span>()) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Access denied! Not authenticated'</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">hasAccessTo</span>(name)) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">Access denied! User does not have permission to <span class="hljs-subst">${name}</span></span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); }; <span class="hljs-keyword">return</span> descriptor; }

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyApi</span> { <span class="hljs-title function_">isAuthenticated</span>(<span class="hljs-params"></span>) { <span class="hljs-comment">// perform authentication check</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; } <span class="hljs-title function_">hasAccessTo</span>(<span class="hljs-params">endpoint</span>) { <span class="hljs-comment">// perform authorization check</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; } @authorization <span class="hljs-title function_">getUsers</span>(<span class="hljs-params"></span>) { <span class="hljs-comment">// return users data</span> } @authorization <span class="hljs-title function_">deleteUser</span>(<span class="hljs-params">id</span>) { <span class="hljs-comment">// delete user with id</span> } }</pre></div><h1 id="646e">Cache</h1><p id="3465">Decorators can also be used to cache the execution results of functions to avoid double evaluation.</p><div id="5257"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">memoize</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>; <span class="hljs-keyword">const</span> cache = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>();

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { <span class="hljs-keyword">const</span> cacheKey = args.<span class="hljs-title function_">toString</span>(); <span class="hljs-keyword">if</span> (cache.<span class="hljs-title function_">has</span>(cacheKey)) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">cache hit: <span class="hljs-subst">${cacheKey}</span></span>); <span class="hljs-keyword">return</span> cache.<span class="hljs-title function_">get</span>(cacheKey); } <span class="hljs-keyword">const</span> result = originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">cache miss: <span class="hljs-subst">${cacheKey}</span></span>); cache.<span class="hljs-title function_">set</span>(cacheKey, result); <span class="hljs-keyword">return</span> result; }; <span class="hljs-keyword">return</span> descriptor; }

<span class="hljs-keyword">class

Options

</span> <span class="hljs-title class_">MyMath</span> { @memoize <span class="hljs-title function_">calculate</span>(<span class="hljs-params">num</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'calculate called'</span>); <span class="hljs-keyword">return</span> num * <span class="hljs-number">2</span>; } }

<span class="hljs-keyword">const</span> math = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyMath</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-title function_">calculate</span>(<span class="hljs-number">10</span>)); <span class="hljs-comment">// Output: </span> <span class="hljs-comment">// calculate called</span> <span class="hljs-comment">// cache miss: 10</span> <span class="hljs-comment">// 20</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-title function_">calculate</span>(<span class="hljs-number">10</span>)); <span class="hljs-comment">// Output: </span> <span class="hljs-comment">// cache hit: 10</span> <span class="hljs-comment">// 20</span></pre></div><h1 id="57d9">Aspect-oriented programming</h1><p id="0d61">Decorators can be used to implement facet-oriented programming, that is, to add functionality at runtime without modifying the original code.</p><div id="bc4d"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">validate</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { <span class="hljs-keyword">const</span> isValid = args.<span class="hljs-title function_">every</span>(<span class="hljs-function"><span class="hljs-params">arg</span> =></span> <span class="hljs-keyword">typeof</span> arg === <span class="hljs-string">'string'</span> && arg.<span class="hljs-property">length</span> > <span class="hljs-number">0</span>); <span class="hljs-keyword">if</span> (!isValid) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Invalid arguments'</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); }; <span class="hljs-keyword">return</span> descriptor; }

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyForm</span> { @validate <span class="hljs-title function_">submit</span>(<span class="hljs-params">name, email, message</span>) { <span class="hljs-comment">// submit the form</span> } }

<span class="hljs-keyword">const</span> form = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyForm</span>(); form.<span class="hljs-title function_">submit</span>(<span class="hljs-string">''</span>, <span class="hljs-string">'[email protected]'</span>, <span class="hljs-string">'Hello world'</span>); <span class="hljs-comment">// Output: Invalid arguments</span></pre></div><h1 id="c8da">Reversible decorators</h1><p id="5322">Decorators can also be applied in reversible scenes, for example, you can add a reversible decorator to modify function behavior.</p><div id="b868"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">reverse</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { args.<span class="hljs-title function_">reverse</span>(); <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); }; <span class="hljs-keyword">return</span> descriptor; } <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyMath</span> { @reverse <span class="hljs-title function_">calculate</span>(<span class="hljs-params">num1, num2</span>) { <span class="hljs-keyword">return</span> num1 + num2; } }

<span class="hljs-keyword">const</span> math = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyMath</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-title function_">calculate</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)); <span class="hljs-comment">// Output: 3</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-property">calculate</span>.<span class="hljs-title function_">reversed</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)); <span class="hljs-comment">// Output: 3</span></pre></div><h1 id="2ddc">Automatic type checking</h1><p id="037c">Decorators can be applied to automatic type checking, for example, you can add a decorator to ensure that the type of a function parameter is correct.</p><div id="a63a"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">checkType</span>(<span class="hljs-params">expectedType</span>) { <span class="hljs-keyword">return</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) {
      <span class="hljs-keyword">const</span> invalidArgs = args.<span class="hljs-title function_">filter</span>(<span class="hljs-function"><span class="hljs-params">arg</span> =&gt;</span> <span class="hljs-keyword">typeof</span> arg !== expectedType);
      <span class="hljs-keyword">if</span> (invalidArgs.<span class="hljs-property">length</span> &gt; <span class="hljs-number">0</span>) {
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`Invalid arguments: <span class="hljs-subst">${invalidArgs}</span>`</span>);
        <span class="hljs-keyword">return</span>;
      }
      <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args);
    };
    <span class="hljs-keyword">return</span> descriptor;
  }

}

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyMath</span> { @<span class="hljs-title function_">checkType</span>(<span class="hljs-string">'number'</span>) <span class="hljs-title function_">add</span>(<span class="hljs-params">num1, num2</span>) { <span class="hljs-keyword">return</span> num1 + num2; } }

<span class="hljs-keyword">const</span> math = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyMath</span>(); math.<span class="hljs-title function_">add</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'2'</span>); <span class="hljs-comment">// Output: Invalid arguments: 2</span></pre></div><p id="dd86"><i>More content at <a href="https://plainenglish.io/"><b>PlainEnglish.io</b></a>.</i></p><p id="5cf7"><i>Sign up for our <a href="http://newsletter.plainenglish.io/"><b>free weekly newsletter</b></a>. Follow us on <a href="https://twitter.com/inPlainEngHQ"><b>Twitter</b></a></i>, <a href="https://www.linkedin.com/company/inplainenglish/"><b><i>LinkedIn</i></b></a><i>, <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><b>YouTube</b></a>, and <a href="https://discord.gg/GtDtUAvyhW"><b>Discord</b></a><b>.</b></i></p><p id="7057"><b><i>Interested in scaling your software startup</i></b><i>? Check out <a href="https://circuit.ooo?utm=publication-post-cta"><b>Circuit</b></a>.</i></p></article></body>

Can You Really Supercharge Email? A Review of Newton Email

Can it draw me away from the brilliant Spark?

Image courtesy of author

Recently, I told you that I’d found the best email client. And it seems lots of other people like Spark, too.

However, as is customary (and wonderful), several readers pointed me in the direction of alternatives. Loads, in fact. Some, I’d tried (hello, AirMail), but a few piqued my interest.

One of them was Newton.

Newton’s website suggests that they have ‘supercharged’ email. I have no idea what that means, but it was enough for me to get Newton installed and pitched against my beloved Spark.

Here’s what I discovered.

The details

Newton is a cross-platform email client which works on macOS, Windows, iOS, Android and even Linux. There’s an M1 native version for new Mac owners, and a subscription model of $49.99 per year, which can be trialled for free — without restriction — for two weeks.

You can grab it here.

What I like about Newton

There are lots of modern takes on email management. It’s something that has always fascinated me, because email is such a simple premise and one of — if not the — oldest forms of digital communication.

Newton goes one step further and suggests that it is an email app “with space-age features for modern-day business communication”.

I have no idea what that means, either, but the net result is an email client which is about as simple as they come. The main window you’re presented with (I did all of my testing on the macOS version) provides you with a list of the emails contained within whichever folder you’re viewing, a search bar and controls for creating a new message and filtering the list.

There’s no preview pane or sticky folder list (I’ll get to that later).

The compose window is a similar story. Fire up a new email message or click ‘reply’, and you’re presented with a window that has shed loads of white space and beautifully designed, minimalist controls. It’s a joy to browse your inbox and write emails in Newton.

The filter buttons deserve special mention. You’re given three options — starred, unread and ‘recap’. I found myself using the unread filter more than I ever have in any other email client. It’s just so clickable, and the simplicity with which emails are displayed in Newton makes this just about the easiest way I’ve found to keep on top of my unread email.

Recap is an interesting idea too. It’s designed to surface emails and conversations you may have missed. Alas, I can’t get it to display anything — but that might be because I’m so obsessed with keeping my email game in order.

So, email triage is what Newton excels at. I also love the simple, super-minimalist design.

Unfortunately, there are a few problems.

What I don’t like about Newton

As much as I like Newton’s no-nonsense approach to design and its user interface, the developers seem to have got a few things inexplicably wrong.

I’ll start with the left-hand pane which displays your email accounts and folders. Try as I might, I cannot find a way to make it sticky; it is obsessed with hiding until called upon. Which is something I do constantly.

To unearth it from its slumber, you either motion your mouse pointer to the far left of the screen or click the menu control at the top-left.

I’m all for keeping apps uncluttered, but I’d like some customisability please, Newton. Maybe it’s the time I’ve spent with email clients that do have a persistent folder view (i.e. all of them), but it just feels slightly unnerving not having it there.

Signatures are a problem too. I can’t find a way in Newton to add images to my signatures. Have I missed a setting somewhere? Let me know in the comments, please.

Spark, Outlook and Apple Mail have all got me rather used to swipe actions for email. They’re incredibly useful not just on iOS, but on the Mac, too, and I was dismayed to find that Newton doesn’t offer any form of swipe-ability on macOS. This is tempered slightly by the ease with which you can triage emails via the simple UI, but it’d be even faster if we could swipe to archive, delete or read/unread.

Again — have I missed a setting somewhere? It feels like this stuff is so obvious.

But there are two annoyances in Newton which make it a no-go for me, and they’re the simplest of things.

Firstly, there doesn’t appear to be any form of built-in spell checking on the macOS version. I really, really dislike this. I’m a fast typist, but mainly because I have a habit of mashing the keys; over the years, I’ve become accustomed to software tidying up the trail of destruction left behind by my fingers.

Newton, therefore, leaves me with the distinct possibility of sending error-strewn emails if I’m in a hurry. Not good enough. And yes, I know I should get better at typing. I just can’t be arsed.

Secondly, and for reasons I cannot fathom, Newton places the controls for replying and forwarding emails at the bottom of the email you’re viewing. If the email in question is a long discussion with multiple replies, you have to scroll forever just to reply to the damn thing. This is silly. Every other email client I’ve used has the controls at the top of the screen (and, in some cases, at the bottom, too).

So, can you supercharge email?!

No. Not with Newton.

The Verge described Newton (or CloudMagic, as it was originally known) as “it isn’t iA Writer for email, but it’s damn close”. I think that’s such a smart comparison.

iA Writer offers a stripped-down writing experience and it is consequently brilliant at helping you get words down onto a page as quickly as possible. Newton is similar; it helps you bash through your inbox like an email superhero, and there’s very little to get in the way if you simply want to deal with a bulging list of messages as quickly as possible.

But it doesn’t quite cut it for me. The problems I’ve listed above seem out of place in what is an otherwise considerately designed app. The reply controls, lack of spell checking and irritatingly shy folder list felt like speed limiters on an immensely potent car.

Newton is so close. I can see what they’re trying to do. But for me, Spark reigns king.

Before you go

Join my behind-the-scenes mailing list

Originally published at https://markellisreviews.com on July 9, 2021.

Technology
Email
Productivity
Apps
Software
Recommended from ReadMedium