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

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

Build:
Build:
LAST BUILD BRANCH: midori
DEFAULT BRANCH: master
Ran 01 Feb 2016 10:58PM UTC
Jobs 3
Files 27
Run time 1min
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

pending completion
200

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%)

10.33 hits per line

Jobs
ID Job ID Ran Files Coverage
1 200.1 01 Feb 2016 10:58PM UTC 0
95.8
Travis Job 200.1
2 200.2 01 Feb 2016 10:59PM UTC 0
95.98
Travis Job 200.2
3 200.3 01 Feb 2016 10:59PM UTC 0
95.98
Travis Job 200.3
Source Files on build 200
Detailed source file information is not available for this build.
  • Back to Repo
  • Travis Build #200
  • e3d625f3 on github
  • Prev Build on thunk (#197)
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