root/lu/LUNotification.lua

Revision 814 (checked in by rsz, 3 years ago)

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               LUNotification.lua
3 -- Description:         Like a square peg in a round hole
4 -- Author:              Raphaël Szwarc http://alt.textdrive.com/lua/
5 -- Creation Date:       February 1, 2005
6 -- Legal:               Copyright (C) 2005 Raphaël Szwarc
7 --------------------------------------------------------------------------------
8
9 -- import dependencies
10 local LUObject = require( "LUObject" )
11 local LUMap = require( "LUMap" )
12
13 -- define the class
14 local super = LUObject
15 local self = super()
16
17 function self:default()
18         if self:class()._default == nil then
19                 self:class()._default = self:new()
20         end
21        
22         return self:class()._default
23 end
24
25 function self:map()
26         if self._map == nil then
27                 self._map = LUMap()
28         end
29        
30         return self._map
31 end
32
33 function self:addObserver( anObserver, aNotification, aMethod )
34         if anObserver ~= nil and aNotification ~= nil then
35                 local aMap = self:map()
36                 local someObservers = aMap:get( aNotification )
37                
38                 if someObservers == nil then
39                         someObservers = LUMap( nil, "k" )
40                         aMap:put( aNotification, someObservers )
41                 end
42                
43                 someObservers:put( anObserver, aMethod or "notify" )
44         end
45        
46         return self
47 end
48
49 function self:removeObserver( anObserver, aNotification )
50         if anObserver ~= nil and aNotification ~= nil then
51                 local someObservers = self:map():get( aNotification )
52                
53                 if someObservers ~= nil then
54                         someObservers:remove( anObserver )
55                 end 
56         end     
57        
58         return self
59 end
60
61 function self:post( aNotification, ... )
62         if aNotification ~= nil then
63                 local someObservers = self:map():get( aNotification )
64                
65                 if someObservers ~= nil then
66                         for anObserver, aMethod in someObservers:iterator() do
67                                 if type( aMethod ) == "string" then
68                                         anObserver:invoke( aMethod, aNotification, ... )
69                                 else
70                                         aMethod( aNotification, ... )
71                                 end             
72                         end
73                 end
74         end
75        
76         return self
77 end
78
79 return self
Note: See TracBrowser for help on using the browser.