Skip to content

Commit

Permalink
Search Cancelled, Still Got Results (resolves #383)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClickerMonkey committed Mar 28, 2018
1 parent a285fcb commit bdfb10e
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 75 deletions.
77 changes: 42 additions & 35 deletions build/rekord.js
Original file line number Diff line number Diff line change
Expand Up @@ -11930,67 +11930,74 @@ Class.create( Search,
this.$url = url || this.$url;
this.$set( props );

var promise = new Promise();
var encoded = this.$encode();
var success = bind( this, this.$handleSuccess );
var failure = bind( this, this.$handleFailure );
var success = bind( this, this.$handleSuccess( promise ) );
var failure = bind( this, this.$handleFailure( promise ) );
var options = this.$options || this.$db.queryOptions;

batchExecute(function()
{
this.$cancel();
this.$promise = new Promise();
this.$promise = promise;
this.$db.rest.query( this.$url, encoded, options, success, failure );

}, this );

return this.$promise;
},

$handleSuccess: function(response)
$handleSuccess: function(promise)
{
if ( !this.$promise.isPending() )
return function(response)
{
return;
}
if ( !this.$promise.isPending() || promise !== this.$promise )
{
return;
}

var models = this.$decode.apply( this, arguments );
var models = this.$decode.apply( this, arguments );

if ( this.$append )
{
this.$results.addAll( models, false, true );
}
else
{
this.$results.reset( models, true );
}
if ( this.$append )
{
this.$results.addAll( models, false, true );
}
else
{
this.$results.reset( models, true );
}

this.$promise.resolve( this, response, this.$results );
this.$promise.resolve( this, response, this.$results );
};
},

$handleFailure: function(response, status)
$handleFailure: function(promise)
{
if ( !this.$promise.isPending() )
return function(response, status)
{
return;
}
if ( !this.$promise.isPending() || promise !== this.$promise )
{
return;
}

var offline = RestStatus.Offline[ status ];
var offline = RestStatus.Offline[ status ];

if ( offline )
{
Rekord.checkNetworkStatus();
if ( offline )
{
Rekord.checkNetworkStatus();

offline = !Rekord.online;
}
offline = !Rekord.online;
}

if ( offline )
{
this.$promise.noline( this, response, status );
}
else
{
this.$promise.reject( this, response, status );
}
if ( offline )
{
this.$promise.noline( this, response, status );
}
else
{
this.$promise.reject( this, response, status );
}
};
},

$cancel: function()
Expand Down
6 changes: 3 additions & 3 deletions build/rekord.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/rekord.min.js.map

Large diffs are not rendered by default.

77 changes: 42 additions & 35 deletions src/lib/search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,67 +81,74 @@ Class.create( Search,
this.$url = url || this.$url;
this.$set( props );

var promise = new Promise();
var encoded = this.$encode();
var success = bind( this, this.$handleSuccess );
var failure = bind( this, this.$handleFailure );
var success = bind( this, this.$handleSuccess( promise ) );
var failure = bind( this, this.$handleFailure( promise ) );
var options = this.$options || this.$db.queryOptions;

batchExecute(function()
{
this.$cancel();
this.$promise = new Promise();
this.$promise = promise;
this.$db.rest.query( this.$url, encoded, options, success, failure );

}, this );

return this.$promise;
},

$handleSuccess: function(response)
$handleSuccess: function(promise)
{
if ( !this.$promise.isPending() )
return function(response)
{
return;
}
if ( !this.$promise.isPending() || promise !== this.$promise )
{
return;
}

var models = this.$decode.apply( this, arguments );
var models = this.$decode.apply( this, arguments );

if ( this.$append )
{
this.$results.addAll( models, false, true );
}
else
{
this.$results.reset( models, true );
}
if ( this.$append )
{
this.$results.addAll( models, false, true );
}
else
{
this.$results.reset( models, true );
}

this.$promise.resolve( this, response, this.$results );
this.$promise.resolve( this, response, this.$results );
};
},

$handleFailure: function(response, status)
$handleFailure: function(promise)
{
if ( !this.$promise.isPending() )
return function(response, status)
{
return;
}
if ( !this.$promise.isPending() || promise !== this.$promise )
{
return;
}

var offline = RestStatus.Offline[ status ];
var offline = RestStatus.Offline[ status ];

if ( offline )
{
Rekord.checkNetworkStatus();
if ( offline )
{
Rekord.checkNetworkStatus();

offline = !Rekord.online;
}
offline = !Rekord.online;
}

if ( offline )
{
this.$promise.noline( this, response, status );
}
else
{
this.$promise.reject( this, response, status );
}
if ( offline )
{
this.$promise.noline( this, response, status );
}
else
{
this.$promise.reject( this, response, status );
}
};
},

$cancel: function()
Expand Down
70 changes: 69 additions & 1 deletion test/cases/rekord.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,76 @@ test( 'refresh with decoding', function()
strictEqual( t1.done, true );

Task.Database.live.liveSave({id: 1, done: 'no'});

notOk( t1.$hasChanges() );
notOk( t1.$hasChange('done') );
strictEqual( t1.done, false );
});


test( 'repeated searches second finishes first', function(assert)
{
var prefix = 'repeated_searches_second_finishes_first_';
var timer = assert.timer();

var Todo = Rekord({
name: prefix + 'todo',
fields: ['name', 'done']
});

var remote = Todo.Database.rest;

remote.queries.put( '1', [
{id: 1, name: 't1', done: true},
{id: 2, name: 't2', done: false},
{id: 3, name: 't3', done: true}
]);

remote.queries.put( '2', [
{id: 4, name: 't4', done: true}
]);

remote.queries.put( '3', [
{id: 5, name: 't5', done: false},
{id: 6, name: 't6', done: false}
]);

remote.delay = 4;

var q0 = Todo.search();
var r0 = q0.$results;
var p0 = q0.$run('1');

remote.delay = 2;

var p1 = q0.$run('2');

remote.delay = 6;

var p2 = q0.$run('3');

expect(6);

wait( 1, function() {
strictEqual( r0.length, 0 );
});

// 2 (p1) finishes, but not latest
wait( 3, function() {
strictEqual( r0.length, 0 );
});

// 1 (p0) finishes, but not latest
wait( 5, function() {
strictEqual( r0.length, 0 );
});

// 3 (p2) finishes, is latest
wait( 7, function() {
strictEqual( r0.length, 2 );
strictEqual( r0[0].id, 5 );
strictEqual( r0[1].id, 6 );
});

timer.run();
});

0 comments on commit bdfb10e

Please sign in to comment.