AsyncQueue Class
A specialized queue class that supports scheduling callbacks to execute sequentially, iteratively, even asynchronously.
Callbacks can be function refs or objects with the following keys. Only
the fn
key is required.
fn
-- The callback functioncontext
-- The execution context for the callbackFn.args
-- Arguments to pass to callbackFn.timeout
-- Millisecond delay before executing callbackFn. (Applies to each iterative execution of callback)iterations
-- Number of times to repeat the callback.until
-- Repeat the callback until this function returns true. This setting trumps iterations.autoContinue
-- Set to false to prevent the AsyncQueue from executing the next callback in the Queue after the callback completes.id
-- Name that can be used to get, promote, get the indexOf, or delete this callback.
Constructor
Item Index
Methods
- _defAddFn
- _defExecFn
- _defPromoteFn
- _defRemoveFn
- _defShiftFn
- _execute
- _getFullType
- _getType
- _hasPotentialSubscribers
- _init
- _initEvents
- _monitor
- _parseType
- _prepare
- _publish
- _schedule
- add
- addTarget
- after
- before
- bubble
- detach
- detachAll
- fire
- getCallback
- getEvent
- getTargets
- indexOf
- isRunning
- next
- on
- once
- onceAfter
- parseType
- pause
- promote
- publish
- remove
- removeTarget
- run
- size
- stop
- subscribe deprecated
- unsubscribe deprecated
- unsubscribeAll deprecated
Events
Methods
_defAddFn
-
e
Default functionality for the "add" event. Adds the callbacks
in the event facade to the queue. Callbacks successfully added to the
queue are present in the event's added
property in the
after phase.
Parameters:
-
e
Eventthe event object
_defExecFn
-
e
Default functionality for the "execute" event. Executes the callback function
Parameters:
-
e
Eventthe event object
_defPromoteFn
-
e
Default functionality for the "promote" event. Promotes the named callback to the head of the queue.
The event object will contain a property "callback", which holds the id of a callback or the callback object itself.
Parameters:
-
e
Eventthe custom event
_defRemoveFn
-
e
Default functionality for the "remove" event. Removes the callback from the queue.
The event object will contain a property "callback", which holds the id of a callback or the callback object itself.
Parameters:
-
e
Eventthe custom event
_defShiftFn
-
e
Default functionality for the "shift" event. Shifts the callback stored in the event object's callback property from the queue if it is the first item.
Parameters:
-
e
EventThe event object
_execute
-
callback
Handles the execution of callbacks. Returns a boolean indicating whether it is appropriate to continue running.
Parameters:
-
callback
Objectthe callback object to execute
Returns:
whether the run loop should continue
_getFullType
-
type
Returns the fully qualified type, given a short type string. That is, returns "foo:bar" when given "bar" if "foo" is the configured prefix.
NOTE: This method, unlike _getType, does no checking of the value passed in, and is designed to be used with the low level _publish() method, for critical path implementations which need to fast-track publish for performance reasons.
Parameters:
-
type
StringThe short type to prefix
Returns:
The prefixed type, if a prefix is set, otherwise the type passed in
_getType
()
private
If the instance has a prefix attribute and the event type is not prefixed, the instance prefix is applied to the supplied type.
_hasPotentialSubscribers
-
fullType
Parameters:
-
fullType
StringThe fully prefixed type name
Returns:
Whether the event has potential subscribers or not
_init
()
protected
Initializes the AsyncQueue instance properties and events.
_initEvents
()
protected
Initializes the instance events.
_monitor
-
what
-
eventType
-
o
This is the entry point for the event monitoring system. You can monitor 'attach', 'detach', 'fire', and 'publish'. When configured, these events generate an event. click -> click_attach, click_detach, click_publish -- these can be subscribed to like other events to monitor the event system. Inividual published events can have monitoring turned on or off (publish can't be turned off before it it published) by setting the events 'monitor' config.
Parameters:
-
what
String'attach', 'detach', 'fire', or 'publish'
-
eventType
String | CustomEventThe prefixed name of the event being monitored, or the CustomEvent object.
-
o
ObjectInformation about the event interaction, such as fire() args, subscription category, publish config
_parseType
()
private
Returns an array with the detach key (if provided), and the prefixed event name from _getType Y.on('detachcategory| menu:click', fn)
_prepare
-
callback
Creates a wrapper function to execute the callback using the aggregated configuration generated by combining the static AsyncQueue.defaults, the instance defaults, and the specified callback settings.
The wrapper function is decorated with the callback configuration as properties for runtime modification.
Returns:
a decorated function wrapper to execute the callback
_publish
-
fullType
-
etOpts
-
ceOpts
The low level event publish implementation. It expects all the massaging to have been done
outside of this method. e.g. the type
to fullType
conversion. It's designed to be a fast
path publish, which can be used by critical code paths to improve performance.
Parameters:
Returns:
The published event. If called without etOpts
or ceOpts
, this will
be the default CustomEvent
instance, and can be configured independently.
_schedule
-
callback
Schedules the execution of asynchronous callbacks.
Parameters:
-
callback
Objectthe callback object to execute
Returns:
whether the run loop should continue
add
-
callback
Add any number of callbacks to the end of the queue. Callbacks may be provided as functions or objects.
Returns:
the AsyncQueue instance
addTarget
-
o
Registers another EventTarget as a bubble target. Bubble order is determined by the order registered. Multiple targets can be specified.
Events can only bubble if emitFacade is true.
Included in the event-custom-complex submodule.
Parameters:
-
o
EventTargetthe target to add
after
-
type
-
fn
-
[context]
-
[arg*]
Subscribe to a custom event hosted by this object. The supplied callback will execute after any listeners add via the subscribe method, and after the default function, if configured for the event, has executed.
Parameters:
Returns:
A subscription handle capable of detaching the subscription
before
()
Executes the callback before a DOM event, custom event or method. If the first argument is a function, it is assumed the target is a method. For DOM and custom events, this is an alias for Y.on.
For DOM and custom events: type, callback, context, 0-n arguments
For methods: callback, object (method host), methodName, context, 0-n arguments
Returns:
detach handle
bubble
-
evt
Propagate an event. Requires the event-custom-complex module.
Parameters:
-
evt
CustomEventthe custom event to propagate
Returns:
the aggregated return value from Event.Custom.fire
detach
-
type
-
fn
-
context
Detach one or more listeners the from the specified event
Parameters:
-
type
String | ObjectEither the handle to the subscriber or the type of event. If the type is not specified, it will attempt to remove the listener from all hosted events.
-
fn
FunctionThe subscribed function to unsubscribe, if not supplied, all subscribers will be removed.
-
context
ObjectThe custom object passed to subscribe. This is optional, but if supplied will be used to disambiguate multiple listeners that are the same (e.g., you subscribe many object using a function that lives on the prototype)
Returns:
the host
detachAll
-
type
Removes all listeners from the specified event. If the event type is not specified, all listeners from all hosted custom events will be removed.
Parameters:
-
type
StringThe type, or name of the event
fire
-
type
-
arguments
Fire a custom event by name. The callback functions will be executed from the context specified when the event was created, and with the following parameters.
The first argument is the event type, and any additional arguments are passed to the listeners as parameters. If the first of these is an object literal, and the event is configured to emit an event facade, that object is mixed into the event facade and the facade is provided in place of the original object.
If the custom event object hasn't been created, then the event hasn't been published and it has no subscribers. For performance sake, we immediate exit in this case. This means the event won't bubble, so if the intention is that a bubble target be notified, the event must be published on this object first.
Parameters:
-
type
String | ObjectThe type of the event, or an object that contains a 'type' property.
-
arguments
Object*an arbitrary set of parameters to pass to the handler. If the first of these is an object literal and the event is configured to emit an event facade, the event facade will replace that parameter after the properties the object literal contains are copied to the event facade.
Returns:
True if the whole lifecycle of the event went through, false if at any point the event propagation was halted.
getCallback
-
id
Retrieve a callback by its id. Useful to modify the configuration while the queue is running.
Parameters:
-
id
Stringthe id assigned to the callback
Returns:
the callback object
getEvent
-
type
-
prefixed
Returns the custom event of the provided type has been created, a falsy value otherwise
Parameters:
Returns:
the custom event or null
getTargets
()
Returns an array of bubble targets for this object.
Returns:
EventTarget[]
indexOf
-
callback
Returns the current index of a callback. Pass in either the id or callback function from getCallback.
Returns:
index of the callback or -1 if not found
isRunning
()
Boolean
Determines if the queue is waiting for a callback to complete execution.
Returns:
true if queue is waiting for a from any initiated transactions
next
()
Function
Returns the next callback needing execution. If a callback is configured to repeat via iterations or until, it will be returned until the completion criteria is met.
When the queue is empty, null is returned.
Returns:
the callback to execute
on
-
type
-
fn
-
[context]
-
[arg*]
Subscribe a callback function to a custom event fired by this object or from an object that bubbles its events to this object.
this.on("change", this._onChange, this);
Callback functions for events published with emitFacade = true
will
receive an EventFacade
as the first argument (typically named "e").
These callbacks can then call e.preventDefault()
to disable the
behavior published to that event's defaultFn
. See the EventFacade
API for all available properties and methods. Subscribers to
non-emitFacade
events will receive the arguments passed to fire()
after the event name.
To subscribe to multiple events at once, pass an object as the first argument, where the key:value pairs correspond to the eventName:callback.
this.on({
"attrChange" : this._onAttrChange,
"change" : this._onChange
});
You can also pass an array of event names as the first argument to subscribe to all listed events with the same callback.
this.on([ "change", "attrChange" ], this._onChange);
Returning false
from a callback is supported as an alternative to
calling e.preventDefault(); e.stopPropagation();
. However, it is
recommended to use the event methods whenever possible.
Parameters:
Returns:
A subscription handle capable of detaching that subscription
once
-
type
-
fn
-
[context]
-
[arg*]
Listen to a custom event hosted by this object one time.
This is the equivalent to on
except the
listener is immediatelly detached when it is executed.
Parameters:
Returns:
A subscription handle capable of detaching the subscription
onceAfter
-
type
-
fn
-
[context]
-
[arg*]
Listen to a custom event hosted by this object one time.
This is the equivalent to after
except the
listener is immediatelly detached when it is executed.
Parameters:
Returns:
A subscription handle capable of detaching that subscription
parseType
-
type
-
[pre]
Takes the type parameter passed to 'on' and parses out the various pieces that could be included in the type. If the event type is passed without a prefix, it will be expanded to include the prefix one is supplied or the event target is configured with a default prefix.
Parameters:
Returns:
an array containing:
- the detach category, if supplied,
- the prefixed event type,
- whether or not this is an after listener,
- the supplied event type
pause
()
AsyncQueue
chainable
Pause the execution of the queue after the execution of the current callback completes. If called from code outside of a queued callback, clears the timeout for the pending callback. Paused queue can be restarted with q.run()
Returns:
the AsyncQueue instance
promote
-
callback
Promotes the named callback to the top of the queue. If a callback is currently executing or looping (via until or iterations), the promotion is scheduled to occur after the current callback has completed.
Returns:
the AsyncQueue instance
publish
-
type
-
opts
Creates a new custom event of the specified type. If a custom event by that name already exists, it will not be re-created. In either case the custom event is returned.
Parameters:
-
type
Stringthe type, or name of the event
-
opts
Objectoptional config params. Valid properties are:
-
[broadcast=false]
Boolean optionalwhether or not the YUI instance and YUI global are notified when the event is fired.
-
[bubbles=true]
Boolean optionalWhether or not this event bubbles. Events can only bubble if
emitFacade
is true. -
[context=this]
Object optionalthe default execution context for the listeners.
-
[defaultFn]
Function optionalthe default function to execute when this event fires if preventDefault was not called.
-
[emitFacade=false]
Boolean optionalwhether or not this event emits a facade.
-
[prefix]
String optionalthe prefix for this targets events, e.g., 'menu' in 'menu:click'.
-
[fireOnce=false]
Boolean optionalif an event is configured to fire once, new subscribers after the fire will be notified immediately.
-
[async=false]
Boolean optionalfireOnce event listeners will fire synchronously if the event has already fired unless
async
istrue
. -
[preventable=true]
Boolean optionalwhether or not
preventDefault()
has an effect. -
[preventedFn]
Function optionala function that is executed when
preventDefault()
is called. -
[queuable=false]
Boolean optionalwhether or not this event can be queued during bubbling.
-
[silent]
Boolean optionalif silent is true, debug messages are not provided for this event.
-
[stoppedFn]
Function optionala function that is executed when stopPropagation is called.
-
[monitored]
Boolean optionalspecifies whether or not this event should send notifications about when the event has been attached, detached, or published.
-
[type]
String optionalthe event type (valid option if not provided as the first parameter to publish).
-
Returns:
the custom event
remove
-
callback
Removes the callback from the queue. If the queue is active, the removal is scheduled to occur after the current callback has completed.
Returns:
the AsyncQueue instance
run
()
AsyncQueue
chainable
Sets the queue in motion. All queued callbacks will be executed in order unless pause() or stop() is called or if one of the callbacks is configured with autoContinue: false.
Returns:
the AsyncQueue instance
stop
()
AsyncQueue
chainable
Stop and clear the queue after the current execution of the current callback completes.
Returns:
the AsyncQueue instance
subscribe
()
deprecated
subscribe to an event
unsubscribe
()
deprecated
detach a listener
unsubscribeAll
-
type
Removes all listeners from the specified event. If the event type is not specified, all listeners from all hosted custom events will be removed.
Parameters:
-
type
StringThe type, or name of the event
Properties
_running
Boolean | Object true for synchronous callback execution, the
return handle from Y.later for async callbacks.
Otherwise false.
protected
Used to indicate the queue is currently executing a callback.
defaults
Object
static
Static default values used to populate callback configuration properties. Preconfigured defaults include:
autoContinue
:true
iterations
: 1timeout
: 10 (10ms between callbacks)until
: (function to run until iterations <= 0)
defaults
Object
Callback defaults for this instance. Static defaults that are not overridden are also included.
Events
complete
Event fired when there is no remaining callback in the running queue. Also fired after stop().