Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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 <[email protected]>
  • Loading branch information
danxuliu committed Mar 21, 2018
commit 6eb5cc54120168351286572b18bd1e07dc3bbc5c
19 changes: 18 additions & 1 deletion core/js/sharedialogview.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
/** @type {object} **/
shareeListView: undefined,

/** @type {object} **/
_lastSuggestions: undefined,

events: {
'focus .shareWithField': 'onShareWithFieldFocus',
'input .shareWithField': 'onShareWithFieldChanged',
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down
174 changes: 174 additions & 0 deletions core/js/tests/specs/sharedialogviewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down