TokenedDataApi: Accept or kill token
Rather than leaving a token in a DONE state, we should either transition to ACCEPTED or DEAD depending on whether the token was superceded. * src/server/dapi/TokenedDataApi.ts (_replyUnlessStale): Accept token when not superceded, otherwise kill. [store]: New param. (request): Use it. * test/server/dapi/TokenedDataApiTest.ts: Update accordingly.master
parent
544fe1a1fe
commit
16409a014f
|
@ -91,7 +91,9 @@ export class TokenedDataApi implements DataApi
|
|||
this._dapiRequest( data, id ).then( resp_data =>
|
||||
store.completeToken( token, JSON.stringify( resp_data ) )
|
||||
.then( newtok =>
|
||||
this._replyUnlessStale( newtok, resp_data, callback, id )
|
||||
this._replyUnlessStale(
|
||||
store, newtok, resp_data, callback, id
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -142,23 +144,25 @@ export class TokenedDataApi implements DataApi
|
|||
* @param id - DataApi id
|
||||
*/
|
||||
private _replyUnlessStale(
|
||||
store: TokenStore,
|
||||
newtok: Token<TokenState.DONE>,
|
||||
resp_data: DataApiResult,
|
||||
callback: NodeCallback<DataApiResult>,
|
||||
id: string
|
||||
): void
|
||||
): Promise<void>
|
||||
{
|
||||
if ( newtok.last_created )
|
||||
{
|
||||
return callback( null, resp_data );
|
||||
return store.acceptToken( newtok, null )
|
||||
.then( () => callback( null, resp_data ) );
|
||||
}
|
||||
|
||||
callback(
|
||||
return store.killToken( newtok, null ).then( () => callback(
|
||||
context(
|
||||
Error( "Request superceded" ),
|
||||
{ id: id },
|
||||
),
|
||||
null
|
||||
);
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,6 +76,7 @@ describe( 'TokenedDataApi', () =>
|
|||
createStubToken( last_created );
|
||||
|
||||
let tok_completed = false;
|
||||
let tok_ackd = false;
|
||||
|
||||
const mock_tstore = new class implements TokenStore
|
||||
{
|
||||
|
@ -109,12 +110,20 @@ describe( 'TokenedDataApi', () =>
|
|||
|
||||
acceptToken()
|
||||
{
|
||||
return Promise.reject( Error( "not used" ) );
|
||||
expect( tok_completed ).to.be.true;
|
||||
expect( last_created ).to.be.true;
|
||||
|
||||
tok_ackd = true;
|
||||
return Promise.resolve( Object.create( stub_tok ) );
|
||||
}
|
||||
|
||||
killToken()
|
||||
{
|
||||
return Promise.reject( Error( "not used" ) );
|
||||
expect( tok_completed ).to.be.true;
|
||||
expect( last_created ).to.be.false;
|
||||
|
||||
tok_ackd = true;
|
||||
return Promise.resolve( Object.create( stub_tok ) );
|
||||
}
|
||||
}();
|
||||
|
||||
|
@ -143,7 +152,7 @@ describe( 'TokenedDataApi', () =>
|
|||
|
||||
const callback: NodeCallback<DataApiResult> = ( e, data ) =>
|
||||
{
|
||||
expect( tok_completed ).to.be.true;
|
||||
expect( tok_ackd ).to.be.true;
|
||||
|
||||
expected_err( e );
|
||||
|
||||
|
|
Loading…
Reference in New Issue