惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
C
Cisco Blogs
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
V2EX - 技术
V2EX - 技术
H
Heimdal Security Blog
S
Security Affairs
L
Lohrmann on Cybersecurity
Hacker News - Newest:
Hacker News - Newest: "LLM"
Simon Willison's Weblog
Simon Willison's Weblog
WordPress大学
WordPress大学
小众软件
小众软件
Security Latest
Security Latest
AWS News Blog
AWS News Blog
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
F
Full Disclosure
S
Schneier on Security
L
LangChain Blog
MyScale Blog
MyScale Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
Google Online Security Blog
Google Online Security Blog
Scott Helme
Scott Helme
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
A
Arctic Wolf
Martin Fowler
Martin Fowler
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
The Register - Security
The Register - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
Latest news
Latest news
F
Fortinet All Blogs
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
Hacker News: Ask HN
Hacker News: Ask HN

Dmitri Pavlutin Blog

Pure Functions in JavaScript: A Beginner's Guide Record Type in TypeScript: A Quick Intro How to Write Comments in React: The Good, the Bad and the Ugly 4 Ways to Create an Enum in JavaScript React forwardRef(): How to Pass Refs to Child Components TypeScript Function Types: A Beginner's Guide How to Use v-model to Access Input Values in Vue Mastering Vue refs: From Zero to Hero Environment Variables in JavaScript: process.env 5 Must-Know Differences Between ref() and reactive() in Vue How to Destructure Props in Vue (Composition API) Triangulation in Test-Driven Development How to Use nextTick() in Vue Programming to Interface Vs to Implementation A Smarter JavaScript Mapper: array.flatMap() Array Grouping in JavaScript: Object.groupBy() How to Access ES Module Metadata using import.meta JSON Modules in JavaScript How to Trim Strings in JavaScript TypeScript Function Overloading How to Debounce and Throttle Callbacks in Vue How to Show/Hide Elements in Vue Sparse vs Dense Arrays in JavaScript How to Fill an Array with Initial Values in JavaScript Covariance and Contravariance in TypeScript What are Higher-Order Functions in JavaScript? How to Use TypeScript with React Components Index Signatures in TypeScript How to Use React useReducer() Hook unknown vs any in TypeScript A Guide to React Context and useContext() Hook How to Use Promise.any() 2 Ways to Remove a Property from an Object in JavaScript 'return await promise' vs 'return promise' in JavaScript How to Use Promise.allSettled() How to Use fetch() with JSON JavaScript Promises: then(f,f) vs then(f).catch(f) What is a Promise in JavaScript? How to Use Promise.all() A Simple Guide to Component Props in React Don't Stop Me Now: How to Use React useTransition() hook A Simple Explanation of JavaScript Variables: const, let, var ES Modules Dynamic Import How to Memoize with React.useMemo() How to Cleanup Async Effects in React Why Math.max() Without Arguments Returns -Infinity How to Debounce and Throttle Callbacks in React Don't Confuse Function Expressions and Function Declarations in JavaScript How to Use ES Modules in Node.js Solving a Mystery Behavior of parseInt() in JavaScript How to Use Array Reduce Method in JavaScript 3 Ways to Merge Arrays in JavaScript A Guide to Jotai: the Minimalist React State Management Library The Difference Between Values and References in JavaScript How to Implement a Queue in JavaScript A Helpful Algorithm to Determine "this" value in JavaScript React useRef() Hook Explained in 3 Steps 7 Interview Questions on "this" keyword in JavaScript. Can You Answer Them? How to Greatly Enhance fetch() with the Decorator Pattern 7 Interview Questions on JavaScript Closures. Can You Answer Them? What's a Method in JavaScript? array.sort() Does Not Simply Sort Numbers in JavaScript How to Solve the Infinite Loop of React.useEffect() The New Array Method You'll Enjoy: array.at(index) What's the Difference between DOM Node and Element? Why Promises Are Faster Than setTimeout()? Everything About Callback Functions in JavaScript How React Updates State 5 Mistakes to Avoid When Using React Hooks 5 Best Practices to Write Quality JavaScript Variables Type checking in JavaScript: typeof and instanceof operators 3 Ways to Check if a Variable is Defined in JavaScript React Forms Tutorial: Access Input Values, Validate, Submit Forms Prototypal Inheritance in JavaScript How to Timeout a fetch() Request How to Learn JavaScript If You're a Beginner A Simple Explanation of React.useEffect() A Simple Explanation of JavaScript Iterators How to Use React Controlled Inputs Everything about null in JavaScript How to Use Fetch with async/await Getting Started with Arrow Functions in JavaScript An Interesting Explanation of async/await in JavaScript Is it Safe to Compare JavaScript Strings? How to Access Object's Keys, Values, and Entries in JavaScript What Actually is a String in JavaScript? 3 Ways to Shallow Clone Objects in JavaScript (w/ bonuses) Checking if an Array Contains a Value in JavaScript JavaScript Event Delegation: A Beginner's Guide How to Parse URL in JavaScript: hostname, pathname, query, hash 3 Ways to Detect an Array in JavaScript How to Get the Screen, Window, and Web Page Sizes in JavaScript 3 Ways to Check If an Object Has a Property/Key in JavaScript How to Compare Objects in JavaScript Object.is() vs Strict Equality Operator in JavaScript Own and Inherited Properties in JavaScript 5 Differences Between Arrow and Regular Functions How to Use Object Destructuring in JavaScript Your Guide to React.useCallback() 5 JavaScript Scope Gotchas
Front-end Architecture: Stable and Volatile Dependencies
Dmitri Pavlutin · 2020-08-25 · via Dmitri Pavlutin Blog

Many components (of libraries like React, Vue, Angular) use the functionality of utility libraries.

Let's consider a React component that displays the number of words in the provided text:


import words from 'lodash.words';

function CountWords({ text }: { text: string }): JSX.Element {

const count = words(text).length;

return (

<div className="words-count">{count}</div>

);

}


The component CountWords uses the library lodash.words to count the number of words in the string text.

CountWords component has a dependency on lodash.words library.

The components using dependencies benefit from the code reuse: you simply import the necessary library and use it.

However, your component might need diverse dependency implementations for various environments (client-side, server-side, testing environment). In such a case importing directly a dependency is a risk.

Designing correctly the dependencies is an important skill to architect Front-end applications. The first step to creating a good design is to identify the stable and volatile dependencies and treat them accordingly. In this post, you're going to find out how.

Table of Contents

  • 1. Stable dependencies
  • 2. Volatile dependencies
    • 2.1 A bad design
    • 2.2 A better design
    • 2.3 Be aware of added complexity
    • 2.4 Injection mechanisms
  • 3. Summary

1. Stable dependencies

Let's recall the example component CountWords from the introduction:


import words from 'lodash.words';

function CountWords({ text }: { text: string }): JSX.Element {

const count = words(text).length;

return (

<div className="words-count">{count}</div>

);

}


The component CountWords is going to use the same library lodash.words no matter the environment: be it on client-side, be it running on the server-side (if you implement Server-Side Rendering), or even when running unit tests.

At the same time, lodash.words is a simple utility function:


const arrayOfWords = words(string);


The signature of words function won't change much in the future.

Because the dependent component always uses one dependency implementation, and the dependency won't change in the future — such dependency is considered stable.

Stable dependency

Examples of stable dependencies are the utility libraries like lodash, ramda.

Moreover, the JavaScript language itself provides:

  • Utility functions, like Object.keys(), Array.from()
  • Methods on primitives and objects, like string.slice(), array.includes(), object.toString()

All the built-in functions that the language provides are also considered stable dependencies. You can use them safely and depend directly upon them.

However, aside from stable ones, some dependencies may change under certain circumstances. Such volatile dependencies have to be segregated from stable ones and designed differently.

Let's see what volatile dependencies are in the next section.

2. Volatile dependencies

Consider a Front-end application that supports also Server-Side Rendering. Your task is to implement a user login page.

A login form is displayed when the user first loads the login page. If the user introduces the correct username and password in the login form and hits submit, then you create a cookie loggedIn with value 1.

As long as the user is logged in (the cookie loggedIn is set and has value 1) display a message 'You are logged in'. Otherwise, just display the login form.

Having the app requirements setup, let's discuss potential ways of implementation.

To determine whether loggedIn cookie is set-up, you have to consider the environment where the application runs. On the client-side, you can access the cookie from document.cookie property, while on the server-side you'd need to read the HTTP request header cookie.

The cookie management is a volatile dependency because the component chooses the concrete implementation by environment: client-side or server-side.

Generally, the dependency is volatile if any of the following criteria are met:

  • The dependency requires runtime environment setup for the application (network access, web services, file system)
  • The dependency is in development
  • The dependency has non-deterministic behavior (random number generator, access of current date, etc).

An example of volatile dependency is, as mentioned, the cookie management library which has different implementations on client and server-side. Another example of volatile dependency is the library to access a database or a fetching library that accesses the network.

Even a dependency that is still in development or one you can probably change for an alternative solution in the future can also be volatile.

A good rule of thumb to distinguish a volatile dependency is to analyze how easy you can unit test the component that depends on it. If the dependency requires a lot of setup ceremony and mocks to be tested (e.g. a fetching library requires mocking network requests), then most likely it's a volatile.

2.1 A bad design

Your component should not directly import volatile dependencies.

But let's deliberately make this mistake:


import { cookieClient } from './libs/cookie-client';

import { cookieServer } from './libs/cookie-server';

import LoginForm from 'Components/LoginForm';

export function Page(): JSX.Element {

const cookieManagement = typeof window === 'undefined'

? cookieServer : cookieClient;

if (cookieManagement.get('loggedIn') === '1') {

return <div>You are logged in</div>;

} else {

return <LoginForm />

}

}


Page component depends directly on both cookieClient and cookieServer libraries. The component selects the necessary implementation by checking whether the window global variable is available (meaning the app runs in a browser) or not (meaning the app runs on server).

Volatile Dependency Bad Design

Why implementing the cookie management volatile dependency such way is a problem? Let's see:

  • Tight coupling to all dependency implementations. The component Page depends directly on cookieClient and cookieServer implementations
  • Dependency on the environment. Every time you need the cookie management library, you have to invoke the expression typeof window === 'undefined' to determine whether the app runs on the client or server-side, and choose according to cookie management implementation.
  • Unnecessary code. The client-side bundle is going to include the cookieServer library which isn't used on the client-side. And vice-versa for server-side.
  • Difficult testing. The unit tests of Page component would require lots of mockups like setting window variable and mockup document.cookie

Is there a better design? Let's find out!

2.2 A better design

Making a better design to handle volatile dependencies requires a bit more work, but the outcome worth it.

The idea consists in applying the Dependency Inversion Principle and decouple Page component from cookieClient and cookieServer. Instead, let's make the Page component depend on an abstract interface Cookie.

First, let's define an interface Cookie that describes what methods a cookie library should implement:


// Cookie.ts

export interface Cookie {

get(name: string): string | null;

set(name: string, value: string): void;

}


Now let's define the React context that's going to hold a specific implementation of the cookie management library:


// CookieContext.tsx

import { createContext } from 'react';

import { Cookie } from './Cookie';

export const CookieContext = createContext<Cookie>(null);


CookieContext injects the dependency into the Page component:


// Page.tsx

import { useContext } from 'react';

import { Cookie } from './Cookie';

import { CookieContext } from './CookieContext';

import { LoginForm } from './LoginForm';

export function Page(): JSX.Element {

const cookie: Cookie = useContext(cookieContext);

if (cookie.get('loggedIn') === '1') {

return <div>You are logged in</div>;

} else {

return <LoginForm />

}

}


The only thing that Page component knows about is the Cookie interface, and nothing more. The component is decoupled from the implementation details of how cookies are accessed.

Page component doesn't care about what concrete implementation it gets. The only requirement is that the injected dependency to conform to the Cookie interface.

Volatile Dependency Better Design

The necessary implementation of the cookie management library is setup by the bootstrap scripts on both client and server sides.

Here's how you would compose the cookie management dependency on client-side:


// index.client.tsx

import ReactDOM from 'react-dom';

import { Page } from './Page';

import { CookieContext } from './CookieContext';

import { cookieClient } from './libs/cookie-client';

ReactDOM.hydrate(

<CookieContext.Provider value={cookieClient}>

<Page />

</CookieContext.Provider>,

document.getElementById('root')

);


and on the server-side:


// index.server.tsx

import express from 'express';

import { renderToString } from 'react-dom/server';

import { Page } from './Page';

import { CookieContext } from './CookieContext';

import { cookieServer } from './libs/cookie-server';

const app = express();

app.get('/', (req, res) => {

const content = renderToString(

<CookieContext.Provider value={cookieServer}>

<Page />

</CookieContext.Provider>

);

res.send(`

<html>

<head><script src="./bundle.js"></script></head>

<body>

<div id="root">

${content}

</div>

</body>

</html>

`);

})

app.listen(env.PORT ?? 3000);


The concrete implementation of a volatile dependency is composed close to the bootstrap (or main) scripts of the application. This place is named Composition Root.

The benefits of good design of volatile dependencies:

  • Loose coupling. The component Page doesn't depend on all possible implementations of the dependency
  • Free of implementation details and environment. The component doesn't care whether it runs on the client or server-side
  • Dependency upon stable abstraction. The component depends only on an abstract interface Cookie
  • Easy testing. The component knows only about the interface, you can easily test such a component by injecting dummy implementations using context.

2.3 Be aware of added complexity

The improved design requires more moving parts: a new interface that describes the dependency and a way to inject the dependency.

All these moving parts add complexity. So you should carefully consider whether the benefits of this design outweigh the added complexity.

2.4 Injection mechanisms

While in the the previous example React context was injecting the concerete implementation — React context is only one of the possible options.

The same way you can inject implementations using props:


// Page.tsx

import { Cookie } from './Cookie';

import { LoginForm } from './LoginForm';

interface PageProps {

cookie: Cookie;

}

export function Page({ cookie }: PageProps): JSX.Element {

if (cookie.get('loggedIn') === '1') {

return <div>You are logged in</div>;

} else {

return <LoginForm />

}

}


However, if the component using volatile dependencies is deep inside the components hierarchy (i.e. is far from Composition Root), you might end up in props drilling. React context, while requiring more setup (context object, useContext() hook), doesn't have this problem.

3. Summary

The components of your Front-end application can use a multitude of libraries.

Some of these libraries, like lodash or even the built-in JavaScript's utilities are stable dependencies and your components are free to depend directly on them.

However, sometimes the component requires dependencies that may change either during runtime, either depending on the environment, either other reason to change. These dependencies fall in the category of volatile.

Good design makes the components not depend directly upon volatile dependency, but rather depend on a stable interface (by using the Dependency Inversion Principle) that describes the dependency, and then allows a dependency injection mechanism (like React context) to supply the concrete dependency implementation.

What's your opinion: does good design of volatile dependencies worth added complexity?