ProxyHandler.js 667 B

123456789101112131415161718192021222324252627
  1. module.exports = ProxyHandler;
  2. function ProxyHandler(cbs){
  3. this._cbs = cbs || {};
  4. }
  5. var EVENTS = require("./").EVENTS;
  6. Object.keys(EVENTS).forEach(function(name){
  7. if(EVENTS[name] === 0){
  8. name = "on" + name;
  9. ProxyHandler.prototype[name] = function(){
  10. if(this._cbs[name]) this._cbs[name]();
  11. };
  12. } else if(EVENTS[name] === 1){
  13. name = "on" + name;
  14. ProxyHandler.prototype[name] = function(a){
  15. if(this._cbs[name]) this._cbs[name](a);
  16. };
  17. } else if(EVENTS[name] === 2){
  18. name = "on" + name;
  19. ProxyHandler.prototype[name] = function(a, b){
  20. if(this._cbs[name]) this._cbs[name](a, b);
  21. };
  22. } else {
  23. throw Error("wrong number of arguments");
  24. }
  25. });