Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(android): apply incoming share fixes to bundled entrypoint

tianma-if9461e7863a74
diff --git a/apps/mobile/src/lib/incoming-share-hook.test.ts b/apps/mobile/src/lib/incoming-share-hook.test.ts
index 7e4d7618..6e4b7471 100644
--- a/apps/mobile/src/lib/incoming-share-hook.test.ts
+++ b/apps/mobile/src/lib/incoming-share-hook.test.ts
@@ -1,9 +1,9 @@
import { describe, expect, test } from "bun:test";
import { readFileSync } from "node:fs";
-// Exercise the patched dependency's actual hook without booting React Native.
+// Exercise the distributed entrypoint Metro bundles, rather than only its TypeScript source.
const hookSource = readFileSync(
- new URL("../../node_modules/expo-sharing/src/useIncomingShare.ts", import.meta.url), "utf8",
+ new URL("../../node_modules/expo-sharing/build/useIncomingShare.js", import.meta.url), "utf8",
);
const hookCode = new Bun.Transpiler({ loader: "ts" }).transformSync(hookSource)
.replace(/^import .* from [^;]+;\n/gm, "")
@@ -17,11 +17,20 @@ const deferred = () => {
return { promise, resolve, reject };
};
-function mountHook() {
+function mountHook(initialPayloads = article) {
const slots: unknown[] = [];
let cursor = 0;
- let nativePayloads = article;
+ let nativePayloads = initialPayloads;
const requests: ReturnType<typeof deferred>[] = [];
+ const effects: (() => void | (() => void))[] = [];
+ const listeners = new Map<string, (event: unknown) => void>();
+ const eventSource = (prefix: string) => ({
+ addEventListener: (event: string, callback: (value: unknown) => void) => {
+ const key = `${prefix}:${event}`;
+ listeners.set(key, callback);
+ return { remove: () => { listeners.delete(key); } };
+ },
+ });
const useState = (initial: unknown) => {
const index = cursor++;
if (!(index in slots)) slots[index] = initial;
@@ -33,22 +42,83 @@ function mountHook() {
return slots[index];
};
const hook = new Function(
- "useState", "useRef", "useCallback", "useEffect", "AppState",
+ "useState", "useRef", "useCallback", "useEffect", "AppState", "Linking",
"clearNativeSharedPayloads", "getSharedPayloads", "getResolvedSharedPayloadsAsync",
`${hookCode}\nreturn useIncomingShare;`,
)(
- useState, useRef, (callback: unknown) => callback, () => {}, {},
+ useState, useRef, (callback: unknown) => callback,
+ (effect: () => void | (() => void)) => { effects.push(effect); },
+ eventSource("app"), eventSource("link"),
() => { nativePayloads = []; }, () => nativePayloads,
() => { const request = deferred(); requests.push(request); return request.promise; },
);
return {
render: () => { cursor = 0; return hook(); },
- shareAgain: () => { nativePayloads = article; },
+ shareAgain: (payloads = article) => { nativePayloads = payloads; },
+ mount: () => effects.shift()?.(),
+ foreground: () => listeners.get("app:change")?.("active"),
+ incomingShare: () => listeners.get("link:url")?.({ url: "edgeever://expo-sharing" }),
requests,
};
}
describe("incoming share hook", () => {
+ test("reports an empty image resolution instead of leaving the share waiting forever", async () => {
+ const images = [{ value: "content://com.zhihu.android/shared-image.jpg", mimeType: "image/jpeg", shareType: "image" }];
+ const app = mountHook(images);
+ const pending = app.render().refreshSharePayloads();
+ app.requests[0]!.resolve([]);
+ await pending;
+ expect(app.render().isResolving).toBe(false);
+ expect(app.render().error?.message).toBe("Shared content could not be resolved");
+ expect(app.render().sharedPayloads).toEqual(images);
+ expect(app.render().resolvedSharedPayloads).toEqual([]);
+ });
+
+ test("keeps a cold-start image share until the workspace is ready to consume it", async () => {
+ const images = [{ value: "content://com.zhihu.android/shared-image.jpg", mimeType: "image/jpeg", shareType: "image" }];
+ const resolvedImages = [{ ...images[0], contentUri: "file:///cache/shared-image.jpg" }];
+ const app = mountHook(images);
+ expect(app.render().sharedPayloads).toEqual(images);
+ const unmount = app.mount();
+ expect(app.render().isResolving).toBe(true);
+ app.foreground();
+ expect(app.requests).toHaveLength(1);
+ app.requests[0]!.resolve(resolvedImages);
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ // Session and persisted notebook restoration can finish after resolution.
+ expect(app.render().resolvedSharedPayloads).toEqual(resolvedImages);
+ app.foreground();
+ expect(app.render().resolvedSharedPayloads).toEqual(resolvedImages);
+ expect(app.requests).toHaveLength(1);
+ app.render().clearSharedPayloads();
+ expect(app.render().resolvedSharedPayloads).toEqual([]);
+ if (typeof unmount === "function") unmount();
+ });
+
+ test("receives a warm image share after the app is already active and removes listeners", async () => {
+ const app = mountHook();
+ app.render().clearSharedPayloads();
+ const unmount = app.mount();
+ app.foreground(); // resume can happen before Android delivers onNewIntent
+ const images = [{ value: "content://com.zhihu.android/shared-image.jpg", mimeType: "image/jpeg", shareType: "image" }];
+ const resolvedImages = [{ ...images[0], contentUri: "file:///cache/shared-image.jpg" }];
+ app.shareAgain(images);
+ app.incomingShare();
+ expect(app.requests).toHaveLength(1);
+ app.incomingShare(); // URL and AppState must not resolve the same intent twice
+ expect(app.requests).toHaveLength(1);
+ app.requests[0]!.resolve(resolvedImages);
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(app.render().resolvedSharedPayloads).toEqual(resolvedImages);
+ if (typeof unmount === "function") unmount();
+ app.render().clearSharedPayloads();
+ app.shareAgain();
+ app.incomingShare();
+ app.foreground();
+ expect(app.requests).toHaveLength(1);
+ });
+
test("clearing during resolution prevents a late result replaying a consumed Zhihu share", async () => {
const app = mountHook();
const pending = app.render().refreshSharePayloads();
diff --git a/patches/[email protected] b/patches/[email protected]
index 1041545d..a58faf46 100644
--- a/patches/[email protected]
+++ b/patches/[email protected]
@@ -154,9 +154,11 @@ diff --git a/android/src/main/java/expo/modules/sharing/dataParsers/SimpleShareI
diff --git a/src/useIncomingShare.ts b/src/useIncomingShare.ts
--- a/src/useIncomingShare.ts
+++ b/src/useIncomingShare.ts
-@@ -2,7 +2,7 @@
+@@ -1,8 +1,8 @@
+ import { useEffect, useState, useCallback, useRef } from 'react';
import type { AppStateStatus } from 'react-native';
- import { AppState } from 'react-native';
+-import { AppState } from 'react-native';
++import { AppState, Linking } from 'react-native';
-import { clearSharedPayloads, getResolvedSharedPayloadsAsync, getSharedPayloads } from './Sharing';
+import { clearSharedPayloads as clearNativeSharedPayloads, getResolvedSharedPayloadsAsync, getSharedPayloads } from './Sharing';
@@ -212,6 +214,27 @@ diff --git a/src/useIncomingShare.ts b/src/useIncomingShare.ts
}
}
} catch (e) {
+@@ -83,7 +100,20 @@
+ }
+ });
+
++ // A warm share may arrive after the activity is already active. Android
++ // emits this URL after storing the intent, so AppState alone is insufficient.
++ const shareSubscription = Linking.addEventListener('url', ({ url }) => {
++ try {
++ if (new URL(url).hostname === 'expo-sharing') {
++ void refreshSharePayloads();
++ }
++ } catch {
++ // Ignore unrelated malformed deep links.
++ }
++ });
++
+ return () => {
++ shareSubscription.remove();
+ subscription.remove();
+ };
+ }, [refreshSharePayloads]);
diff --git a/android/src/main/java/expo/modules/sharing/SharingModule.kt b/android/src/main/java/expo/modules/sharing/SharingModule.kt
--- a/android/src/main/java/expo/modules/sharing/SharingModule.kt
+++ b/android/src/main/java/expo/modules/sharing/SharingModule.kt
@@ -233,3 +256,86 @@ diff --git a/android/src/main/java/expo/modules/sharing/SharingModule.kt b/andro
}
OnActivityResult { _, (requestCode) ->
+diff --git a/build/useIncomingShare.js b/build/useIncomingShare.js
+--- a/build/useIncomingShare.js
++++ b/build/useIncomingShare.js
+@@ -1,6 +1,6 @@
+ import { useEffect, useState, useCallback, useRef } from 'react';
+-import { AppState } from 'react-native';
+-import { clearSharedPayloads, getResolvedSharedPayloadsAsync, getSharedPayloads } from './Sharing';
++import { AppState, Linking } from 'react-native';
++import { clearSharedPayloads as clearNativeSharedPayloads, getResolvedSharedPayloadsAsync, getSharedPayloads } from './Sharing';
+ function sharePayloadsAreEqual(a, b) {
+ if (a.length !== b.length) {
+ return false;
+@@ -30,6 +30,16 @@
+ const [isResolving, setIsResolving] = useState(false);
+ const [error, setError] = useState(null);
+ const currentSharedDataRef = useRef([]);
++ const resolutionVersionRef = useRef(0);
++ const clearSharedPayloads = useCallback(() => {
++ clearNativeSharedPayloads();
++ resolutionVersionRef.current += 1;
++ currentSharedDataRef.current = [];
++ setSharedPayloads([]);
++ setResolvedSharedPayloads([]);
++ setError(null);
++ setIsResolving(false);
++ }, []);
+ const refreshSharePayloads = useCallback(async () => {
+ try {
+ const newSharedData = getSharedPayloads();
+@@ -37,21 +47,30 @@
+ if (sharePayloadsAreEqual(newSharedData, currentSharedDataRef.current)) {
+ return;
+ }
++ const version = ++resolutionVersionRef.current;
+ currentSharedDataRef.current = newSharedData;
+ setSharedPayloads(newSharedData);
+ setResolvedSharedPayloads([]);
+ setError(null);
++ setIsResolving(newSharedData.length > 0);
+ if (newSharedData.length > 0) {
+- setIsResolving(true);
+ try {
+ const resolved = await getResolvedSharedPayloadsAsync();
++ if (version !== resolutionVersionRef.current)
++ return;
++ if (resolved.length === 0) {
++ throw new Error('Shared content could not be resolved');
++ }
+ setResolvedSharedPayloads(resolved);
+ }
+ catch (e) {
++ if (version !== resolutionVersionRef.current)
++ return;
+ setError(e instanceof Error ? e : new Error('Unknown error during shared payload resolution'));
+ }
+ finally {
+- setIsResolving(false);
++ if (version === resolutionVersionRef.current)
++ setIsResolving(false);
+ }
+ }
+ }
+@@ -66,7 +85,20 @@
+ refreshSharePayloads();
+ }
+ });
++ // A warm share may arrive after the activity is already active. Android
++ // emits this URL after storing the intent, so AppState alone is insufficient.
++ const shareSubscription = Linking.addEventListener('url', ({ url }) => {
++ try {
++ if (new URL(url).hostname === 'expo-sharing') {
++ void refreshSharePayloads();
++ }
++ }
++ catch {
++ // Ignore unrelated malformed deep links.
++ }
++ });
+ return () => {
++ shareSubscription.remove();
+ subscription.remove();
+ };
+ }, [refreshSharePayloads]);