Skip to content

Instantly share code, notes, and snippets.

@auser
Created July 24, 2018 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auser/d75cddb5438afe040c88ea4033c89882 to your computer and use it in GitHub Desktop.
Save auser/d75cddb5438afe040c88ea4033c89882 to your computer and use it in GitHub Desktop.
// Not the fastest thing in the world
export default class Middleware {
constructor (methods) {
this.methods = methods;
this.methodsStack = {};
this.originalMethod = {};
methods.forEach (method => {
this.methodsStack[method] = [];
this.originalMethod[method] = this[method];
this[method] = (...args) => {
const next = (idx, nextArgs) => {
const m = this.methodsStack[method][idx];
const orig = this.originalMethod[method];
m
? m.call (this, nextArgs, next.bind (this, idx + 1))
: orig.call (this, nextArgs);
};
next (0, args);
};
});
}
use (obj) {
this.methods.forEach (method => {
this.methodsStack[method].indexOf (obj[method]) >= 0 ||
this.methodsStack[method].push (obj[method]);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment