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
141 changes: 141 additions & 0 deletions example/__tests__/navigation-lifecycle.harness.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import {
describe,
it,
expect,
render,
waitFor,
cleanup,
} from 'react-native-harness';
import { useEffect } from 'react';
import { ScreenContainer, Screen } from 'react-native-screens';
import {
RiveView,
RiveFileFactory,
Fit,
type RiveFile,
type RiveViewRef,
} from '@rive-app/react-native';
import type { ViewModelInstance } from '@rive-app/react-native';

const QUICK_START = require('../assets/rive/quick_start.riv');

const SCREEN_INACTIVE = 0 as const;
const SCREEN_ACTIVE = 2 as const;

function expectDefined<T>(value: T): asserts value is NonNullable<T> {
expect(value).toBeDefined();
}

type TestContext = {
ref: RiveViewRef | null;
error: string | null;
};

function ScreenWithRive({
file,
instance,
context,
activityState,
}: {
file: RiveFile;
instance: ViewModelInstance;
context: TestContext;
activityState: typeof SCREEN_INACTIVE | typeof SCREEN_ACTIVE;
}) {
useEffect(() => {
return () => {
context.ref = null;
};
}, [context]);

return (
<ScreenContainer style={{ width: 200, height: 200 }}>
<Screen activityState={activityState} style={{ flex: 1 }}>
<RiveView
hybridRef={{
f: (ref: RiveViewRef | null) => {
context.ref = ref;
},
}}
style={{ flex: 1 }}
file={file}
autoPlay={false}
dataBind={instance}
fit={Fit.Contain}
stateMachineName="State Machine 1"
onError={(e) => {
context.error = e.message;
}}
/>
</Screen>
</ScreenContainer>
);
}

describe('navigation lifecycle (PR #46)', () => {
it('RiveView preserves state after screen detach/reattach', async () => {
const file = await RiveFileFactory.fromSource(QUICK_START, undefined);
const vm = file.defaultArtboardViewModel();
expectDefined(vm);
const instance = vm.createDefaultInstance();
expectDefined(instance);

const context: TestContext = { ref: null, error: null };

const { rerender } = await render(
<ScreenWithRive
file={file}
instance={instance}
context={context}
activityState={SCREEN_ACTIVE}
/>
);

await waitFor(
() => {
expect(context.ref).not.toBeNull();
},
{ timeout: 5000 }
);
await context.ref!.awaitViewReady();

// Set health to 75 via the native view's VMI
const boundVmi = context.ref!.getViewModelInstance();
expectDefined(boundVmi);
const health = boundVmi.numberProperty('health');
expectDefined(health);
health.value = 75;

// Detach screen (simulates navigating away — triggers onDetachedFromWindow)
await rerender(
<ScreenWithRive
file={file}
instance={instance}
context={context}
activityState={SCREEN_INACTIVE}
/>
);
await new Promise((r) => setTimeout(r, 300));

// Reattach screen (simulates navigating back)
await rerender(
<ScreenWithRive
file={file}
instance={instance}
context={context}
activityState={SCREEN_ACTIVE}
/>
);
await new Promise((r) => setTimeout(r, 300));

// Query the VMI from the native view after reattach
expect(context.error).toBeNull();
const reattachedVmi = context.ref!.getViewModelInstance();
expectDefined(reattachedVmi);
const reattachedHealth = reattachedVmi.numberProperty('health');
expectDefined(reattachedHealth);
expect(reattachedHealth.value).toBe(75);

cleanup();
});
});
Binary file added example/assets/rive/counter.riv
Binary file not shown.
13 changes: 11 additions & 2 deletions example/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const path = require('path');
const { getDefaultConfig } = require('@react-native/metro-config');
const { getConfig } = require('react-native-builder-bob/metro-config');
const { withRnHarness } = require('react-native-harness/metro');
const { withSingleReactNative } = require('./metro.helpers');
const {
withSingleReactNative,
withBlockedSiblingDeps,
} = require('./metro.helpers');

const root = path.resolve(__dirname, '..');

Expand All @@ -21,4 +24,10 @@ const finalConfig = getConfig(config, {
project: __dirname,
});

module.exports = withRnHarness(withSingleReactNative(finalConfig, __dirname));
const expoExampleDir = path.resolve(root, 'expo-example');
module.exports = withRnHarness(
withSingleReactNative(
withBlockedSiblingDeps(finalConfig, __dirname, expoExampleDir),
__dirname
)
);
49 changes: 48 additions & 1 deletion example/metro.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,51 @@ function withSingleReactNative(config, projectDir) {
};
}

module.exports = { withSingleReactNative };
/**
* Blocks a sibling workspace's node_modules for any dependencies shared between the two.
* Prevents duplicate native module registration when builder-bob watches the monorepo root.
*
* @param {import('metro-config').MetroConfig} config - Metro configuration
* @param {string} projectDir - This project's directory
* @param {string} siblingDir - The sibling workspace's directory
* @returns {import('metro-config').MetroConfig}
*/
function withBlockedSiblingDeps(config, projectDir, siblingDir) {
const myPkg = require(path.resolve(projectDir, 'package.json'));
const siblingPkg = require(path.resolve(siblingDir, 'package.json'));

const myDeps = new Set([
...Object.keys(myPkg.dependencies || {}),
...Object.keys(myPkg.devDependencies || {}),
]);
const siblingDeps = [
...Object.keys(siblingPkg.dependencies || {}),
...Object.keys(siblingPkg.devDependencies || {}),
];

const shared = siblingDeps.filter((dep) => myDeps.has(dep));
if (shared.length === 0) return config;

const patterns = shared.map((dep) => {
const escaped = path
.resolve(siblingDir, 'node_modules', dep)
.replace(/[/\\]/g, '[/\\\\]');
return new RegExp(escaped + '[/\\\\].*$');
});

const existing = config.resolver?.blockList;
const blockList = [
...(existing ? (Array.isArray(existing) ? existing : [existing]) : []),
...patterns,
];

return {
...config,
resolver: {
...config.resolver,
blockList,
},
};
}

module.exports = { withSingleReactNative, withBlockedSiblingDeps };
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
"react": "19.0.0",
"react-native": "0.79.2",
"react-native-gesture-handler": "2.29.1",
"react-native-nitro-modules": "0.33.2",
"react-native-nitro-modules": "0.35.0",
"react-native-reanimated": "4.1.5",
"react-native-safe-area-context": "^5.4.0",
"react-native-screens": "~4.18.0",
"react-native-worklets": "0.6.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions expo-example/app/[pageId].tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@example/polyfills';
import { useLocalSearchParams, Stack } from 'expo-router';
import { PagesList, type PageItem } from '@example/PagesList';
import { View, Text, StyleSheet } from 'react-native';
Expand Down
1 change: 1 addition & 0 deletions expo-example/app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@example/polyfills';
import {
DarkTheme,
DefaultTheme,
Expand Down
1 change: 1 addition & 0 deletions expo-example/app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@example/polyfills';
import { useEffect, useState } from 'react';
import { StyleSheet, TouchableOpacity } from 'react-native';
import { useRouter } from 'expo-router';
Expand Down
1 change: 1 addition & 0 deletions expo-example/components/ui/icon-symbol.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const MAPPING = {
'paperplane.fill': 'send',
'chevron.left.forwardslash.chevron.right': 'code',
'chevron.right': 'chevron-right',
'wrench.fill': 'build',
} as IconMapping;

/**
Expand Down
11 changes: 9 additions & 2 deletions expo-example/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
const { getDefaultConfig } = require('expo/metro-config');
const { getConfig } = require('react-native-builder-bob/metro-config');
const path = require('path');
const { withSingleReactNative } = require('../example/metro.helpers');
const {
withSingleReactNative,
withBlockedSiblingDeps,
} = require('../example/metro.helpers');

const root = path.resolve(__dirname, '..');

Expand Down Expand Up @@ -60,4 +63,8 @@ const configWithAlias = {
},
};

module.exports = withSingleReactNative(configWithAlias, __dirname);
const exampleDir = path.resolve(root, 'example');
module.exports = withSingleReactNative(
withBlockedSiblingDeps(configWithAlias, __dirname, exampleDir),
__dirname
);
2 changes: 1 addition & 1 deletion expo-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"react-dom": "19.1.0",
"react-native": "0.81.5",
"react-native-gesture-handler": "2.29.1",
"react-native-nitro-modules": "0.33.2",
"react-native-nitro-modules": "0.35.0",
"react-native-reanimated": "4.1.5",
"react-native-safe-area-context": "~5.6.0",
"react-native-screens": "~4.16.0",
Expand Down
2 changes: 1 addition & 1 deletion ios/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum DataSource {
return .bundle(resource: name, extension: ext.isEmpty ? nil : ext)
}

static func bytes(from buffer: ArrayBufferHolder) -> DataSource {
static func bytes(from buffer: ArrayBuffer) -> DataSource {
return .bytes(data: buffer.toData(copyIfNeeded: false))
}

Expand Down
2 changes: 1 addition & 1 deletion ios/HybridRiveFileFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ final class HybridRiveFileFactory: HybridRiveFileFactorySpec, @unchecked Sendabl
return try fromResource(resource: resource, loadCdn: loadCdn, referencedAssets: nil)
}

func fromBytes(bytes: ArrayBufferHolder, loadCdn: Bool, referencedAssets: ReferencedAssetsType?)
func fromBytes(bytes: ArrayBuffer, loadCdn: Bool, referencedAssets: ReferencedAssetsType?)
throws -> Promise<
(any HybridRiveFileSpec)
> {
Expand Down
2 changes: 1 addition & 1 deletion ios/HybridRiveImageFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class HybridRiveImageFactory: HybridRiveImageFactorySpec {
return loadFromDataSource(.bundle(nameWithExtension: resource))
}

func loadFromBytesAsync(bytes: ArrayBufferHolder) throws -> Promise<(any HybridRiveImageSpec)> {
func loadFromBytesAsync(bytes: ArrayBuffer) throws -> Promise<(any HybridRiveImageSpec)> {
return loadFromDataSource(.bytes(from: bytes))
}
}
44 changes: 19 additions & 25 deletions nitrogen/generated/android/c++/JHybridBindableArtboardSpec.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading