Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Transformer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Platform } from 'react-native';

export type Selection = { start: number; end: number };

export type TransformerWorklet = (input: {
Expand All @@ -17,11 +19,17 @@ export class Transformer {
private readonly _worklet: TransformerWorklet;

constructor(worklet: TransformerWorklet) {
const workletHash = (worklet as { __workletHash?: number }).__workletHash;
if (workletHash == null) {
throw new Error(
'[rntti] Transformer must be a worklet. Did you forget to add the "worklet" directive?',
);
// On web there is no UI runtime: the web TransformerTextInput runs the
// worklet synchronously in JS, so it doesn't need to be compiled by the
// worklets plugin. Native runs it on the UI thread and requires a real
// worklet, so keep enforcing the directive there.
if (Platform.OS !== 'web') {
const workletHash = (worklet as { __workletHash?: number }).__workletHash;
if (workletHash == null) {
throw new Error(
'[rntti] Transformer must be a worklet. Did you forget to add the "worklet" directive?',
);
}
}
this._worklet = worklet;
}
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/Transformer.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Platform } from 'react-native';

import { Transformer } from '../Transformer';

describe('Transformer', () => {
Expand All @@ -10,6 +12,18 @@ describe('Transformer', () => {
).toThrow(/Transformer must be a worklet/i);
});

it('does not require a compiled worklet on web', () => {
// On web the transformer runs synchronously in JS, so a plain function
// (no worklets plugin) is accepted.
jest.replaceProperty(Platform, 'OS', 'web');
expect(
() =>
new Transformer(({ value }) => {
return { value };
}),
).not.toThrow();
});

it('accepts a worklet transformer', () => {
expect(
() =>
Expand Down
Loading