util.xpcall for lua5.1;

This commit is contained in:
bjorn 2015-11-16 21:00:26 -08:00
parent 124252bb12
commit 3f3f18dd72
2 changed files with 14 additions and 2 deletions

8
rx.lua
View File

@ -6,6 +6,12 @@ local util = {}
util.pack = table.pack or function(...) return { n = select('#', ...), ... } end util.pack = table.pack or function(...) return { n = select('#', ...), ... } end
util.unpack = table.unpack or unpack util.unpack = table.unpack or unpack
util.xpcall = function(fn, err, ...)
local arg = util.pack(...)
return xpcall(function()
fn(util.unpack(arg))
end, err)
end
util.eq = function(x, y) return x == y end util.eq = function(x, y) return x == y end
util.noop = function() end util.noop = function() end
util.identity = function(x) return x end util.identity = function(x) return x end
@ -14,7 +20,7 @@ util.isa = function(object, class)
return type(object) == 'table' and getmetatable(object).__index == class return type(object) == 'table' and getmetatable(object).__index == class
end end
util.tryWithObserver = function(observer, fn, ...) util.tryWithObserver = function(observer, fn, ...)
return xpcall(fn, function(...) return util.xpcall(fn, function(...)
return observer:onError(...) return observer:onError(...)
end, ...) end, ...)
end end

View File

@ -2,6 +2,12 @@ local util = {}
util.pack = table.pack or function(...) return { n = select('#', ...), ... } end util.pack = table.pack or function(...) return { n = select('#', ...), ... } end
util.unpack = table.unpack or unpack util.unpack = table.unpack or unpack
util.xpcall = function(fn, err, ...)
local arg = util.pack(...)
return xpcall(function()
fn(util.unpack(arg))
end, err)
end
util.eq = function(x, y) return x == y end util.eq = function(x, y) return x == y end
util.noop = function() end util.noop = function() end
util.identity = function(x) return x end util.identity = function(x) return x end
@ -10,7 +16,7 @@ util.isa = function(object, class)
return type(object) == 'table' and getmetatable(object).__index == class return type(object) == 'table' and getmetatable(object).__index == class
end end
util.tryWithObserver = function(observer, fn, ...) util.tryWithObserver = function(observer, fn, ...)
return xpcall(fn, function(...) return util.xpcall(fn, function(...)
return observer:onError(...) return observer:onError(...)
end, ...) end, ...)
end end