@connected/client
Version:
The hassle free way to call your server-side code
36 lines • 1.03 kB
JavaScript
export default class Middleware {
handlers = [];
use(handler) {
this.handlers.push({ handler, type: 'Normal' });
}
useErrorHandler(handler) {
this.handlers.push({ handler, type: 'Error' });
}
go(done, ...args) {
this.processHandlers(this.handlers, done, args, undefined);
}
processHandlers(handlers, done, args, error) {
const [handler, ...tail] = handlers;
if (!handler) {
done(error);
return;
}
const next = (error) => this.processHandlers(tail, done, args, error);
try {
const skipHandler = !!error !== (handler.type === 'Error');
if (skipHandler) {
next(error);
}
else if (error) {
handler.handler(error, next, ...args);
}
else {
handler.handler(next, ...args);
}
}
catch (error) {
next(error);
}
}
}
//# sourceMappingURL=middleware.js.map