react-native-gesture-image-viewer 2.0.0-beta.2 → 2.0.0-beta.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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- import { useEffect, useMemo, useRef, useState } from 'react';
3
+ import { useCallback, useMemo, useRef, useSyncExternalStore } from 'react';
4
4
  import { registry } from "./GestureViewerRegistry.js";
5
5
  /**
6
6
  * Hook to control the gesture viewer programmatically.
@@ -55,42 +55,72 @@ import { registry } from "./GestureViewerRegistry.js";
55
55
  * ```
56
56
  */
57
57
  export const useGestureViewerController = (id = 'default') => {
58
- const [state, setState] = useState({
58
+ const managerRef = useRef(null);
59
+ const stateRef = useRef({
59
60
  currentIndex: 0,
60
61
  totalCount: 0
61
62
  });
62
- const [manager, setManager] = useState(null);
63
- const unsubscribeRef = useRef(null);
64
- useEffect(() => {
65
- const handleManagerChange = newManager => {
66
- unsubscribeRef.current?.();
67
- unsubscribeRef.current = null;
68
- setManager(newManager);
63
+ const updateState = useCallback((newState, onStoreChange) => {
64
+ if (stateRef.current.currentIndex !== newState.currentIndex || stateRef.current.totalCount !== newState.totalCount) {
65
+ stateRef.current = {
66
+ currentIndex: newState.currentIndex,
67
+ totalCount: newState.totalCount
68
+ };
69
+ onStoreChange();
70
+ }
71
+ }, []);
72
+ const subscribe = useCallback(onStoreChange => {
73
+ let unsubscribeFromManager = null;
74
+ const unsubscribeFromRegistry = registry.subscribeToManager(id, newManager => {
75
+ unsubscribeFromManager?.();
76
+ unsubscribeFromManager = null;
77
+ managerRef.current = newManager;
69
78
  if (newManager) {
70
- setState(newManager.getState());
71
- unsubscribeRef.current = newManager.subscribe(setState);
79
+ unsubscribeFromManager = newManager.subscribe(newState => {
80
+ updateState(newState, onStoreChange);
81
+ });
82
+ updateState(newManager.getState(), onStoreChange);
72
83
  return;
73
84
  }
74
- setState({
85
+ updateState({
75
86
  currentIndex: 0,
76
87
  totalCount: 0
77
- });
78
- };
79
- const unsubscribeFromRegistry = registry.subscribeToManager(id, handleManagerChange);
88
+ }, onStoreChange);
89
+ });
80
90
  return () => {
81
91
  unsubscribeFromRegistry();
82
- unsubscribeRef.current?.();
92
+ unsubscribeFromManager?.();
83
93
  };
84
- }, [id]);
85
- const noopFunction = useMemo(() => () => {}, []);
94
+ }, [id, updateState]);
95
+ const getSnapshot = useCallback(() => {
96
+ return stateRef.current;
97
+ }, []);
98
+ const state = useSyncExternalStore(subscribe, getSnapshot);
99
+ const controller = useMemo(() => ({
100
+ goToIndex: index => {
101
+ managerRef.current?.goToIndex(index);
102
+ },
103
+ goToPrevious: () => {
104
+ managerRef.current?.goToPrevious();
105
+ },
106
+ goToNext: () => {
107
+ managerRef.current?.goToNext();
108
+ },
109
+ zoomIn: multiplier => {
110
+ managerRef.current?.zoomIn(multiplier);
111
+ },
112
+ zoomOut: multiplier => {
113
+ managerRef.current?.zoomOut(multiplier);
114
+ },
115
+ resetZoom: scale => {
116
+ managerRef.current?.resetZoom(scale);
117
+ },
118
+ rotate: (angle, clockwise) => {
119
+ managerRef.current?.rotate(angle, clockwise);
120
+ }
121
+ }), []);
86
122
  return {
87
- goToIndex: manager?.goToIndex || noopFunction,
88
- goToPrevious: manager?.goToPrevious || noopFunction,
89
- goToNext: manager?.goToNext || noopFunction,
90
- zoomIn: manager?.zoomIn || noopFunction,
91
- zoomOut: manager?.zoomOut || noopFunction,
92
- resetZoom: manager?.resetZoom || noopFunction,
93
- rotate: manager?.rotate || noopFunction,
123
+ ...controller,
94
124
  ...state
95
125
  };
96
126
  };
@@ -1 +1 @@
1
- {"version":3,"names":["useEffect","useMemo","useRef","useState","registry","useGestureViewerController","id","state","setState","currentIndex","totalCount","manager","setManager","unsubscribeRef","handleManagerChange","newManager","current","getState","subscribe","unsubscribeFromRegistry","subscribeToManager","noopFunction","goToIndex","goToPrevious","goToNext","zoomIn","zoomOut","resetZoom","rotate"],"sourceRoot":"../../src","sources":["useGestureViewerController.ts"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M;MACXK,uBAAuB,CAAC,CAAC;MACzBN,cAAc,CAACG,OAAO,GAAG,CAAC;IAC5B,CAAC;EACH,CAAC,EAAE,CAACV,EAAE,CAAC,CAAC;EAER,MAAMe,YAAY,GAAGpB,OAAO,CAAC,MAAM,MAAM,CAAC,CAAC,EAAE,EAAE,CAAC;EAEhD,OAAO;IACLqB,SAAS,EAAEX,OAAO,EAAEW,SAAS,IAAID,YAAY;IAC7CE,YAAY,EAAEZ,OAAO,EAAEY,YAAY,IAAIF,YAAY;IACnDG,QAAQ,EAAEb,OAAO,EAAEa,QAAQ,IAAIH,YAAY;IAC3CI,MAAM,EAAEd,OAAO,EAAEc,MAAM,IAAIJ,YAAY;IACvCK,OAAO,EAAEf,OAAO,EAAEe,OAAO,IAAIL,YAAY;IACzCM,SAAS,EAAEhB,OAAO,EAAEgB,SAAS,IAAIN,YAAY;IAC7CO,MAAM,EAAEjB,OAAO,EAAEiB,MAAM,IAAIP,YAAY;IACvC,GAAGd;EACL,CAAC;AACH,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["useCallback","useMemo","useRef","useSyncExternalStore","registry","useGestureViewerController","id","managerRef","stateRef","currentIndex","totalCount","updateState","newState","onStoreChange","current","subscribe","unsubscribeFromManager","unsubscribeFromRegistry","subscribeToManager","newManager","getState","getSnapshot","state","controller","goToIndex","index","goToPrevious","goToNext","zoomIn","multiplier","zoomOut","resetZoom","scale","rotate","angle","clockwise"],"sourceRoot":"../../src","sources":["useGestureViewerController.ts"],"mappings":";;AAAA,SAASA,WAAW,EAAEC,OAAO,EAAEC,MAAM,EAAEC,oBAAoB,QAAQ,OAAO;AAE1E,SAASC,QAAQ,QAAQ,4BAAyB;AAGlD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA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,CAAC;AACH,CAAC","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"file":"useGestureViewerController.d.ts","sourceRoot":"","sources":["../../../src/useGestureViewerController.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,uBAAuB,EAAgC,MAAM,SAAS,CAAC;AAErF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,eAAO,MAAM,0BAA0B,GAAI,WAAc,KAAG,uBA6C3D,CAAC"}
1
+ {"version":3,"file":"useGestureViewerController.d.ts","sourceRoot":"","sources":["../../../src/useGestureViewerController.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,uBAAuB,EAAgC,MAAM,SAAS,CAAC;AAErF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,eAAO,MAAM,0BAA0B,GAAI,WAAc,KAAG,uBAmF3D,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-gesture-image-viewer",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-beta.3",
4
4
  "description": "🖼️ A highly customizable and easy-to-use React Native image viewer with gesture support and external controls",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -1,4 +1,4 @@
1
- import { useEffect, useMemo, useRef, useState } from 'react';
1
+ import { useCallback, useMemo, useRef, useSyncExternalStore } from 'react';
2
2
  import type GestureViewerManager from './GestureViewerManager';
3
3
  import { registry } from './GestureViewerRegistry';
4
4
  import type { GestureViewerController, GestureViewerControllerState } from './types';
@@ -56,48 +56,86 @@ import type { GestureViewerController, GestureViewerControllerState } from './ty
56
56
  * ```
57
57
  */
58
58
  export const useGestureViewerController = (id = 'default'): GestureViewerController => {
59
- const [state, setState] = useState<GestureViewerControllerState>({
60
- currentIndex: 0,
61
- totalCount: 0,
62
- });
59
+ const managerRef = useRef<GestureViewerManager | null>(null);
60
+ const stateRef = useRef<GestureViewerControllerState>({ currentIndex: 0, totalCount: 0 });
63
61
 
64
- const [manager, setManager] = useState<GestureViewerManager | null>(null);
65
- const unsubscribeRef = useRef<(() => void) | null>(null);
62
+ const updateState = useCallback((newState: GestureViewerControllerState, onStoreChange: () => void) => {
63
+ if (
64
+ stateRef.current.currentIndex !== newState.currentIndex ||
65
+ stateRef.current.totalCount !== newState.totalCount
66
+ ) {
67
+ stateRef.current = {
68
+ currentIndex: newState.currentIndex,
69
+ totalCount: newState.totalCount,
70
+ };
71
+ onStoreChange();
72
+ }
73
+ }, []);
66
74
 
67
- useEffect(() => {
68
- const handleManagerChange = (newManager: GestureViewerManager | null) => {
69
- unsubscribeRef.current?.();
70
- unsubscribeRef.current = null;
75
+ const subscribe = useCallback(
76
+ (onStoreChange: () => void) => {
77
+ let unsubscribeFromManager: (() => void) | null = null;
71
78
 
72
- setManager(newManager);
79
+ const unsubscribeFromRegistry = registry.subscribeToManager(id, (newManager) => {
80
+ unsubscribeFromManager?.();
81
+ unsubscribeFromManager = null;
82
+ managerRef.current = newManager;
73
83
 
74
- if (newManager) {
75
- setState(newManager.getState());
76
- unsubscribeRef.current = newManager.subscribe(setState);
77
- return;
78
- }
84
+ if (newManager) {
85
+ unsubscribeFromManager = newManager.subscribe((newState) => {
86
+ updateState(newState, onStoreChange);
87
+ });
79
88
 
80
- setState({ currentIndex: 0, totalCount: 0 });
81
- };
89
+ updateState(newManager.getState(), onStoreChange);
90
+ return;
91
+ }
82
92
 
83
- const unsubscribeFromRegistry = registry.subscribeToManager(id, handleManagerChange);
93
+ updateState({ currentIndex: 0, totalCount: 0 }, onStoreChange);
94
+ });
84
95
 
85
- return () => {
86
- unsubscribeFromRegistry();
87
- unsubscribeRef.current?.();
88
- };
89
- }, [id]);
96
+ return () => {
97
+ unsubscribeFromRegistry();
98
+ unsubscribeFromManager?.();
99
+ };
100
+ },
101
+ [id, updateState],
102
+ );
90
103
 
91
- const noopFunction = useMemo(() => () => {}, []);
104
+ const getSnapshot = useCallback(() => {
105
+ return stateRef.current;
106
+ }, []);
107
+
108
+ const state = useSyncExternalStore(subscribe, getSnapshot);
109
+
110
+ const controller = useMemo<Omit<GestureViewerController, 'currentIndex' | 'totalCount'>>(
111
+ () => ({
112
+ goToIndex: (index) => {
113
+ managerRef.current?.goToIndex(index);
114
+ },
115
+ goToPrevious: () => {
116
+ managerRef.current?.goToPrevious();
117
+ },
118
+ goToNext: () => {
119
+ managerRef.current?.goToNext();
120
+ },
121
+ zoomIn: (multiplier) => {
122
+ managerRef.current?.zoomIn(multiplier);
123
+ },
124
+ zoomOut: (multiplier) => {
125
+ managerRef.current?.zoomOut(multiplier);
126
+ },
127
+ resetZoom: (scale) => {
128
+ managerRef.current?.resetZoom(scale);
129
+ },
130
+ rotate: (angle, clockwise) => {
131
+ managerRef.current?.rotate(angle, clockwise);
132
+ },
133
+ }),
134
+ [],
135
+ );
92
136
 
93
137
  return {
94
- goToIndex: manager?.goToIndex || noopFunction,
95
- goToPrevious: manager?.goToPrevious || noopFunction,
96
- goToNext: manager?.goToNext || noopFunction,
97
- zoomIn: manager?.zoomIn || noopFunction,
98
- zoomOut: manager?.zoomOut || noopFunction,
99
- resetZoom: manager?.resetZoom || noopFunction,
100
- rotate: manager?.rotate || noopFunction,
138
+ ...controller,
101
139
  ...state,
102
140
  };
103
141
  };