diff options
author | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2018-03-20 13:14:26 +0100 |
---|---|---|
committer | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2018-03-21 04:31:08 +0100 |
commit | 6eb5cc54120168351286572b18bd1e07dc3bbc5c (patch) | |
tree | e506ff9159d1a88acc509c00eaba8d4ceea49ece | |
parent | 10a4f8e45ee1e3b6a8f2b6c9348271724eb3371b (diff) | |
download | nextcloud-server-6eb5cc54120168351286572b18bd1e07dc3bbc5c.tar.gz nextcloud-server-6eb5cc54120168351286572b18bd1e07dc3bbc5c.zip |
Reuse last suggestions if the same parameters are used
When a share is confirmed the suggestions are got to check if there is
an exact match. Usually the suggestions were already got with the same
parameters in order to fill the autocomplete dropdown, so to avoid a
superfluous request now the last suggestions are reused when got again,
although only if the same parameters as the last time are used.
Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
-rw-r--r-- | core/js/sharedialogview.js | 19 | ||||
-rw-r--r-- | core/js/tests/specs/sharedialogviewSpec.js | 174 |
2 files changed, 192 insertions, 1 deletions
diff --git a/core/js/sharedialogview.js b/core/js/sharedialogview.js index 0f1d8f39550..96f076c6a47 100644 --- a/core/js/sharedialogview.js +++ b/core/js/sharedialogview.js @@ -65,6 +65,9 @@ /** @type {object} **/ shareeListView: undefined, + /** @type {object} **/ + _lastSuggestions: undefined, + events: { 'focus .shareWithField': 'onShareWithFieldFocus', 'input .shareWithField': 'onShareWithFieldChanged', @@ -136,6 +139,13 @@ }, _getSuggestions: function(searchTerm, perPage, model) { + if (this._lastSuggestions && + this._lastSuggestions.searchTerm === searchTerm && + this._lastSuggestions.perPage === perPage && + this._lastSuggestions.model === model) { + return this._lastSuggestions.promise; + } + var deferred = $.Deferred(); $.get( @@ -289,7 +299,14 @@ deferred.reject(); }); - return deferred.promise(); + this._lastSuggestions = { + searchTerm: searchTerm, + perPage: perPage, + model: model, + promise: deferred.promise() + }; + + return this._lastSuggestions.promise; }, autocompleteHandler: function (search, response) { diff --git a/core/js/tests/specs/sharedialogviewSpec.js b/core/js/tests/specs/sharedialogviewSpec.js index 7ef38c022dd..1b4c19bbb44 100644 --- a/core/js/tests/specs/sharedialogviewSpec.js +++ b/core/js/tests/specs/sharedialogviewSpec.js @@ -718,6 +718,180 @@ describe('OC.Share.ShareDialogView', function() { }])).toEqual(true); expect(failStub.called).toEqual(false); }); + + it('does not send a request to the server again for the same parameters', function() { + var doneStub = sinon.stub(); + var failStub = sinon.stub(); + + dialog._getSuggestions('bob', 42, shareModel).done(doneStub).fail(failStub); + + var jsonData = JSON.stringify({ + 'ocs': { + 'meta': { + 'status': 'success', + 'statuscode': 100, + 'message': null + }, + 'data': { + 'exact': { + 'users': [ + { + 'label': 'bob', + 'value': { + 'shareType': OC.Share.SHARE_TYPE_USER, + 'shareWith': 'user1' + } + } + ], + 'groups': [], + 'remotes': [] + }, + 'users': [], + 'groups': [], + 'remotes': [], + 'lookup': [] + } + } + }); + + expect(doneStub.called).toEqual(false); + expect(failStub.called).toEqual(false); + + fakeServer.requests[0].respond( + 200, + {'Content-Type': 'application/json'}, + jsonData + ); + + expect(doneStub.calledOnce).toEqual(true); + expect(doneStub.calledWithExactly([{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }], [{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }])).toEqual(true); + expect(failStub.called).toEqual(false); + + var done2Stub = sinon.stub(); + var fail2Stub = sinon.stub(); + + dialog._getSuggestions('bob', 42, shareModel).done(done2Stub).fail(fail2Stub); + + expect(doneStub.calledOnce).toEqual(true); + expect(failStub.called).toEqual(false); + + expect(done2Stub.calledOnce).toEqual(true); + expect(done2Stub.calledWithExactly([{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }], [{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }])).toEqual(true); + expect(fail2Stub.called).toEqual(false); + }); + + it('sends a request to the server again for the same parameters if the calls are not consecutive', function() { + var doneStub = sinon.stub(); + var failStub = sinon.stub(); + + dialog._getSuggestions('bob', 42, shareModel).done(doneStub).fail(failStub); + + var jsonData = JSON.stringify({ + 'ocs': { + 'meta': { + 'status': 'success', + 'statuscode': 100, + 'message': null + }, + 'data': { + 'exact': { + 'users': [ + { + 'label': 'bob', + 'value': { + 'shareType': OC.Share.SHARE_TYPE_USER, + 'shareWith': 'user1' + } + } + ], + 'groups': [], + 'remotes': [] + }, + 'users': [], + 'groups': [], + 'remotes': [], + 'lookup': [] + } + } + }); + + expect(doneStub.called).toEqual(false); + expect(failStub.called).toEqual(false); + + fakeServer.requests[0].respond( + 200, + {'Content-Type': 'application/json'}, + jsonData + ); + + expect(doneStub.calledOnce).toEqual(true); + expect(doneStub.calledWithExactly([{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }], [{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }])).toEqual(true); + expect(failStub.called).toEqual(false); + + var done2Stub = sinon.stub(); + var fail2Stub = sinon.stub(); + + dialog._getSuggestions('bob', 108, shareModel).done(done2Stub).fail(fail2Stub); + + expect(done2Stub.called).toEqual(false); + expect(fail2Stub.called).toEqual(false); + + fakeServer.requests[1].respond( + 200, + {'Content-Type': 'application/json'}, + jsonData + ); + + expect(done2Stub.calledOnce).toEqual(true); + expect(fail2Stub.called).toEqual(false); + + var done3Stub = sinon.stub(); + var fail3Stub = sinon.stub(); + + dialog._getSuggestions('bob', 42, shareModel).done(done3Stub).fail(fail3Stub); + + expect(done3Stub.called).toEqual(false); + expect(fail3Stub.called).toEqual(false); + + fakeServer.requests[2].respond( + 200, + {'Content-Type': 'application/json'}, + jsonData + ); + + expect(doneStub.calledOnce).toEqual(true); + expect(failStub.called).toEqual(false); + expect(done2Stub.calledOnce).toEqual(true); + expect(fail2Stub.called).toEqual(false); + + expect(done3Stub.calledOnce).toEqual(true); + expect(done3Stub.calledWithExactly([{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }], [{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }])).toEqual(true); + expect(fail3Stub.called).toEqual(false); + }); }); describe('autocompletion of users', function() { var showTemporaryNotificationStub; |