@@ -72,14 +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 ) ) ;
75+ const get_remote_function_result = ( ) => run_remote_function ( event , state , false , arg , validate , fn ) ;
7876
7977 /** @type {Promise<any> & Partial<RemoteQuery<any>> } */
80- const promise = get_response ( __ , arg , state , ( ) =>
81- run_remote_function ( event , state , false , arg , validate , fn )
82- ) ;
78+ const promise = get_response ( __ , arg , state , get_remote_function_result ) ;
8379
8480 promise . catch ( ( ) => { } ) ;
8581
@@ -94,6 +90,7 @@ export function query(validate_or_fn, maybe_fn) {
9490 ) ;
9591 }
9692
93+ // TODO: Shouldn't this throw to signify that non-exported queries can't be set? Or still update the cache without touching refreshes?
9794 if ( __ . id ) {
9895 const cache = get_cache ( __ , state ) ;
9996 const key = stringify_remote_arg ( arg , state . transport ) ;
@@ -102,7 +99,7 @@ export function query(validate_or_fn, maybe_fn) {
10299 } ;
103100
104101 promise . refresh = ( ) => {
105- const { event , state } = get_request_store ( ) ;
102+ const { state } = get_request_store ( ) ;
106103 const refreshes = state . refreshes ;
107104
108105 if ( ! refreshes ) {
@@ -111,22 +108,20 @@ export function query(validate_or_fn, maybe_fn) {
111108 ) ;
112109 }
113110
111+ const cache = get_cache ( __ , state ) ;
114112 const key = stringify_remote_arg ( arg , state . transport ) ;
115113 const cache_key = create_remote_cache_key ( __ . id , key ) ;
116114
117- if ( immediate_refresh ) {
115+ // First time query has ever been triggered and .refresh() was called within the same microtask
116+ if ( ! cache [ key ] ) {
118117 refreshes [ cache_key ] = promise ;
119-
120118 return promise . then ( ( ) => { } ) ;
121119 }
122120
123- const refreshed = Promise . resolve (
124- run_remote_function ( event , state , false , arg , validate , fn )
125- ) ;
121+ const refreshed = get_remote_function_result ( ) ;
126122
127123 refreshed . catch ( ( ) => { } ) ;
128124
129- const cache = get_cache ( __ , state ) ;
130125 cache [ key ] = refreshes [ cache_key ] = refreshed ;
131126
132127 return refreshed . then ( ( ) => { } ) ;
@@ -218,12 +213,7 @@ function batch(validate_or_fn, maybe_fn) {
218213
219214 const { event, state } = get_request_store ( ) ;
220215
221- let immediate_refresh = true ;
222-
223- queueMicrotask ( ( ) => ( immediate_refresh = false ) ) ;
224-
225- /** @type {Promise<any> & Partial<RemoteQuery<any>> } */
226- const promise = get_response ( __ , arg , state , ( ) => {
216+ const get_remote_function_result = ( ) => {
227217 // Collect all the calls to the same query in the same macrotask,
228218 // then execute them as one backend request.
229219 return new Promise ( ( resolve , reject ) => {
@@ -261,12 +251,32 @@ function batch(validate_or_fn, maybe_fn) {
261251 }
262252 } , 0 ) ;
263253 } ) ;
264- } ) ;
254+ } ;
255+
256+ /** @type {Promise<any> & Partial<RemoteQuery<any>> } */
257+ const promise = get_response ( __ , arg , state , get_remote_function_result ) ;
265258
266259 promise . catch ( ( ) => { } ) ;
267260
261+ promise . set = ( value ) => {
262+ const { state } = get_request_store ( ) ;
263+ const refreshes = state . refreshes ;
264+
265+ if ( ! refreshes ) {
266+ throw new Error (
267+ `Cannot call set on query.batch '${ __ . name } ' because it is not executed in the context of a command/form remote function`
268+ ) ;
269+ }
270+
271+ if ( __ . id ) {
272+ const key = stringify_remote_arg ( arg , state . transport ) ;
273+ const cache_key = create_remote_cache_key ( __ . id , key ) ;
274+ refreshes [ cache_key ] = Promise . resolve ( value ) ;
275+ }
276+ }
277+
268278 promise . refresh = ( ) => {
269- const { event , state } = get_request_store ( ) ;
279+ const { state } = get_request_store ( ) ;
270280 const refreshes = state . refreshes ;
271281
272282 if ( ! refreshes ) {
@@ -275,22 +285,20 @@ function batch(validate_or_fn, maybe_fn) {
275285 ) ;
276286 }
277287
288+ const cache = get_cache ( __ , state ) ;
278289 const key = stringify_remote_arg ( arg , state . transport ) ;
279290 const cache_key = create_remote_cache_key ( __ . id , key ) ;
280291
281- if ( immediate_refresh ) {
292+ // First time query has ever been triggered and .refresh() was called within the same microtask
293+ if ( ! cache [ key ] ) {
282294 refreshes [ cache_key ] = promise ;
283-
284295 return promise . then ( ( ) => { } ) ;
285296 }
286297
287- const refreshed = Promise . resolve (
288- run_remote_function ( event , state , false , arg , validate , fn )
289- ) ;
298+ const refreshed = get_remote_function_result ( ) ;
290299
291300 refreshed . catch ( ( ) => { } ) ;
292301
293- const cache = get_cache ( __ , state ) ;
294302 cache [ key ] = refreshes [ cache_key ] = refreshed ;
295303
296304 return refreshed . then ( ( ) => { } ) ;
0 commit comments