View Single Post
08-17-14, 08:49 PM   #5
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Let's break this down, line by line.
Code:
local lib = LibStub:NewLibrary("MyLibrary-1.0", 1)
When creating a new library, it needs to be registered with LibStub. To do so, you need MAJOR, MINOR. The MAJOR is a string value, always the full name of the library you are creating. MINOR is a number, incremented each time a non-breaking, fully backwards compatible, change is implemented.

It is author preference to start the MINOR at 1; I've seen authors start at 90000 + tonumber(repository commit number [ie:// v46]) which would become 90046. Both methods are fine.

As Phanx said, this would be equivalent to
Code:
local ADDON_NAME, private_table = ...
Although LibStub looks for a number value instead of private_table, the MAJOR and MINOR get converted into a table and returned to lib.

If LibStub rejects lib, it is usually because a library with that MAJOR name and MINOR version or higher already exists in LibStub's internal table structure.
Code:
if not lib then
    return    --HERE, NOTICE ME
    -- LibStub has rejected this library with the same MAJOR, MINOR; it exists already
end
You will notice I said "or higher" just above. Let's say MyAddOn and YourAddOn both load SomeLib. During the load process, regardless of which AddOn calls SomeLib in whatever order, LibStub will dump the lower MINOR version if a higher version is found.

Presuming alphabetical load order (not a guarantee, btw), if MyAddOn loads SomeLib (MINOR 1), it will stay in LibStub's structure. Then YourAddOn loads SomeLib (MINOR 2), and SomeLib (MINOR 1) gets unloaded.

If both MINOR versions of SomeLib are the same (1 == 1) then when YourAddOn loads SomeLib, LibStub reports back "already loaded and here it is".

Keep in mind that the only reason to load two version of the same library is because they are not truly the same; there has been breaking changes between versions. In that case, the MAJOR is incremented by the author, and by convention, the MINOR would be reset by the author. The original version of the library could continue wholly on its own, completely independently.

"SomeLib-1.0" =/= "SomeLib-1.1" =/= "SomeLib-2.0". All these MAJORs would be loaded by LibStub concurrently, and each AddOn that needs a particular version would work fine. When such a case exists (LibDataBroker-1.0 and LibDataBroker-1.1 for example) it is highly recommended that the author of MyAddOn update their code to use the newer version.

Hopefully this helps!
  Reply With Quote