theta-client-react-native
Version:
This library provides a way to control RICOH THETA using.
63 lines (61 loc) • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _thetaWebApiError = require("./theta-repository/theta-web-api-error");
var _libs = require("./theta-repository/libs");
// New Architecture only: resolve TurboModule on first access
let cachedModule = null;
function getModule() {
if (cachedModule != null) {
return cachedModule;
}
const turbo = _reactNative.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;
}
var _default = exports.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 (!(0, _libs.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((0, _thetaWebApiError.normalizeNativeError)(err)) : Promise.reject((0, _thetaWebApiError.normalizeNativeError)(err));
return target.then(onFulfilled ?? undefined, normalizedRejected);
};
}
if (p === 'catch') {
return onRejected => target.catch(err => onRejected != null ? onRejected((0, _thetaWebApiError.normalizeNativeError)(err)) : Promise.reject((0, _thetaWebApiError.normalizeNativeError)(err)));
}
return target[p];
}
});
};
}
});
//# sourceMappingURL=NativeThetaClientReactNative.js.map