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

API BucketList v1.0

Started by Espen, 24 April 2013 - 11:50 AM
Espen #1
Posted 24 April 2013 - 01:50 PM
buck·et list [buhk-it list]
noun

  1. A list of things to accomplish before one's death.


Description:
[indent=1]Adds onUnload() functionality to APIs.
That means APIs get the ability to react to os.unloadAPI() and execute some code before they're being unloaded.
API BucketList also cleans up after itself, i.e. after the last API that has been loaded with it is unloaded, API BucketList will revert everything to how it was before.[/indent]


Download:
[indent=1]http://pastebin.com/wZaSER5P[/indent]


How to use it?
  1. Download API BucketList
  2. Open up your API and put the following code at the end of it:
  3. function onUnload()
      -- Code that you want to be executed when the API is being unloaded.
    end
    
    loadfile( "/apis/BucketList" )()( "yourAPIName", onUnload )
    You can name onUnload() anything you like. The important part is the loadfile call:
    • The argument in the first paranthesis needs to be the absolute path of API BucketList. (as a String)
    • The first argument of the third paranthesis needs to be the name of your API file. (as a String)
    • The second argument of the third paranthesis needs to be your custom unload function. (as a reference)
    Practical example:
    SpoilerLet's say we have the following files:
    • /myAPIs/testAPI - The API you want to use BucketList with.
    • /testProgram - A program that makes use of testAPI.
    • /BucketList - The code from this thread.
    /myAPIs/testAPI:
    Spoiler
    -- ---------------------------------------------------------
    -- Your main API code.
    -- ---------------------------------------------------------
    
    -- Backup native print
    nativePrint = _G.print
    
    -- Custom print
    function myPrint( ... )
      write("Simon says: ")
      nativePrint( ... )
    end
    
    -- Overwrite native print
    _G.print = myPrint
    
    -- ---------------------------------------------------------
    -- The unload code for BucketList, added at the end.
    -- ---------------------------------------------------------
    
    -- Cleanup
    local function cleanUp()
      nativePrint("cleanUp() called")
      _G.print = nativePrint
    end
    
    -- Enable BucketList for cleanUp()
    loadfile("/BucketList")()("testAPI", cleanUp)

    /testProgram:
    Spoiler
    os.loadAPI("/myAPIs/testAPI")
    print("Hello!")
    
    os.unloadAPI("testAPI")
    print("Hello!")

    Then the output of /testProgram would be:
    Simon says: Hello!
    cleanUp() called
    Hello!

    Which means that /myAPIs/testAPI successfully executed its cleanUp() function during os.unloadAPI("testAPI").
lieudusty #2
Posted 16 May 2013 - 10:57 PM
Oh nice API. Sadly I don't think much people saw this thread because there are no replies D:
theoriginalbit #3
Posted 17 May 2013 - 01:04 AM
Oh nice API. Sadly I don't think much people saw this thread because there are no replies D:
There were some replies before the time travel.