@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.
- package/BETA-STATUS.md +13 -0
- package/CHANGELOG.md +40 -0
- package/LICENSE.md +13 -0
- package/NOTICES.md +230 -0
- package/README.md +61 -2
- package/RanaMapsReactNative.podspec +17 -0
- package/android/build.gradle +25 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/ranamaps/reactnative/RanaMapManager.kt +115 -0
- package/android/src/main/java/com/ranamaps/reactnative/RanaMapsModule.kt +41 -0
- package/android/src/main/java/com/ranamaps/reactnative/RanaMapsPackage.kt +11 -0
- package/app.plugin.js +127 -0
- package/ios/RanaMapViewComponentView.mm +116 -0
- package/ios/RanaMapsModule.mm +32 -0
- package/ios/RanaNativeRuntime.swift +42 -0
- package/lib/bridge.d.ts +40 -0
- package/lib/bridge.js +152 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.js +113 -0
- package/lib/map-lifecycle.d.ts +5 -0
- package/lib/map-lifecycle.js +10 -0
- package/lib/models.d.ts +509 -0
- package/lib/models.js +2 -0
- package/lib/sdk.d.ts +309 -0
- package/lib/sdk.js +588 -0
- package/lib/specs/NativeRanaMaps.d.ts +8 -0
- package/lib/specs/NativeRanaMaps.js +4 -0
- package/lib/specs/RanaMapViewNativeComponent.d.ts +13 -0
- package/lib/specs/RanaMapViewNativeComponent.js +4 -0
- package/lib/specs/RanaMarkerViewNativeComponent.d.ts +9 -0
- package/lib/specs/RanaMarkerViewNativeComponent.js +4 -0
- package/package.json +59 -4
- package/react-native.config.js +12 -0
- package/src/bridge.ts +184 -0
- package/src/index.tsx +168 -0
- package/src/map-lifecycle.ts +11 -0
- package/src/models.ts +510 -0
- package/src/sdk.ts +732 -0
- package/src/specs/NativeRanaMaps.ts +16 -0
- package/src/specs/RanaMapViewNativeComponent.ts +10 -0
- package/src/specs/RanaMarkerViewNativeComponent.ts +9 -0
package/app.plugin.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
const {
|
|
2
|
+
withInfoPlist,
|
|
3
|
+
withAppDelegate,
|
|
4
|
+
withAndroidManifest,
|
|
5
|
+
withProjectBuildGradle,
|
|
6
|
+
withPodfile,
|
|
7
|
+
withPodfileProperties,
|
|
8
|
+
withGradleProperties,
|
|
9
|
+
} = require("@expo/config-plugins");
|
|
10
|
+
const iosMarker = "// Rana Maps host configuration";
|
|
11
|
+
function configureSwiftAppDelegate(contents) {
|
|
12
|
+
if (contents.includes(iosMarker)) return contents;
|
|
13
|
+
const launch = /(didFinishLaunchingWithOptions\s+\w+\s*:\s*\[UIApplication\.LaunchOptionsKey:\s*Any\]\?[^\n]*\n\s*\)\s*->\s*Bool\s*\{)/;
|
|
14
|
+
if (!launch.test(contents)) throw new Error("Rana Maps requires a Swift AppDelegate with didFinishLaunchingWithOptions; configure RanaMaps manually for other hosts.");
|
|
15
|
+
return (/(?:^|\n)\s*(?:internal\s+)?import RanaMapsSDK\b/.test(contents) ? "" : "import RanaMapsSDK\n") + contents.replace(launch, `$1
|
|
16
|
+
${iosMarker}
|
|
17
|
+
if let key = Bundle.main.object(forInfoDictionaryKey: "RanaMapsAPIKey") as? String {
|
|
18
|
+
do { try RanaMaps.configure(apiKey: key) }
|
|
19
|
+
catch { NSLog("Rana Maps configuration failed. Check the public API key.") }
|
|
20
|
+
}`);
|
|
21
|
+
}
|
|
22
|
+
function configureAndroidKey(manifest, key) {
|
|
23
|
+
if (key) {
|
|
24
|
+
const application = manifest.application?.[0];
|
|
25
|
+
if (!application) throw new Error("Rana Maps requires an Android application element.");
|
|
26
|
+
const metadata = application["meta-data"] ?? [];
|
|
27
|
+
const entry = metadata.find((item) => item.$?.["android:name"] === "com.ranamaps.API_KEY");
|
|
28
|
+
if (entry) entry.$["android:value"] = key;
|
|
29
|
+
else metadata.push({ $: { "android:name": "com.ranamaps.API_KEY", "android:value": key } });
|
|
30
|
+
application["meta-data"] = metadata;
|
|
31
|
+
}
|
|
32
|
+
return manifest;
|
|
33
|
+
}
|
|
34
|
+
/** Native permissions remain host-owned; a description does not request location permission. */
|
|
35
|
+
module.exports = function withRanaMaps(config, options = {}) {
|
|
36
|
+
const key = process.env.RANA_PUBLIC_KEY;
|
|
37
|
+
if (key && (!key.startsWith("pk_live_") || key.length <= "pk_live_".length || /\s/u.test(key)))
|
|
38
|
+
throw new Error("RANA_PUBLIC_KEY must be a valid app-restricted public key starting with pk_live_.");
|
|
39
|
+
config = withPodfileProperties(config, (result) => {
|
|
40
|
+
if (
|
|
41
|
+
Number.parseFloat(result.modResults["ios.deploymentTarget"] ?? "0") < 17
|
|
42
|
+
)
|
|
43
|
+
result.modResults["ios.deploymentTarget"] = "17.0";
|
|
44
|
+
return result;
|
|
45
|
+
});
|
|
46
|
+
config = withGradleProperties(config, (result) => {
|
|
47
|
+
for (const [key, minimum] of [
|
|
48
|
+
["android.minSdkVersion", 26],
|
|
49
|
+
["android.compileSdkVersion", 37],
|
|
50
|
+
]) {
|
|
51
|
+
const existing = result.modResults.find(
|
|
52
|
+
(item) => item.type === "property" && item.key === key,
|
|
53
|
+
);
|
|
54
|
+
if (existing) {
|
|
55
|
+
if (Number(existing.value) < minimum) existing.value = String(minimum);
|
|
56
|
+
} else
|
|
57
|
+
result.modResults.push({
|
|
58
|
+
type: "property",
|
|
59
|
+
key,
|
|
60
|
+
value: String(minimum),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
64
|
+
});
|
|
65
|
+
config = withInfoPlist(config, (result) => {
|
|
66
|
+
if (key) result.modResults.RanaMapsAPIKey = key;
|
|
67
|
+
if (options.locationWhenInUsePermission)
|
|
68
|
+
result.modResults.NSLocationWhenInUseUsageDescription =
|
|
69
|
+
options.locationWhenInUsePermission;
|
|
70
|
+
return result;
|
|
71
|
+
});
|
|
72
|
+
if (options.configureAppDelegate !== false) config = withAppDelegate(config, (result) => {
|
|
73
|
+
if (result.modResults.language !== "swift") throw new Error("Rana Maps Expo plugin supports Swift AppDelegate only; configure other hosts manually.");
|
|
74
|
+
result.modResults.contents = configureSwiftAppDelegate(result.modResults.contents);
|
|
75
|
+
return result;
|
|
76
|
+
});
|
|
77
|
+
config = withAndroidManifest(config, (result) => {
|
|
78
|
+
const manifest = result.modResults.manifest;
|
|
79
|
+
const permissions = manifest["uses-permission"] ?? [];
|
|
80
|
+
for (const name of options.locationPermission
|
|
81
|
+
? [
|
|
82
|
+
"android.permission.INTERNET",
|
|
83
|
+
"android.permission.ACCESS_COARSE_LOCATION",
|
|
84
|
+
"android.permission.ACCESS_FINE_LOCATION",
|
|
85
|
+
]
|
|
86
|
+
: ["android.permission.INTERNET"]) {
|
|
87
|
+
if (!permissions.some((p) => p.$?.["android:name"] === name))
|
|
88
|
+
permissions.push({ $: { "android:name": name } });
|
|
89
|
+
}
|
|
90
|
+
manifest["uses-permission"] = permissions;
|
|
91
|
+
configureAndroidKey(manifest, key);
|
|
92
|
+
return result;
|
|
93
|
+
});
|
|
94
|
+
config = withProjectBuildGradle(config, (result) => {
|
|
95
|
+
const marker = "// Rana Maps native Maven repository";
|
|
96
|
+
if (!result.modResults.contents.includes(marker))
|
|
97
|
+
result.modResults.contents += `
|
|
98
|
+
${marker}
|
|
99
|
+
allprojects { repositories { maven { url(System.getenv("RANA_MAVEN_REPOSITORY") ?: "https://assets.ranamaps.com/maven") } } }
|
|
100
|
+
`;
|
|
101
|
+
return result;
|
|
102
|
+
});
|
|
103
|
+
return withPodfile(config, (result) => {
|
|
104
|
+
const marker = "# Rana Maps native binary";
|
|
105
|
+
if (!result.modResults.contents.includes(marker)) {
|
|
106
|
+
const setup = `${marker}
|
|
107
|
+
if ENV['RANA_IOS_BINARY_DIR']
|
|
108
|
+
pod 'RanaMapsSDK', :path => ENV['RANA_IOS_BINARY_DIR']
|
|
109
|
+
else
|
|
110
|
+
pod 'RanaMapsSDK', :podspec => 'https://assets.ranamaps.com/sdk/ios/0.4.0-beta.0/RanaMapsSDK.podspec'
|
|
111
|
+
end`;
|
|
112
|
+
if (!result.modResults.contents.includes("use_expo_modules!"))
|
|
113
|
+
throw new Error(
|
|
114
|
+
"Rana Maps config plugin requires an Expo Podfile with use_expo_modules!",
|
|
115
|
+
);
|
|
116
|
+
result.modResults.contents = result.modResults.contents.replace(
|
|
117
|
+
"use_expo_modules!",
|
|
118
|
+
`use_expo_modules!
|
|
119
|
+
${setup}`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
module.exports.configureSwiftAppDelegate = configureSwiftAppDelegate;
|
|
127
|
+
module.exports.configureAndroidKey = configureAndroidKey;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
#import <React/RCTViewComponentView.h>
|
|
2
|
+
#import <react/renderer/components/RanaMapsSpec/ComponentDescriptors.h>
|
|
3
|
+
#import <react/renderer/components/RanaMapsSpec/EventEmitters.h>
|
|
4
|
+
#import <react/renderer/components/RanaMapsSpec/Props.h>
|
|
5
|
+
#import "RanaMapsReactNative-Swift.h"
|
|
6
|
+
using namespace facebook::react;
|
|
7
|
+
|
|
8
|
+
@interface RanaMarkerViewComponentView : RCTViewComponentView
|
|
9
|
+
- (void)removeMarker;
|
|
10
|
+
@end
|
|
11
|
+
@interface RanaMapViewComponentView : RCTViewComponentView
|
|
12
|
+
@end
|
|
13
|
+
@implementation RanaMapViewComponentView {
|
|
14
|
+
RanaNativeRuntime *_runtime;
|
|
15
|
+
UIView *_map;
|
|
16
|
+
NSString *_mapId;
|
|
17
|
+
BOOL _announced;
|
|
18
|
+
NSString *_pendingMapError;
|
|
19
|
+
NSMutableArray<UIView<RCTComponentViewProtocol> *> *_managedChildren;
|
|
20
|
+
}
|
|
21
|
+
+ (ComponentDescriptorProvider)componentDescriptorProvider { return concreteComponentDescriptorProvider<RanaMapViewComponentDescriptor>(); }
|
|
22
|
+
- (instancetype)initWithFrame:(CGRect)frame {
|
|
23
|
+
if ((self = [super initWithFrame:frame])) { _props = std::make_shared<const RanaMapViewProps>(); _managedChildren = [NSMutableArray new]; }
|
|
24
|
+
return self;
|
|
25
|
+
}
|
|
26
|
+
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps {
|
|
27
|
+
const auto &next = *std::static_pointer_cast<const RanaMapViewProps>(props);
|
|
28
|
+
const auto &previous = *std::static_pointer_cast<const RanaMapViewProps>(_props);
|
|
29
|
+
NSString *options = [NSString stringWithUTF8String:next.optionsJSON.c_str()];
|
|
30
|
+
if (!_map) {
|
|
31
|
+
_announced = NO;
|
|
32
|
+
if (_mapId) [_runtime removeMap:_mapId];
|
|
33
|
+
[_map removeFromSuperview];
|
|
34
|
+
_map = nil; _mapId = nil; _pendingMapError = nil;
|
|
35
|
+
_runtime = [RanaNativeRuntime shared];
|
|
36
|
+
NSError *error = nil;
|
|
37
|
+
NSDictionary *host = [_runtime makeMap:options error:&error];
|
|
38
|
+
if (host) {
|
|
39
|
+
_mapId = host[@"id"]; _map = host[@"view"];
|
|
40
|
+
[self insertSubview:_map atIndex:0]; _map.frame = self.bounds;
|
|
41
|
+
} else {
|
|
42
|
+
NSDictionary *details = @{ @"code": error.domain ?: @"native_error", @"message": error.localizedDescription ?: @"Map creation failed", @"status": @(error.code), @"creationFailed": @YES };
|
|
43
|
+
NSData *data = [NSJSONSerialization dataWithJSONObject:details options:0 error:nil];
|
|
44
|
+
_pendingMapError = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
45
|
+
}
|
|
46
|
+
} else if (_mapId && previous.optionsJSON != next.optionsJSON) {
|
|
47
|
+
[_runtime execute:NSUUID.UUID.UUIDString target:_mapId method:@"updateProps" argumentsJSON:options completion:^(NSString *answer, NSError *error) {}];
|
|
48
|
+
}
|
|
49
|
+
[super updateProps:props oldProps:oldProps];
|
|
50
|
+
}
|
|
51
|
+
- (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask {
|
|
52
|
+
[super finalizeUpdates:updateMask];
|
|
53
|
+
if (_pendingMapError && _eventEmitter) {
|
|
54
|
+
std::static_pointer_cast<const RanaMapViewEventEmitter>(_eventEmitter)->onMapError({std::string(_pendingMapError.UTF8String)});
|
|
55
|
+
_pendingMapError = nil;
|
|
56
|
+
}
|
|
57
|
+
if (_mapId && _eventEmitter && !_announced) {
|
|
58
|
+
_announced = YES;
|
|
59
|
+
auto emitter = std::static_pointer_cast<const RanaMapViewEventEmitter>(_eventEmitter);
|
|
60
|
+
emitter->onMapCreated({std::string(_mapId.UTF8String), NO});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
- (void)layoutSubviews { [super layoutSubviews]; _map.frame = self.bounds; }
|
|
64
|
+
- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)child index:(NSInteger)index {
|
|
65
|
+
[_managedChildren insertObject:child atIndex:index];
|
|
66
|
+
if (![child isKindOfClass:RanaMarkerViewComponentView.class]) [super mountChildComponentView:child index:index];
|
|
67
|
+
}
|
|
68
|
+
- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)child index:(NSInteger)index {
|
|
69
|
+
[_managedChildren removeObject:child];
|
|
70
|
+
if ([child isKindOfClass:RanaMarkerViewComponentView.class]) [(RanaMarkerViewComponentView *)child removeMarker];
|
|
71
|
+
else [super unmountChildComponentView:child index:index];
|
|
72
|
+
}
|
|
73
|
+
- (void)prepareForRecycle {
|
|
74
|
+
if (_mapId) [_runtime removeMap:_mapId]; [_map removeFromSuperview]; _map = nil; _mapId = nil; _announced = NO; _pendingMapError = nil; [_managedChildren removeAllObjects];
|
|
75
|
+
[super prepareForRecycle];
|
|
76
|
+
}
|
|
77
|
+
@end
|
|
78
|
+
|
|
79
|
+
@implementation RanaMarkerViewComponentView {
|
|
80
|
+
RanaNativeRuntime *_runtime;
|
|
81
|
+
NSString *_handle;
|
|
82
|
+
NSString *_positionJSON;
|
|
83
|
+
}
|
|
84
|
+
+ (ComponentDescriptorProvider)componentDescriptorProvider { return concreteComponentDescriptorProvider<RanaMarkerViewComponentDescriptor>(); }
|
|
85
|
+
- (instancetype)initWithFrame:(CGRect)frame {
|
|
86
|
+
if ((self = [super initWithFrame:frame])) { _props = std::make_shared<const RanaMarkerViewProps>(); }
|
|
87
|
+
return self;
|
|
88
|
+
}
|
|
89
|
+
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps {
|
|
90
|
+
const auto &next = *std::static_pointer_cast<const RanaMarkerViewProps>(props);
|
|
91
|
+
const auto &previous = *std::static_pointer_cast<const RanaMarkerViewProps>(_props);
|
|
92
|
+
NSString *map = [NSString stringWithUTF8String:next.mapId.c_str()];
|
|
93
|
+
NSString *point = [NSString stringWithUTF8String:next.positionJSON.c_str()];
|
|
94
|
+
_positionJSON = point;
|
|
95
|
+
if (map.length && point.length) {
|
|
96
|
+
_runtime = [RanaNativeRuntime shared];
|
|
97
|
+
if (!_handle || previous.optionsJSON != next.optionsJSON || previous.mapId != next.mapId) {
|
|
98
|
+
[self removeMarker]; [self removeFromSuperview]; NSError *error = nil;
|
|
99
|
+
_handle = [_runtime attachMarker:map view:self positionJSON:point optionsJSON:[NSString stringWithUTF8String:next.optionsJSON.c_str()] error:&error];
|
|
100
|
+
}
|
|
101
|
+
if (_handle) {
|
|
102
|
+
[_runtime execute:NSUUID.UUID.UUIDString target:_handle method:@"setPosition" argumentsJSON:[NSString stringWithFormat:@"{\"point\":%@}",point] completion:^(NSString *a, NSError *e) {}];
|
|
103
|
+
[_runtime execute:NSUUID.UUID.UUIDString target:_handle method:@"setVisible" argumentsJSON:next.visible ? @"{\"visible\":true}" : @"{\"visible\":false}" completion:^(NSString *a, NSError *e) {}];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
[super updateProps:props oldProps:oldProps];
|
|
107
|
+
}
|
|
108
|
+
- (void)layoutSubviews {
|
|
109
|
+
[super layoutSubviews];
|
|
110
|
+
if (_handle && _positionJSON) [_runtime execute:NSUUID.UUID.UUIDString target:_handle method:@"setPosition" argumentsJSON:[NSString stringWithFormat:@"{\"point\":%@}",_positionJSON] completion:^(NSString *a, NSError *e) {}];
|
|
111
|
+
}
|
|
112
|
+
- (void)removeMarker {
|
|
113
|
+
if (_handle) { [_runtime removeMarker:_handle]; _handle = nil; }
|
|
114
|
+
}
|
|
115
|
+
- (void)prepareForRecycle { [self removeMarker]; [super prepareForRecycle]; }
|
|
116
|
+
@end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#import <RanaMapsSpec/RanaMapsSpec.h>
|
|
2
|
+
#import <React/RCTInvalidating.h>
|
|
3
|
+
#import "RanaMapsReactNative-Swift.h"
|
|
4
|
+
|
|
5
|
+
@interface RanaMapsModule : NativeRanaMapsSpecBase <NativeRanaMapsSpec, RCTInvalidating>
|
|
6
|
+
@end
|
|
7
|
+
@implementation RanaMapsModule {
|
|
8
|
+
RanaNativeRuntime *_runtime;
|
|
9
|
+
}
|
|
10
|
+
RCT_EXPORT_MODULE(RanaMaps)
|
|
11
|
+
+ (BOOL)requiresMainQueueSetup { return YES; }
|
|
12
|
+
- (instancetype)init {
|
|
13
|
+
if ((self = [super init])) {
|
|
14
|
+
_runtime = [RanaNativeRuntime shared];
|
|
15
|
+
__weak RanaMapsModule *weakSelf = self;
|
|
16
|
+
_runtime.onEvent = ^(NSString *json) { [weakSelf emitOnEvent:json]; };
|
|
17
|
+
}
|
|
18
|
+
return self;
|
|
19
|
+
}
|
|
20
|
+
- (void)execute:(NSString *)requestId target:(NSString *)target method:(NSString *)method argumentsJson:(NSString *)argumentsJson resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
|
|
21
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
22
|
+
[self->_runtime execute:requestId target:target method:method argumentsJSON:argumentsJson completion:^(NSString *answer, NSError *error) {
|
|
23
|
+
if (error) reject(error.domain, error.localizedDescription, error); else resolve(answer);
|
|
24
|
+
}];
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
- (void)cancel:(NSString *)requestId { dispatch_async(dispatch_get_main_queue(), ^{ [self->_runtime cancel:requestId]; }); }
|
|
28
|
+
- (void)invalidate { dispatch_async(dispatch_get_main_queue(), ^{ [self->_runtime invalidate]; }); }
|
|
29
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params {
|
|
30
|
+
return std::make_shared<facebook::react::NativeRanaMapsSpecJSI>(params);
|
|
31
|
+
}
|
|
32
|
+
@end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import UIKit
|
|
3
|
+
import RanaMapsSDK
|
|
4
|
+
|
|
5
|
+
@objc(RanaNativeRuntime)
|
|
6
|
+
@MainActor public final class RanaNativeRuntime: NSObject {
|
|
7
|
+
private static var instance: RanaNativeRuntime?
|
|
8
|
+
@objc public static func shared() -> RanaNativeRuntime {
|
|
9
|
+
if let instance { return instance }
|
|
10
|
+
let value = RanaNativeRuntime(); instance = value; return value
|
|
11
|
+
}
|
|
12
|
+
let bridge = RanaBridge()
|
|
13
|
+
@objc public var onEvent: ((String) -> Void)? { didSet { bridge.onEvent = onEvent } }
|
|
14
|
+
private var requests: [String: Task<Void, Never>] = [:]
|
|
15
|
+
@objc public func makeMap(_ optionsJSON: String) throws -> NSDictionary {
|
|
16
|
+
let host = try bridge.createMap(optionsJSON: optionsJSON)
|
|
17
|
+
return ["id": host.id, "view": host.view, "ready": host.view.controller != nil]
|
|
18
|
+
}
|
|
19
|
+
@objc public func removeMarker(_ id: String) { bridge.disposeResource(id) }
|
|
20
|
+
@objc public func removeMap(_ id: String) { bridge.disposeMap(id) }
|
|
21
|
+
@objc public func attachMarker(_ mapID: String, view: UIView, positionJSON: String, optionsJSON: String) throws -> String {
|
|
22
|
+
try bridge.attachViewMarker(mapID: mapID, view: view, positionJSON: positionJSON, optionsJSON: optionsJSON)
|
|
23
|
+
}
|
|
24
|
+
@objc public func execute(_ requestID: String, target: String, method: String, argumentsJSON: String, completion: @escaping (String?, NSError?) -> Void) {
|
|
25
|
+
guard requests[requestID] == nil else { completion(nil, NSError(domain: "RanaMaps", code: 0, userInfo: [NSLocalizedDescriptionKey: "Duplicate request"])); return }
|
|
26
|
+
requests[requestID] = Task { @MainActor [weak self] in
|
|
27
|
+
guard let self else { return }; defer { requests[requestID] = nil }
|
|
28
|
+
do { completion(try await bridge.execute(target: target, method: method, argumentsJSON: argumentsJSON), nil) }
|
|
29
|
+
catch {
|
|
30
|
+
let e = error as? RanaError ?? RanaError(code: error is CancellationError ? "cancelled" : "native_error", message: error.localizedDescription, status: 0)
|
|
31
|
+
let details: [String: Any] = ["code": e.code, "message": e.message, "status": e.status, "requestId": e.requestId.map { $0 as Any } ?? NSNull(), "retryAfter": e.retryAfter.map { $0 as Any } ?? NSNull()]
|
|
32
|
+
let json = (try? JSONSerialization.data(withJSONObject: details)).map { String(decoding: $0, as: UTF8.self) } ?? "{}"
|
|
33
|
+
completion(nil, NSError(domain: e.code, code: e.status, userInfo: [NSLocalizedDescriptionKey: e.message, "rana": json]))
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
@objc public func cancel(_ requestID: String) { requests[requestID]?.cancel() }
|
|
38
|
+
@objc public func invalidate() {
|
|
39
|
+
requests.values.forEach { $0.cancel() }; requests.removeAll(); bridge.dispose()
|
|
40
|
+
if Self.instance === self { Self.instance = nil }
|
|
41
|
+
}
|
|
42
|
+
}
|
package/lib/bridge.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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(requestId: string, target: string, method: string, argumentsJson: string): Promise<string>;
|
|
9
|
+
cancel(requestId: string): void;
|
|
10
|
+
subscribe(listener: (eventJson: string) => void): () => void;
|
|
11
|
+
}
|
|
12
|
+
export declare class RanaError extends Error implements ErrorInfo {
|
|
13
|
+
readonly code: string;
|
|
14
|
+
readonly status: number;
|
|
15
|
+
readonly requestId?: string;
|
|
16
|
+
readonly retryAfter?: number;
|
|
17
|
+
constructor(info: ErrorInfo);
|
|
18
|
+
static from(error: unknown): RanaError;
|
|
19
|
+
}
|
|
20
|
+
export declare class RanaRuntime {
|
|
21
|
+
readonly transport: BridgeTransport;
|
|
22
|
+
private sequence;
|
|
23
|
+
private readyHandles;
|
|
24
|
+
isMapReady(handle: string): boolean;
|
|
25
|
+
forgetHandle(handle: string): void;
|
|
26
|
+
private callbacks;
|
|
27
|
+
private listeners;
|
|
28
|
+
private inFlight;
|
|
29
|
+
private unsubscribe;
|
|
30
|
+
constructor(transport: BridgeTransport);
|
|
31
|
+
call<T>(target: string, method: string, args?: unknown, signal?: AbortSignal): Promise<T>;
|
|
32
|
+
on(target: string, listener: (event: RanaEvent) => void): () => void;
|
|
33
|
+
registerCallback(callback: (kind: string, value: unknown, signal: AbortSignal) => Promise<unknown>): {
|
|
34
|
+
id: string;
|
|
35
|
+
dispose: () => void;
|
|
36
|
+
};
|
|
37
|
+
private receive;
|
|
38
|
+
private answer;
|
|
39
|
+
dispose(): void;
|
|
40
|
+
}
|
package/lib/bridge.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RanaRuntime = exports.RanaError = void 0;
|
|
4
|
+
class RanaError extends Error {
|
|
5
|
+
code;
|
|
6
|
+
status;
|
|
7
|
+
requestId;
|
|
8
|
+
retryAfter;
|
|
9
|
+
constructor(info) {
|
|
10
|
+
super(info.message);
|
|
11
|
+
this.name = "RanaError";
|
|
12
|
+
this.code = info.code;
|
|
13
|
+
this.status = info.status;
|
|
14
|
+
this.requestId = info.requestId ?? undefined;
|
|
15
|
+
this.retryAfter = info.retryAfter ?? undefined;
|
|
16
|
+
}
|
|
17
|
+
static from(error) {
|
|
18
|
+
if (error instanceof RanaError)
|
|
19
|
+
return error;
|
|
20
|
+
const e = error;
|
|
21
|
+
const raw = e?.userInfo?.rana ?? e?.details;
|
|
22
|
+
if (raw) {
|
|
23
|
+
try {
|
|
24
|
+
return new RanaError(JSON.parse(raw));
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
/* Preserve the platform error below. */
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return new RanaError({
|
|
31
|
+
code: e?.code ?? "native_error",
|
|
32
|
+
message: e?.message ?? String(error),
|
|
33
|
+
status: 0,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.RanaError = RanaError;
|
|
38
|
+
class RanaRuntime {
|
|
39
|
+
transport;
|
|
40
|
+
sequence = 0;
|
|
41
|
+
readyHandles = new Set();
|
|
42
|
+
isMapReady(handle) {
|
|
43
|
+
return this.readyHandles.has(handle);
|
|
44
|
+
}
|
|
45
|
+
forgetHandle(handle) {
|
|
46
|
+
this.readyHandles.delete(handle);
|
|
47
|
+
}
|
|
48
|
+
callbacks = new Map();
|
|
49
|
+
listeners = new Map();
|
|
50
|
+
inFlight = new Map();
|
|
51
|
+
unsubscribe;
|
|
52
|
+
constructor(transport) {
|
|
53
|
+
this.transport = transport;
|
|
54
|
+
this.unsubscribe = transport.subscribe((json) => this.receive(JSON.parse(json)));
|
|
55
|
+
}
|
|
56
|
+
async call(target, method, args = {}, signal) {
|
|
57
|
+
if (signal?.aborted)
|
|
58
|
+
throw new RanaError({
|
|
59
|
+
code: "cancelled",
|
|
60
|
+
message: "Operation cancelled",
|
|
61
|
+
status: 0,
|
|
62
|
+
});
|
|
63
|
+
const id = `request-${++this.sequence}`;
|
|
64
|
+
const cancel = () => this.transport.cancel(id);
|
|
65
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
66
|
+
try {
|
|
67
|
+
return JSON.parse(await this.transport.execute(id, target, method, JSON.stringify(args)));
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
throw RanaError.from(error);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
signal?.removeEventListener("abort", cancel);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
on(target, listener) {
|
|
77
|
+
const entries = this.listeners.get(target) ?? new Set();
|
|
78
|
+
entries.add(listener);
|
|
79
|
+
this.listeners.set(target, entries);
|
|
80
|
+
return () => {
|
|
81
|
+
entries.delete(listener);
|
|
82
|
+
if (!entries.size)
|
|
83
|
+
this.listeners.delete(target);
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
registerCallback(callback) {
|
|
87
|
+
const id = `callback-${++this.sequence}`;
|
|
88
|
+
this.callbacks.set(id, callback);
|
|
89
|
+
return { id, dispose: () => this.callbacks.delete(id) };
|
|
90
|
+
}
|
|
91
|
+
receive(event) {
|
|
92
|
+
if (event.type === "ready")
|
|
93
|
+
this.readyHandles.add(event.target);
|
|
94
|
+
if (event.type === "callbackCancelled") {
|
|
95
|
+
this.inFlight
|
|
96
|
+
.get(event.value.requestId)
|
|
97
|
+
?.abort();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (event.type === "callback") {
|
|
101
|
+
void this.answer(event);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this.listeners.get(event.target)?.forEach((listener) => listener(event));
|
|
105
|
+
}
|
|
106
|
+
async answer(event) {
|
|
107
|
+
const request = event.value;
|
|
108
|
+
const controller = new AbortController();
|
|
109
|
+
this.inFlight.set(request.requestId, controller);
|
|
110
|
+
let reply;
|
|
111
|
+
try {
|
|
112
|
+
const callback = this.callbacks.get(event.target);
|
|
113
|
+
if (!callback)
|
|
114
|
+
throw new RanaError({
|
|
115
|
+
code: "callback_unavailable",
|
|
116
|
+
message: "Host callback was removed",
|
|
117
|
+
status: 0,
|
|
118
|
+
});
|
|
119
|
+
reply = {
|
|
120
|
+
requestId: request.requestId,
|
|
121
|
+
value: await callback(request.kind, request.arguments, controller.signal),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
const e = RanaError.from(error);
|
|
126
|
+
reply = {
|
|
127
|
+
requestId: request.requestId,
|
|
128
|
+
error: {
|
|
129
|
+
code: e.code,
|
|
130
|
+
message: e.message,
|
|
131
|
+
status: e.status,
|
|
132
|
+
requestId: e.requestId,
|
|
133
|
+
retryAfter: e.retryAfter,
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
this.inFlight.delete(request.requestId);
|
|
139
|
+
}
|
|
140
|
+
if (!controller.signal.aborted)
|
|
141
|
+
await this.call("runtime", "resolveCallback", reply);
|
|
142
|
+
}
|
|
143
|
+
dispose() {
|
|
144
|
+
this.unsubscribe();
|
|
145
|
+
this.inFlight.forEach((c) => c.abort());
|
|
146
|
+
this.inFlight.clear();
|
|
147
|
+
this.callbacks.clear();
|
|
148
|
+
this.listeners.clear();
|
|
149
|
+
this.readyHandles.clear();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.RanaRuntime = RanaRuntime;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React, { type ReactNode } from "react";
|
|
2
|
+
import { type ViewProps } from "react-native";
|
|
3
|
+
import { RanaRuntime, RanaError, type RanaEvent } from "./bridge";
|
|
4
|
+
import { RanaMapController, RanaClient as Client, RanaOfflineManager as OfflineManager, type HttpTransport } from "./sdk";
|
|
5
|
+
import type { MapOptions, LatLng, ViewMarkerOptions } from "./models";
|
|
6
|
+
export * from "./sdk";
|
|
7
|
+
export { RanaRuntime } from "./bridge";
|
|
8
|
+
export type { RanaEvent } from "./bridge";
|
|
9
|
+
export declare const runtime: RanaRuntime;
|
|
10
|
+
export declare function createClient(key?: string, transport?: HttpTransport): Promise<Client>;
|
|
11
|
+
export declare function createOfflineManager(key?: string): Promise<OfflineManager>;
|
|
12
|
+
export declare const RanaMaps: {
|
|
13
|
+
setTelemetry: (enabled: boolean) => Promise<void>;
|
|
14
|
+
version: () => Promise<string>;
|
|
15
|
+
};
|
|
16
|
+
export interface RanaMapProps extends ViewProps {
|
|
17
|
+
options?: MapOptions;
|
|
18
|
+
children?: ReactNode;
|
|
19
|
+
onMapCreated?: (controller: RanaMapController) => void;
|
|
20
|
+
onReady?: (controller: RanaMapController) => void;
|
|
21
|
+
onEvent?: (event: RanaEvent) => void;
|
|
22
|
+
onError?: (error: RanaError) => void;
|
|
23
|
+
}
|
|
24
|
+
export declare const RanaMap: React.ForwardRefExoticComponent<RanaMapProps & React.RefAttributes<RanaMapController | null>>;
|
|
25
|
+
export interface RanaViewMarkerProps extends ViewProps {
|
|
26
|
+
point: LatLng;
|
|
27
|
+
options?: ViewMarkerOptions;
|
|
28
|
+
visible?: boolean;
|
|
29
|
+
children: ReactNode;
|
|
30
|
+
}
|
|
31
|
+
export declare function RanaViewMarker({ point, options, visible, children, ...props }: RanaViewMarkerProps): import("react/jsx-runtime").JSX.Element | null;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.RanaMap = exports.RanaMaps = exports.runtime = exports.RanaRuntime = void 0;
|
|
21
|
+
exports.createClient = createClient;
|
|
22
|
+
exports.createOfflineManager = createOfflineManager;
|
|
23
|
+
exports.RanaViewMarker = RanaViewMarker;
|
|
24
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
25
|
+
const react_1 = require("react");
|
|
26
|
+
const react_native_1 = require("react-native");
|
|
27
|
+
const NativeRanaMaps_1 = __importDefault(require("./specs/NativeRanaMaps"));
|
|
28
|
+
const RanaMapViewNativeComponent_1 = __importDefault(require("./specs/RanaMapViewNativeComponent"));
|
|
29
|
+
const RanaMarkerViewNativeComponent_1 = __importDefault(require("./specs/RanaMarkerViewNativeComponent"));
|
|
30
|
+
const bridge_1 = require("./bridge");
|
|
31
|
+
const sdk_1 = require("./sdk");
|
|
32
|
+
const map_lifecycle_1 = require("./map-lifecycle");
|
|
33
|
+
__exportStar(require("./sdk"), exports);
|
|
34
|
+
var bridge_2 = require("./bridge");
|
|
35
|
+
Object.defineProperty(exports, "RanaRuntime", { enumerable: true, get: function () { return bridge_2.RanaRuntime; } });
|
|
36
|
+
exports.runtime = new bridge_1.RanaRuntime({
|
|
37
|
+
execute: (id, target, method, args) => NativeRanaMaps_1.default.execute(id, target, method, args),
|
|
38
|
+
cancel: (id) => NativeRanaMaps_1.default.cancel(id),
|
|
39
|
+
subscribe: (listener) => {
|
|
40
|
+
const subscription = NativeRanaMaps_1.default.onEvent(listener);
|
|
41
|
+
return () => subscription.remove();
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
function createClient(key, transport) {
|
|
45
|
+
return sdk_1.RanaClient.create(exports.runtime, key, transport);
|
|
46
|
+
}
|
|
47
|
+
function createOfflineManager(key) {
|
|
48
|
+
return sdk_1.RanaOfflineManager.create(exports.runtime, key);
|
|
49
|
+
}
|
|
50
|
+
exports.RanaMaps = {
|
|
51
|
+
setTelemetry: (enabled) => exports.runtime.call("runtime", "setTelemetry", { enabled }),
|
|
52
|
+
version: () => exports.runtime.call("runtime", "version"),
|
|
53
|
+
};
|
|
54
|
+
const MapContext = (0, react_1.createContext)(null);
|
|
55
|
+
exports.RanaMap = (0, react_1.forwardRef)(function RanaMap({ options = {}, onMapCreated, onReady, onEvent, onError, children, style, ...props }, ref) {
|
|
56
|
+
const [controller, setController] = (0, react_1.useState)(null);
|
|
57
|
+
const callbacks = (0, react_1.useRef)({ onMapCreated, onReady, onEvent, onError });
|
|
58
|
+
callbacks.current = { onMapCreated, onReady, onEvent, onError };
|
|
59
|
+
const [readyMapId, setReadyMapId] = (0, react_1.useState)(null);
|
|
60
|
+
const active = (0, react_1.useRef)(null);
|
|
61
|
+
const unsubscribe = (0, react_1.useRef)(undefined);
|
|
62
|
+
(0, react_1.useImperativeHandle)(ref, () => controller, [controller]);
|
|
63
|
+
const created = (0, react_1.useCallback)((event) => {
|
|
64
|
+
if (active.current?.handle === event.nativeEvent.mapId)
|
|
65
|
+
return;
|
|
66
|
+
unsubscribe.current?.();
|
|
67
|
+
unsubscribe.current = undefined;
|
|
68
|
+
const c = new sdk_1.RanaMapController(exports.runtime, event.nativeEvent.mapId);
|
|
69
|
+
(0, map_lifecycle_1.replaceController)(active, c);
|
|
70
|
+
setReadyMapId(null);
|
|
71
|
+
setController(c);
|
|
72
|
+
let ready = false;
|
|
73
|
+
const becameReady = () => {
|
|
74
|
+
if (!ready && active.current === c) {
|
|
75
|
+
ready = true;
|
|
76
|
+
setReadyMapId(c.handle);
|
|
77
|
+
callbacks.current.onReady?.(c);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
unsubscribe.current = c.onEvent((e) => {
|
|
81
|
+
if (e.type === "ready")
|
|
82
|
+
becameReady();
|
|
83
|
+
if (e.type === "error")
|
|
84
|
+
callbacks.current.onError?.(new bridge_1.RanaError(e.value));
|
|
85
|
+
callbacks.current.onEvent?.(e);
|
|
86
|
+
});
|
|
87
|
+
callbacks.current.onMapCreated?.(c);
|
|
88
|
+
if (event.nativeEvent.ready || exports.runtime.isMapReady(c.handle))
|
|
89
|
+
becameReady();
|
|
90
|
+
}, []);
|
|
91
|
+
(0, react_1.useEffect)(() => () => {
|
|
92
|
+
unsubscribe.current?.();
|
|
93
|
+
(0, map_lifecycle_1.replaceController)(active, null);
|
|
94
|
+
}, []);
|
|
95
|
+
return ((0, jsx_runtime_1.jsx)(RanaMapViewNativeComponent_1.default, { ...props, optionsJSON: JSON.stringify(options), onMapCreated: created, onMapError: (event) => {
|
|
96
|
+
const error = JSON.parse(event.nativeEvent.errorJSON);
|
|
97
|
+
if (error.creationFailed) {
|
|
98
|
+
unsubscribe.current?.();
|
|
99
|
+
unsubscribe.current = undefined;
|
|
100
|
+
(0, map_lifecycle_1.replaceController)(active, null);
|
|
101
|
+
setController(null);
|
|
102
|
+
setReadyMapId(null);
|
|
103
|
+
}
|
|
104
|
+
callbacks.current.onError?.(new bridge_1.RanaError(error));
|
|
105
|
+
}, style: [styles.map, style], children: (0, jsx_runtime_1.jsx)(MapContext.Provider, { value: readyMapId, children: children }) }));
|
|
106
|
+
});
|
|
107
|
+
function RanaViewMarker({ point, options = {}, visible = true, children, ...props }) {
|
|
108
|
+
const mapId = (0, react_1.useContext)(MapContext);
|
|
109
|
+
if (!mapId)
|
|
110
|
+
return null;
|
|
111
|
+
return ((0, jsx_runtime_1.jsx)(RanaMarkerViewNativeComponent_1.default, { ...props, mapId: mapId, positionJSON: JSON.stringify(point), optionsJSON: JSON.stringify(options), visible: visible, children: children }));
|
|
112
|
+
}
|
|
113
|
+
const styles = react_native_1.StyleSheet.create({ map: { flex: 1 } });
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { RanaMapController } from "./sdk";
|
|
2
|
+
/** Transfer ownership before disposing so a failed native cleanup cannot retain the old map. */
|
|
3
|
+
export declare function replaceController(active: {
|
|
4
|
+
current: RanaMapController | null;
|
|
5
|
+
}, next: RanaMapController | null): void;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.replaceController = replaceController;
|
|
4
|
+
/** Transfer ownership before disposing so a failed native cleanup cannot retain the old map. */
|
|
5
|
+
function replaceController(active, next) {
|
|
6
|
+
const previous = active.current;
|
|
7
|
+
active.current = next;
|
|
8
|
+
if (previous && previous !== next)
|
|
9
|
+
void previous.dispose().catch(() => { });
|
|
10
|
+
}
|