All Projects → yoshuawuyts → nanostack

yoshuawuyts / nanostack

Licence: MIT license
Small middleware stack library

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to nanostack

Leetcode
High-quality LeetCode solutions
Stars: ✭ 178 (+356.41%)
Mutual labels:  data-structure, stack
Algorithm
Algorithm is a library of tools that is used to create intelligent applications.
Stars: ✭ 787 (+1917.95%)
Mutual labels:  data-structure, stack
C Macro Collections
Easy to use, header only, macro generated, generic and type-safe Data Structures in C
Stars: ✭ 192 (+392.31%)
Mutual labels:  data-structure, stack
Gods
GoDS (Go Data Structures). Containers (Sets, Lists, Stacks, Maps, Trees), Sets (HashSet, TreeSet, LinkedHashSet), Lists (ArrayList, SinglyLinkedList, DoublyLinkedList), Stacks (LinkedListStack, ArrayStack), Maps (HashMap, TreeMap, HashBidiMap, TreeBidiMap, LinkedHashMap), Trees (RedBlackTree, AVLTree, BTree, BinaryHeap), Comparators, Iterators, …
Stars: ✭ 10,883 (+27805.13%)
Mutual labels:  data-structure, stack
Staticvec
Implements a fixed-capacity stack-allocated Vec alternative backed by an array, using const generics.
Stars: ✭ 236 (+505.13%)
Mutual labels:  data-structure, stack
CPTH
🌟 Competitive Programming Template Headers | With documentation, CI tests and Codecov
Stars: ✭ 23 (-41.03%)
Mutual labels:  data-structure
Vue
💻 Vue - Boilerplate Front : Vue 3, Vuex, Vuetify 3, JWT, Jest (Beta)
Stars: ✭ 12 (-69.23%)
Mutual labels:  stack
ctl
My variant of the C Template Library
Stars: ✭ 105 (+169.23%)
Mutual labels:  stack
NALib
General purpose C sourcecode collection
Stars: ✭ 16 (-58.97%)
Mutual labels:  stack
quetie
🎀 Just the cutest and tiniest queue/deque implementation!
Stars: ✭ 111 (+184.62%)
Mutual labels:  stack
swarm-router
Scalable stateless «zero config» service-name ingress for docker swarm mode with a fresh more secure approach
Stars: ✭ 58 (+48.72%)
Mutual labels:  stack
needle
📌📚 An extensive standalone data structure library for JavaScript.
Stars: ✭ 25 (-35.9%)
Mutual labels:  stack
fortran-octree
A Fortran octree implementation
Stars: ✭ 17 (-56.41%)
Mutual labels:  data-structure
algorithm
得到每周一算法
Stars: ✭ 44 (+12.82%)
Mutual labels:  data-structure
charm
A [ functional stack ] based language.
Stars: ✭ 26 (-33.33%)
Mutual labels:  stack
TclForth
Multi-platform desktop Forth based on Tcl/Tk
Stars: ✭ 55 (+41.03%)
Mutual labels:  stack
trie
Trie (a.k.a. prefix tree) C# implementation. Has constant-time string prefix lookup.
Stars: ✭ 84 (+115.38%)
Mutual labels:  data-structure
docker-lemp
A single container LEMP complete fullstack with latest release of PHP7.4.33, 8.0.26 & 8.1.13/8.2RC and MySQL, nginx, PostgreSQL, phalcon, swoole, mailcatcher, beanstalkd, elasticsearch, memcached, redis, adminer and all you ever need; on top alpine3.15
Stars: ✭ 106 (+171.79%)
Mutual labels:  stack
uC-USBH
Efficient USB host stack for embedded systems equipped with a USB host or OTG controller. Includes many class drivers such as MSC, HID, CDC-ACM, and USB2Ser.
Stars: ✭ 44 (+12.82%)
Mutual labels:  stack
js-symbol-tree
Turn any collection of objects into its own efficient tree or linked list using Symbol
Stars: ✭ 86 (+120.51%)
Mutual labels:  data-structure

nanostack stability

npm version build status test coverage downloads js-standard-style

Small middleware stack library. Analogous to co but without relying on fancy language features. Weighs ~0.4kb gzipped.

Usage

var nanostack = require('nanostack')
var stack = nanostack()

stack.push(function timeElapsed (ctx, next) {
  var start = Date.now()

  next(null, function (err, ctx, next) {
    if (err) return next(err)
    var now = Date.now()
    var elapsed = start - now
    console.log('time elapsed: ' + elapsed + 'ms')
    next()
  })
})

var ctx = {}
stack.walk(ctx, function (err, data, next) {
  if (err) throw err
})

How does this work?

A stack is a "last-in, first-out" type structure. The last thing that's added is also the first thing that's taken off when you "unwind the stack".

In nanostack we push middleware onto the stack. This means that middleware is first executed upwards (e.g. next function in sequence) until next() is called without a callback. When that happens the stack starts to unwind, and middleware is executed downwards. You can think of the execution order like this:

  Nanostack
1.          7.  Middleware 1
==============
2.          6.  Middleware 2
==============
3.          5.  Middleware 3
==============
      4.        Middleware 4
Sequence: middleware 1, middleware 2, middleware 3
          middleware 4, middleware 3, middleware 2
          middleware 1

A keyd thing to note here is that any part of middleware can cause the stack to unwind. This is done by not passing a callback into the next() function. This is for example useful to handle create generic error handlers for the whole stack of middleware.

API

stack = nanostack

Create a new nanostack instance.

stack.push(cb(ctx, next))

Push a new handler onto the middleware stack.

stack.walk(ctx, next)

Call the functions on the middleware stack. Takes an initial context object and a callback.

next([err], [value], [handler])

Call the next function in the stack. If handler is not passed (e.g. last argument is not a function) the stack unwinds, calling all previous handler functions in reverse order (e.g. as a stack).

FAQ

Why did you write this?

I realized that most frontend and backend plugin systems / middleware is often best expressed as a stack that can execute some code at the start, handing off control to a function up the stack and then execute some more code when it regains control (before handing control off again down the stack).

co figured this out a while ago, but relying on newer language features prevented it from being generally applicable (for me). This package takes the same ideas and allows them to run in more environments.

When shouldn't I use this?

This package is probably best left for frameworks to consume / when doing more complex-ish architecture things. Generally the last handler on the stack would be a message bus / router that enables multiple handlers to resolve. If you're building something simple that might be all you need actually. Or if you want to get fancy you might want to consider using a package that consumes this one and exposes a neato pattern on top.

Similar Packages

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].