From de00939d8d23ce1fbfa5d21492217991aad84bcb Mon Sep 17 00:00:00 2001 From: Aiosa Date: Sat, 21 Jan 2023 09:00:24 +0100 Subject: [PATCH] Revert async support and event breaking support in EventSource. --- src/eventsource.js | 41 ++---------- src/openseadragon.js | 18 ----- test/modules/event-source.js | 124 +---------------------------------- 3 files changed, 8 insertions(+), 175 deletions(-) diff --git a/src/eventsource.js b/src/eventsource.js index c367eb83..f92adb67 100644 --- a/src/eventsource.js +++ b/src/eventsource.js @@ -40,7 +40,6 @@ * @callback EventHandler * @memberof OpenSeadragon * @param {Object} event - See individual events for event-specific properties. - * @return {undefined|Promise} */ @@ -165,10 +164,8 @@ $.EventSource.prototype = { * Get a function which iterates the list of all handlers registered for a given event, calling the handler for each. * @function * @param {String} eventName - Name of event to get handlers for. - * @param {boolean} waitForPromiseHandlers - true to wait for asynchronous functions (promises are returned) - * or plain functions that return promise */ - getHandler: function ( eventName, waitForPromiseHandlers) { + getHandler: function ( eventName) { var events = this.events[ eventName ]; if ( !events || !events.length ) { return null; @@ -176,31 +173,11 @@ $.EventSource.prototype = { events = events.length === 1 ? [ events[ 0 ] ] : Array.apply( null, events ); - return waitForPromiseHandlers ? function ( source, args ) { - var length = events.length; - function loop(index) { - if ( index >= length || !events[ index ] ) { - return $.Promise.resolve(); - } - args.stopPropagation = function () { - index = length; - }; - args.eventSource = source; - args.userData = events[ index ].userData; - var result = events[ index ].handler( args ); - result = (!result || $.type(result) !== "promise") ? $.Promise.resolve() : result; - return result.then(function () { - loop(index + 1); - }); - } - return loop(0); - } : function ( source, args ) { + return function ( source, args ) { var i, - length = events.length, - stop = function () { i = length; }; + length = events.length; for ( i = 0; i < length; i++ ) { if ( events[ i ] ) { - args.stopPropagation = stop; args.eventSource = source; args.userData = events[ i ].userData; events[ i ].handler( args ); @@ -214,22 +191,14 @@ $.EventSource.prototype = { * @function * @param {String} eventName - Name of event to register. * @param {Object} eventArgs - Event-specific data. - * @param {boolean} eventArgs.waitForPromiseHandlers - Synchronizes asynchronous-like handlers - * @return {undefined|Promise} - A promise is returned in case waitForPromiseHandlers = true */ raiseEvent: function( eventName, eventArgs ) { //uncomment if you want to get a log of all events //$.console.log( eventName ); - var awaits = (eventArgs && eventArgs.waitForPromiseHandlers) || false, - handler = this.getHandler( eventName, awaits ); - + var handler = this.getHandler( eventName ); if ( handler ) { - if ( !eventArgs ) { - eventArgs = {}; - } - - return handler( this, eventArgs ); + return handler( this, eventArgs || {} ); } return undefined; } diff --git a/src/openseadragon.js b/src/openseadragon.js index b3ee4dbb..cb111195 100644 --- a/src/openseadragon.js +++ b/src/openseadragon.js @@ -851,24 +851,6 @@ function OpenSeadragon( options ){ return $.type(obj) === "function"; }; - /** - * Promise proxy in OpenSeadragon, can be removed once IE11 support is dropped - * @type {PromiseConstructor|(function())|*} - */ - $.Promise = (function () { - if (window.Promise) { - return window.Promise; - } - var promise = function () {}; - promise.prototype.then = function () { - throw "OpenSeadragon needs promises API. Your browser do not support promises. You can add polyfill.js to import promises."; - }; - promise.prototype.resolve = function () { - throw "OpenSeadragon needs promises API. Your browser do not support promises. You can add polyfill.js to import promises."; - }; - return promise; - })(); - /** * Taken from jQuery 1.6.1 * @function isArray diff --git a/test/modules/event-source.js b/test/modules/event-source.js index 3eb23851..2f61e5ee 100644 --- a/test/modules/event-source.js +++ b/test/modules/event-source.js @@ -6,19 +6,15 @@ function evaluateTest(e) { if (finished) return; finished = true; - e.stopPropagation(); e.assert.strictEqual(JSON.stringify(result), JSON.stringify(e.expected), e.message); e.done(); } - function executor(i, ms, breaks=false) { + function executor(i, ms) { if (ms === undefined) return function (e) { eventCounter++; result.push(i); - if (breaks) { - e.stopPropagation(); - evaluateTest(e); - } else if (eventCounter === context.numberOfHandlers(eName)) { + if (eventCounter === context.numberOfHandlers(eName)) { evaluateTest(e); } }; @@ -28,10 +24,7 @@ setTimeout(function () { eventCounter++; result.push(i); - if (breaks) { - e.stopPropagation(); - evaluateTest(e); - } else if (eventCounter === context.numberOfHandlers(eName)) { + if (eventCounter === context.numberOfHandlers(eName)) { evaluateTest(e); } resolve(); @@ -76,18 +69,6 @@ }); }); - QUnit.test('EventSource: simple callbacks order with break', function(assert) { - context.addHandler(eName, executor(1)); - context.addHandler(eName, executor(2, undefined, true)); - context.addHandler(eName, executor(3)); - runTest({ - assert: assert, - done: assert.async(), - expected: [1, 2], - message: 'Simple callback order should follow [1,2] since 2 breaks the event.' - }); - }); - QUnit.test('EventSource: priority callbacks order', function(assert) { context.addHandler(eName, executor(1), undefined, 20); context.addHandler(eName, executor(2), undefined, 124); @@ -101,103 +82,4 @@ message: 'Prioritized callback order should follow [2,1,4,5,3].' }); }); - - QUnit.test('EventSource: async non-synchronized order', function(assert) { - context.addHandler(eName, executor(1, 5)); - context.addHandler(eName, executor(2, 50)); - context.addHandler(eName, executor(3)); - context.addHandler(eName, executor(4)); - runTest({ - assert: assert, - done: assert.async(), - expected: [3, 4, 1, 2], - message: 'Async callback order should follow [3,4,1,2].' - }); - }); - - QUnit.test('EventSource: async non-synchronized priority order', function(assert) { - context.addHandler(eName, executor(1, 5)); - context.addHandler(eName, executor(2, 50), undefined, -100); - context.addHandler(eName, executor(3), undefined, -500); - context.addHandler(eName, executor(4), undefined, 675); - runTest({ - assert: assert, - done: assert.async(), - expected: [4, 3, 1, 2], - message: 'Async callback order with priority should follow [4,3,1,2]. Async functions do not respect priority.' - }); - }); - - QUnit.test('EventSource: async synchronized order', function(assert) { - context.addHandler(eName, executor(1, 5)); - context.addHandler(eName, executor(2, 50)); - context.addHandler(eName, executor(3)); - context.addHandler(eName, executor(4)); - runTest({ - waitForPromiseHandlers: true, - assert: assert, - done: assert.async(), - expected: [1, 2, 3, 4], - message: 'Async callback order should follow [1,2,3,4], since it is synchronized.' - }); - }); - - QUnit.test('EventSource: async synchronized priority order', function(assert) { - context.addHandler(eName, executor(1, 5)); - context.addHandler(eName, executor(2), undefined, -500); - context.addHandler(eName, executor(3, 50), undefined, -200); - context.addHandler(eName, executor(4), undefined, 675); - runTest({ - waitForPromiseHandlers: true, - assert: assert, - done: assert.async(), - expected: [4, 1, 3, 2], - message: 'Async callback order with priority should follow [4,1,3,2], since priority is respected when synchronized.' - }); - }); - - QUnit.test('EventSource: async non-synchronized with breaking', function(assert) { - context.addHandler(eName, executor(1, 5)); - context.addHandler(eName, executor(2, 50, true)); - context.addHandler(eName, executor(3, 80)); - context.addHandler(eName, executor(4)); - runTest({ - assert: assert, - done: assert.async(), - expected: [4, 1, 2], - message: 'Async breaking should follow [4,1,2,3]. Async functions do not necessarily respect breaking, but unit tests finish after 50 ms.' - }); - }); - - // These tests fail despite being 'correct' - inspection shows that callabacks are called with mixed - // data in closures or even twice one 'setTimeout' handler. No issues in isolated test run. Possibly - // an issue with Qunit. - // - - // QUnit.test('EventSource: async synchronized priority order with breaking', function(assert) { - // context.addHandler(eName, executor(1, 5)); - // context.addHandler(eName, executor(2, 50, true), undefined, -100); - // context.addHandler(eName, executor(3), undefined, -500); - // context.addHandler(eName, executor(4), undefined, 675) - // runTest({ - // waitForPromiseHandlers: true, - // assert: assert, - // done: assert.async(), - // expected: [4, 1, 2], - // message: 'Async callback order with synced priority should follow [4,1,2], since 2 stops execution.' - // }); - // }); - // QUnit.test('EventSource: async synchronized priority order with breaking', function(assert) { - // context.addHandler(eName, executor(1, 50)); - // context.addHandler(eName, executor(2, 5), undefined, -300); - // context.addHandler(eName, executor(3, 80, true), undefined, -70); - // context.addHandler(eName, executor(4), undefined, 675); - // runTest({ - // waitForPromiseHandlers: true, - // assert: assert, - // done: assert.async(), - // expected: [4, 1, 3], - // message: 'Async callback order with sync should follow [4,1,3]. Async break works when synchronized.' - // }); - // }); } )();