()
| 866 | } |
| 867 | |
| 868 | apply() { |
| 869 | if (!async_mode_flag || (!this.is_fork && this.#prev === null && this.#next === null)) { |
| 870 | batch_values = null; |
| 871 | return; |
| 872 | } |
| 873 | |
| 874 | // if there are multiple batches, we are 'time travelling' — |
| 875 | // we need to override values with the ones in this batch... |
| 876 | batch_values = new Map(); |
| 877 | for (const [source, [value]] of this.current) { |
| 878 | batch_values.set(source, value); |
| 879 | } |
| 880 | |
| 881 | // ...and undo changes belonging to other batches unless they intersect |
| 882 | for (let batch = first_batch; batch !== null; batch = batch.#next) { |
| 883 | if (batch === this || batch.is_fork) continue; |
| 884 | |
| 885 | // If two batches intersect, the latter batch will be merged into the earlier batch, |
| 886 | // and we should treat them as a single set of changes |
| 887 | var intersects = false; |
| 888 | |
| 889 | if (batch.id < this.id) { |
| 890 | for (const [source, [, is_derived]] of batch.current) { |
| 891 | // Derived values don't partake in the intersection mechanism, because a derived could |
| 892 | // be triggered in one batch already but not the other one yet, causing a false-positive |
| 893 | if (is_derived) continue; |
| 894 | |
| 895 | if (this.current.has(source)) { |
| 896 | intersects = true; |
| 897 | break; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | // Since the latter batch merges into the earlier (if it resolves before the earlier one), |
| 903 | // we treat the earlier values as "already applied". This way we don't need to rerun async |
| 904 | // effects of the earlier batch in case they are merged. |
| 905 | // As a result you can think of batch_values as having the latest values of all intersecting |
| 906 | // batches up until this batch. |
| 907 | if (!intersects) { |
| 908 | for (const [source, previous] of batch.previous) { |
| 909 | if (!batch_values.has(source)) { |
| 910 | batch_values.set(source, previous); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * |
no test coverage detected