.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / htmlparser2 / lib / CollectingHandler.js
1 module.exports = CollectingHandler;
2
3 function CollectingHandler(cbs) {
4     this._cbs = cbs || {};
5     this.events = [];
6 }
7
8 var EVENTS = require("./").EVENTS;
9 Object.keys(EVENTS).forEach(function(name) {
10     if (EVENTS[name] === 0) {
11         name = "on" + name;
12         CollectingHandler.prototype[name] = function() {
13             this.events.push([name]);
14             if (this._cbs[name]) this._cbs[name]();
15         };
16     } else if (EVENTS[name] === 1) {
17         name = "on" + name;
18         CollectingHandler.prototype[name] = function(a) {
19             this.events.push([name, a]);
20             if (this._cbs[name]) this._cbs[name](a);
21         };
22     } else if (EVENTS[name] === 2) {
23         name = "on" + name;
24         CollectingHandler.prototype[name] = function(a, b) {
25             this.events.push([name, a, b]);
26             if (this._cbs[name]) this._cbs[name](a, b);
27         };
28     } else {
29         throw Error("wrong number of arguments");
30     }
31 });
32
33 CollectingHandler.prototype.onreset = function() {
34     this.events = [];
35     if (this._cbs.onreset) this._cbs.onreset();
36 };
37
38 CollectingHandler.prototype.restart = function() {
39     if (this._cbs.onreset) this._cbs.onreset();
40
41     for (var i = 0, len = this.events.length; i < len; i++) {
42         if (this._cbs[this.events[i][0]]) {
43             var num = this.events[i].length;
44
45             if (num === 1) {
46                 this._cbs[this.events[i][0]]();
47             } else if (num === 2) {
48                 this._cbs[this.events[i][0]](this.events[i][1]);
49             } else {
50                 this._cbs[this.events[i][0]](
51                     this.events[i][1],
52                     this.events[i][2]
53                 );
54             }
55         }
56     }
57 };