From 9d822bc1c13dd3393b418210a99537c22c06f2c3 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Mon, 9 Jan 2017 11:33:39 -0800 Subject: [PATCH] Callbacks: Prevent add() from unlocking with-memory lists Fixes gh-3469 Closes gh-3470 --- src/callbacks.js | 2 +- test/unit/callbacks.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/callbacks.js b/src/callbacks.js index a6d4df03f..8831e531a 100644 --- a/src/callbacks.js +++ b/src/callbacks.js @@ -69,7 +69,7 @@ jQuery.Callbacks = function( options ) { fire = function() { // Enforce single-firing - locked = options.once; + locked = locked || options.once; // Execute callbacks for all pending executions, // respecting firingIndex overrides and runtime changes diff --git a/test/unit/callbacks.js b/test/unit/callbacks.js index c6c379ded..04d44443d 100644 --- a/test/unit/callbacks.js +++ b/test/unit/callbacks.js @@ -366,3 +366,24 @@ QUnit.test( "jQuery.Callbacks() - disabled callback doesn't fire (gh-1790)", fun cb.fire(); assert.ok( !fired, "Disabled callback function didn't fire" ); } ); + +QUnit.test( "jQuery.Callbacks() - list with memory stays locked (gh-3469)", function( assert ) { + + assert.expect( 3 ); + + var cb = jQuery.Callbacks( "memory" ), + fired = 0, + count1 = function() { fired += 1; }, + count2 = function() { fired += 10; }; + + cb.add( count1 ); + cb.fire(); + assert.equal( fired, 1, "Pre-lock() fire" ); + + cb.lock(); + cb.add( count2 ); + assert.equal( fired, 11, "Post-lock() add" ); + + cb.fire(); + assert.equal( fired, 11, "Post-lock() fire ignored" ); +} ); -- 2.39.5