Jump to content




ComputerCraft-EventLoop | A powerful API for Event handling

api lua

  • You cannot reply to this topic
8 replies to this topic

#1 Ferdi265

  • Members
  • 21 posts

Posted 25 August 2014 - 06: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:

Posted Image

API Documentation

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

Version History

Spoiler

Edited by Ferdi265, 29 August 2014 - 04:51 PM.


#2 Ferdi265

  • Members
  • 21 posts

Posted 27 August 2014 - 09:45 AM

Update v1.1 - Post edited accordingly

#3 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 27 August 2014 - 10:10 AM

Probably more jQuery events than anything, but this looks rather nice. Good job!

#4 Ferdi265

  • Members
  • 21 posts

Posted 27 August 2014 - 01:06 PM

View Postoeed, on 27 August 2014 - 10:10 AM, said:

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.

#5 Ferdi265

  • Members
  • 21 posts

Posted 27 August 2014 - 04:24 PM

Update v1.4 post edited accordingly

#6 Ferdi265

  • Members
  • 21 posts

Posted 28 August 2014 - 01:11 PM

Update v1.4.1 fixed broken build

#7 Ferdi265

  • Members
  • 21 posts

Posted 29 August 2014 - 04:52 PM

Update v1.5, added kill(), changed running() and terminate()

#8 comp500

  • Members
  • 66 posts

Posted 07 April 2015 - 06: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?

#9 Ferdi265

  • Members
  • 21 posts

Posted 06 July 2015 - 08:24 PM

View Postcomp500, on 07 April 2015 - 06:56 AM, said:

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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users