(fn)
| 1256 | * @returns {T} |
| 1257 | */ |
| 1258 | export function eager(fn) { |
| 1259 | var initial = true; |
| 1260 | var value = /** @type {T} */ (undefined); |
| 1261 | |
| 1262 | if (active_reaction === null) { |
| 1263 | return fn(); |
| 1264 | } |
| 1265 | |
| 1266 | let parent = active_reaction; |
| 1267 | |
| 1268 | let version = version_map.get(parent) ?? source(0); |
| 1269 | version_map.set(parent, version); |
| 1270 | |
| 1271 | teardown(() => { |
| 1272 | if (parent.f & DESTROYING) version_map.delete(parent); |
| 1273 | }); |
| 1274 | |
| 1275 | get(version); |
| 1276 | |
| 1277 | eager_effect(() => { |
| 1278 | if (initial) { |
| 1279 | // the first time this runs, we create an eager effect |
| 1280 | // that will run eagerly whenever the expression changes |
| 1281 | var previous_batch_values = batch_values; |
| 1282 | |
| 1283 | try { |
| 1284 | batch_values = null; |
| 1285 | value = fn(); |
| 1286 | } finally { |
| 1287 | batch_values = previous_batch_values; |
| 1288 | } |
| 1289 | |
| 1290 | return; |
| 1291 | } |
| 1292 | |
| 1293 | // the second time this effect runs, it's to schedule a |
| 1294 | // `version` update. since this will recreate the effect, |
| 1295 | // we don't need to evaluate the expression here |
| 1296 | if (eager_versions.length === 0) { |
| 1297 | queue_micro_task(eager_flush); |
| 1298 | } |
| 1299 | |
| 1300 | eager_versions.push(version); |
| 1301 | }); |
| 1302 | |
| 1303 | initial = false; |
| 1304 | |
| 1305 | return value; |
| 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * Mark all the effects inside a skipped branch CLEAN, so that |
nothing calls this directly
no test coverage detected