This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Ferdi265's profile picture

ComputerCraft-EventLoop | A powerful API for Event handling

Started by Ferdi265, 25 August 2014 - 05:20 PM
Ferdi265 #1
Posted 25 August 2014 - 07:20 PM
ComputerCraft-EventLoop Version 1.5

This API allows you to register Event Listeners and fire custom events in a way that is very similar to JavaScript and Node.js.

Installation

To install this program via pastebin, run


pastebin get RWaMMZVM eventloop

or go to http://pastebin.com/RWaMMZVM

Alternatively, the code is available on gitHub:
https://github.com/F...Craft-EventLoop

This API is available on LNETeam's CCAPT Package Manager under the name EventLoop

Usage

A program using ComputerCraft-EventLoop may look like this:


os.loadAPI('eventloop') --load the ComputerCraft-EventLoop API
local loop = eventloop.create() --get an EventLoop instance
loop:run(function () --run a function in the event loop
  print('I\'m in an event loop!')
  loop:timeout(2, function ()
	print('This will happen 2 seconds later.')
  end)
  loop:interval(1, function ()
	print('This will happen every second!')
  end)
  loop:on('char', 's', function ()
	print('You pressed s!')
  end)
  loop:timeout(6, function ()
	print('Goodbye')
	loop:terminate() --stop the loop after 6 seconds
  end)
end)

Example output:



API Documentation

A full API Documentation of ComputerCraft-EventLoop can be found on gitHub:
https://github.com/F...Craft-EventLoop

Version History

Spoiler

Version 1.5: GitHub Changelog
Version 1.4.1: GitHub Changelog
Version 1.4: GitHub Changelog
Version 1.3.1: GitHub Changelog
Version 1.3: GitHub Changelog
Version 1.2: GitHub Changelog
Version 1.1: GitHub Changelog
Version 1.0: GitHub Changelog

A full Version history (all commits and branches) can be found on gitHub:
https://github.com/F...Craft-EventLoop
Edited on 29 August 2014 - 03:51 PM
Ferdi265 #2
Posted 27 August 2014 - 10:45 AM
Update v1.1 - Post edited accordingly
oeed #3
Posted 27 August 2014 - 11:10 AM
Probably more jQuery events than anything, but this looks rather nice. Good job!
Ferdi265 #4
Posted 27 August 2014 - 02:06 PM
Probably more jQuery events than anything, but this looks rather nice. Good job!

jQuery Events look similar, but this is actually based on Node.js EventEmitter. The difference is that jQuery doesn't allow multiple event parameters.
Ferdi265 #5
Posted 27 August 2014 - 05:24 PM
Update v1.4 post edited accordingly
Ferdi265 #6
Posted 28 August 2014 - 02:11 PM
Update v1.4.1 fixed broken build
Ferdi265 #7
Posted 29 August 2014 - 05:52 PM
Update v1.5, added kill(), changed running() and terminate()
comp500 #8
Posted 07 April 2015 - 07:56 AM
now this is nice! i have been thinking of doing the (awesome) eventemitter node.js stuff for a while. I might use this in 85974975832582375984759843725897345 of my things. :D/> One question… why is the loop:run needed? Can't you just do loop:timeout without using loop:run?
Ferdi265 #9
Posted 06 July 2015 - 09:24 PM
One question… why is the loop:run needed? Can't you just do loop:timeout without using loop:run?

The way I implemented it,


loop:run()

is a while loop that goes through all events that happen, and execute the event listeners.

A timeout is also an event listener, it listenes for a timer event.

In order for all my functions to work, I need to make sure all event registrations and emissions go through my code (this is why I replaced os.pullEvent and os.pullEventRaw with my own wrapper versions). The while loop then executes all listeners for events until there are no listeners left.

For example:


os.loadAPI('eventloop')
local loop = eventloop.create()

loop:timeout(0, function ()
  print('Second')
end) -- This just puts the timeout in the "stuff I have to run later" list

print('First') -- This executes immediately

local text = ''
loop:on('char', function (c)
    text = text .. c
    write(c)
end)
loop:on('key', 28 --[[ Return ]], function ()
    loop:terminate()
end)

loop:run() -- Timeouts and events will be executed here, the first one executed will be the "print('Second')" timeout

print('You wrote: ' .. text) -- This runs once the loop terminates