@buoy-gg/images 7.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +58 -0
- package/README.md +58 -0
- package/lib/commonjs/capture/actions.js +227 -0
- package/lib/commonjs/capture/actions.js.map +1 -0
- package/lib/commonjs/capture/install.js +41 -0
- package/lib/commonjs/capture/install.js.map +1 -0
- package/lib/commonjs/capture/installExpoImage.js +99 -0
- package/lib/commonjs/capture/installExpoImage.js.map +1 -0
- package/lib/commonjs/capture/installRnImage.js +128 -0
- package/lib/commonjs/capture/installRnImage.js.map +1 -0
- package/lib/commonjs/capture/overrides.js +286 -0
- package/lib/commonjs/capture/overrides.js.map +1 -0
- package/lib/commonjs/capture/savings.js +193 -0
- package/lib/commonjs/capture/savings.js.map +1 -0
- package/lib/commonjs/capture/shared.js +367 -0
- package/lib/commonjs/capture/shared.js.map +1 -0
- package/lib/commonjs/components/ImageDetail.js +444 -0
- package/lib/commonjs/components/ImageDetail.js.map +1 -0
- package/lib/commonjs/components/ImageDetailFooter.js +232 -0
- package/lib/commonjs/components/ImageDetailFooter.js.map +1 -0
- package/lib/commonjs/components/ImageRow.js +164 -0
- package/lib/commonjs/components/ImageRow.js.map +1 -0
- package/lib/commonjs/components/ImagesHeader.js +63 -0
- package/lib/commonjs/components/ImagesHeader.js.map +1 -0
- package/lib/commonjs/components/ImagesIcon.js +59 -0
- package/lib/commonjs/components/ImagesIcon.js.map +1 -0
- package/lib/commonjs/components/ImagesListFooter.js +194 -0
- package/lib/commonjs/components/ImagesListFooter.js.map +1 -0
- package/lib/commonjs/components/ImagesModal.js +173 -0
- package/lib/commonjs/components/ImagesModal.js.map +1 -0
- package/lib/commonjs/components/ImagesRoot.js +24 -0
- package/lib/commonjs/components/ImagesRoot.js.map +1 -0
- package/lib/commonjs/components/InsightsBar.js +59 -0
- package/lib/commonjs/components/InsightsBar.js.map +1 -0
- package/lib/commonjs/export.js +81 -0
- package/lib/commonjs/export.js.map +1 -0
- package/lib/commonjs/format.js +117 -0
- package/lib/commonjs/format.js.map +1 -0
- package/lib/commonjs/hooks/useImageRecords.js +20 -0
- package/lib/commonjs/hooks/useImageRecords.js.map +1 -0
- package/lib/commonjs/index.js +236 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/preset.js +45 -0
- package/lib/commonjs/preset.js.map +1 -0
- package/lib/commonjs/register.js +19 -0
- package/lib/commonjs/register.js.map +1 -0
- package/lib/commonjs/state.js +54 -0
- package/lib/commonjs/state.js.map +1 -0
- package/lib/commonjs/store/imageEventStore.js +160 -0
- package/lib/commonjs/store/imageEventStore.js.map +1 -0
- package/lib/commonjs/store/insights.js +76 -0
- package/lib/commonjs/store/insights.js.map +1 -0
- package/lib/commonjs/sync/imagesSyncAdapter.js +315 -0
- package/lib/commonjs/sync/imagesSyncAdapter.js.map +1 -0
- package/lib/commonjs/types.js +2 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/capture/actions.js +214 -0
- package/lib/module/capture/actions.js.map +1 -0
- package/lib/module/capture/install.js +36 -0
- package/lib/module/capture/install.js.map +1 -0
- package/lib/module/capture/installExpoImage.js +94 -0
- package/lib/module/capture/installExpoImage.js.map +1 -0
- package/lib/module/capture/installRnImage.js +122 -0
- package/lib/module/capture/installRnImage.js.map +1 -0
- package/lib/module/capture/overrides.js +269 -0
- package/lib/module/capture/overrides.js.map +1 -0
- package/lib/module/capture/savings.js +189 -0
- package/lib/module/capture/savings.js.map +1 -0
- package/lib/module/capture/shared.js +359 -0
- package/lib/module/capture/shared.js.map +1 -0
- package/lib/module/components/ImageDetail.js +440 -0
- package/lib/module/components/ImageDetail.js.map +1 -0
- package/lib/module/components/ImageDetailFooter.js +228 -0
- package/lib/module/components/ImageDetailFooter.js.map +1 -0
- package/lib/module/components/ImageRow.js +160 -0
- package/lib/module/components/ImageRow.js.map +1 -0
- package/lib/module/components/ImagesHeader.js +59 -0
- package/lib/module/components/ImagesHeader.js.map +1 -0
- package/lib/module/components/ImagesIcon.js +55 -0
- package/lib/module/components/ImagesIcon.js.map +1 -0
- package/lib/module/components/ImagesListFooter.js +190 -0
- package/lib/module/components/ImagesListFooter.js.map +1 -0
- package/lib/module/components/ImagesModal.js +169 -0
- package/lib/module/components/ImagesModal.js.map +1 -0
- package/lib/module/components/ImagesRoot.js +20 -0
- package/lib/module/components/ImagesRoot.js.map +1 -0
- package/lib/module/components/InsightsBar.js +55 -0
- package/lib/module/components/InsightsBar.js.map +1 -0
- package/lib/module/export.js +76 -0
- package/lib/module/export.js.map +1 -0
- package/lib/module/format.js +108 -0
- package/lib/module/format.js.map +1 -0
- package/lib/module/hooks/useImageRecords.js +16 -0
- package/lib/module/hooks/useImageRecords.js.map +1 -0
- package/lib/module/index.js +73 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/preset.js +40 -0
- package/lib/module/preset.js.map +1 -0
- package/lib/module/register.js +19 -0
- package/lib/module/register.js.map +1 -0
- package/lib/module/state.js +50 -0
- package/lib/module/state.js.map +1 -0
- package/lib/module/store/imageEventStore.js +146 -0
- package/lib/module/store/imageEventStore.js.map +1 -0
- package/lib/module/store/insights.js +71 -0
- package/lib/module/store/insights.js.map +1 -0
- package/lib/module/sync/imagesSyncAdapter.js +311 -0
- package/lib/module/sync/imagesSyncAdapter.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/capture/actions.d.ts +49 -0
- package/lib/typescript/capture/actions.d.ts.map +1 -0
- package/lib/typescript/capture/install.d.ts +15 -0
- package/lib/typescript/capture/install.d.ts.map +1 -0
- package/lib/typescript/capture/installExpoImage.d.ts +23 -0
- package/lib/typescript/capture/installExpoImage.d.ts.map +1 -0
- package/lib/typescript/capture/installRnImage.d.ts +24 -0
- package/lib/typescript/capture/installRnImage.d.ts.map +1 -0
- package/lib/typescript/capture/overrides.d.ts +83 -0
- package/lib/typescript/capture/overrides.d.ts.map +1 -0
- package/lib/typescript/capture/savings.d.ts +37 -0
- package/lib/typescript/capture/savings.d.ts.map +1 -0
- package/lib/typescript/capture/shared.d.ts +61 -0
- package/lib/typescript/capture/shared.d.ts.map +1 -0
- package/lib/typescript/components/ImageDetail.d.ts +13 -0
- package/lib/typescript/components/ImageDetail.d.ts.map +1 -0
- package/lib/typescript/components/ImageDetailFooter.d.ts +19 -0
- package/lib/typescript/components/ImageDetailFooter.d.ts.map +1 -0
- package/lib/typescript/components/ImageRow.d.ts +15 -0
- package/lib/typescript/components/ImageRow.d.ts.map +1 -0
- package/lib/typescript/components/ImagesHeader.d.ts +25 -0
- package/lib/typescript/components/ImagesHeader.d.ts.map +1 -0
- package/lib/typescript/components/ImagesIcon.d.ts +13 -0
- package/lib/typescript/components/ImagesIcon.d.ts.map +1 -0
- package/lib/typescript/components/ImagesListFooter.d.ts +8 -0
- package/lib/typescript/components/ImagesListFooter.d.ts.map +1 -0
- package/lib/typescript/components/ImagesModal.d.ts +17 -0
- package/lib/typescript/components/ImagesModal.d.ts.map +1 -0
- package/lib/typescript/components/ImagesRoot.d.ts +11 -0
- package/lib/typescript/components/ImagesRoot.d.ts.map +1 -0
- package/lib/typescript/components/InsightsBar.d.ts +13 -0
- package/lib/typescript/components/InsightsBar.d.ts.map +1 -0
- package/lib/typescript/export.d.ts +12 -0
- package/lib/typescript/export.d.ts.map +1 -0
- package/lib/typescript/format.d.ts +26 -0
- package/lib/typescript/format.d.ts.map +1 -0
- package/lib/typescript/hooks/useImageRecords.d.ts +11 -0
- package/lib/typescript/hooks/useImageRecords.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +33 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/preset.d.ts +38 -0
- package/lib/typescript/preset.d.ts.map +1 -0
- package/lib/typescript/register.d.ts +15 -0
- package/lib/typescript/register.d.ts.map +1 -0
- package/lib/typescript/state.d.ts +54 -0
- package/lib/typescript/state.d.ts.map +1 -0
- package/lib/typescript/store/imageEventStore.d.ts +34 -0
- package/lib/typescript/store/imageEventStore.d.ts.map +1 -0
- package/lib/typescript/store/insights.d.ts +11 -0
- package/lib/typescript/store/insights.d.ts.map +1 -0
- package/lib/typescript/sync/imagesSyncAdapter.d.ts +205 -0
- package/lib/typescript/sync/imagesSyncAdapter.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +121 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/package.json +67 -0
- package/register.d.ts +1 -0
- package/register.js +15 -0
- package/src/capture/actions.ts +206 -0
- package/src/capture/install.ts +36 -0
- package/src/capture/installExpoImage.ts +115 -0
- package/src/capture/installRnImage.tsx +133 -0
- package/src/capture/overrides.ts +282 -0
- package/src/capture/savings.ts +193 -0
- package/src/capture/shared.ts +387 -0
- package/src/components/ImageDetail.tsx +456 -0
- package/src/components/ImageDetailFooter.tsx +270 -0
- package/src/components/ImageRow.tsx +148 -0
- package/src/components/ImagesHeader.tsx +77 -0
- package/src/components/ImagesIcon.tsx +66 -0
- package/src/components/ImagesListFooter.tsx +193 -0
- package/src/components/ImagesModal.tsx +195 -0
- package/src/components/ImagesRoot.tsx +18 -0
- package/src/components/InsightsBar.tsx +51 -0
- package/src/export.ts +100 -0
- package/src/format.ts +85 -0
- package/src/hooks/useImageRecords.ts +24 -0
- package/src/index.tsx +108 -0
- package/src/preset.tsx +35 -0
- package/src/register.ts +17 -0
- package/src/state.ts +88 -0
- package/src/store/imageEventStore.ts +162 -0
- package/src/store/insights.ts +66 -0
- package/src/sync/imagesSyncAdapter.ts +261 -0
- package/src/types.ts +111 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* expo-image interception via class-prototype patch.
|
|
3
|
+
*
|
|
4
|
+
* expo-image's <Image> is a class component (React.PureComponent) whose
|
|
5
|
+
* render() reads `this.props`, so wrapping the prototype's render and swapping
|
|
6
|
+
* `this.props` for an instrumented copy for the duration of the original
|
|
7
|
+
* render is timing-immune (prototype methods are looked up dynamically — no
|
|
8
|
+
* import-order race like the RN core decorator). Props are restored in
|
|
9
|
+
* `finally`, so PureComponent's shouldComponentUpdate still compares the
|
|
10
|
+
* app's real props.
|
|
11
|
+
*
|
|
12
|
+
* Double-patch safety: install flags live on the globalThis singleton AND a
|
|
13
|
+
* marker is stamped on the prototype itself, so even two module instances of
|
|
14
|
+
* this package can't wrap render() twice.
|
|
15
|
+
*
|
|
16
|
+
* expo-image is an optional peer: if it isn't installed, this is a no-op.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { imagesState } from "../state";
|
|
20
|
+
import { registerExpoInstance } from "./actions";
|
|
21
|
+
import { applyOverrides } from "./overrides";
|
|
22
|
+
import { ImageInstanceTracker, isInternalImage } from "./shared";
|
|
23
|
+
import type { OriginalHandlers } from "./shared";
|
|
24
|
+
|
|
25
|
+
interface ExpoImageInstance {
|
|
26
|
+
props: Record<string, unknown>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const PATCH_MARKER = "__buoyImagesPatched";
|
|
30
|
+
|
|
31
|
+
const trackers = new WeakMap<object, ImageInstanceTracker>();
|
|
32
|
+
|
|
33
|
+
function trackerFor(instance: object): ImageInstanceTracker {
|
|
34
|
+
let tracker = trackers.get(instance);
|
|
35
|
+
if (!tracker) {
|
|
36
|
+
tracker = new ImageInstanceTracker("expo");
|
|
37
|
+
trackers.set(instance, tracker);
|
|
38
|
+
}
|
|
39
|
+
return tracker;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function installExpoImageInterceptor(): void {
|
|
43
|
+
const s = imagesState();
|
|
44
|
+
if (s.expoInstalled) return;
|
|
45
|
+
s.expoInstalled = true;
|
|
46
|
+
|
|
47
|
+
let ImageClass:
|
|
48
|
+
| {
|
|
49
|
+
prototype: ExpoImageInstance & {
|
|
50
|
+
render: () => unknown;
|
|
51
|
+
componentWillUnmount?: () => void;
|
|
52
|
+
[PATCH_MARKER]?: boolean;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
| undefined;
|
|
56
|
+
try {
|
|
57
|
+
ImageClass = require("expo-image").Image;
|
|
58
|
+
} catch {
|
|
59
|
+
s.expoAvailable = false;
|
|
60
|
+
return; // expo-image not installed in this app
|
|
61
|
+
}
|
|
62
|
+
if (!ImageClass?.prototype?.render) {
|
|
63
|
+
s.expoAvailable = false;
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
s.expoAvailable = true;
|
|
67
|
+
|
|
68
|
+
const proto = ImageClass.prototype;
|
|
69
|
+
if (proto[PATCH_MARKER]) {
|
|
70
|
+
s.expoPatched = true;
|
|
71
|
+
return; // already patched by another module instance of this package
|
|
72
|
+
}
|
|
73
|
+
proto[PATCH_MARKER] = true;
|
|
74
|
+
|
|
75
|
+
const originalRender = proto.render;
|
|
76
|
+
const originalUnmount = proto.componentWillUnmount;
|
|
77
|
+
|
|
78
|
+
proto.render = function buoyImagesRender(this: ExpoImageInstance) {
|
|
79
|
+
const originalProps = this.props;
|
|
80
|
+
if (isInternalImage(originalProps?.testID)) {
|
|
81
|
+
return originalRender.call(this);
|
|
82
|
+
}
|
|
83
|
+
const tracker = trackerFor(this);
|
|
84
|
+
tracker.syncSource(originalProps?.source);
|
|
85
|
+
tracker.noteA11y(
|
|
86
|
+
Boolean(originalProps?.alt ?? originalProps?.accessibilityLabel)
|
|
87
|
+
);
|
|
88
|
+
// Keep the action layer pointed at the live instance (reloadAsync etc.).
|
|
89
|
+
registerExpoInstance(tracker.getRecordId(), this);
|
|
90
|
+
const wrapped = tracker.handlers(originalProps as OriginalHandlers);
|
|
91
|
+
try {
|
|
92
|
+
// Simulation layer: global modes + per-record override + locate-flash.
|
|
93
|
+
this.props = applyOverrides(
|
|
94
|
+
{ ...originalProps, ...wrapped },
|
|
95
|
+
tracker.getRecordId(),
|
|
96
|
+
"expo"
|
|
97
|
+
);
|
|
98
|
+
return originalRender.call(this);
|
|
99
|
+
} finally {
|
|
100
|
+
this.props = originalProps;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
proto.componentWillUnmount = function buoyImagesUnmount(this: ExpoImageInstance) {
|
|
105
|
+
trackers.get(this)?.unmount();
|
|
106
|
+
originalUnmount?.call(this);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
s.expoPatched = true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function expoCaptureStatus(): { available: boolean; patched: boolean } {
|
|
113
|
+
const s = imagesState();
|
|
114
|
+
return { available: s.expoAvailable, patched: s.expoPatched };
|
|
115
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RN core <Image> interception via the official injection hook.
|
|
3
|
+
*
|
|
4
|
+
* `unstable_setImageComponentDecorator` wraps EVERY RN <Image> app-wide — but
|
|
5
|
+
* the decorator is latched ONCE, at Image module evaluation
|
|
6
|
+
* (`Image.ios.js` / `Image.android.js` read it at module scope). So install
|
|
7
|
+
* order matters: this must run before anything touches `Image` from
|
|
8
|
+
* 'react-native' — hence the `@buoy-gg/images/register` first-import.
|
|
9
|
+
*
|
|
10
|
+
* Detection strategy: the decorator sets `decoratorInvoked` when RN's Image
|
|
11
|
+
* module applies it. `unstable_registerImageAttachedCallback` is a live Set
|
|
12
|
+
* (never latched), so image mounts are always visible — if mounts happen but
|
|
13
|
+
* the decorator never ran, we were too late and CaptureStatus reports it.
|
|
14
|
+
*
|
|
15
|
+
* All flags live on the globalThis singleton (../state.ts) so a second module
|
|
16
|
+
* instance of this package neither re-installs nor reports a stale status.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import * as React from "react";
|
|
20
|
+
import { imagesState } from "../state";
|
|
21
|
+
import {
|
|
22
|
+
getReloadNonce,
|
|
23
|
+
getReloadRequest,
|
|
24
|
+
subscribeActions,
|
|
25
|
+
} from "./actions";
|
|
26
|
+
import { applyOverrides, getRenderVersion, patchSourceCache } from "./overrides";
|
|
27
|
+
import { ImageInstanceTracker, isInternalImage } from "./shared";
|
|
28
|
+
import type { OriginalHandlers } from "./shared";
|
|
29
|
+
|
|
30
|
+
/** Wraps one RN Image render with capture instrumentation + reload actions. */
|
|
31
|
+
function useImageCapture(props: Record<string, unknown>): {
|
|
32
|
+
props: Record<string, unknown>;
|
|
33
|
+
reloadKey: number;
|
|
34
|
+
} {
|
|
35
|
+
const trackerRef = React.useRef<ImageInstanceTracker | null>(null);
|
|
36
|
+
if (trackerRef.current == null) {
|
|
37
|
+
trackerRef.current = new ImageInstanceTracker("rn");
|
|
38
|
+
}
|
|
39
|
+
const tracker = trackerRef.current;
|
|
40
|
+
|
|
41
|
+
const internal = isInternalImage(props.testID);
|
|
42
|
+
if (!internal) {
|
|
43
|
+
tracker.syncSource(props.source);
|
|
44
|
+
tracker.noteA11y(
|
|
45
|
+
Boolean(props.alt ?? props.accessibilityLabel ?? props["aria-label"])
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Action/override bus: version bumps re-render just this instance (snapshot
|
|
50
|
+
// is per-record with a global floor, so unrelated actions don't invalidate
|
|
51
|
+
// other images but global-mode toggles reach everyone).
|
|
52
|
+
React.useSyncExternalStore(subscribeActions, () =>
|
|
53
|
+
getRenderVersion(internal ? null : tracker.getRecordId())
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
React.useEffect(() => {
|
|
57
|
+
if (internal) return undefined;
|
|
58
|
+
return () => tracker.unmount();
|
|
59
|
+
}, [tracker, internal]);
|
|
60
|
+
|
|
61
|
+
if (internal) return { props, reloadKey: 0 };
|
|
62
|
+
|
|
63
|
+
const recordId = tracker.getRecordId();
|
|
64
|
+
const wrapped = tracker.handlers(props as OriginalHandlers);
|
|
65
|
+
const out: Record<string, unknown> = { ...props, ...wrapped };
|
|
66
|
+
|
|
67
|
+
// While a bust request is pending (cleared when the load settles), refetch
|
|
68
|
+
// bypassing caches — VERIFIED both platforms honor source.cache: 'reload'.
|
|
69
|
+
const request = getReloadRequest(recordId);
|
|
70
|
+
if (request?.bust) {
|
|
71
|
+
out.source = patchSourceCache(out.source, "reload");
|
|
72
|
+
}
|
|
73
|
+
// Simulation layer: global modes + per-record override + locate-flash.
|
|
74
|
+
applyOverrides(out, recordId, "rn");
|
|
75
|
+
return { props: out, reloadKey: getReloadNonce(recordId) };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function installRnImageInterceptor(): void {
|
|
79
|
+
const s = imagesState();
|
|
80
|
+
if (s.rnInstalled) return;
|
|
81
|
+
s.rnInstalled = true;
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
// Deep import: evaluates ONLY the injection module, never Image itself.
|
|
85
|
+
const injection = require("react-native/Libraries/Image/ImageInjection");
|
|
86
|
+
|
|
87
|
+
const setDecorator: undefined | ((d: unknown) => void) =
|
|
88
|
+
injection.unstable_setImageComponentDecorator;
|
|
89
|
+
const registerAttached: undefined | ((cb: (node: unknown) => void) => void) =
|
|
90
|
+
injection.unstable_registerImageAttachedCallback;
|
|
91
|
+
|
|
92
|
+
if (typeof setDecorator === "function") {
|
|
93
|
+
setDecorator((BaseImage: React.ComponentType<Record<string, unknown>>) => {
|
|
94
|
+
imagesState().decoratorInvoked = true;
|
|
95
|
+
const CapturedImage = React.forwardRef<unknown, Record<string, unknown>>(
|
|
96
|
+
function BuoyCapturedImage(props, ref) {
|
|
97
|
+
const { props: instrumented, reloadKey } = useImageCapture(props);
|
|
98
|
+
// key bump forces a native remount → fresh load cycle (retry).
|
|
99
|
+
return <BaseImage key={`buoy-r${reloadKey}`} {...instrumented} ref={ref} />;
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
return CapturedImage;
|
|
103
|
+
});
|
|
104
|
+
s.decoratorRegistered = true;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Live mount signal — works regardless of decorator timing. Used purely
|
|
108
|
+
// to detect the "decorator registered too late" condition.
|
|
109
|
+
if (typeof registerAttached === "function") {
|
|
110
|
+
registerAttached(() => {
|
|
111
|
+
imagesState().attachSeen++;
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
} catch {
|
|
115
|
+
// Internal RN path moved (version drift) or non-RN platform — expo-image
|
|
116
|
+
// capture still works; status will report the decorator as inactive.
|
|
117
|
+
s.decoratorRegistered = false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function rnCaptureStatus(): {
|
|
122
|
+
decoratorRegistered: boolean;
|
|
123
|
+
decoratorActive: boolean;
|
|
124
|
+
tooLate: boolean;
|
|
125
|
+
} {
|
|
126
|
+
const s = imagesState();
|
|
127
|
+
return {
|
|
128
|
+
decoratorRegistered: s.decoratorRegistered,
|
|
129
|
+
decoratorActive: s.decoratorInvoked,
|
|
130
|
+
// Mounts observed but our wrapper never rendered → Image latched first.
|
|
131
|
+
tooLate: s.decoratorRegistered && !s.decoratorInvoked && s.attachSeen > 0,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simulation overrides — the "we own the props" tier of actions.
|
|
3
|
+
*
|
|
4
|
+
* Because the RN decorator and the expo-image render patch control every
|
|
5
|
+
* mounted image's props, anything the component accepts is a simulation:
|
|
6
|
+
* forced errors (instant, offline-safe file:// source), forced loading
|
|
7
|
+
* (blackhole-IP source that never resolves), URL swaps, blanking, style
|
|
8
|
+
* merges (locate-flash border), and app-wide modes (offline images /
|
|
9
|
+
* cold-start / blank images).
|
|
10
|
+
*
|
|
11
|
+
* Re-render plumbing: every mutation bumps a per-record or global version.
|
|
12
|
+
* RN wrappers subscribe via useSyncExternalStore with a per-record snapshot
|
|
13
|
+
* (only the touched image re-renders); expo class instances are re-rendered
|
|
14
|
+
* directly via forceUpdate() from the per-record instance registry.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { imagesState } from "../state";
|
|
18
|
+
import { getRecord, getSnapshot, updateRecord } from "../store/imageEventStore";
|
|
19
|
+
import type { ImageLib } from "../types";
|
|
20
|
+
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
// Override model
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
export type OverrideSource =
|
|
26
|
+
| { type: "url"; uri: string }
|
|
27
|
+
/** Nonexistent file:// — native fires onError immediately, no network needed. */
|
|
28
|
+
| { type: "error" }
|
|
29
|
+
/** Blackhole IP — the request hangs, onLoad never fires (loading forever). */
|
|
30
|
+
| { type: "hang" }
|
|
31
|
+
/** No source at all (expo keeps showing its placeholder). */
|
|
32
|
+
| { type: "blank" };
|
|
33
|
+
|
|
34
|
+
export interface ImageOverride {
|
|
35
|
+
source?: OverrideSource;
|
|
36
|
+
/** Merged over the app's style (size experiments etc.). */
|
|
37
|
+
style?: Record<string, unknown>;
|
|
38
|
+
/** Extra props merged last (expo contentFit, priority, cachePolicy…). */
|
|
39
|
+
extraProps?: Record<string, unknown>;
|
|
40
|
+
/** Shown as the record's override badge, e.g. "Forced error". */
|
|
41
|
+
label: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const FORCED_ERROR_URI = "file:///buoy-images-forced-error.png";
|
|
45
|
+
const HANG_URI = "https://10.255.255.1/buoy-images-hang.png";
|
|
46
|
+
/** Offline mode replaces network sources with this (fails immediately). */
|
|
47
|
+
const OFFLINE_URI = "file:///buoy-images-offline-no-network.png";
|
|
48
|
+
const FLASH_STYLE = { borderWidth: 3, borderColor: "#FF453A" } as const;
|
|
49
|
+
/**
|
|
50
|
+
* RN core has NO safe "empty source": undefined resolves to the app bundle
|
|
51
|
+
* path and [] does too on RN 0.85 — both fire a bogus onError ("The file …
|
|
52
|
+
* couldn't be opened", observed live). So RN images blank via opacity
|
|
53
|
+
* instead (visual-only; the decoded image stays resident). expo-image
|
|
54
|
+
* supports a true `source={null}` and keeps showing its placeholder.
|
|
55
|
+
*/
|
|
56
|
+
const RN_BLANK_STYLE = { opacity: 0 } as const;
|
|
57
|
+
|
|
58
|
+
export type NetworkMode = "normal" | "offline" | "cold";
|
|
59
|
+
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// Version bus (re-render triggers)
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
|
|
64
|
+
function emit(): void {
|
|
65
|
+
for (const l of imagesState().actionListeners) l();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function forceUpdateExpo(recordId?: number): void {
|
|
69
|
+
const s = imagesState();
|
|
70
|
+
if (recordId != null) {
|
|
71
|
+
const instance = s.expoInstances.get(recordId) as
|
|
72
|
+
| { forceUpdate?: () => void }
|
|
73
|
+
| undefined;
|
|
74
|
+
instance?.forceUpdate?.();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
for (const instance of s.expoInstances.values()) {
|
|
78
|
+
(instance as { forceUpdate?: () => void })?.forceUpdate?.();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Bump one record's render version (re-renders just that image). */
|
|
83
|
+
export function touchRecord(recordId: number): void {
|
|
84
|
+
const s = imagesState();
|
|
85
|
+
s.recordVersions.set(recordId, ++s.versionCounter);
|
|
86
|
+
emit();
|
|
87
|
+
forceUpdateExpo(recordId);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Bump the global render version (re-renders every wrapped image). */
|
|
91
|
+
export function touchGlobal(): void {
|
|
92
|
+
const s = imagesState();
|
|
93
|
+
s.globalVersion = ++s.versionCounter;
|
|
94
|
+
emit();
|
|
95
|
+
forceUpdateExpo();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Snapshot for the RN wrapper's useSyncExternalStore. */
|
|
99
|
+
export function getRenderVersion(recordId: number | null): number {
|
|
100
|
+
const s = imagesState();
|
|
101
|
+
const record = recordId != null ? (s.recordVersions.get(recordId) ?? 0) : 0;
|
|
102
|
+
return Math.max(record, s.globalVersion);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
// Per-record overrides
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
|
|
109
|
+
export function setOverride(recordId: number, override: ImageOverride): void {
|
|
110
|
+
imagesState().overrides.set(recordId, override);
|
|
111
|
+
updateRecord(recordId, { overrideLabel: override.label });
|
|
112
|
+
touchRecord(recordId);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function clearOverride(recordId: number): void {
|
|
116
|
+
imagesState().overrides.delete(recordId);
|
|
117
|
+
updateRecord(recordId, { overrideLabel: undefined });
|
|
118
|
+
touchRecord(recordId);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function getOverride(recordId: number | null): ImageOverride | undefined {
|
|
122
|
+
if (recordId == null) return undefined;
|
|
123
|
+
return imagesState().overrides.get(recordId) as ImageOverride | undefined;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ---------------------------------------------------------------------------
|
|
127
|
+
// Mass actions (every mounted image at once)
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
|
|
130
|
+
/** Apply one override to every mounted record. Returns how many were hit. */
|
|
131
|
+
export function setOverrideForAllMounted(source: OverrideSource, label: string): number {
|
|
132
|
+
let count = 0;
|
|
133
|
+
for (const record of getSnapshot()) {
|
|
134
|
+
if (!record.mounted) continue;
|
|
135
|
+
setOverride(record.id, { source, label });
|
|
136
|
+
count++;
|
|
137
|
+
}
|
|
138
|
+
return count;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Remove every active override. Returns how many were cleared. */
|
|
142
|
+
export function clearAllOverrides(): number {
|
|
143
|
+
const ids = [...imagesState().overrides.keys()];
|
|
144
|
+
for (const id of ids) clearOverride(id);
|
|
145
|
+
return ids.length;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Flash every mounted image (locate everything the tool tracks). */
|
|
149
|
+
export function flashAllMounted(durationMs = 2500): number {
|
|
150
|
+
let count = 0;
|
|
151
|
+
for (const record of getSnapshot()) {
|
|
152
|
+
if (!record.mounted) continue;
|
|
153
|
+
flashRecord(record.id, durationMs);
|
|
154
|
+
count++;
|
|
155
|
+
}
|
|
156
|
+
return count;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Flash a border on the on-screen image so it can be located visually. */
|
|
160
|
+
export function flashRecord(recordId: number, durationMs = 2500): void {
|
|
161
|
+
const s = imagesState();
|
|
162
|
+
s.flashUntil.set(recordId, Date.now() + durationMs);
|
|
163
|
+
touchRecord(recordId);
|
|
164
|
+
setTimeout(() => {
|
|
165
|
+
imagesState().flashUntil.delete(recordId);
|
|
166
|
+
touchRecord(recordId);
|
|
167
|
+
}, durationMs + 50);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ---------------------------------------------------------------------------
|
|
171
|
+
// Global simulation modes
|
|
172
|
+
// ---------------------------------------------------------------------------
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* offline — every NETWORK image load fails immediately (both libs, cache or
|
|
176
|
+
* not); bundled/local assets still load, like a real offline device showing
|
|
177
|
+
* shipped images. Blunt on purpose: toggle + reload = images visibly gone.
|
|
178
|
+
* (The subtler `only-if-cached` variant was correct-but-confusing, and its
|
|
179
|
+
* uncached requests HANG on iOS instead of erroring — bad for QA.)
|
|
180
|
+
* cold — every image bypasses caches (RN `cache:'reload'`, expo
|
|
181
|
+
* `cachePolicy:'none'`): first-launch behavior without clearing anything.
|
|
182
|
+
*/
|
|
183
|
+
export function setNetworkMode(mode: NetworkMode): void {
|
|
184
|
+
imagesState().globalModes.network = mode;
|
|
185
|
+
touchGlobal();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Chrome-style "disable images": render every image without a source. */
|
|
189
|
+
export function setBlankImages(enabled: boolean): void {
|
|
190
|
+
imagesState().globalModes.blank = enabled;
|
|
191
|
+
touchGlobal();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function getGlobalModes(): { network: NetworkMode; blank: boolean } {
|
|
195
|
+
return imagesState().globalModes;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ---------------------------------------------------------------------------
|
|
199
|
+
// Application (called from the RN wrapper + expo render patch)
|
|
200
|
+
// ---------------------------------------------------------------------------
|
|
201
|
+
|
|
202
|
+
/** Inject a `cache` policy into an RN source (object or array shape). */
|
|
203
|
+
export function patchSourceCache(source: unknown, cache: string): unknown {
|
|
204
|
+
if (Array.isArray(source)) {
|
|
205
|
+
return source.map((entry) =>
|
|
206
|
+
entry && typeof entry === "object" && typeof (entry as { uri?: unknown }).uri === "string"
|
|
207
|
+
? { ...(entry as object), cache }
|
|
208
|
+
: entry
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
if (source && typeof source === "object" && typeof (source as { uri?: unknown }).uri === "string") {
|
|
212
|
+
return { ...(source as object), cache };
|
|
213
|
+
}
|
|
214
|
+
return source; // numeric assets etc. — nothing to patch
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function resolveOverrideSource(source: OverrideSource, lib: ImageLib): unknown {
|
|
218
|
+
switch (source.type) {
|
|
219
|
+
case "url":
|
|
220
|
+
return { uri: source.uri };
|
|
221
|
+
case "error":
|
|
222
|
+
return { uri: FORCED_ERROR_URI };
|
|
223
|
+
case "hang":
|
|
224
|
+
return { uri: HANG_URI };
|
|
225
|
+
case "blank":
|
|
226
|
+
// Only reached for expo — RN blanking is style-based (see RN_BLANK_STYLE).
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Merge global modes + per-record override + locate-flash into an
|
|
233
|
+
* already-copied props object. Mutates and returns `out`.
|
|
234
|
+
*/
|
|
235
|
+
export function applyOverrides(
|
|
236
|
+
out: Record<string, unknown>,
|
|
237
|
+
recordId: number | null,
|
|
238
|
+
lib: ImageLib
|
|
239
|
+
): Record<string, unknown> {
|
|
240
|
+
const s = imagesState();
|
|
241
|
+
const modes = s.globalModes;
|
|
242
|
+
|
|
243
|
+
if (modes.network === "offline") {
|
|
244
|
+
// Hard offline: network sources fail outright (both libs); bundled/local
|
|
245
|
+
// assets keep loading like a real offline device.
|
|
246
|
+
const record = recordId != null ? getRecord(recordId) : undefined;
|
|
247
|
+
if (record?.sourceKind === "network") {
|
|
248
|
+
out.source = { uri: OFFLINE_URI };
|
|
249
|
+
}
|
|
250
|
+
} else if (modes.network === "cold") {
|
|
251
|
+
if (lib === "rn") {
|
|
252
|
+
out.source = patchSourceCache(out.source, "reload");
|
|
253
|
+
} else {
|
|
254
|
+
out.cachePolicy = "none";
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (modes.blank) {
|
|
258
|
+
if (lib === "expo") out.source = null;
|
|
259
|
+
else out.style = [out.style, RN_BLANK_STYLE];
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const override = getOverride(recordId);
|
|
263
|
+
if (override) {
|
|
264
|
+
if (override.source) {
|
|
265
|
+
if (override.source.type === "blank" && lib === "rn") {
|
|
266
|
+
out.style = [out.style, RN_BLANK_STYLE];
|
|
267
|
+
} else {
|
|
268
|
+
out.source = resolveOverrideSource(override.source, lib);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (override.extraProps) Object.assign(out, override.extraProps);
|
|
272
|
+
if (override.style) out.style = [out.style, override.style];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (recordId != null) {
|
|
276
|
+
const until = s.flashUntil.get(recordId);
|
|
277
|
+
if (until != null && until > Date.now()) {
|
|
278
|
+
out.style = [out.style, FLASH_STYLE];
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return out;
|
|
282
|
+
}
|