• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

metalabdesign / http-middleware-metalab / 200 / 3
96%
master: 97%

Build:
Build:
LAST BUILD BRANCH: midori
DEFAULT BRANCH: master
Ran 01 Feb 2016 10:59PM UTC
Files 27
Run time 1s
Badge
Embed ▾
README BADGES
x

If you need to use a raster PNG badge, change the '.svg' to '.png' in the link

Markdown

Textile

RDoc

HTML

Rst

01 Feb 2016 10:55PM UTC coverage: 95.979%. Remained the same
200.3

push

travis-ci

izaakschroeder
Add `thunk` middleware.

This is essentially the `apply` method of an applicative.

Sometimes you want to change the processing mechanism of your application at runtime instead of construction time. For example:

```javascript
const app = compose(
  match(path('/foo'), send())
);
```

This app structure is static. That app will always match `/foo` to `send()`. What if we wanted the structure of the app to be the result of a function instead? For example, swapping between a random app per request or using routes from a database? Such a thing is now possible in relatively elegant fashion:

Swapping between two apps:

```javascript
const createApp = compose(
  thunk((app) => {
    const a = send('Hello')(app);
    const b = send('World')(app);
    return () => {
      return (Math.random() > 0.5) ? a : b;
    };
  })
);
```

Updating routes at runtime:

```javascript
const createApp = compose(
  thunk((app) => {
    let result = app;
    db.on('update', (routes) => {
      result = routes.reduce((result, route) => {
        return middlewareForRoute(route)(result);
      }, app);
    });
    return () => result;
  })
);
```

The `thunk` middleware basically defers access to the appropriate method (e.g. `request`, `error`, etc.) to the last possible moment at which point it calls the thunk to get the method at that instant.

You provide one argument to `thunk` which is your thunk creator: a function that takes, as input, an `app` and returns as output not an app but a _function_ returning an app. This function is then invoked whenever the app is actually needed (i.e. on any of the event calls like `request`).

358 of 373 relevant lines covered (95.98%)

3.61 hits per line

Source Files on job 200.3
  • Tree
  • List 0
  • Changed 1
  • Source Changed 1
  • Coverage Changed 1
Coverage ∆ File Lines Relevant Covered Missed Hits/Line
  • Back to Build 200
  • Travis Job 200.3
  • e3d625f3 on github
  • Prev Job for on thunk (#197.3)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc