| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | // Used as a way to call batchedUpdates when we don't have a reference to |
| 9 | // the renderer. Such as when we're dispatching events or if third party |
| 10 | // libraries need to call batchedUpdates. Eventually, this API will go away when |
| 11 | // everything is batched by default. We'll then have a similar API to opt-out of |
| 12 | // scheduled work and instead do synchronous work. |
| 13 | |
| 14 | // Defaults |
| 15 | let batchedUpdatesImpl = function (fn, bookkeeping) { |
| 16 | return fn(bookkeeping); |
| 17 | }; |
| 18 | let discreteUpdatesImpl = function (fn, a, b, c, d) { |
| 19 | return fn(a, b, c, d); |
| 20 | }; |
| 21 | |
| 22 | let isInsideEventHandler = false; |
| 23 | |
| 24 | export function batchedUpdates(fn, bookkeeping) { |
| 25 | if (isInsideEventHandler) { |
| 26 | // If we are currently inside another batch, we need to wait until it |
| 27 | // fully completes before restoring state. |
| 28 | return fn(bookkeeping); |
| 29 | } |
| 30 | isInsideEventHandler = true; |
| 31 | try { |
| 32 | return batchedUpdatesImpl(fn, bookkeeping); |
| 33 | } finally { |
| 34 | isInsideEventHandler = false; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | export function discreteUpdates(fn, a, b, c, d) { |
| 39 | const prevIsInsideEventHandler = isInsideEventHandler; |
| 40 | isInsideEventHandler = true; |
| 41 | try { |
| 42 | return discreteUpdatesImpl(fn, a, b, c, d); |
| 43 | } finally { |
| 44 | isInsideEventHandler = prevIsInsideEventHandler; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | export function setBatchingImplementation( |
| 49 | _batchedUpdatesImpl, |
| 50 | _discreteUpdatesImpl, |
| 51 | ) { |
| 52 | batchedUpdatesImpl = _batchedUpdatesImpl; |
| 53 | discreteUpdatesImpl = _discreteUpdatesImpl; |
| 54 | } |