Permalink
Browse files

allow chaining for emitters

  • Loading branch information...
1 parent c52ea76 commit b2c52b8df9d86e0292c510ed2a40c1672646f097 @dvv dvv committed Mar 13, 2012
Showing with 34 additions and 1 deletion.
  1. +3 −1 lib/luvit/core.lua
  2. +31 −0 tests/test-emitter.lua
View
@@ -126,6 +126,7 @@ function Emitter:once(name, callback)
callback(...)
end
self:on(name, wrapped)
+ return self
end
-- Adds an event listener (`callback`) for the named event `name`.
@@ -144,6 +145,7 @@ function Emitter:on(name, callback)
rawset(handlers, name, handlers_for_type)
end
table.insert(handlers_for_type, callback)
+ return self
end
-- Emit a named event to all listeners with optional data argument(s).
@@ -166,7 +168,7 @@ function Emitter:emit(name, ...)
table.remove(handlers_for_type, i)
end
end
-
+ return self
end
-- Remove a listener so that it no longer catches events.
View
@@ -0,0 +1,31 @@
+--[[
+
+Copyright 2012 The Luvit Authors. All Rights Reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS-IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+--]]
+
+require("helper")
+
+--
+-- chaining works
+--
+require('core').Emitter:new()
+ :on("foo", function (x)
+ assert(deep_equal(x, { a = "b" }))
+ process.exit(0)
+ end)
+ :emit("foo", { a = "b" })
+
+assert(false)

0 comments on commit b2c52b8

Please sign in to comment.