| 66 | //============================================================================== |
| 67 | |
| 68 | inline void registerTimerFunctions (Context& context) |
| 69 | { |
| 70 | struct TimerList |
| 71 | { |
| 72 | TimerList (Context& c) : context (c) {} |
| 73 | |
| 74 | Context& context; |
| 75 | std::unordered_map<int64_t, choc::messageloop::Timer> activeTimers; |
| 76 | int64_t nextTimerID = 0; |
| 77 | |
| 78 | int64_t setTimeout (uint32_t interval) |
| 79 | { |
| 80 | auto timerID = ++nextTimerID; |
| 81 | |
| 82 | activeTimers[timerID] = choc::messageloop::Timer (interval, [this, timerID] |
| 83 | { |
| 84 | auto timeIDCopy = timerID; // local copy as this lambda will get deleted.. |
| 85 | activeTimers.erase (timeIDCopy); |
| 86 | |
| 87 | try |
| 88 | { |
| 89 | context.invoke ("_choc_invokeTimeout", timeIDCopy); |
| 90 | } |
| 91 | catch (const choc::javascript::Error& e) |
| 92 | { |
| 93 | std::cerr << e.what() << std::endl; |
| 94 | } |
| 95 | |
| 96 | return false; |
| 97 | }); |
| 98 | |
| 99 | return timerID; |
| 100 | } |
| 101 | |
| 102 | int64_t setInterval (uint32_t interval) |
| 103 | { |
| 104 | auto timerID = ++nextTimerID; |
| 105 | |
| 106 | activeTimers[timerID] = choc::messageloop::Timer (interval, [this, timerID] |
| 107 | { |
| 108 | try |
| 109 | { |
| 110 | context.invoke ("_choc_invokeInterval", timerID); |
| 111 | } |
| 112 | catch (const choc::javascript::Error& e) |
| 113 | { |
| 114 | std::cerr << e.what() << std::endl; |
| 115 | } |
| 116 | |
| 117 | return true; |
| 118 | }); |
| 119 | |
| 120 | return timerID; |
| 121 | } |
| 122 | |
| 123 | void clearInterval (int64_t timerID) |
| 124 | { |
| 125 | activeTimers.erase (timerID); |