Giter Club home page Giter Club logo

middleware-hook's People

Contributors

matthewmueller avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

middleware-hook's Issues

Example of accessing application context inside of reducer

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.