Skip to content

jakob-lilliemarck/before-n-after

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A generic apollo-link-like composing function to create functional pipelines featuring both middlewares (called before) and afterwares (called after) the decorated function.

The middleware function may optionally return an afterware function, which may access and alter return values of the preceeding afterware.

// With afterware:
const foo = (next) => (...args) => {
  /* Executed before the terminating function is called.
  Arguments may be modified here. */
  next(...args)
  return (result) => {
    /* Executed after the terminating function is called.
    Returns may be modifed here. */
    return result
  }
}

// Without afterware
const bar = (next) => (...args) => {
  next(...args)
  // Nothing is returned. Non-function returns will throw an error.
}

next() calls the next function in the pipeline. As such, the pipeline may be interrupted by omitting the call to next():

const baz = (next) => (value) => {
  // interrupts pipeline if the value is falsy
  if (value) {
    next(value)
  }
}

usePipeline() returns two functions with argument-functions composed from right to left. The first argument is the function to decorate which will be called last. The rest are the middle- and after-wares.

runMiddleware(...args) runs the function with middleware only, no afterware.

runAfterware(...args) run the function with both middle- and after-ware.

const fnToDecorate = (value) => value

const {
  runMiddleware,
  runAfterware
} = usePipeline(fnToDecorate, foo, bar)

About

Middle- and after-ware pipeline composing function

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published