polyfill structuredclone on legacy browsers
Fixes APP-T4SK
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import {RichText} from '@bsky/sdk/richtext'
|
||||
|
||||
describe('web polyfills', () => {
|
||||
const nativeStructuredClone = globalThis.structuredClone
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.structuredClone = nativeStructuredClone
|
||||
})
|
||||
|
||||
it('supports rich text when structuredClone is unavailable', async () => {
|
||||
Reflect.deleteProperty(globalThis, 'structuredClone')
|
||||
|
||||
let structuredCloneReady: Promise<void> | undefined
|
||||
jest.isolateModules(() => {
|
||||
const polyfills =
|
||||
require('./polyfills.web') as typeof import('./polyfills.web')
|
||||
structuredCloneReady = polyfills.structuredCloneReady
|
||||
})
|
||||
await structuredCloneReady
|
||||
|
||||
const richText = new RichText(
|
||||
{text: 'hello\n\n\nworld'},
|
||||
{cleanNewlines: true},
|
||||
)
|
||||
|
||||
expect(richText.text).toBe('hello\n\nworld')
|
||||
expect(richText.clone()).toEqual(richText)
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,16 @@
|
||||
import 'array.prototype.findlast/auto'
|
||||
import 'setimmediate'
|
||||
|
||||
export const structuredCloneReady: Promise<void> =
|
||||
typeof globalThis.structuredClone === 'function'
|
||||
? Promise.resolve()
|
||||
: import('@ungap/structured-clone').then(({default: structuredClone}) => {
|
||||
if (typeof globalThis.structuredClone !== 'function') {
|
||||
globalThis.structuredClone =
|
||||
structuredClone as typeof globalThis.structuredClone
|
||||
}
|
||||
})
|
||||
|
||||
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.
|
||||
@@ -30,5 +40,3 @@ if (process.env.NODE_ENV !== 'production') {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {}
|
||||
|
||||
Reference in New Issue
Block a user