Blog Spot!


Basic Pipeline / Middleware Pattern in JavaScript

Ever wondered how the middlewares in popular web frameworks, e.g. Express or Koa, work?

Article Basic Middleware Pattern in JavaScript
-- Published on October 4, 2020

Implementation

function Pipeline(...middlewares) {
  const stack = middlewares

  const push = (...middlewares) => {
    stack.push(...middlewares)
  }

  const execute = async (context) => {
    let prevIndex = -1

    const runner = async (index) => {
      if (index === prevIndex) {
        throw new Error('next() called multiple times')
      }

      prevIndex = index

      const middleware = stack[index]

      if (middleware) {
        await middleware(context, () => {
          return runner(index + 1)
        })
      }
    }

    await runner(0)
  }

  return { push, execute }
}

Usage

// create a middleware pipeline
const pipeline = Pipeline(
  // with an initial middleware
  (ctx, next) => {
    console.log(ctx)
    next()
  }
)

// add some more middlewares
pipeline.push(
  (ctx, next) => {
    ctx.value = ctx.value + 21
    next()
  },
  (ctx, next) => {
    ctx.value = ctx.value * 2
    next()
  }
)

// add the terminating middleware
pipeline.push((ctx, next) => {
  console.log(ctx)
  // not calling `next()`
})

// add another one for fun ¯\_(ツ)_/¯
pipeline.push((ctx, next) => {
  console.log('this will not be logged')
})

// execute the pipeline with initial value of `ctx`
pipeline.execute({ value: 0 })

Added on 27.Oct.2021
Tags: pipeline middleware js javascript pattern

Web Galleries

Galleries:


Article top-10 ..picked from the list:

  1. Grid Gallery. A lightweight, responsive photo grid gallery with lightbox integrated that allows the user to switch between images in a popup by clicking on any thumbnail.
  2. Light Gallery. The Vanilla JavaScript Version of the lightGallery jQuery plugin. Light Gallery is a modular, responsive, touch-enabled, dependency-free JavaScript lightbox gallery library that enables you to present various types of media (images, videos, etc) in a fullscreen, scalable, navigatable, shareable, downloadable, and CSS3 animated gallery popup. And the beast off all it's modular.

Added on 17.Oct.2021
Tags: css gallery js lightbox

Boosting up PHP-project with cache

A really good article for PHP caching & structuring.

What are the approaches?

There are many approaches to caching. You can check the list of PHP-compatible tools on the php-cache page. However, the most common ones are:

Added on 04.Oct.2021
Tags: php cache apcu redis simple-cache psr

KODI -- All related topics

Repositories

Added on 22.Jul.2021
Tags: kodi libreelec rpi raspberry pi tv retro gaming

Use `.hidden` to hide directories in Linux without using '.' in front

Added on 14.May.2021
Tags: linux hidden hide config

Search


PHP Libraries


Carbon lib / docs
Idiorm lib / docs
Image Workshop lib / docs
lorenzos/Minixed lib / docs
Parsedown lib / docs
PHP Paginator lib / docs
PHP Redis lib / docs
QrCode lib / docs
Requests lib / docs
Slim lib / docs
Spyc lib / docs
TWIG lib / docs
Upload lib / docs
Validation lib / docs
Zebra Image lib / docs

JS Libraries


AJV lib / docs
BackboneJS lib / docs
Bootstrap Notify lib / docs
C3.js lib / docs
ChartJS lib / docs
FastMD5 lib / docs
HighlightJS lib / docs
jQuery-Storage lib / docs
JS-Cookie lib / docs
Leaflet JS lib / docs
LowDB lib / docs
Marked lib / docs
NeedlyJS lib / docs
ParcelJS lib / docs
RequireJS lib / docs
Swig lib / docs
Toastr lib / docs
Underscore lib / docs
ValidateJS lib / docs
top