about summary refs log tree commit diff
path: root/src/platform/polyfills.web.ts
blob: 462f65a260588b8b9d049e1829dc75a09b02dc20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// @ts-ignore no decl -prf
import * as findLast from 'array.prototype.findlast'
/// <reference lib="dom" />

findLast.shim()

// @ts-ignore whatever typescript wants to complain about here, I dont care about -prf
window.setImmediate = (cb: () => void) => setTimeout(cb, 0)

if (process.env.NODE_ENV !== 'production') {
  // In development, react-native-web's <View> tries to validate that
  // text is wrapped into <Text>. It doesn't catch all cases but is useful.
  // Unfortunately, it only does that via console.error so it's easy to miss.
  // This is a hack to get it showing as a redbox on the web so we catch it early.
  const realConsoleError = console.error
  const thrownErrors = new WeakSet()
  console.error = function consoleErrorWrapper(msgOrError) {
    if (
      typeof msgOrError === 'string' &&
      msgOrError.startsWith('Unexpected text node')
    ) {
      if (
        msgOrError ===
        'Unexpected text node: . A text node cannot be a child of a <View>.'
      ) {
        // This is due to a stray empty string.
        // React already handles this fine, so RNW warning is a false positive. Ignore.
        return
      }
      const err = new Error(msgOrError)
      thrownErrors.add(err)
      throw err
    } else if (!thrownErrors.has(msgOrError)) {
      return realConsoleError.apply(this, arguments as any)
    }
  }
}