(fn)
| 1367 | * @since 5.42 |
| 1368 | */ |
| 1369 | export function fork(fn) { |
| 1370 | if (!async_mode_flag) { |
| 1371 | e.experimental_async_required('fork'); |
| 1372 | } |
| 1373 | |
| 1374 | if (current_batch !== null) { |
| 1375 | e.fork_timing(); |
| 1376 | } |
| 1377 | |
| 1378 | var batch = Batch.ensure(); |
| 1379 | batch.is_fork = true; |
| 1380 | batch_values = new Map(); |
| 1381 | |
| 1382 | var committed = false; |
| 1383 | var settled = batch.settled(); |
| 1384 | |
| 1385 | flushSync(fn); |
| 1386 | |
| 1387 | return { |
| 1388 | commit: async () => { |
| 1389 | if (committed) { |
| 1390 | await settled; |
| 1391 | return; |
| 1392 | } |
| 1393 | |
| 1394 | if (!batch.linked) { |
| 1395 | e.fork_discarded(); |
| 1396 | } |
| 1397 | |
| 1398 | committed = true; |
| 1399 | |
| 1400 | batch.is_fork = false; |
| 1401 | |
| 1402 | // apply changes and update write versions so deriveds see the change |
| 1403 | for (var [source, [value]] of batch.current) { |
| 1404 | source.v = value; |
| 1405 | source.wv = increment_write_version(); |
| 1406 | } |
| 1407 | |
| 1408 | // trigger any `$state.eager(...)` expressions with the new state. |
| 1409 | // eager effects don't get scheduled like other effects, so we |
| 1410 | // can't just encounter them during traversal, we need to |
| 1411 | // proactively flush them |
| 1412 | // TODO maybe there's a better implementation? |
| 1413 | flushSync(() => { |
| 1414 | /** @type {Set<Effect>} */ |
| 1415 | var eager_effects = new Set(); |
| 1416 | |
| 1417 | for (var source of batch.current.keys()) { |
| 1418 | mark_eager_effects(source, eager_effects); |
| 1419 | } |
| 1420 | |
| 1421 | set_eager_effects(eager_effects); |
| 1422 | flush_eager_effects(); |
| 1423 | }); |
| 1424 | |
| 1425 | batch.flush(); |
| 1426 | await settled; |
nothing calls this directly
no test coverage detected