koajs / middleware-hook Goto Github PK
View Code? Open in Web Editor NEWlow-level hooks for your middleware
low-level hooks for your middleware
Must have lost my mind because I cannot figure out how to do this.
I'd really appreciate if someone could chime in on how to accomplish accessing the koa req context inside of the reducer
function. I need to attach meta data to the state
property.
Eg: for a simplified version of koajs/timer
// SimpleTimer.js
const middlewareHook = require('koa-middleware-hook');
module.exports = SimpleTimer;
function SimpleTimer() {
var hook = middlewareHook(marker, reducer);
return function timer(generator) {
return hook(generator);
};
}
function marker() {
return new Date();
}
function reducer(name, topStart, topEnd, bottomStart, bottomEnd) {
let top = topEnd - topStart;
let bottom = 0;
if (bottomStart && bottomEnd) {
bottom = bottomEnd - bottomStart;
}
// access the context here to attach properties to state
(this.state.metrics = (this.state.metrics || [])).push({
name: name,
duration: top + bottom
});
}
// eg: index.js
let timer = require('./SimpleTimer')();
app.use(function *entryPoint(next) {
yield next;
console.log(this.state.metrics); // ==> [{ name: 'foo', duration: 250 }]
});
app.use(timer(function *foo(next) {
yield wait(250);
}));
// ... ala koajs/timer ...
function wait (ms) {
return function (done) {
setTimeout(done, ms)
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.