Get an Epic Experience with Premium

LibStub

Libraries Login to Add Favorites
  • 8 Likes
  • World of Warcraft
  • 42,024 Monthly Downloads
  • Supports: 6.0.2
  • 1,084,941 Total Downloads
  • Updated 10/15/2014
  • Created 04/14/2008
  • 105 Favorites
  • Project Site
  • Comments
  • Release Type: Mature
  • License: Public Domain
  • Newest File: 1.0.2.60000
or
Support development! **
Donate

About LibStub

LibStub is a minimalistic versioning library that allows other libraries to easily register themselves and upgrade. It is meant to be a cross-community library sharing system.

LibStub is hereby placed in the Public Domain

Credits: Kaelten, Cladhaire, ckknight, Mikk, Ammo, Nevcairiel, joshborke

LibStub-1.0 API

:GetLibrary(major [, silent])

Returns

The table instance of a registered library or nil if not found and the minor version of the library as the second return value.

Arguments

major
The name of the library you are requesting
silent
(Optional) Suppresses errors when the library is not found

:IterateLibraries()

Returns

An iterator over the registered major libraries.

:NewLibrary(major , minor)

Returns

The table to be used by the library as well as the minor version of the previously registered library, if any.

Arguments

major
The name of the library you are requesting
minor
The minor for the registering library


Who uses LibStub?

  • Ace3, for which it was originally designed
  • Rock, ckknight's framework
  • Norganna has announced that the next major version of Auctioneer will use it for its libraries
  • Some Dongle-related libraries use it.
  • ... and hopefully many more to come!

How to include LibStub in a library or addon

Library

if using the WoWAce repositories

  • setup an external pointing to svn://svn.wowace.com/wow/libstub/mainline/trunk in the .pkgmeta file
  • disable nolib creation by adding enable-nolib-creation: no to the .pkgmeta file
  • set up your <library>.toc file to load LibStub.lua (in case you support stand alone loading of the lib)
  • don't load LibStub via the xml file ment for embedded loading of your lib
  • don't set LibStub as X-embeded or OptDep

otherwise

  • get a copy of the current version
  • copy LibStub.lua into your library's folder
  • set up your <library>.toc file to load LibStub.lua (in case you support stand alone loading of the lib)
  • don't load LibStub via the xml file ment for embedded loading of your lib
  • don't set LibStub as X-embeded or OptDep

AddOn

if using the WoWAce repositories

  • set up an external pointing to svn://svn.wowace.com/wow/libstub/mainline/trunk in the .pkgmeta file
  • set up your <addon>.toc or embeds.xml file to load LibStub.lua
  • don't set LibStub as X-embeded or OptDep

otherwise

  • get a copy of the current version
  • copy LibStub.lua into your addon's folder or a subfolder of it
  • set up your <addon>.toc or embeds.xml file to load LibStub.lua
  • don't set LibStub as X-embeded or OptDep

Examples

Basic example

local lib = LibStub:NewLibrary("MyLibrary-1.0", 1)

if not lib then
  return	-- already loaded and no upgrade necessary
end

lib.somearray = lib.somearray or {}

if not lib.frame then
  lib.frame=CreateFrame("Frame")
end


function lib:SomeFunction()
  -- do stuff here
end

function lib:SomeOtherFunction()
  -- do other stuff here
end

local function OnUpdate()
  -- timing stuff here
end

lib.frame:SetScript("OnUpdate", OnUpdate);

Using revision control system tags for minor version

local lib = LibStub:NewLibrary("MyLibrary-1.0", "$Revision: 12345$")

Do be aware that moving a library from one repository to another will change revision numbers. Do not ever let it slide backwards. If you are caught in this situation, you might want to use something like:

local lib = LibStub:NewLibrary("MyLibrary-1.0", 
  12345+tonumber(strmatch("%d+","$Revision: 2$")) 
)

Embedding / Mixing in

This is a convention rather than a function of the specification, but all Ace3 and Rock related libraries use the following semantics for doing embedding / mixing in (specifically, libraries with an .Embed() member can be specified as embeds during addon object creation rather than having to embed them explicitly):

lib.mixinTargets = lib.mixinTargets or {}
local mixins = {"SomeFunction", "SomeOtherFunction" }

function lib:Embed(target)
  for _,name in pairs(mixins) do
    target[name] = lib[name]
  end
  lib.mixinTargets[target] = true
end

... and at the end of the file, we handle library upgrades by simply re-embedding the library in all positions where it has previously been embedded / mixed in:

for target,_ in pairs(mixinTargets) do
  lib:Embed(target)
end

------------------------------------------------------------------------
r104 | mikk | 2014-10-16 03:04:23 +0000 (Thu, 16 Oct 2014) | 1 line
Changed paths:
   A /tags/1.0.2.60000 (from /trunk:103)

Tagging as 1.0.2.60000
------------------------------------------------------------------------
r103 | mikk | 2014-10-16 03:02:50 +0000 (Thu, 16 Oct 2014) | 1 line
Changed paths:
   M /trunk/LibStub.lua
   M /trunk/LibStub.toc

TOC 60000
------------------------------------------------------------------------
r102 | kaelten | 2013-09-11 03:26:11 +0000 (Wed, 11 Sep 2013) | 1 line
Changed paths:
   M /trunk/LibStub.toc

Toc Update for 5.4
------------------------------------------------------------------------

Comments

Login to Curse

Don't have an account? Create One.

Get an epic experience with Curse Premium
  • Faster addon downloads
  • Premium-Only Beta Giveaways
  • Ad-Free Curse experience
  • Premium Curse Client
  • and many More Features
  • Learn More »

VIP trip to SMITE World Championship