|
6 | 6 | * |
7 | 7 | * Original File by lil_Toady |
8 | 8 | * |
9 | | -**************************************]] |
10 | | -local aCoroutines = {} |
11 | | - |
12 | | -sourceCoroutine = nil |
13 | | - |
14 | | -local _addEventHandler = addEventHandler |
15 | | -function addEventHandler(event, element, handler, ...) |
16 | | - if (not _addEventHandler(event, element, corouteHandler(handler), ...)) then |
17 | | - local info = debug.getinfo(2, "nS") |
18 | | - outputDebugString("addEventHandler call failed from: " .. tostring(info.name) .. " (" .. info.short_src .. ")") |
19 | | - end |
20 | | -end |
21 | | - |
22 | | -local _removeEventHandler = removeEventHandler |
23 | | -function removeEventHandler(event, element, handler) |
24 | | - if (aCoroutines[handler]) then |
25 | | - for id, wrapper in ipairs(aCoroutines[handler]) do |
26 | | - _removeEventHandler(event, element, wrapper) |
27 | | - end |
28 | | - aCoroutines[handler] = {} |
29 | | - else |
30 | | - _removeEventHandler(event, element, handler) |
31 | | - end |
32 | | -end |
33 | | - |
34 | | -function corouteHandler(handler) |
35 | | - local wrapper = function(...) |
36 | | - local c = coroutine.create(handler) |
37 | | - sourceCoroutine = c |
38 | | - local result, error = coroutine.resume(c, ...) |
39 | | - if (not result) then |
40 | | - outputDebugString(tostring(error)) |
41 | | - end |
42 | | - if (sourceCoroutine == c) then |
43 | | - sourceCoroutine = nil |
44 | | - end |
45 | | - end |
46 | | - if (not aCoroutines[handler]) then |
47 | | - aCoroutines[handler] = {} |
48 | | - end |
49 | | - table.insert(aCoroutines[handler], wrapper) |
50 | | - return wrapper |
51 | | -end |
52 | | - |
53 | | -function coroutineKill(cr) |
54 | | - if (coroutine.status(c) ~= "dead") then |
55 | | - coroutine.resume(c, nil) |
56 | | - end |
57 | | -end |
| 9 | +**************************************]] |
| 10 | +local aCoroutines = {} |
| 11 | + |
| 12 | +sourceCoroutine = nil |
| 13 | + |
| 14 | +local _addEventHandler = addEventHandler |
| 15 | +function addEventHandler(event, element, handler, ...) |
| 16 | + if (not _addEventHandler(event, element, corouteHandler(handler), ...)) then |
| 17 | + local info = debug.getinfo(2, "nS") |
| 18 | + outputDebugString("addEventHandler call failed from: " .. tostring(info.name) .. " (" .. info.short_src .. ")") |
| 19 | + end |
| 20 | +end |
| 21 | + |
| 22 | +local _removeEventHandler = removeEventHandler |
| 23 | +function removeEventHandler(event, element, handler) |
| 24 | + if (aCoroutines[handler]) then |
| 25 | + for id, wrapper in ipairs(aCoroutines[handler]) do |
| 26 | + _removeEventHandler(event, element, wrapper) |
| 27 | + end |
| 28 | + aCoroutines[handler] = {} |
| 29 | + else |
| 30 | + _removeEventHandler(event, element, handler) |
| 31 | + end |
| 32 | +end |
| 33 | + |
| 34 | +function corouteHandler(handler) |
| 35 | + local wrapper = function(...) |
| 36 | + local c = coroutine.create(handler) |
| 37 | + sourceCoroutine = c |
| 38 | + local result, error = coroutine.resume(c, ...) |
| 39 | + if (not result) then |
| 40 | + outputDebugString(tostring(error)) |
| 41 | + end |
| 42 | + if (sourceCoroutine == c) then |
| 43 | + sourceCoroutine = nil |
| 44 | + end |
| 45 | + end |
| 46 | + if (not aCoroutines[handler]) then |
| 47 | + aCoroutines[handler] = {} |
| 48 | + end |
| 49 | + table.insert(aCoroutines[handler], wrapper) |
| 50 | + return wrapper |
| 51 | +end |
| 52 | + |
| 53 | +function coroutineKill(cr) |
| 54 | + if (coroutine.status(c) ~= "dead") then |
| 55 | + coroutine.resume(c, nil) |
| 56 | + end |
| 57 | +end |
0 commit comments