@@ -72,6 +72,10 @@ export function query(validate_or_fn, maybe_fn) {
7272
7373 const { event, state } = get_request_store ( ) ;
7474
75+ let immediate_refresh = true ;
76+
77+ queueMicrotask ( ( ) => ( immediate_refresh = false ) ) ;
78+
7579 /** @type {Promise<any> & Partial<RemoteQuery<any>> } */
7680 const promise = get_response ( __ , arg , state , ( ) =>
7781 run_remote_function ( event , state , false , arg , validate , fn )
@@ -98,7 +102,7 @@ export function query(validate_or_fn, maybe_fn) {
98102 } ;
99103
100104 promise . refresh = ( ) => {
101- const { state } = get_request_store ( ) ;
105+ const { event , state } = get_request_store ( ) ;
102106 const refreshes = state . refreshes ;
103107
104108 if ( ! refreshes ) {
@@ -107,11 +111,25 @@ export function query(validate_or_fn, maybe_fn) {
107111 ) ;
108112 }
109113
110- const cache_key = create_remote_cache_key ( __ . id , stringify_remote_arg ( arg , state . transport ) ) ;
111- refreshes [ cache_key ] = promise ;
114+ const key = stringify_remote_arg ( arg , state . transport ) ;
115+ const cache_key = create_remote_cache_key ( __ . id , key ) ;
116+
117+ if ( immediate_refresh ) {
118+ refreshes [ cache_key ] = promise ;
119+
120+ return promise . then ( ( ) => { } ) ;
121+ }
122+
123+ const refreshed = Promise . resolve (
124+ run_remote_function ( event , state , false , arg , validate , fn )
125+ ) ;
126+
127+ refreshed . catch ( ( ) => { } ) ;
128+
129+ const cache = get_cache ( __ , state ) ;
130+ cache [ key ] = refreshes [ cache_key ] = refreshed ;
112131
113- // TODO we could probably just return promise here, but would need to update the types
114- return promise . then ( ( ) => { } ) ;
132+ return refreshed . then ( ( ) => { } ) ;
115133 } ;
116134
117135 promise . withOverride = ( ) => {
@@ -200,6 +218,10 @@ function batch(validate_or_fn, maybe_fn) {
200218
201219 const { event, state } = get_request_store ( ) ;
202220
221+ let immediate_refresh = true ;
222+
223+ queueMicrotask ( ( ) => ( immediate_refresh = false ) ) ;
224+
203225 /** @type {Promise<any> & Partial<RemoteQuery<any>> } */
204226 const promise = get_response ( __ , arg , state , ( ) => {
205227 // Collect all the calls to the same query in the same macrotask,
@@ -243,8 +265,8 @@ function batch(validate_or_fn, maybe_fn) {
243265
244266 promise . catch ( ( ) => { } ) ;
245267
246- promise . refresh = async ( ) => {
247- const { state } = get_request_store ( ) ;
268+ promise . refresh = ( ) => {
269+ const { event , state } = get_request_store ( ) ;
248270 const refreshes = state . refreshes ;
249271
250272 if ( ! refreshes ) {
@@ -253,8 +275,25 @@ function batch(validate_or_fn, maybe_fn) {
253275 ) ;
254276 }
255277
256- const cache_key = create_remote_cache_key ( __ . id , stringify_remote_arg ( arg , state . transport ) ) ;
257- refreshes [ cache_key ] = await /** @type {Promise<any> } */ ( promise ) ;
278+ const key = stringify_remote_arg ( arg , state . transport ) ;
279+ const cache_key = create_remote_cache_key ( __ . id , key ) ;
280+
281+ if ( immediate_refresh ) {
282+ refreshes [ cache_key ] = promise ;
283+
284+ return promise . then ( ( ) => { } ) ;
285+ }
286+
287+ const refreshed = Promise . resolve (
288+ run_remote_function ( event , state , false , arg , validate , fn )
289+ ) ;
290+
291+ refreshed . catch ( ( ) => { } ) ;
292+
293+ const cache = get_cache ( __ , state ) ;
294+ cache [ key ] = refreshes [ cache_key ] = refreshed ;
295+
296+ return refreshed . then ( ( ) => { } ) ;
258297 } ;
259298
260299 promise . withOverride = ( ) => {
0 commit comments