From 1f6b575437104d64ede8a2c23d141160360d2cd5 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 11 Feb 2023 20:12:12 +0330 Subject: [PATCH] feat(fetch): enhance fetchContext with dispatchOptions and only cache_only request in first time Co-authored-by: S. Amir Mohammad Najafi --- core/fetch/src/fetch.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/core/fetch/src/fetch.ts b/core/fetch/src/fetch.ts index 711644dfa..a80af321d 100644 --- a/core/fetch/src/fetch.ts +++ b/core/fetch/src/fetch.ts @@ -1,5 +1,5 @@ import {createLogger, globalAlwatr, isBrowser} from '@alwatr/logger'; -import {contextProvider} from '@alwatr/signal'; +import {contextProvider, type DispatchOptions} from '@alwatr/signal'; import {getClientId} from '@alwatr/util'; import type {FetchOptions, CacheDuplicate, CacheStrategy} from './type.js'; @@ -31,12 +31,16 @@ const cacheSupported = 'caches' in globalThis; const duplicateRequestStorage: Record> = {}; -export async function fetchContext(contextName: string, fetchOption: FetchOptions): Promise { - if (cacheSupported) { +export async function fetchContext( + contextName: string, + fetchOption: FetchOptions, + dispatchOptions?: Partial, +): Promise { + if (cacheSupported && contextProvider.getValue(contextName) == null) { try { fetchOption.cacheStrategy = 'cache_only'; const response = await serviceRequest(fetchOption); - contextProvider.setValue(contextName, response); + contextProvider.setValue(contextName, response, dispatchOptions); } catch (err) { if ((err as Error).message === 'fetch_cache_not_found') { @@ -56,7 +60,7 @@ export async function fetchContext(contextName: string, fetchOption: FetchOption response.meta?.lastUpdated === undefined || // skip lastUpdated check response.meta?.lastUpdated !== contextProvider.getValue(contextName)?.meta?.reversion ) { - contextProvider.setValue(contextName, response); + contextProvider.setValue(contextName, response, dispatchOptions); } } catch (err) {