-
-
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 (#6284)
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 `__workletContextObject` on it. The value of this property doesn't matter. ```ts // Explicit Context Object const obj = { foo() { return 1; }, bar() { return this.foo(); }, __workletContextObject: true, } ``` Context Objects are also auto-detected when Babel processes files marked with a worklet directive. If a top level object has a method that references `this` inside we treat is as a Context Object. ```ts 'worklet'; // Implicit Context Object const obj = { foo() { return 1; }, bar() { return this.foo(); }, }; ``` ## 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
12 changed files
with
884 additions
and
90 deletions.
There are no files selected for viewing
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
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('non-context methods are workletized', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo() { | ||
return 1; | ||
}, | ||
__workletContextObject: 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('non-context properties are workletized', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo: () => 1, | ||
__workletContextObject: 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; | ||
}, | ||
__workletContextObject: 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; | ||
}, | ||
__workletContextObject: 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; | ||
}, | ||
__workletContextObject: 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; | ||
}, | ||
__workletContextObject: 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.