(start)
| 52 | * @since 5.7.0 |
| 53 | */ |
| 54 | export function createSubscriber(start) { |
| 55 | let subscribers = 0; |
| 56 | let version = source(0); |
| 57 | /** @type {(() => void) | void} */ |
| 58 | let stop; |
| 59 | |
| 60 | if (DEV) { |
| 61 | tag(version, class="st">'createSubscriber version'); |
| 62 | } |
| 63 | |
| 64 | return () => { |
| 65 | if (effect_tracking()) { |
| 66 | get(version); |
| 67 | |
| 68 | render_effect(() => { |
| 69 | if (subscribers === 0) { |
| 70 | stop = untrack(() => start(() => increment(version))); |
| 71 | } |
| 72 | |
| 73 | subscribers += 1; |
| 74 | |
| 75 | return () => { |
| 76 | queue_micro_task(() => { |
| 77 | class="cm">// Only count down after a microtask, else we would reach 0 before our own render effect reruns, |
| 78 | class="cm">// but reach 1 again when the tick callback of the prior teardown runs. That would mean we |
| 79 | class="cm">// re-subcribe unnecessarily and create a memory leak because the old subscription is never cleaned up. |
| 80 | subscribers -= 1; |
| 81 | |
| 82 | if (subscribers === 0) { |
| 83 | stop?.(); |
| 84 | stop = undefined; |
| 85 | class="cm">// Increment the version to ensure any dependent deriveds are marked dirty when the subscription is picked up again later. |
| 86 | class="cm">// If we didn't do this then the comparison of write versions would determine that the derived has a later version than |
| 87 | class="cm">// the subscriber, and it would not be re-run. |
| 88 | increment(version); |
| 89 | } |
| 90 | }); |
| 91 | }; |
| 92 | }); |
| 93 | } |
| 94 | }; |
| 95 | } |
nothing calls this directly
no test coverage detected