opal/browser/event/base.rb in opal-browser-0.2.0 vs opal/browser/event/base.rb in opal-browser-0.3.0

- old
+ new

@@ -1,13 +1,13 @@ module Browser class Event - include Native + include Native::Wrapper # @see https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events class Definition - include Native + include Native::Wrapper # @private def self.new(&block) data = super(`{ bubbles: true, cancelable: true }`) block.call(data) if block @@ -268,10 +268,41 @@ def attach!(*) raise NotImplementedError end end + # @overload one(name, &block) + # + # Start listening for an event on the target. Remove the event after firing + # so that it is fired at most once. + # + # @param name [String] the event name + # + # @yieldparam event [Event] the event + # + # @return [Callback] + # + # @overload one(name, selector, &block) + # + # Start listening for an event on the target children. Remove the event after + # firing so that it is fired at most once. + # + # @param name [String] the event name + # @param selector [String] the CSS selector to trigger the event on + # + # @yieldparam event [Event] the event + # + # @return [Delegate] + def one (name, selector = nil, &block) + raise ArgumentError, 'no block has been given' unless block + + cb = on name, selector do |*args| + out = block.call(*args) + cb.off + out + end + end # @overload off() # Stop listening for any event. # # @overload off(what) # Stop listening for an event. @@ -342,11 +373,11 @@ end end # Trigger an event on the target. # - # @param name [String] the event name + # @param event [String] the event name # @param args [Array] optional arguments to the event callback # # @yieldparam definition [Definition] definition to customize the event def trigger(event, *args, &block) if event.is_a? String @@ -356,10 +387,10 @@ dispatch(event) end # Trigger an event on the target without bubbling. # - # @param name [String] the event name + # @param event [String] the event name # @param args [Array] optional arguments to the event callback # # @yieldparam definition [Definition] definition to customize the event def trigger!(event, *args, &block) trigger event, *args do |e|