Last change
on this file was 208, checked in by wouter, 3 years ago |
#87 add test just importing the file, as smoke test....
|
File size:
1.4 KB
|
Line | |
---|
1 | from abc import ABC
|
---|
2 | import threading
|
---|
3 | import traceback
|
---|
4 | from typing import TypeVar, Generic, List
|
---|
5 |
|
---|
6 | from tudelft_utilities_listener.Listenable import Listenable
|
---|
7 | from tudelft_utilities_listener.Listener import Listener
|
---|
8 |
|
---|
9 |
|
---|
10 | TYPE= TypeVar('TYPE')
|
---|
11 |
|
---|
12 |
|
---|
13 | class DefaultListenable (Listenable[TYPE], Generic[TYPE]) :
|
---|
14 | '''
|
---|
15 | a default implementation for Listenable. Thread safe. Intercepts any
|
---|
16 | exceptions from children and prints them as stacktrace.
|
---|
17 | @param <TYPE> the type of the data being passed around.
|
---|
18 | '''
|
---|
19 |
|
---|
20 | def __init__(self):
|
---|
21 | self.__listeners:List[Listener[TYPE]] = []
|
---|
22 | self.__lock = threading.Lock()
|
---|
23 |
|
---|
24 |
|
---|
25 | #Override
|
---|
26 | def addListener(self, l:Listener[TYPE]):
|
---|
27 | self.__lock.acquire()
|
---|
28 | self.__listeners.append(l);
|
---|
29 | self.__lock.release()
|
---|
30 |
|
---|
31 | #Override
|
---|
32 | def removeListener(self,l:Listener[TYPE]):
|
---|
33 | self.__lock.acquire()
|
---|
34 | self.__listeners.remove(l);
|
---|
35 | self.__lock.release()
|
---|
36 |
|
---|
37 | def notifyListeners(self,data:TYPE ):
|
---|
38 | '''
|
---|
39 | This should only be called by the owner of the listenable, not by
|
---|
40 | listeners or others. Avoid calling this from synchronized blocks as a
|
---|
41 | notified listener might immediately make more calls to you.
|
---|
42 | <p>
|
---|
43 | Any listeners that throw an exception will be intercepted and their
|
---|
44 | stacktrace is printed.
|
---|
45 |
|
---|
46 | @param data information about the change.
|
---|
47 | '''
|
---|
48 | l:Listener[TYPE]
|
---|
49 | for l in self.__listeners:
|
---|
50 | try:
|
---|
51 | l.notifyChange(data);
|
---|
52 | except BaseException as e:
|
---|
53 | traceback.print_exc()
|
---|
54 | |
---|
Note:
See
TracBrowser
for help on using the repository browser.