Build: migrate grunt authors to a custom script
[jquery.git] / src / queue.js
blob3621eaf45130c137307ec75802ca17c9d96b783d
1 import { jQuery } from "./core.js";
2 import { dataPriv } from "./data/var/dataPriv.js";
4 import "./deferred.js";
5 import "./callbacks.js";
7 jQuery.extend( {
8 queue: function( elem, type, data ) {
9 var queue;
11 if ( elem ) {
12 type = ( type || "fx" ) + "queue";
13 queue = dataPriv.get( elem, type );
15 // Speed up dequeue by getting out quickly if this is just a lookup
16 if ( data ) {
17 if ( !queue || Array.isArray( data ) ) {
18 queue = dataPriv.access( elem, type, jQuery.makeArray( data ) );
19 } else {
20 queue.push( data );
23 return queue || [];
27 dequeue: function( elem, type ) {
28 type = type || "fx";
30 var queue = jQuery.queue( elem, type ),
31 startLength = queue.length,
32 fn = queue.shift(),
33 hooks = jQuery._queueHooks( elem, type ),
34 next = function() {
35 jQuery.dequeue( elem, type );
38 // If the fx queue is dequeued, always remove the progress sentinel
39 if ( fn === "inprogress" ) {
40 fn = queue.shift();
41 startLength--;
44 if ( fn ) {
46 // Add a progress sentinel to prevent the fx queue from being
47 // automatically dequeued
48 if ( type === "fx" ) {
49 queue.unshift( "inprogress" );
52 // Clear up the last queue stop function
53 delete hooks.stop;
54 fn.call( elem, next, hooks );
57 if ( !startLength && hooks ) {
58 hooks.empty.fire();
62 // Not public - generate a queueHooks object, or return the current one
63 _queueHooks: function( elem, type ) {
64 var key = type + "queueHooks";
65 return dataPriv.get( elem, key ) || dataPriv.access( elem, key, {
66 empty: jQuery.Callbacks( "once memory" ).add( function() {
67 dataPriv.remove( elem, [ type + "queue", key ] );
68 } )
69 } );
71 } );
73 jQuery.fn.extend( {
74 queue: function( type, data ) {
75 var setter = 2;
77 if ( typeof type !== "string" ) {
78 data = type;
79 type = "fx";
80 setter--;
83 if ( arguments.length < setter ) {
84 return jQuery.queue( this[ 0 ], type );
87 return data === undefined ?
88 this :
89 this.each( function() {
90 var queue = jQuery.queue( this, type, data );
92 // Ensure a hooks for this queue
93 jQuery._queueHooks( this, type );
95 if ( type === "fx" && queue[ 0 ] !== "inprogress" ) {
96 jQuery.dequeue( this, type );
98 } );
100 dequeue: function( type ) {
101 return this.each( function() {
102 jQuery.dequeue( this, type );
103 } );
105 clearQueue: function( type ) {
106 return this.queue( type || "fx", [] );
109 // Get a promise resolved when queues of a certain type
110 // are emptied (fx is the type by default)
111 promise: function( type, obj ) {
112 var tmp,
113 count = 1,
114 defer = jQuery.Deferred(),
115 elements = this,
116 i = this.length,
117 resolve = function() {
118 if ( !( --count ) ) {
119 defer.resolveWith( elements, [ elements ] );
123 if ( typeof type !== "string" ) {
124 obj = type;
125 type = undefined;
127 type = type || "fx";
129 while ( i-- ) {
130 tmp = dataPriv.get( elements[ i ], type + "queueHooks" );
131 if ( tmp && tmp.empty ) {
132 count++;
133 tmp.empty.add( resolve );
136 resolve();
137 return defer.promise( obj );
139 } );
141 export { jQuery, jQuery as $ };