{ "source": "doc/api/core.markdown", "modules": [ { "textRaw": "Core", "name": "core", "desc": "
Core object model for luvit using simple prototypes and inheritance. We support single inheritance for classes.\n\n
\n", "methods": [ { "textRaw": "core.instanceof(obj, class)", "type": "method", "name": "instanceof", "desc": "Given an object which inherits from a class, and a class, returns whether the object is an instance of that class.\n\n
\n> em = core.Emitter:new()\n> core.instanceof(em, core.Emitter)\ntrue
\n",
"signatures": [
{
"params": [
{
"name": "obj"
},
{
"name": "class"
}
]
}
]
}
],
"classes": [
{
"textRaw": "Class: core.Object",
"type": "class",
"name": "core.Object",
"desc": "The base object class. It provides simple prototypal inheritance and inheritable constructors. \nAll other objects inherit from this.\n\n
\n", "methods": [ { "textRaw": "Object:create()", "type": "method", "name": "Object:create", "desc": "Creates a new instance of the base object\n\n
\n", "signatures": [ { "params": [] } ] }, { "textRaw": "Object:new(...)", "type": "method", "name": "Object:new", "desc": "Creates a new instance and calls obj:initialize(...)
if it exists.\n
local Rectangle = Object:extend()\nfunction Rectangle:initialize(w, h)\n self.w = w\n self.h = h\nend\nfunction Rectangle:getArea()\n return self.w * self.h\nend\nlocal rect = Rectangle:new(3, 4)\np(rect:getArea())
\n",
"signatures": [
{
"params": [
{
"name": "..."
}
]
}
]
},
{
"textRaw": "Object:extend()",
"type": "method",
"name": "Object:extend",
"desc": "Creates a new sub-class.\n\n
\nlocal Square = Rectangle:extend()\nfunction Square:initialize(w)\n self.w = w\n self.h = h\nend
\n",
"signatures": [
{
"params": []
}
]
}
]
}
],
"properties": [
{
"textRaw": "core.Emitter",
"name": "Emitter",
"desc": "This class can be used directly whenever an event emitter is needed.\n
\nlocal emitter = Emitter:new()\nemitter:on('foo', p)\nemitter:emit('foo', 1, 2, 3)
\nAlso it can easily be sub-classed.\n
\nlocal Custom = Emitter:extend()\nlocal c = Custom:new()\nc:on('bar', onBar)
\nUnlike EventEmitter in node.js, Emitter class doesn't auto binds self
\nreference. This means, if a callback handler is expecting a self
reference,\nutils.bind() should be used, and the callback handler should have a self
at\nthe beginning its parameter list.\n
function some_func(self, a, b, c) end\nemitter:on('end', utils.bind(some_func, emitter))\nemitter:emit('end', 'a', 'b', 'c')
\n",
"methods": [
{
"textRaw": "Emitter:on(name. callback)",
"type": "method",
"name": "Emitter:on",
"desc": "Adds an event listener (callback
) for the named event name
.\n\n
em = Emitter:new()\nem:on('data', function(data) print(data) end)
\n",
"signatures": [
{
"params": [
{
"name": "name. callback"
}
]
}
]
},
{
"textRaw": "Emitter:once(name, callback)",
"type": "method",
"name": "Emitter:once",
"desc": "Same as Emitter:on
except it de-registers itself after the first event.\n\n
Returns the count of the listeners bound to an event with the name name
\n\n
Emit a named event to all listeners with optional data arguments\n\n
\n> em = Emitter:new()\n> em:on('data', function(data) print(data) end)\n> em:emit('data', 'foo')\n'foo'
\n",
"signatures": [
{
"params": [
{
"name": "name"
},
{
"name": "..."
}
]
}
]
},
{
"textRaw": "Emitter:removeListener(name, callback)",
"type": "method",
"name": "Emitter:removeListener",
"desc": "Removes a listener so it no longer catches events\n\n
\n> em = Emitter:new()\n> em:on('data', function(data) print(data) end)\n> em:emit('data', 'foo')\nfoo\n> em:removeListener('data', function() em:emit('data', 'foo') end)\n> -- nothing printed
\n",
"signatures": [
{
"params": [
{
"name": "name"
},
{
"name": "callback"
}
]
}
]
},
{
"textRaw": "Emitter:removeAllListeners(name)",
"type": "method",
"name": "Emitter:removeAllListeners",
"desc": "Removes all listeners. Name is optional and if supplied will make it act like removeListener\n\n
\n", "signatures": [ { "params": [ { "name": "name" } ] } ] }, { "textRaw": "Emitter:listeners(name)", "type": "method", "name": "Emitter:listeners", "desc": "Returns listeners for the event with name name
\n\n
Utility that binds the named method self[name]
for use as a callback. The\nfirst argument (err
) is re-routed to the "error" event instead.\n\n\n
local Joystick = Emitter:extend()\nfunction Joystick:initialize(device)\n self:wrap("onOpen")\n FS.open(device, self.onOpen)\nend\nfunction Joystick:onOpen(fd)\n -- and so forth\nend
\n",
"signatures": [
{
"params": [
{
"name": "name"
}
]
}
]
},
{
"textRaw": "Emitter:propagate(eventName, target)",
"type": "method",
"name": "Emitter:propagate",
"desc": "Propagate the event to another emitter.\n\n
\n", "signatures": [ { "params": [ { "name": "eventName" }, { "name": "target" } ] } ] } ] }, { "textRaw": "core.Error", "name": "Error", "desc": "This is for code that wants structured error messages.\n\n
\n", "methods": [ { "textRaw": "Error:initialize(message)", "type": "method", "name": "Error:initialize", "desc": "How to handle strings\n
\n", "signatures": [ { "params": [ { "name": "message" } ] } ] } ] } ], "type": "module", "displayName": "Core" } ] }