Skip to content

Commit 88c01dd

Browse files
Replace deprecated method InteractionManager.runAfterInteractions (#80)
Resolves: observation/app#1116
1 parent 03d1356 commit 88c01dd

5 files changed

Lines changed: 66 additions & 29 deletions

File tree

jest.mocks.js

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,58 @@
22
import React from 'react'
33
import { Image } from 'react-native'
44

5-
// Mock font awesome
6-
const Icon = 'Icon'
7-
const getIconType = (prefix) => {
8-
switch (prefix) {
9-
case 'fas':
10-
return 'solid'
11-
case 'fal':
12-
return 'light'
13-
case 'fab':
14-
return 'brand'
15-
default:
16-
throw new Error()
5+
// === Globals ===
6+
7+
if (typeof global.requestIdleCallback === 'undefined') {
8+
global.requestIdleCallback = function (callback) {
9+
return setTimeout(function () {
10+
callback({
11+
timeRemaining: function () {
12+
return 50
13+
},
14+
didTimeout: false,
15+
})
16+
}, 0)
1717
}
1818
}
19-
jest.mock('@fortawesome/react-native-fontawesome', () => ({
20-
FontAwesomeIcon: (faIcon) => (
21-
<Icon
22-
testID={faIcon.testID}
23-
color={faIcon.color}
24-
name={faIcon.icon.iconName}
25-
size={faIcon.size}
26-
style={faIcon.style}
27-
type={getIconType(faIcon.icon.prefix)}
28-
transform={faIcon.transform}
29-
/>
30-
),
31-
}))
19+
20+
if (typeof global.cancelIdleCallback === 'undefined') {
21+
global.cancelIdleCallback = function (id) {
22+
clearTimeout(id)
23+
}
24+
}
25+
26+
// === Third-party libraries ===
27+
28+
jest.mock('@fortawesome/react-native-fontawesome', () => {
29+
const Icon = 'Icon'
30+
31+
const getIconType = (prefix) => {
32+
switch (prefix) {
33+
case 'fas':
34+
return 'solid'
35+
case 'fal':
36+
return 'light'
37+
case 'fab':
38+
return 'brand'
39+
default:
40+
throw new Error()
41+
}
42+
}
43+
44+
return {
45+
FontAwesomeIcon: (faIcon) => (
46+
<Icon
47+
testID={faIcon.testID}
48+
color={faIcon.color}
49+
name={faIcon.icon.iconName}
50+
size={faIcon.size}
51+
style={faIcon.style}
52+
type={getIconType(faIcon.icon.prefix)}
53+
transform={faIcon.transform}
54+
/>
55+
),
56+
}
57+
})
3258

3359
Image.getSizeWithHeaders = jest.fn(() => Promise.resolve({ width: 0, height: 0 }))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@observation.org/react-native-components",
3-
"version": "1.67.0",
3+
"version": "1.68.0",
44
"main": "src/index.ts",
55
"repository": "git@github.com:observation/react-native-components.git",
66
"author": "Observation.org",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare global {
2+
function requestIdleCallback(
3+
callback: (deadline: { timeRemaining(): number; didTimeout: boolean }) => void,
4+
options?: { timeout: number },
5+
): number
6+
7+
function cancelIdleCallback(handle: number): void
8+
}
9+
10+
export {}

src/hooks/useShowBlurView.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState } from 'react'
2-
import { InteractionManager, Platform } from 'react-native'
2+
import { Platform } from 'react-native'
33

44
import { useFocusEffect } from '@react-navigation/native'
55

@@ -21,14 +21,14 @@ const useShowBlurView = () => {
2121
React.useCallback(() => {
2222
let timer: Timeout
2323
const timeout = isIOSDevice ? 0 : 200
24-
const task = InteractionManager.runAfterInteractions(() => {
24+
const taskHandle = requestIdleCallback(() => {
2525
timer = setTimeout(() => {
2626
setShowBlurView(true)
2727
}, timeout)
2828
})
2929

3030
return () => {
31-
task.cancel()
31+
cancelIdleCallback(taskHandle)
3232
clearTimeout(timer)
3333
setShowBlurView(isIOSDevice)
3434
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"extends": "@react-native/typescript-config",
3+
"typeRoots": ["./src/@types", "./node_modules/@types"],
34
"compilerOptions": {
45
"types": ["jest"],
56
},

0 commit comments

Comments
 (0)