summaryrefslogtreecommitdiff
path: root/modules/std/event.qs
blob: d4f2a10b232e2aa1e39c1de4aabcf5255eeccdcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import debug for debug; 

class event { 
    func init() {
        self._func = [];  // callback functions list
    }
    
    func clear() {
        self._func.clear();
    }  
    
    func += (fn) {
        if(typeof(fn) != "function") 
            debug.assert("wrong type");
        if(!self._func.contains(fn))
            self._func.push(fn); 
        else 
            debug.logError("已经存在这个方法");
    } 
    
    func -= (fn) { 
        if(typeof(fn) != "function") 
            debug.assert("wrong type");
        self._func.remove(fn); 
    } 
    
    func () (...){ 
        foreach(var fn in self._func) {
            fn(...);
        } 
    } 
} 

/*
var Say = new event(); 
Say += func(str) { 
    io.print(str);
} 
Say("hi");
Say.clear();

*/