Skip to content

Commit

Permalink
feat: allow implicit detection of context objects
Browse files Browse the repository at this point in the history
  • Loading branch information
tjzel committed Jul 17, 2024
1 parent ce914e6 commit 4c2e509
Show file tree
Hide file tree
Showing 10 changed files with 487 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default function RuntimeTestsExample() {
},
},
{
testSuiteName: 'babelPlugin',
testSuiteName: 'babel plugin',
importTest: () => {
require('./tests/plugin/fileWorkletization.test');
require('./tests/plugin/contextObjects.test');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Test context objects', () => {
foo() {
return 1;
},
__workletObject: true,
__workletContextObject: true,
};

useEffect(() => {
Expand All @@ -45,7 +45,7 @@ describe('Test context objects', () => {
registerValue(SHARED_VALUE_REF, output);
const contextObject = {
foo: () => 1,
__workletObject: true,
__workletContextObject: true,
};

useEffect(() => {
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('Test context objects', () => {
bar() {
return this.foo() + 1;
},
__workletObject: true,
__workletContextObject: true,
};

useEffect(() => {
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('Test context objects', () => {
bar() {
return this.foo.call(contextObject) + 1;
},
__workletObject: true,
__workletContextObject: true,
};

useEffect(() => {
Expand All @@ -127,7 +127,7 @@ describe('Test context objects', () => {
bar() {
this.foo += 1;
},
__workletObject: true,
__workletContextObject: true,
};

useEffect(() => {
Expand All @@ -154,7 +154,7 @@ describe('Test context objects', () => {
bar() {
this.foo += 1;
},
__workletObject: true,
__workletContextObject: true,
};

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,46 @@ import {
test,
expect,
} from '../../ReanimatedRuntimeTestsRunner/RuntimeTestsApi';
import { getThree } from './fileWorkletization';
import { getThree, implicitContextObject } from './fileWorkletization';

const SHARED_VALUE_REF = 'SHARED_VALUE_REF';

describe('Test workletization', () => {
const ExampleComponent = () => {
const output = useSharedValue<number | null>(null);
registerValue(SHARED_VALUE_REF, output);
describe('Test file workletization', () => {
test('Functions and methods are workletized', async () => {
const ExampleComponent = () => {
const output = useSharedValue<number | null>(null);
registerValue(SHARED_VALUE_REF, output);

useEffect(() => {
runOnUI(() => {
output.value = getThree();
})();
});
useEffect(() => {
runOnUI(() => {
output.value = getThree();
})();
});

return <View />;
};

test('Test file workletization', async () => {
return <View />;
};
await render(<ExampleComponent />);
await wait(100);
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF);
expect(sharedValue.onUI).toBe(3);
});

test('WorkletContextObjects are workletized', async () => {
const ExampleComponent = () => {
const output = useSharedValue<number | null>(null);
registerValue(SHARED_VALUE_REF, output);

useEffect(() => {
runOnUI(() => {
output.value = implicitContextObject.getFive();
})();
});

return <View />;
};
await render(<ExampleComponent />);
await wait(100);
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF);
expect(sharedValue.onUI).toBe(5);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ const getterContainer = {
export const getThree = () => {
return getOne() + getterContainer.getTwo();
};

export const implicitContextObject = {
getFour() {
return 4;
},
getFive() {
return this.getFour() + 1;
},
};
Loading

0 comments on commit 4c2e509

Please sign in to comment.