(params: Ref<Parameter<F>>, forceFresh = false)
| 30 | }); |
| 31 | |
| 32 | const execute = async (params: Ref<Parameter<F>>, forceFresh = false) => { |
| 33 | result.loading = true; |
| 34 | result.temporaryCache = false; |
| 35 | result.error = null; |
| 36 | if (!options.keepData) result.data = null; |
| 37 | const tempId = Math.random(); |
| 38 | id = tempId; |
| 39 | |
| 40 | let state; |
| 41 | let cache; |
| 42 | if (options.cache) { |
| 43 | cache = new Cache( |
| 44 | options.cache.prefix + |
| 45 | (options.cache.keyFn ? options.cache.keyFn(params) : JSON.stringify(params.value)), |
| 46 | options.cache.ttl, |
| 47 | true, |
| 48 | options.cache.refetchTtl, |
| 49 | ); |
| 50 | |
| 51 | if (forceFresh) { |
| 52 | cache.clearValue(); |
| 53 | } else { |
| 54 | state = await cache.fullState(); |
| 55 | if (state.hasValue) { |
| 56 | result.data = state.value.data; |
| 57 | result.loading = false; |
| 58 | result.cache = true; |
| 59 | result.temporaryCache = state.refetch; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (state && !state.refetch && state.hasValue) return Promise.resolve(); |
| 65 | |
| 66 | if (options.executeCondition && !options.executeCondition(params)) { |
| 67 | result.loading = false; |
| 68 | return Promise.resolve(); |
| 69 | } |
| 70 | |
| 71 | return fn(params) |
| 72 | .then(res => { |
| 73 | if (tempId !== id) return; |
| 74 | result.loading = false; |
| 75 | result.temporaryCache = false; |
| 76 | result.cache = false; |
| 77 | result.data = res; |
| 78 | if (cache && res) cache.setValue(res); |
| 79 | }) |
| 80 | .catch(err => { |
| 81 | if (tempId !== id) return; |
| 82 | con.error(err); |
| 83 | result.loading = false; |
| 84 | result.temporaryCache = false; |
| 85 | result.cache = false; |
| 86 | result.error = err; |
| 87 | }); |
| 88 | }; |
| 89 |
no test coverage detected