From 8bd34edd06da3a46ff26809ac51f87e5ed74df78 Mon Sep 17 00:00:00 2001 From: Matias Ahumada Date: Thu, 8 Apr 2021 15:31:06 -0300 Subject: [PATCH 1/2] FE-256-fix-force-ttl-0 auto-commit --- CHANGELOG.md | 4 ++++ src/common.ts | 4 ++-- src/service.spec.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26e9b0c7..157d6a28 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.1.19] - 2020-08-25 ### Fixed diff --git a/src/common.ts b/src/common.ts index a93858c7..7dda8f51 100644 --- a/src/common.ts +++ b/src/common.ts @@ -5,9 +5,9 @@ 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; + return Date.now() <= cacheable.cache_last_update + ttl_in_seconds * 1000; } // @todo test required for hasMany and hasOne diff --git a/src/service.spec.ts b/src/service.spec.ts index dfc43d0a..a402fe43 100644 --- a/src/service.spec.ts +++ b/src/service.spec.ts @@ -148,6 +148,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) })); From 7d3a30b87364e1de79cb5813e2826fd6a703a090 Mon Sep 17 00:00:00 2001 From: Matias Ahumada Date: Thu, 8 Apr 2021 16:06:04 -0300 Subject: [PATCH 2/2] FE-256-fix-force-ttl-0 auto-commit --- CHANGELOG.md | 1 + src/common.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 166e3ad5..d76b2430 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### 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 7dda8f51..fffb7c30 100644 --- a/src/common.ts +++ b/src/common.ts @@ -7,7 +7,7 @@ import { Resource } from './resource'; export function isLive(cacheable: ICacheable, ttl?: number): boolean { let ttl_in_seconds = typeof ttl === 'number' ? ttl : cacheable.ttl || 0; - return Date.now() <= cacheable.cache_last_update + ttl_in_seconds * 1000; + return Date.now() < cacheable.cache_last_update + ttl_in_seconds * 1000; } // @todo test required for hasMany and hasOne