-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workletizable Context Objects (#6229)
This pull request introduces the concept of _Context Objects_ on the Worklet Runtime. ## What? Context Object is a shareable object that doesn't lose its `this` binding when it goes to the Worklet Runtime. To define an object as a Context Object, the user has to define property `__workletObject` on it. (Should it be `__contextObject`?) ## Why? Currently it's not possible to refer to an object's methods from its other methods. ```ts const obj = { foo() { console.log("foo"); } bar() { this.foo(); } } runOnUI(() => { obj.bar(); // Crash, the binding is lost on serialization. })(); ``` ## How? Context Objects are handled as another type of shareable entity on the React Runtime. 1. The plugin adds a special property `__workletObjectFactory` to Context Objects. It's a function that creates an identical object. 2. We serialize the factory instead of the object itself. 3. We recreate the object on the Worklet Runtime via ShareableHandle mechanism. ## Test plan - [x] Add unit tests - [x] Add a runtime test suite
- Loading branch information
Showing
8 changed files
with
515 additions
and
4 deletions.
There are no files selected for viewing
178 changes: 178 additions & 0 deletions
178
apps/common-app/src/examples/RuntimeTests/tests/plugin/contextObjects.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import React, { useEffect } from 'react'; | ||
import { View } from 'react-native'; | ||
import { useSharedValue, runOnUI } from 'react-native-reanimated'; | ||
import { | ||
render, | ||
wait, | ||
describe, | ||
getRegisteredValue, | ||
registerValue, | ||
test, | ||
expect, | ||
} from '../../ReanimatedRuntimeTestsRunner/RuntimeTestsApi'; | ||
|
||
const SHARED_VALUE_REF = 'SHARED_VALUE_REF'; | ||
|
||
describe('Test context objects', () => { | ||
test('methods are workletized', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo() { | ||
return 1; | ||
}, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
output.value = contextObject.foo(); | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(1); | ||
}); | ||
|
||
test('properties are workletized', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo: () => 1, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
output.value = contextObject.foo(); | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(1); | ||
}); | ||
|
||
test('methods preserve implicit context', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo() { | ||
return 1; | ||
}, | ||
bar() { | ||
return this.foo() + 1; | ||
}, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
output.value = contextObject.bar(); | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(2); | ||
}); | ||
|
||
test('methods preserve explicit context', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo() { | ||
return 1; | ||
}, | ||
bar() { | ||
return this.foo.call(contextObject) + 1; | ||
}, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
output.value = contextObject.bar(); | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(2); | ||
}); | ||
|
||
test('methods change the state of the object', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo: 1, | ||
bar() { | ||
this.foo += 1; | ||
}, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
contextObject.bar(); | ||
output.value = contextObject.foo; | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(2); | ||
}); | ||
|
||
test("the object doesn't persist in memory", async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo: 1, | ||
bar() { | ||
this.foo += 1; | ||
}, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
contextObject.bar(); | ||
output.value = contextObject.foo; | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(2); | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue2 = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue2.onUI).toBe(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.