koajs/koa — pull request #36 by jonathanong (2013-08-24 04:04:39+00:00)
Title: app.use(generator) middleware signature

i really want the `app.use(generator)` signature because it would seriously make this the best node framework out there. since you had a problem with mounting apps, i figured out a way to flatten middleware into generators. see: https://github.com/jonathanong/co-dispatch

you can now use "conditional functions" as middleware. for example:

```js
app.use(function homepageMiddleware(){
if (this.path === '/')
return homepageGeneratorFunction
})
```

which would only execute `homepageGeneratorFunction` if the path matches. if a "conditional middleware" returns a falsey value, it just skips it. you can also use arrays of generators or conditional functions as middleware:

```js
app.use([
homepageMiddleware,
someGeneratorFunction
])
```

Thus, adding a router would be really easy:

```js
app.use(function (){
var match = router.match(this.path);
if (match) return match.middleware;
})
```

Mounting works, since it's basically a mini router.

The only issue I have is that one of the tests don't pass - generator errors aren't catchable. this is probably because i don't know generators well enough.

`co-dispatch` also only supports thunks right now. i think that the `co` should just integrate `co-dispatch`'s features since it's basically a superset of `co` (same function signature). i'm not sure if you'd want to do that as it would add 250+ lines of code to `co`.

also, I jsut realized that you named this framework as `koa` because it's `co-a`. why not move `co` to this org? aside: rework would be cool as a its own organization too.

this pull request is not mergable because:

- [ ] `co-dispatch` needs work or should be merged into `co`
- [ ] not sure how to deal with the context. i expect the top app's context properties, but i also expect the mounted app's custom context properties to override them.
- [ ] i need more tests for mount
- [ ] we should merge `app.mount` into `app.use` like connect/express