@ranamaps/maps-react-native 0.0.0-stage → 0.4.0-beta.0

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.
Files changed (41) hide show
  1. package/BETA-STATUS.md +13 -0
  2. package/CHANGELOG.md +40 -0
  3. package/LICENSE.md +13 -0
  4. package/NOTICES.md +230 -0
  5. package/README.md +61 -2
  6. package/RanaMapsReactNative.podspec +17 -0
  7. package/android/build.gradle +25 -0
  8. package/android/src/main/AndroidManifest.xml +1 -0
  9. package/android/src/main/java/com/ranamaps/reactnative/RanaMapManager.kt +115 -0
  10. package/android/src/main/java/com/ranamaps/reactnative/RanaMapsModule.kt +41 -0
  11. package/android/src/main/java/com/ranamaps/reactnative/RanaMapsPackage.kt +11 -0
  12. package/app.plugin.js +127 -0
  13. package/ios/RanaMapViewComponentView.mm +116 -0
  14. package/ios/RanaMapsModule.mm +32 -0
  15. package/ios/RanaNativeRuntime.swift +42 -0
  16. package/lib/bridge.d.ts +40 -0
  17. package/lib/bridge.js +152 -0
  18. package/lib/index.d.ts +31 -0
  19. package/lib/index.js +113 -0
  20. package/lib/map-lifecycle.d.ts +5 -0
  21. package/lib/map-lifecycle.js +10 -0
  22. package/lib/models.d.ts +509 -0
  23. package/lib/models.js +2 -0
  24. package/lib/sdk.d.ts +309 -0
  25. package/lib/sdk.js +588 -0
  26. package/lib/specs/NativeRanaMaps.d.ts +8 -0
  27. package/lib/specs/NativeRanaMaps.js +4 -0
  28. package/lib/specs/RanaMapViewNativeComponent.d.ts +13 -0
  29. package/lib/specs/RanaMapViewNativeComponent.js +4 -0
  30. package/lib/specs/RanaMarkerViewNativeComponent.d.ts +9 -0
  31. package/lib/specs/RanaMarkerViewNativeComponent.js +4 -0
  32. package/package.json +59 -4
  33. package/react-native.config.js +12 -0
  34. package/src/bridge.ts +184 -0
  35. package/src/index.tsx +168 -0
  36. package/src/map-lifecycle.ts +11 -0
  37. package/src/models.ts +510 -0
  38. package/src/sdk.ts +732 -0
  39. package/src/specs/NativeRanaMaps.ts +16 -0
  40. package/src/specs/RanaMapViewNativeComponent.ts +10 -0
  41. package/src/specs/RanaMarkerViewNativeComponent.ts +9 -0
package/package.json CHANGED
@@ -1,6 +1,61 @@
1
1
  {
2
2
  "name": "@ranamaps/maps-react-native",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.4.0-beta.0",
4
+ "description": "Rana Maps native SDK for React Native",
5
+ "license": "SEE LICENSE IN LICENSE.md",
6
+ "main": "lib/index.js",
7
+ "types": "lib/index.d.ts",
8
+ "react-native": "src/index.tsx",
9
+ "files": [
10
+ "lib",
11
+ "src",
12
+ "ios",
13
+ "*.podspec",
14
+ "app.plugin.js",
15
+ "LICENSE.md",
16
+ "NOTICES.md",
17
+ "README.md",
18
+ "react-native.config.js",
19
+ "android/build.gradle",
20
+ "android/src",
21
+ "BETA-STATUS.md",
22
+ "CHANGELOG.md"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsc",
26
+ "test": "node --test test/*.test.mjs",
27
+ "typecheck": "tsc --noEmit"
28
+ },
29
+ "peerDependencies": {
30
+ "react": ">=19.2.3 <20",
31
+ "react-native": ">=0.87.0 <0.89.0"
32
+ },
33
+ "devDependencies": {
34
+ "@react-native/codegen": "0.88.1",
35
+ "@types/react": "19.2.14",
36
+ "prettier": "3.6.2",
37
+ "react": "19.3.0",
38
+ "react-native": "0.88.1",
39
+ "typescript": "5.9.3"
40
+ },
41
+ "codegenConfig": {
42
+ "name": "RanaMapsSpec",
43
+ "type": "all",
44
+ "jsSrcsDir": "src/specs",
45
+ "android": {
46
+ "javaPackageName": "com.ranamaps.reactnative"
47
+ },
48
+ "ios": {
49
+ "componentProvider": {
50
+ "RanaMapView": "RanaMapViewComponentView",
51
+ "RanaMarkerView": "RanaMarkerViewComponentView"
52
+ },
53
+ "modulesProvider": {
54
+ "RanaMaps": "RanaMapsModule"
55
+ }
56
+ }
57
+ },
58
+ "dependencies": {
59
+ "@expo/config-plugins": "58.0.13"
60
+ }
61
+ }
@@ -0,0 +1,12 @@
1
+ module.exports = {
2
+ dependency: {
3
+ platforms: {
4
+ android: {
5
+ sourceDir: "./android",
6
+ packageImportPath: "import com.ranamaps.reactnative.RanaMapsPackage;",
7
+ packageInstance: "new RanaMapsPackage()",
8
+ },
9
+ ios: { podspecPath: "./RanaMapsReactNative.podspec" },
10
+ },
11
+ },
12
+ };
package/src/bridge.ts ADDED
@@ -0,0 +1,184 @@
1
+ import type { ErrorInfo } from "./models";
2
+ export interface RanaEvent<T = unknown> {
3
+ target: string;
4
+ type: string;
5
+ value: T;
6
+ }
7
+ export interface BridgeTransport {
8
+ execute(
9
+ requestId: string,
10
+ target: string,
11
+ method: string,
12
+ argumentsJson: string,
13
+ ): Promise<string>;
14
+ cancel(requestId: string): void;
15
+ subscribe(listener: (eventJson: string) => void): () => void;
16
+ }
17
+ export class RanaError extends Error implements ErrorInfo {
18
+ readonly code: string;
19
+ readonly status: number;
20
+ readonly requestId?: string;
21
+ readonly retryAfter?: number;
22
+ constructor(info: ErrorInfo) {
23
+ super(info.message);
24
+ this.name = "RanaError";
25
+ this.code = info.code;
26
+ this.status = info.status;
27
+ this.requestId = info.requestId ?? undefined;
28
+ this.retryAfter = info.retryAfter ?? undefined;
29
+ }
30
+ static from(error: unknown): RanaError {
31
+ if (error instanceof RanaError) return error;
32
+ const e = error as {
33
+ code?: string;
34
+ message?: string;
35
+ userInfo?: { rana?: string };
36
+ details?: string;
37
+ };
38
+ const raw = e?.userInfo?.rana ?? e?.details;
39
+ if (raw) {
40
+ try {
41
+ return new RanaError(JSON.parse(raw) as ErrorInfo);
42
+ } catch {
43
+ /* Preserve the platform error below. */
44
+ }
45
+ }
46
+ return new RanaError({
47
+ code: e?.code ?? "native_error",
48
+ message: e?.message ?? String(error),
49
+ status: 0,
50
+ });
51
+ }
52
+ }
53
+ export class RanaRuntime {
54
+ private sequence = 0;
55
+ private readyHandles = new Set<string>();
56
+ isMapReady(handle: string) {
57
+ return this.readyHandles.has(handle);
58
+ }
59
+ forgetHandle(handle: string) {
60
+ this.readyHandles.delete(handle);
61
+ }
62
+ private callbacks = new Map<
63
+ string,
64
+ (kind: string, value: unknown, signal: AbortSignal) => Promise<unknown>
65
+ >();
66
+ private listeners = new Map<string, Set<(event: RanaEvent) => void>>();
67
+ private inFlight = new Map<string, AbortController>();
68
+ private unsubscribe: () => void;
69
+ constructor(readonly transport: BridgeTransport) {
70
+ this.unsubscribe = transport.subscribe((json) =>
71
+ this.receive(JSON.parse(json) as RanaEvent),
72
+ );
73
+ }
74
+ async call<T>(
75
+ target: string,
76
+ method: string,
77
+ args: unknown = {},
78
+ signal?: AbortSignal,
79
+ ): Promise<T> {
80
+ if (signal?.aborted)
81
+ throw new RanaError({
82
+ code: "cancelled",
83
+ message: "Operation cancelled",
84
+ status: 0,
85
+ });
86
+ const id = `request-${++this.sequence}`;
87
+ const cancel = () => this.transport.cancel(id);
88
+ signal?.addEventListener("abort", cancel, { once: true });
89
+ try {
90
+ return JSON.parse(
91
+ await this.transport.execute(id, target, method, JSON.stringify(args)),
92
+ ) as T;
93
+ } catch (error) {
94
+ throw RanaError.from(error);
95
+ } finally {
96
+ signal?.removeEventListener("abort", cancel);
97
+ }
98
+ }
99
+ on(target: string, listener: (event: RanaEvent) => void): () => void {
100
+ const entries = this.listeners.get(target) ?? new Set();
101
+ entries.add(listener);
102
+ this.listeners.set(target, entries);
103
+ return () => {
104
+ entries.delete(listener);
105
+ if (!entries.size) this.listeners.delete(target);
106
+ };
107
+ }
108
+ registerCallback(
109
+ callback: (
110
+ kind: string,
111
+ value: unknown,
112
+ signal: AbortSignal,
113
+ ) => Promise<unknown>,
114
+ ): { id: string; dispose: () => void } {
115
+ const id = `callback-${++this.sequence}`;
116
+ this.callbacks.set(id, callback);
117
+ return { id, dispose: () => this.callbacks.delete(id) };
118
+ }
119
+ private receive(event: RanaEvent) {
120
+ if (event.type === "ready") this.readyHandles.add(event.target);
121
+ if (event.type === "callbackCancelled") {
122
+ this.inFlight
123
+ .get((event.value as { requestId: string }).requestId)
124
+ ?.abort();
125
+ return;
126
+ }
127
+ if (event.type === "callback") {
128
+ void this.answer(event);
129
+ return;
130
+ }
131
+ this.listeners.get(event.target)?.forEach((listener) => listener(event));
132
+ }
133
+ private async answer(event: RanaEvent) {
134
+ const request = event.value as {
135
+ requestId: string;
136
+ kind: string;
137
+ arguments: unknown;
138
+ };
139
+ const controller = new AbortController();
140
+ this.inFlight.set(request.requestId, controller);
141
+ let reply: object;
142
+ try {
143
+ const callback = this.callbacks.get(event.target);
144
+ if (!callback)
145
+ throw new RanaError({
146
+ code: "callback_unavailable",
147
+ message: "Host callback was removed",
148
+ status: 0,
149
+ });
150
+ reply = {
151
+ requestId: request.requestId,
152
+ value: await callback(
153
+ request.kind,
154
+ request.arguments,
155
+ controller.signal,
156
+ ),
157
+ };
158
+ } catch (error) {
159
+ const e = RanaError.from(error);
160
+ reply = {
161
+ requestId: request.requestId,
162
+ error: {
163
+ code: e.code,
164
+ message: e.message,
165
+ status: e.status,
166
+ requestId: e.requestId,
167
+ retryAfter: e.retryAfter,
168
+ },
169
+ };
170
+ } finally {
171
+ this.inFlight.delete(request.requestId);
172
+ }
173
+ if (!controller.signal.aborted)
174
+ await this.call("runtime", "resolveCallback", reply);
175
+ }
176
+ dispose() {
177
+ this.unsubscribe();
178
+ this.inFlight.forEach((c) => c.abort());
179
+ this.inFlight.clear();
180
+ this.callbacks.clear();
181
+ this.listeners.clear();
182
+ this.readyHandles.clear();
183
+ }
184
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,168 @@
1
+ import React, {
2
+ createContext,
3
+ forwardRef,
4
+ useCallback,
5
+ useContext,
6
+ useEffect,
7
+ useImperativeHandle,
8
+ useRef,
9
+ useState,
10
+ type ReactNode,
11
+ } from "react";
12
+ import { StyleSheet, type ViewProps } from "react-native";
13
+ import NativeRanaMaps from "./specs/NativeRanaMaps";
14
+ import NativeMap from "./specs/RanaMapViewNativeComponent";
15
+ import NativeMarker from "./specs/RanaMarkerViewNativeComponent";
16
+ import { RanaRuntime, RanaError, type RanaEvent } from "./bridge";
17
+ import {
18
+ RanaMapController,
19
+ RanaClient as Client,
20
+ RanaOfflineManager as OfflineManager,
21
+ type HttpTransport,
22
+ } from "./sdk";
23
+ import type { MapOptions, LatLng, ViewMarkerOptions } from "./models";
24
+ import { replaceController } from "./map-lifecycle";
25
+ export * from "./sdk";
26
+ export { RanaRuntime } from "./bridge";
27
+ export type { RanaEvent } from "./bridge";
28
+ export const runtime = new RanaRuntime({
29
+ execute: (id, target, method, args) =>
30
+ NativeRanaMaps.execute(id, target, method, args),
31
+ cancel: (id) => NativeRanaMaps.cancel(id),
32
+ subscribe: (listener) => {
33
+ const subscription = NativeRanaMaps.onEvent(listener);
34
+ return () => subscription.remove();
35
+ },
36
+ });
37
+ export function createClient(key?: string, transport?: HttpTransport) {
38
+ return Client.create(runtime, key, transport);
39
+ }
40
+ export function createOfflineManager(key?: string) {
41
+ return OfflineManager.create(runtime, key);
42
+ }
43
+ export const RanaMaps = {
44
+ setTelemetry: (enabled: boolean) =>
45
+ runtime.call<void>("runtime", "setTelemetry", { enabled }),
46
+ version: () => runtime.call<string>("runtime", "version"),
47
+ };
48
+ const MapContext = createContext<string | null>(null);
49
+ export interface RanaMapProps extends ViewProps {
50
+ options?: MapOptions;
51
+ children?: ReactNode;
52
+ onMapCreated?: (controller: RanaMapController) => void;
53
+ onReady?: (controller: RanaMapController) => void;
54
+ onEvent?: (event: RanaEvent) => void;
55
+ onError?: (error: RanaError) => void;
56
+ }
57
+ export const RanaMap = forwardRef<RanaMapController | null, RanaMapProps>(
58
+ function RanaMap(
59
+ {
60
+ options = {},
61
+ onMapCreated,
62
+ onReady,
63
+ onEvent,
64
+ onError,
65
+ children,
66
+ style,
67
+ ...props
68
+ },
69
+ ref,
70
+ ) {
71
+ const [controller, setController] = useState<RanaMapController | null>(
72
+ null,
73
+ );
74
+ const callbacks = useRef({ onMapCreated, onReady, onEvent, onError });
75
+ callbacks.current = { onMapCreated, onReady, onEvent, onError };
76
+ const [readyMapId, setReadyMapId] = useState<string | null>(null);
77
+ const active = useRef<RanaMapController | null>(null);
78
+ const unsubscribe = useRef<(() => void) | undefined>(undefined);
79
+ useImperativeHandle(ref, () => controller!, [controller]);
80
+ const created = useCallback(
81
+ (event: { nativeEvent: { mapId: string; ready: boolean } }) => {
82
+ if (active.current?.handle === event.nativeEvent.mapId) return;
83
+ unsubscribe.current?.();
84
+ unsubscribe.current = undefined;
85
+ const c = new RanaMapController(runtime, event.nativeEvent.mapId);
86
+ replaceController(active, c);
87
+ setReadyMapId(null);
88
+ setController(c);
89
+ let ready = false;
90
+ const becameReady = () => {
91
+ if (!ready && active.current === c) {
92
+ ready = true;
93
+ setReadyMapId(c.handle);
94
+ callbacks.current.onReady?.(c);
95
+ }
96
+ };
97
+ unsubscribe.current = c.onEvent((e) => {
98
+ if (e.type === "ready") becameReady();
99
+ if (e.type === "error")
100
+ callbacks.current.onError?.(
101
+ new RanaError(e.value as import("./models").ErrorInfo),
102
+ );
103
+ callbacks.current.onEvent?.(e);
104
+ });
105
+ callbacks.current.onMapCreated?.(c);
106
+ if (event.nativeEvent.ready || runtime.isMapReady(c.handle))
107
+ becameReady();
108
+ },
109
+ [],
110
+ );
111
+ useEffect(
112
+ () => () => {
113
+ unsubscribe.current?.();
114
+ replaceController(active, null);
115
+ },
116
+ [],
117
+ );
118
+ return (
119
+ <NativeMap
120
+ {...props}
121
+ optionsJSON={JSON.stringify(options)}
122
+ onMapCreated={created}
123
+ onMapError={(event) => {
124
+ const error = JSON.parse(event.nativeEvent.errorJSON);
125
+ if (error.creationFailed) {
126
+ unsubscribe.current?.();
127
+ unsubscribe.current = undefined;
128
+ replaceController(active, null);
129
+ setController(null);
130
+ setReadyMapId(null);
131
+ }
132
+ callbacks.current.onError?.(new RanaError(error));
133
+ }}
134
+ style={[styles.map, style]}
135
+ >
136
+ <MapContext.Provider value={readyMapId}>{children}</MapContext.Provider>
137
+ </NativeMap>
138
+ );
139
+ },
140
+ );
141
+ export interface RanaViewMarkerProps extends ViewProps {
142
+ point: LatLng;
143
+ options?: ViewMarkerOptions;
144
+ visible?: boolean;
145
+ children: ReactNode;
146
+ }
147
+ export function RanaViewMarker({
148
+ point,
149
+ options = {},
150
+ visible = true,
151
+ children,
152
+ ...props
153
+ }: RanaViewMarkerProps) {
154
+ const mapId = useContext(MapContext);
155
+ if (!mapId) return null;
156
+ return (
157
+ <NativeMarker
158
+ {...props}
159
+ mapId={mapId}
160
+ positionJSON={JSON.stringify(point)}
161
+ optionsJSON={JSON.stringify(options)}
162
+ visible={visible}
163
+ >
164
+ {children}
165
+ </NativeMarker>
166
+ );
167
+ }
168
+ const styles = StyleSheet.create({ map: { flex: 1 } });
@@ -0,0 +1,11 @@
1
+ import type { RanaMapController } from "./sdk";
2
+
3
+ /** Transfer ownership before disposing so a failed native cleanup cannot retain the old map. */
4
+ export function replaceController(
5
+ active: { current: RanaMapController | null },
6
+ next: RanaMapController | null,
7
+ ) {
8
+ const previous = active.current;
9
+ active.current = next;
10
+ if (previous && previous !== next) void previous.dispose().catch(() => {});
11
+ }