Adds abstractions on waiting and once-ready EventEmitters.
authordsc <dsc@wikimedia.org>
Wed, 25 Apr 2012 01:05:36 +0000 (18:05 -0700)
committerdsc <dsc@wikimedia.org>
Wed, 25 Apr 2012 01:05:36 +0000 (18:05 -0700)
lib/util/event/index.co [new file with mode: 0644]
lib/util/event/ready-emitter.co [new file with mode: 0644]
lib/util/event/waiting-emitter.co [new file with mode: 0644]
lib/util/index.co
lib/util/wait-event.co [deleted file]
www/modules.yaml

diff --git a/lib/util/event/index.co b/lib/util/event/index.co
new file mode 100644 (file)
index 0000000..18c3e40
--- /dev/null
@@ -0,0 +1,2 @@
+exports.WaitingEmitter = require 'kraken/util/event/waiting-emitter'
+exports.ReadyEmitter   = require 'kraken/util/event/ready-emitter'
diff --git a/lib/util/event/ready-emitter.co b/lib/util/event/ready-emitter.co
new file mode 100644 (file)
index 0000000..fb24be4
--- /dev/null
@@ -0,0 +1,52 @@
+{EventEmitter} = require 'events'
+
+
+/**
+ * @class An EventEmitter that auto-triggers new handlers once "ready".
+ */
+class ReadyEmitter extends EventEmitter
+    readyEventName : 'ready'
+    ready : false
+    
+    /**
+     * Triggers the 'ready' event if it has not yet been triggered.
+     * Subsequent listeners added to this event will be auto-triggered.
+     * @returns {this}
+     */
+    triggerReady: ->
+        return this if @ready
+        @ready = true
+        @emit @readyEventName, this
+        this
+    
+    /**
+     * Resets the 'ready' event to its non-triggered state, firing a
+     * 'ready-reset' event.
+     * @returns {this}
+     */
+    resetReady: ->
+        return this unless @ready
+        @ready = false
+        @emit "#{@readyEventName}-reset", this
+        this
+    
+    
+    /**
+     * Wrap {@link EventEmitter#on} registration to handle registrations
+     * on 'ready' after we've broadcast the event. Handler will always still
+     * be registered, however, in case the emitter is reset.
+     * 
+     * @param {String} events Space-separated events for which to register.
+     * @param {Function} callback
+     * @param {Object} [context]
+     * @returns {this}
+     */
+    on: (events, callback, context=this) ->
+        return this if not callback
+        super ...
+        if @ready and -1 is not events.split(/\s+/).indexOf @readyEventName
+            callback.call context, this
+        this
+
+
+module.exports = exports = ReadyEmitter
diff --git a/lib/util/event/waiting-emitter.co b/lib/util/event/waiting-emitter.co
new file mode 100644 (file)
index 0000000..fc98aa6
--- /dev/null
@@ -0,0 +1,54 @@
+{EventEmitter} = require 'events'
+
+
+/**
+ * @class An EventEmitter with a ratchet-up waiting counter.
+ */
+class WaitingEmitter extends EventEmitter
+    
+    /**
+     * Count of outstanding tasks.
+     * @type Number
+     */
+    waitingOn : 0
+    
+    
+    /**
+     * Increment the waiting task counter.
+     * @returns {this}
+     */
+    wait: ->
+        count = @waitingOn
+        @waitingOn += 1
+        # console.log "#this.wait! #count --> #{@waitingOn}"
+        # console.trace()
+        @trigger('start-waiting', this) if count is 0 and @waitingOn > 0
+        this
+    
+    /**
+     * Decrement the waiting task counter.
+     * @returns {this}
+     */
+    unwait: ->
+        count = @waitingOn
+        @waitingOn -= 1
+        # console.warn "#this.unwait! #{@waitingOn} < 0" if @waitingOn < 0
+        # console.log "#this.unwait! #count --> #{@waitingOn}"
+        # console.trace()
+        @trigger('stop-waiting', this) if @waitingOn is 0 and count > 0
+        this
+    
+    /**
+     * @param {Function} fn Function to wrap.
+     * @returns {Function} A function wrapping the passed function with a call
+     *  to `unwait()`, then delegating with current context and arguments.
+     */
+    unwaitAnd: (fn) ->
+        self = this
+        ->
+            # console.log "#self.unwaitAnd( function #{fn.name or fn.displayName}() )"
+            # console.trace()
+            self.unwait(); fn ...
+    
+
+module.exports = exports = WaitingEmitter
index 1b5c01e..ddcacba 100644 (file)
@@ -26,6 +26,8 @@ root.jQuery?.fn.invoke = (method, ...args) ->
         jQuery(el)[method] ...args
 
 
+exports import require 'kraken/util/event'
+
 backbone = exports.backbone = require 'kraken/util/backbone'
 parser   = exports.parser   = require 'kraken/util/parser'
 Cascade  = exports.Cascade  = require 'kraken/util/cascade'
diff --git a/lib/util/wait-event.co b/lib/util/wait-event.co
deleted file mode 100644 (file)
index 96e2009..0000000
+++ /dev/null
@@ -1,101 +0,0 @@
-{EventEmitter} = require 'events'
-
-
-/**
- * @class An EventEmitter with a ratchet-up waiting counter.
- */
-class exports.WaitingEmitter extends EventEmitter
-    
-    /**
-     * Count of outstanding tasks.
-     * @type Number
-     */
-    waitingOn : 0
-    
-    
-    /**
-     * Increment the waiting task counter.
-     * @returns {this}
-     */
-    wait: ->
-        count = @waitingOn
-        @waitingOn += 1
-        # console.log "#this.wait! #count --> #{@waitingOn}"
-        # console.trace()
-        @trigger('start-waiting', this) if count is 0 and @waitingOn > 0
-        this
-    
-    /**
-     * Decrement the waiting task counter.
-     * @returns {this}
-     */
-    unwait: ->
-        count = @waitingOn
-        @waitingOn -= 1
-        # console.warn "#this.unwait! #{@waitingOn} < 0" if @waitingOn < 0
-        # console.log "#this.unwait! #count --> #{@waitingOn}"
-        # console.trace()
-        @trigger('stop-waiting', this) if @waitingOn is 0 and count > 0
-        this
-    
-    /**
-     * @param {Function} fn Function to wrap.
-     * @returns {Function} A function wrapping the passed function with a call
-     *  to `unwait()`, then delegating with current context and arguments.
-     */
-    unwaitAnd: (fn) ->
-        self = this
-        ->
-            # console.log "#self.unwaitAnd( function #{fn.name or fn.displayName}() )"
-            # console.trace()
-            self.unwait(); fn ...
-    
-
-
-/**
- * @class An EventEmitter that auto-triggers new handlers once "ready".
- */
-class exports.ReadyEmitter extends EventEmitter
-    readyEventName : 'ready'
-    ready : false
-    
-    /**
-     * Triggers the 'ready' event if it has not yet been triggered.
-     * Subsequent listeners added to this event will be auto-triggered.
-     * @returns {this}
-     */
-    triggerReady: ->
-        return this if @ready
-        @ready = true
-        @emit @readyEventName, this
-        this
-    
-    /**
-     * Resets the 'ready' event to its non-triggered state, firing the 
-     */
-    resetReady: ->
-        return this unless @ready
-        @ready = false
-        @emit "#{@readyEventName}-reset", this
-        this
-    
-    
-    /**
-     * Wrap {@link EventEmitter#on} registration to handle registrations
-     * on 'ready' after we've broadcast the event. Handler will always still
-     * be registered, however, in case the emitter is reset.
-     * 
-     * @param {String} events Space-separated events for which to register.
-     * @param {Function} callback
-     * @param {Object} [context]
-     * @returns {this}
-     */
-    on: (events, callback, context) ->
-        return this if not callback
-        super ...
-        if @ready and _.contains events.split(/\s+/), @readyEventName
-            callback.call context or this, this
-        this
-
-
-
index 4e3fb97..c553b9f 100644 (file)
@@ -21,8 +21,8 @@ dev:
         - jquery.spin.min
         - bootstrap.min
         
-        ### CommonJS Support Starts Here:
-        ### Browserify must come before any .mod files
+        ### CommonJS Support Starts Here
+        ### (this means Browserify must come before any .mod files)
         - browserify
         
         - underscore.mod
@@ -48,6 +48,10 @@ dev:
                     - kv
                     - string
                     - index
+                - event:
+                    - ready-emitter
+                    - waiting-emitter
+                    - index
                 - op
                 - backbone
                 - parser
@@ -60,8 +64,8 @@ dev:
             - base:
                 - base-mixin
                 - base-model
-                - data-binding
                 - base-view
+                - data-binding
                 - model-cache
                 - cascading-model
                 - index