diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b99dbdd..d76b2430 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +### Fixed + +- when ttl 0 was set, a request to the server was not made again, it brought it from memory, it was corrected in the isLive function of common. + ## [2.2.1] - 2020-10-17 ### Fixed diff --git a/src/common.ts b/src/common.ts index a93858c7..fffb7c30 100644 --- a/src/common.ts +++ b/src/common.ts @@ -5,7 +5,7 @@ import { DocumentCollection } from './document-collection'; import { Resource } from './resource'; export function isLive(cacheable: ICacheable, ttl?: number): boolean { - let ttl_in_seconds = ttl && typeof ttl === 'number' ? ttl : cacheable.ttl || 0; + let ttl_in_seconds = typeof ttl === 'number' ? ttl : cacheable.ttl || 0; return Date.now() < cacheable.cache_last_update + ttl_in_seconds * 1000; } diff --git a/src/service.spec.ts b/src/service.spec.ts index ea99474c..4d848c3e 100644 --- a/src/service.spec.ts +++ b/src/service.spec.ts @@ -156,6 +156,32 @@ for (let store_cache_method of store_cache_methods) { expect(http_request_spy).toHaveBeenCalledTimes(0); }); + it(`with cached on memory (live) collection emits source ^memory-server| when force ttl = 0 on call`, async () => { + // caching collection + test_response_subject.next(new HttpResponse({ body: TestFactory.getCollectionDocumentData(Book) })); + booksService.collections_ttl = 5; // live + await booksService.all({ store_cache_method: store_cache_method }).toPromise(); + + let http_request_spy = spyOn(HttpClient.prototype, 'request').and.callThrough(); + let expected = [ + // expected emits + { builded: true, loaded: false, source: 'memory' }, + { builded: true, loaded: true, source: 'server' } + ]; + + let emits = await booksService + .all({ ttl: 0, store_cache_method: store_cache_method }) + .pipe( + map(emit => { + return { builded: emit.builded, loaded: emit.loaded, source: emit.source }; + }), + toArray() + ) + .toPromise(); + expect(emits).toMatchObject(expected); + expect(http_request_spy).toHaveBeenCalledTimes(1); + }); + it(`with cached on memory (dead) collection emits source ^memory-server|`, async () => { // caching collection test_response_subject.next(new HttpResponse({ body: TestFactory.getCollectionDocumentData(Book) }));