'use strict'; import { isBundleModeEnabled } from '../debug/bundleMode'; import { checkCppVersion } from '../debug/checkCppVersion'; import { jsVersion } from '../debug/jsVersion'; import { installCustomSerializableUnpacker } from '../memory/customSerializableUnpacker'; import { installRemoteFunctionUnpacker } from '../memory/remoteFunctionUnpacker'; import { installShareableGuestUnpacker } from '../memory/shareableGuestUnpacker'; import { installShareableHostUnpacker } from '../memory/shareableHostUnpacker'; import { installSynchronizableUnpacker } from '../memory/synchronizableUnpacker'; import type { RemoteFunction, SerializableRef, SynchronizableRef, } from '../memory/types'; import { installValueUnpacker } from '../memory/valueUnpacker'; import { isRNRuntime } from '../runtimeKind'; import { WorkletsTurboModule } from '../specs'; import type { WorkletFunction, WorkletRuntime } from '../types'; import type { IWorkletsModule, WorkletsModuleProxy, } from './workletsModuleProxy'; class NativeWorklets implements IWorkletsModule { #workletsModuleProxy: WorkletsModuleProxy; #serializableUndefined: SerializableRef; #serializableNull: SerializableRef; #serializableTrue: SerializableRef; #serializableFalse: SerializableRef; constructor() { const bundleModeEnabled = isBundleModeEnabled(); globalThis._WORKLETS_VERSION_JS = jsVersion; const onRNRuntime = isRNRuntime(); if (globalThis.__workletsModuleProxy === undefined && onRNRuntime) { WorkletsTurboModule?.installTurboModule(bundleModeEnabled); if (!bundleModeEnabled) { installUnpackers(globalThis.__workletsModuleProxy); } WorkletsTurboModule?.start(); } if (globalThis.__workletsModuleProxy === undefined) { throw new Error( `[Worklets] Native part of Worklets doesn't seem to be initialized. See https://docs.swmansion.com/react-native-worklets/docs/guides/troubleshooting#native-part-of-worklets-doesnt-seem-to-be-initialized for more details.` ); } if (__DEV__ && onRNRuntime) { checkCppVersion(); } this.#workletsModuleProxy = globalThis.__workletsModuleProxy; this.#serializableNull = this.#workletsModuleProxy.createSerializableNull(); this.#serializableUndefined = this.#workletsModuleProxy.createSerializableUndefined(); this.#serializableTrue = this.#workletsModuleProxy.createSerializableBoolean(true); this.#serializableFalse = this.#workletsModuleProxy.createSerializableBoolean(false); } createSerializableImport( from: string, to: string ): SerializableRef { return this.#workletsModuleProxy.createSerializableImport(from, to); } createSerializableString(str: string) { return this.#workletsModuleProxy.createSerializableString(str); } createSerializableNumber(num: number) { return this.#workletsModuleProxy.createSerializableNumber(num); } createSerializableBoolean(bool: boolean) { return bool ? this.#serializableTrue : this.#serializableFalse; } createSerializableBigInt(bigInt: bigint) { return this.#workletsModuleProxy.createSerializableBigInt(bigInt); } createSerializableUndefined() { return this.#serializableUndefined; } createSerializableNull() { return this.#serializableNull; } createSerializableTurboModuleLike< TProps extends object, TProto extends object, >(props: TProps, proto: TProto): SerializableRef { return this.#workletsModuleProxy.createSerializableTurboModuleLike( props, proto ); } createSerializableObject( obj: T, shouldRetainRemote: boolean, nativeStateSource?: object ): SerializableRef { return this.#workletsModuleProxy.createSerializableObject( obj, shouldRetainRemote, nativeStateSource ); } createSerializableHostObject(obj: T) { return this.#workletsModuleProxy.createSerializableHostObject(obj); } createSerializableArray( array: unknown[], shouldRetainRemote: boolean = false ) { return this.#workletsModuleProxy.createSerializableArray( array, shouldRetainRemote ); } createSerializableArrayBuffer(arrayBuffer: ArrayBuffer) { return this.#workletsModuleProxy.createSerializableArrayBuffer(arrayBuffer); } createSerializableMap( keys: TKey[], values: TValue[] ): SerializableRef> { return this.#workletsModuleProxy.createSerializableMap(keys, values); } createSerializableSet( values: TValues[] ): SerializableRef> { return this.#workletsModuleProxy.createSerializableSet(values); } createSerializableError( name: string, message: string, stack: string | undefined ): SerializableRef { return this.#workletsModuleProxy.createSerializableError( name, message, stack ); } createSerializableRegExp( pattern: string, flags: string ): SerializableRef { return this.#workletsModuleProxy.createSerializableRegExp(pattern, flags); } createSerializableArrayBufferView( typeName: string, buffer: ArrayBuffer, byteOffset: number, length: number ): SerializableRef { return this.#workletsModuleProxy.createSerializableArrayBufferView( typeName, buffer, byteOffset, length ); } createSerializableNonWorkletFunction( fun: (...args: TArgs) => TReturn, functionName: string | undefined ): SerializableRef<(...args: TArgs) => TReturn> { return this.#workletsModuleProxy.createSerializableNonWorkletFunction( fun, functionName ); } createSerializableWorklet(worklet: object, shouldPersistRemote: boolean) { return this.#workletsModuleProxy.createSerializableWorklet( worklet, shouldPersistRemote ); } makeWeakSerializableRef(serializable: SerializableRef): object { return this.#workletsModuleProxy.makeWeakSerializableRef(serializable); } derefWeakSerializableRef( marker: object ): SerializableRef | undefined { return this.#workletsModuleProxy.derefWeakSerializableRef(marker); } createCustomSerializable( data: SerializableRef, typeId: number ): SerializableRef { return this.#workletsModuleProxy.createCustomSerializable(data, typeId); } registerCustomSerializable( determine: SerializableRef, pack: SerializableRef, unpack: SerializableRef, typeId: number ): void { this.#workletsModuleProxy.registerCustomSerializable( determine, pack, unpack, typeId ); } createShareable( hostRuntimeId: number, initial: SerializableRef, initSynchronously: boolean, decorateHost: SerializableRef, decorateRef: SerializableRef ): SerializableRef { return this.#workletsModuleProxy.createShareable( hostRuntimeId, initial, initSynchronously, decorateHost, decorateRef ); } scheduleOnRN( fun: RemoteFunction | ((...args: TArgs) => unknown), args: SerializableRef | undefined, scheduleStack?: string ): void { this.#workletsModuleProxy.scheduleOnRN(fun, args, scheduleStack); } runOnRNSync( fun: RemoteFunction | SerializableRef | ((...args: TArgs) => unknown), args: SerializableRef | undefined, scheduleStack?: string ): TReturn { return this.#workletsModuleProxy.runOnRNSync(fun, args, scheduleStack); } scheduleOnUI( serializableArrayOfWorklets: SerializableRef, serializableArrayOfArguments: SerializableRef, scheduleStacks: string[] | undefined ) { return this.#workletsModuleProxy.scheduleOnUI( serializableArrayOfWorklets, serializableArrayOfArguments, scheduleStacks ); } runOnUISync( worklet: SerializableRef, scheduleStack: string | undefined ): TReturn { return this.#workletsModuleProxy.runOnUISync(worklet, scheduleStack); } createWorkletRuntime( name: string, initializer: SerializableRef<() => void>, useDefaultQueue: boolean, customQueue: object | undefined, enableEventLoop: boolean, enableLocking: boolean, enableNetworking: boolean, abortable: boolean ) { return this.#workletsModuleProxy.createWorkletRuntime( name, initializer, useDefaultQueue, customQueue, enableEventLoop, enableLocking, enableNetworking, abortable ); } scheduleOnRuntime( workletRuntime: WorkletRuntime, serializableWorklet: SerializableRef, scheduleStack: string | undefined, resolve?: SerializableRef, reject?: SerializableRef ) { return this.#workletsModuleProxy.scheduleOnRuntime( workletRuntime, serializableWorklet, scheduleStack, resolve, reject ); } scheduleOnRuntimeWithId( runtimeId: number, worklet: SerializableRef, scheduleStack: string | undefined, resolve?: SerializableRef, reject?: SerializableRef ) { return this.#workletsModuleProxy.scheduleOnRuntimeWithId( runtimeId, worklet, scheduleStack, resolve, reject ); } abortWorkletRuntime(runtimeId: number): boolean { return this.#workletsModuleProxy.abortWorkletRuntime(runtimeId); } runOnRuntimeSync( workletRuntime: WorkletRuntime, worklet: SerializableRef, scheduleStack: string | undefined ): TReturn { return this.#workletsModuleProxy.runOnRuntimeSync( workletRuntime, worklet, scheduleStack ); } runOnRuntimeSyncWithId( runtimeId: number, worklet: SerializableRef, scheduleStack: string | undefined ): TReturn { return this.#workletsModuleProxy.runOnRuntimeSyncWithId( runtimeId, worklet, scheduleStack ); } handlePromise( resolveOrReject: | ((value: TValue | PromiseLike) => void) | RemoteFunction, valueOrError: SerializableRef ) { return this.#workletsModuleProxy.handlePromise( resolveOrReject, valueOrError ); } createSynchronizable( value: SerializableRef | TValue, isFixed: boolean ): SynchronizableRef { return this.#workletsModuleProxy.createSynchronizable(value, isFixed); } synchronizableGetDirty( synchronizableRef: SynchronizableRef ): TValue { return this.#workletsModuleProxy.synchronizableGetDirty(synchronizableRef); } synchronizableGetBlocking( synchronizableRef: SynchronizableRef ): TValue { return this.#workletsModuleProxy.synchronizableGetBlocking( synchronizableRef ); } synchronizableSetBlocking( synchronizableRef: SynchronizableRef, value: SerializableRef | TValue ) { return this.#workletsModuleProxy.synchronizableSetBlocking( synchronizableRef, value ); } synchronizableSetDirty( synchronizableRef: SynchronizableRef, value: TValue ): void { return this.#workletsModuleProxy.synchronizableSetDirty( synchronizableRef, value ); } synchronizableLock( synchronizableRef: SynchronizableRef ): void { return this.#workletsModuleProxy.synchronizableLock(synchronizableRef); } synchronizableUnlock( synchronizableRef: SynchronizableRef ): void { return this.#workletsModuleProxy.synchronizableUnlock(synchronizableRef); } reportFatalErrorOnJS(message: string, stack: string, name: string) { return this.#workletsModuleProxy.reportFatalErrorOnJS(message, stack, name); } getStaticFeatureFlag(name: string): boolean { return this.#workletsModuleProxy.getStaticFeatureFlag(name); } setDynamicFeatureFlag(name: string, value: boolean) { this.#workletsModuleProxy.setDynamicFeatureFlag(name, value); } isRuntimeAlive(runtimeId: number): boolean { return this.#workletsModuleProxy.isRuntimeAlive(runtimeId); } getCurrentThreadId(): string { return this.#workletsModuleProxy.getCurrentThreadId(); } getUIRuntimeHolder(): object { return this.#workletsModuleProxy.getUIRuntimeHolder(); } getUISchedulerHolder(): object { return this.#workletsModuleProxy.getUISchedulerHolder(); } toggleSlowAnimationsOnUIRuntime(): boolean { return WorkletsTurboModule?.toggleSlowAnimationsOnUIRuntime() ?? false; } } export const WorkletsModule: IWorkletsModule = new NativeWorklets(); function installUnpackers(workletsModuleProxy: WorkletsModuleProxy) { const value = (installValueUnpacker as WorkletFunction).__initData!; const synchronizable = (installSynchronizableUnpacker as WorkletFunction) .__initData!; const customSerializable = ( installCustomSerializableUnpacker as WorkletFunction ).__initData!; const shareableHost = (installShareableHostUnpacker as WorkletFunction) .__initData!; const shareableGuest = (installShareableGuestUnpacker as WorkletFunction) .__initData!; const remoteFunction = (installRemoteFunctionUnpacker as WorkletFunction) .__initData!; if (value.bytecode !== undefined) { if (__DEV__) { throw new Error( '[Worklets] Unpackers were compiled to Hermes bytecode in a development build, which is not supported.' ); } workletsModuleProxy.loadUnpackersWithBytecode( value.bytecode, synchronizable.bytecode!, customSerializable.bytecode!, shareableHost.bytecode!, shareableGuest.bytecode!, remoteFunction.bytecode! ); } else { workletsModuleProxy.loadUnpackersWithCode( value.code!, value.location ?? '', value.sourceMap ?? '', synchronizable.code!, synchronizable.location ?? '', synchronizable.sourceMap ?? '', customSerializable.code!, customSerializable.location ?? '', customSerializable.sourceMap ?? '', shareableHost.code!, shareableHost.location ?? '', shareableHost.sourceMap ?? '', shareableGuest.code!, shareableGuest.location ?? '', shareableGuest.sourceMap ?? '', remoteFunction.code!, remoteFunction.location ?? '', remoteFunction.sourceMap ?? '', __DEV__ ); } }