UNPKG

kr-observable

Version:
40 lines (39 loc) 1.05 kB
import { Admin } from './Admin.js'; import { Global } from './global.js'; export class ActionHandler { ctx; constructor(receiver) { this.ctx = receiver; } apply(target, _, args) { if (Global.action) return target.apply(this.ctx, args); Global.action = true; try { let result = target.apply(this.ctx, args); const thenable = result instanceof Promise; if (thenable) { result = result.then(ActionHandler.resolve, ActionHandler.reject); } ActionHandler.flush(); Global.action = thenable; return result; } catch (e) { ActionHandler.reject(e); } } static flush() { Global.queue.forEach(Admin.batch); Global.queue.clear(); Global.action = false; } static resolve(result) { ActionHandler.flush(); return result; } static reject(error) { ActionHandler.flush(); throw error; } }