summaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/framework/src/common
diff options
context:
space:
mode:
authorHerbert Eiselt <herbert.eiselt@highstreet-technologies.com>2019-02-11 14:54:12 +0100
committerHerbert Eiselt <herbert.eiselt@highstreet-technologies.com>2019-02-11 14:54:53 +0100
commit3d202a04b99f0e61b6ccf8b7a5610e1a15ca58e7 (patch)
treeab756cfa8de5eced886d3947423d198be8c0ce62 /sdnr/wt/odlux/framework/src/common
parent12a8c669f52c0e84d580c078cee849b25133b585 (diff)
Add sdnr wt odlux
Add complete sdnr wireless transport app odlux core and apps Change-Id: I5dcbfb8f3b790e3bda7c8df67bd69d81958f65e5 Issue-ID: SDNC-576 Signed-off-by: Herbert Eiselt <herbert.eiselt@highstreet-technologies.com>
Diffstat (limited to 'sdnr/wt/odlux/framework/src/common')
-rw-r--r--sdnr/wt/odlux/framework/src/common/event.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/sdnr/wt/odlux/framework/src/common/event.ts b/sdnr/wt/odlux/framework/src/common/event.ts
new file mode 100644
index 000000000..f71b0164a
--- /dev/null
+++ b/sdnr/wt/odlux/framework/src/common/event.ts
@@ -0,0 +1,62 @@
+/**
+ * Represents an event.
+ * Events enable a class or object to notify other classes or objects when something of interest occurs.
+ * The class that sends (or invokes) the event is called the publisher and the classes that receive (or handle) the event are called subscribers.
+ *
+ * Objects can create an instances of an Events and offer that Events for other objects to attach to.
+ * Objects who want to be informed about an Event can attach a function (an event handler) to the event which is then called when the event is fired.
+ *
+ * @template TEventArg Type of the event argument. Use void if the event does not has an argument.
+ */
+export class Event<TEventArg> {
+
+ /**
+ * Creates a new instance of the Event class.
+ */
+ constructor() {
+ this.eventHandlers = new Array<(arg: TEventArg) => void>();
+ }
+
+ /**
+ * Adds an event handler to this event, so that when the event is fired the given event handler function is called.
+ *
+ * @param eventHandler The event handler function to add to this event.
+ * @throws {Error} Thrown if the given event handler function has already been added to this event.
+ */
+ public addHandler = (eventHandler: (arg: TEventArg) => void): void => {
+ if (this.eventHandlers.indexOf(eventHandler) > -1) {
+ throw new Error("The given event handler is already added to this event.");
+ }
+
+ this.eventHandlers.push(eventHandler);
+ }
+
+ /**
+ * Removes an event handler from this event, so that the given event handler function will not be called anymore when the event is fired.
+ *
+ * @param eventHandler: The event handler function to remove.
+ * @throws {Error} Thrown if the given event handler function has not been added to this event before.
+ */
+ public removeHandler = (eventHandler: (arg: TEventArg) => void): void => {
+ const index = this.eventHandlers.indexOf(eventHandler);
+ if (!(index > -1)) {
+ throw new Error("The given event handler has not been added to this event yet.");
+ }
+
+ this.eventHandlers.splice(index, 1);
+ }
+
+ /**
+ * Invokes the event and calls all event handler functions currently registered on the event.
+ *
+ * @param argument The argument for the event. The argument will be passed to all registered event handler functions.
+ */
+ public invoke = (argument?: TEventArg): void => {
+ this.eventHandlers.forEach((eventHandler: (arg?: TEventArg) => void, index: number, array: Array<(arg: TEventArg) => void>): void => {
+ eventHandler(argument);
+ });
+ }
+
+ private eventHandlers: Array<(arg?: TEventArg) => void>;
+
+}