This repository was archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvents.lua
More file actions
157 lines (142 loc) · 4.3 KB
/
Events.lua
File metadata and controls
157 lines (142 loc) · 4.3 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#! /usr/bin/env lua
--
-- Events.lua
-- Copyright (C) 2015 beck <beck@Kataomoi>
--
-- Distributed under terms of the MIT license.
--
-- Similar to emit_signal/connect_signal, but with order control.
-- TODO async/detatched polls
-- TODO import advanced openrc style order control from IdleScript.
local naughty = require("naughty")
local Events = {
event = { }
}
-----------------------
-- Helper Functions {{{
-----------------------
local function TableOverwrite(orig,newvals)
newvals = newvals or {}
for k,v in pairs(newvals) do
orig[k] = v
end
return orig -- Not necessary, but possibly convienent
end
-- Clone: Originily by Doub on https://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value
-- Modified by FireFish5000@gmail.com
local function TableCopy(orig,newvals)
local t2 = {}
for k,v in pairs(orig) do
t2[k] = v
end
newvals = newvals or {}
for k,v in pairs(newvals) do
t2[k] = v
end
return t2
end
local function TableClone(newvals)
return self.TableCopy(self, newvals)
end
-- Helper Functions }}}
-----------------------
function Events.new()
local EventHandler = TableCopy(Events,{event = {} })
EventHandler:new_event("NewEvent")
EventHandler:new_event("AssociatedCall")
EventHandler:new_event("PolledEvent")
EventHandler:new_event("Error::EventExists")
EventHandler:new_event("Error::NoName")
EventHandler:new_event("Error::NoSuchEvent")
EventHandler:add_call("Error::EventExists", function(t)
naughty.notify({timeout=1000,title="Error",text="Attempting to re-register '" .. t.name .. "', an alread registered event!"});
end
)
EventHandler:add_call("Error::NoName", function(t)
naughty.notify({timeout=1000,title="Error",text="Register, Polled, or Called called without an event."});
end
)
EventHandler:add_call("Error::NoSuchEvent", function(t)
if t.flaged == 0 then return end
naughty.notify({timeout=1000,title="Error",text="Non-existing event ".. t.name .." polled or called. Creating event"});
t.EventHandler.event[t.name]={}
-- Flag indecates weather psudo error is handeld or not.
t.flaged=0
end
)
return EventHandler
end
-- Registers a new event prior to use
function Events:new_event(name)
if name == nil then
self:error_poll("Error::NoName", { EventHandler=self })
return false
end
if self.event[name] == nil then
self.event[name]={}
else
self:error_poll("Error::EventExists", { EventHandler=self, name=name })
end
return self.event[name]
end
function Events:add_call(name,func)
if name == nil then
self:error_poll("Error::NoName", { EventHandler=self })
return false
end
if self.event[name] == nil then
self:error_poll("Error::NoSuchEvent", { EventHandler=self, name=name })
end
local idx = #self.event[name]+1
self.event[name][idx] = func
return self.event[name][idx]
end
-- name: name of event
-- ... : arguments to pass to called functions
-- NOTE polls cannot get return value from called functions, as returning values from numerous unknown functions is nonsensical.
-- If you need values from called functions, pass a variable for them to store it in.
function Events:poll(name, ... )
if name == nil then
self:error_poll("Error::NoName", { EventHandler=self })
return false
end
if self.event[name] == nil then
self:error_poll("Error::NoSuchEvent", { EventHandler=self, name=name })
end
local calls = self.event[name]
for idx=1,#calls do
if type(calls[idx]) == "function" then
calls[idx](...) -- passing variable num of args
end
end
end
-- Error polls require a tabel argument.
-- And will exit early if flag is lowered
-- FIXME should error_poll throw an error if it cannot handle it?
function Events:error_poll(name, t)
t = t or {}
if type(t) ~= "table" then
-- TODO Some error handeling
return
end
t.flaged = t.flaged or 1;
if name == nil then
-- self:error_poll("Error::NoName", { EventHandler=self })
-- return false
error("EventHandler: error_poll was called without an event")
end
if self.event[name] == nil then
-- self:error_poll("Error::NoSuchEvent", { EventHandler=self, name=name })
error("EventHandler: error_poll was called on a non existent event")
end
local calls = self.event[name]
for idx=1,#calls do
if (t.flaged == 0 or not t.flaged) then
return
end
if type(calls[idx]) == "function" then
calls[idx](t) -- passing variable num of args
end
end
end
return Events