theta-client-react-native
Version:
This library provides a way to control RICOH THETA using.
57 lines (56 loc) • 2.3 kB
JavaScript
import { TurboModuleRegistry } from 'react-native';
import { normalizeNativeError } from './theta-repository/theta-web-api-error';
import { isPromiseLike } from './theta-repository/libs';
// New Architecture only: resolve TurboModule on first access
let cachedModule = null;
function getModule() {
if (cachedModule != null) {
return cachedModule;
}
const turbo = TurboModuleRegistry.get('ThetaClientReactNative');
if (turbo == null) {
// In test environment, return a mock object instead of throwing
if (process.env.NODE_ENV === 'test' || process.env.JEST_WORKER_ID !== undefined) {
cachedModule = {};
return cachedModule;
}
throw new Error('ThetaClientReactNative TurboModule could not be found. Ensure New Architecture is enabled and the module is registered in the native binary.');
}
cachedModule = turbo;
return cachedModule;
}
export default new Proxy({}, {
get(_, prop) {
const module = getModule();
const value = module[prop];
if (typeof value !== 'function') {
return value;
}
// Wrap native async methods so rejected promises carry parsed THETA Web API details,
// without adding extra microtask ticks on the success path.
return (...args) => {
const result = value.apply(module, args);
if (!isPromiseLike(result)) {
return result;
}
// Wrap the original promise in a Proxy that intercepts .then()/.catch() to
// normalize errors, while preserving the microtask timing of the original
// promise on the success path (no extra .then() hops added).
return new Proxy(result, {
get(target, p) {
if (p === 'then') {
return (onFulfilled, onRejected) => {
const normalizedRejected = err => onRejected != null ? onRejected(normalizeNativeError(err)) : Promise.reject(normalizeNativeError(err));
return target.then(onFulfilled ?? undefined, normalizedRejected);
};
}
if (p === 'catch') {
return onRejected => target.catch(err => onRejected != null ? onRejected(normalizeNativeError(err)) : Promise.reject(normalizeNativeError(err)));
}
return target[p];
}
});
};
}
});
//# sourceMappingURL=NativeThetaClientReactNative.js.map