Jump to content




[0.7.5] Events API


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

#1 Pinkishu

  • Members
  • 484 posts

Posted 22 June 2012 - 10:04 AM

Hi there,

so I had that Idea after reading that help topic where someone asked about receiving a rednet message while using read()

No clue if something like this has been posted before either :P/> so tell me if it has been :)/>

Also let me know if you have suggestions or find bugs

Version: 0.7.5
Known Issues:
  • None I've encountered, be sure to report them if you find any.

Future Plans:
  • Maybe provide better events (e.g. a redstone event telling the callback function which colors changed and such)
  • Add an "all" event
  • Add a flag that allows programs to keep their events registered after they close

= Installation =


I might change how to install that at a later version. For now:

Make a file called events with the code:
http://pastebin.com/MWLPr157


Put into startup (at end):
term.clear() term.setCursorPos(1,1)
parallel.waitForAny(function() shell.run("shell") end, function() shell.run("events") end)
os.queueEvent("terminate")

= Usage =


Functions:

events.registerEvent(eventName,eventCallback)
  • eventName: string / the name of the event to catch, see wiki for a CC event list
  • eventCallback: function to call (arguments: event,p1,p2,p3,p4,p5,p6)
- Returns the registration ID
Registers the events and calls the callback function if an event is caught



events.unregisterEvent(registrationID)
  • registrationID: the ID returned by events.registerEvent
Unregisters the event so the callback function is no longer called

= Example =


Example of how to use it:

function redstoneCallback()
  local cX,cY = term.getCursorPos()
  term.setCursorPos(1,cY+1)
  term.clearLine()
  term.write("Redstone at " .. os.clock())
  term.setCursorPos(cX,cY)
end

local regID = events.registerEvent("redstone",redstoneCallback) -- Registers the event
read() -- You can type now and it will print the redstone thingy if it receives a signal change
events.unregisterEvent(regID) -- Unregisters the event
read() -- Now if it receives a redstone signal it will not trigger redstoneCallback() anymore



= ChangeLog =


v0.7.5
- changed from parallel API to own co-routine handling to avoid locking up and missing events

v0.7
- changed arguments of events.unregisterEvent to require regID
- some internal changes
- events of progams that end without unregistering them should now be unregistered
- changeg the code for startup a little

v0.5
- Release


#2 kazagistar

  • Members
  • 365 posts

Posted 24 June 2012 - 01:31 PM

I can't believe you posted something this awesome and it got no attention. I was just making one of these myself, but yours is very well done, good job.

#3 Pinkishu

  • Members
  • 484 posts

Posted 24 June 2012 - 03:35 PM

updated to 0.7 :P/>

#4 kazagistar

  • Members
  • 365 posts

Posted 24 June 2012 - 11:54 PM

There are some neat features in there such as the automated unlink when a process ends. Giving people an ID when you request a callback is brilliant: it is simple and matches the structure set up by the built in timer infrastructure (I used a given text based string, which puts the burden of handling multiple callbacks on the user, which is terrible on my part.) Your code is also very safe... I have a tenancy to just be like "if you use it wrong it will break everywhere, so just be smart about it", which is probably bad for sharing. Finally, linking in with the os.run, and holding copies of the pull events functions in local space is wise... if everything else gets burned down and overwritten, it will still continue to function, and you get a small speed boost out of it as well. If you are interested, here is my version of this that I was developing in parallel, but it is kinda... unpolished, and I really want to change it up. It comes in two parts, the event system, and a basic emulator thing that runs on top of it, and lets you run a process (basically, a custom reimplementation of parallel to some degree).

Eos v0.3 Reactor
Spoiler

Eos v0.3 Emulator
Spoiler
If you want, I can take these out of your thread, btw, but they are much less friendly to use atm, so I don't think it should be much competition. :P/>

EDIT: Also, I have a question: why did you decide to delete event handlers on coroutine exit? I could see a legitimate use case for a library registering event handlers and exiting, and the event handlers taking care of the rest. I am just curious if I misunderstood your code. Also, I am not sure what the purpose of hold-deletion is. Your deletion system is confusing... :)/>

#5 Pinkishu

  • Members
  • 484 posts

Posted 25 June 2012 - 01:23 AM

View Postkazagistar, on 24 June 2012 - 11:54 PM, said:

There are some neat features in there such as the automated unlink when a process ends. Giving people an ID when you request a callback is brilliant: it is simple and matches the structure set up by the built in timer infrastructure (I used a given text based string, which puts the burden of handling multiple callbacks on the user, which is terrible on my part.) Your code is also very safe... I have a tenancy to just be like "if you use it wrong it will break everywhere, so just be smart about it", which is probably bad for sharing. Finally, linking in with the os.run, and holding copies of the pull events functions in local space is wise... if everything else gets burned down and overwritten, it will still continue to function, and you get a small speed boost out of it as well. If you are interested, here is my version of this that I was developing in parallel, but it is kinda... unpolished, and I really want to change it up. It comes in two parts, the event system, and a basic emulator thing that runs on top of it, and lets you run a process (basically, a custom reimplementation of parallel to some degree).

Eos v0.3 Reactor
Spoiler

Eos v0.3 Emulator
Spoiler
If you want, I can take these out of your thread, btw, but they are much less friendly to use atm, so I don't think it should be much competition. :P/>

EDIT: Also, I have a question: why did you decide to delete event handlers on coroutine exit? I could see a legitimate use case for a library registering event handlers and exiting, and the event handlers taking care of the rest. I am just curious if I misunderstood your code. Also, I am not sure what the purpose of hold-deletion is. Your deletion system is confusing... :)/>

haha will have to look through that tomorrow

hm well I unregister events of a program once it ends - just so if some program registers events and crashes or such it won't keep calling its events although the program isn't anymore around but the events weren't unregistered, maybe i can add a persistance flag or something so they don't get deleted

holdDeletion is just a little optimization cause it would run executeDeletion() everytime it unregisters an event so it would be like
if a program had 10 events registered and exited without unregistering them, it would unregister them one by one
running executeDeletion() all the time, so if it sets holdDeletion the unregister function won't call executeDeletion()
but then once all of them are unregistered the function that unregisters them calls it so all 10 are deleted in 1 go xD
If that makes sense- i suck at explaining B)/>

I might still change how that works anyway B)/>

And I use a weird deletion cause i was told its bad to delete while looping through a table <.< So i add them to a queue and delete them then

#6 Pinkishu

  • Members
  • 484 posts

Posted 04 July 2012 - 02:28 PM

And v0.7.5 is out :3

#7 toxicwolf

  • Members
  • 201 posts
  • LocationUnited Kingdom

Posted 12 July 2012 - 08:32 PM

Dunno if you noticed, or if it's an error on my part, but with this, startup files from the root of a computer or on a disk aren't run at all. If the "add this to the startup file code" is added to a new startup file in the root of a computer instead, disk startups work. Not sure what's up with that.

#8 Pinkishu

  • Members
  • 484 posts

Posted 13 July 2012 - 12:38 PM

Hm not sure what you mean? Will test around with disks later
Usually you'd just stick it at the end of your existing startup file (be that on the disk or the PC)

#9 toxicwolf

  • Members
  • 201 posts
  • LocationUnited Kingdom

Posted 13 July 2012 - 02:50 PM

View PostPinkishu, on 13 July 2012 - 12:38 PM, said:

Hm not sure what you mean? Will test around with disks later
Usually you'd just stick it at the end of your existing startup file (be that on the disk or the PC)
Might be the problem then. I added the code to the startup file in the rom directory, and that's what prevented the startup files on a computer or disk to run.
The problem was actually discovered by minizbot2012 over at the OCL thread, but I checked it out and then posted here.

#10 Pinkishu

  • Members
  • 484 posts

Posted 13 July 2012 - 03:22 PM

Oh ok, i'll have a look at putting it in the rom startup then~

#11 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 13 July 2012 - 04:08 PM

If you put it in the rom startup it won't load any other startup, that's because the new shell (the one that is called from the startup on the parallel call) won't load startup files cause it has a parent shell. I'm not sure about this, but if you call shell.exit() (or use the exit program), it might run the startup file from computer or disk, since it will exit the current shell and return to the old one, wich is still loading the startup files, but this api won't work, since it's not running in the background anymore.
So, it's better to just put this in the computer or disk startup.

#12 Pinkishu

  • Members
  • 484 posts

Posted 13 July 2012 - 04:09 PM

Well yeah i'll look into how to solve this :)/> might change the way it starts up anyway





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users