@@ -80,52 +80,13 @@ export function query(validate_or_fn, maybe_fn) {
8080
8181 promise . catch ( ( ) => { } ) ;
8282
83- /** @param {Output } value */
84- promise . set = ( value ) => {
85- const { state } = get_request_store ( ) ;
86- const refreshes = state . refreshes ;
87-
88- if ( ! refreshes ) {
89- throw new Error (
90- `Cannot call set on query '${ __ . name } ' because it is not executed in the context of a command/form remote function`
91- ) ;
92- }
93-
94- if ( __ . id ) {
95- const key = stringify_remote_arg ( arg , state . transport ) ;
96- const cache_key = create_remote_cache_key ( __ . id , key ) ;
97- const cache = get_cache ( __ , state ) ;
98- cache [ key ] = refreshes [ cache_key ] = Promise . resolve ( value ) ;
99- }
100- } ;
83+ promise . set = ( value ) => update_refresh_value ( get_refresh_context ( __ , 'set' , arg ) , value ) ;
10184
10285 promise . refresh = ( ) => {
103- const { state } = get_request_store ( ) ;
104- const refreshes = state . refreshes ;
105-
106- if ( ! refreshes ) {
107- throw new Error (
108- `Cannot call refresh on query '${ __ . name } ' because it is not executed in the context of a command/form remote function`
109- ) ;
110- }
111-
112- const cache = get_cache ( __ , state ) ;
113- const key = stringify_remote_arg ( arg , state . transport ) ;
114- const cache_key = create_remote_cache_key ( __ . id , key ) ;
115-
116- // First time query has ever been triggered and .refresh() was called within the same microtask
117- if ( ! cache [ key ] ) {
118- refreshes [ cache_key ] = promise ;
119- return promise . then ( ( ) => { } ) ;
120- }
121-
122- const refreshed = get_remote_function_result ( ) ;
123-
124- refreshed . catch ( ( ) => { } ) ;
125-
126- cache [ key ] = refreshes [ cache_key ] = refreshed ;
127-
128- return refreshed . then ( ( ) => { } ) ;
86+ const refresh_context = get_refresh_context ( __ , 'refresh' , arg ) ;
87+ const is_immediate_refresh = ! refresh_context . cache [ refresh_context . key ] ;
88+ const value = is_immediate_refresh ? promise : get_remote_function_result ( ) ;
89+ return update_refresh_value ( refresh_context , value , is_immediate_refresh ) ;
12990 } ;
13091
13192 promise . withOverride = ( ) => {
@@ -259,51 +220,13 @@ function batch(validate_or_fn, maybe_fn) {
259220
260221 promise . catch ( ( ) => { } ) ;
261222
262- promise . set = ( value ) => {
263- const { state } = get_request_store ( ) ;
264- const refreshes = state . refreshes ;
265-
266- if ( ! refreshes ) {
267- throw new Error (
268- `Cannot call set on query.batch '${ __ . name } ' because it is not executed in the context of a command/form remote function`
269- ) ;
270- }
271-
272- if ( __ . id ) {
273- const key = stringify_remote_arg ( arg , state . transport ) ;
274- const cache_key = create_remote_cache_key ( __ . id , key ) ;
275- const cache = get_cache ( __ , state ) ;
276- cache [ key ] = refreshes [ cache_key ] = Promise . resolve ( value ) ;
277- }
278- } ;
223+ promise . set = ( value ) => update_refresh_value ( get_refresh_context ( __ , 'set' , arg ) , value ) ;
279224
280225 promise . refresh = ( ) => {
281- const { state } = get_request_store ( ) ;
282- const refreshes = state . refreshes ;
283-
284- if ( ! refreshes ) {
285- throw new Error (
286- `Cannot call refresh on query.batch '${ __ . name } ' because it is not executed in the context of a command/form remote function`
287- ) ;
288- }
289-
290- const cache = get_cache ( __ , state ) ;
291- const key = stringify_remote_arg ( arg , state . transport ) ;
292- const cache_key = create_remote_cache_key ( __ . id , key ) ;
293-
294- // First time query has ever been triggered and .refresh() was called within the same microtask
295- if ( ! cache [ key ] ) {
296- refreshes [ cache_key ] = promise ;
297- return promise . then ( ( ) => { } ) ;
298- }
299-
300- const refreshed = get_remote_function_result ( ) ;
301-
302- refreshed . catch ( ( ) => { } ) ;
303-
304- cache [ key ] = refreshes [ cache_key ] = refreshed ;
305-
306- return refreshed . then ( ( ) => { } ) ;
226+ const refresh_context = get_refresh_context ( __ , 'refresh' , arg ) ;
227+ const is_immediate_refresh = ! refresh_context . cache [ refresh_context . key ] ;
228+ const value = is_immediate_refresh ? promise : get_remote_function_result ( ) ;
229+ return update_refresh_value ( refresh_context , value , is_immediate_refresh ) ;
307230 } ;
308231
309232 promise . withOverride = ( ) => {
@@ -320,3 +243,57 @@ function batch(validate_or_fn, maybe_fn) {
320243
321244// Add batch as a property to the query function
322245Object . defineProperty ( query , 'batch' , { value : batch , enumerable : true } ) ;
246+
247+ /**
248+ * @param {RemoteInfo } __
249+ * @param {'set' | 'refresh' } action
250+ * @param {any } [arg]
251+ * @returns {{ __: RemoteInfo; state: any; refreshes: Record<string, Promise<any>>; cache: Record<string, Promise<any>>; cache_key: string; key: string } }
252+ */
253+ function get_refresh_context ( __ , action , arg ) {
254+ const { state } = get_request_store ( ) ;
255+ const { refreshes } = state ;
256+
257+ if ( ! refreshes ) {
258+ throw new Error (
259+ `Cannot call ${ action } on ${ format_remote_name ( __ ) } because it is not executed in the context of a command/form remote function`
260+ ) ;
261+ }
262+
263+ const key = stringify_remote_arg ( arg , state . transport ) ;
264+ const cache_key = create_remote_cache_key ( __ . id , key ) ;
265+ const cache = get_cache ( __ , state ) ;
266+
267+ return { __ , state, refreshes, cache, cache_key, key } ;
268+ }
269+
270+ /**
271+ * @param {{ __: RemoteInfo; refreshes: Record<string, Promise<any>>; cache: Record<string, Promise<any>>; cache_key: string; key: string } } context
272+ * @param {any } value
273+ * @param {boolean } [is_immediate_refresh=false]
274+ * @returns {Promise<void> }
275+ */
276+ function update_refresh_value (
277+ { __ , refreshes, cache, cache_key, key } ,
278+ value ,
279+ is_immediate_refresh = false
280+ ) {
281+ const promise = Promise . resolve ( value ) ;
282+
283+ if ( ! is_immediate_refresh ) {
284+ cache [ key ] = promise ;
285+ }
286+
287+ if ( __ . id ) {
288+ refreshes [ cache_key ] = promise ;
289+ }
290+
291+ return promise . then ( ( ) => { } ) ;
292+ }
293+
294+ /**
295+ * @param {RemoteInfo } __
296+ */
297+ function format_remote_name ( __ ) {
298+ return __ . type === 'query_batch' ? `query.batch '${ __ . name } '` : `query '${ __ . name } '` ;
299+ }
0 commit comments