Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,40 @@
"description": "Mendix native mobile package",
"main": "./lib/module/index.js",
"types": "./lib/typescript/src/index.d.ts",
"typesVersions": {
"*": {
"file-system": [
"lib/typescript/src/file-system/index.d.ts"
],
"native-modules": [
"lib/typescript/src/native-modules/index.d.ts"
],
"navigation-mode": [
"lib/typescript/src/navigation-mode/index.d.ts"
]
}
},
"exports": {
".": {
"source": "./src/index.tsx",
"types": "./lib/typescript/src/index.d.ts",
"default": "./lib/module/index.js"
},
"./file-system": {
"source": "./src/file-system/index.ts",
"types": "./lib/typescript/src/file-system/index.d.ts",
"default": "./lib/module/file-system/index.js"
},
"./native-modules": {
"source": "./src/native-modules/index.ts",
"types": "./lib/typescript/src/native-modules/index.d.ts",
"default": "./lib/module/native-modules/index.js"
},
"./navigation-mode": {
"source": "./src/navigation-mode/index.ts",
"types": "./lib/typescript/src/navigation-mode/index.d.ts",
"default": "./lib/module/navigation-mode/index.js"
},
"./package.json": "./package.json"
},
"files": [
Expand Down
25 changes: 19 additions & 6 deletions src/file-system/NativeMxFileSystem.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
TurboModuleRegistry,
type TurboModule,
type CodegenTypes,
} from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import type { TurboModule, CodegenTypes } from 'react-native';

type BlobData = {
blobId: string;
Expand Down Expand Up @@ -34,6 +31,22 @@ export interface Spec extends TurboModule {
setEncryptionEnabled(enabled: boolean): void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('MxFileSystem');
let cachedModule: Spec | undefined;
const getModule = (): Spec => {
if (!cachedModule) {
cachedModule = TurboModuleRegistry.getEnforcing<Spec>('MxFileSystem');
}
return cachedModule;
};

// Resolves the native module lazily (on first property access) instead of at import time,
// so requiring this file on web, where TurboModuleRegistry is stubbed out, never crashes.
const NativeMxFileSystem = new Proxy({} as Spec, {
get(_target, prop) {
return getModule()[prop as keyof Spec];
},
});

export default NativeMxFileSystem;

export type { BlobData, FsConstants };
17 changes: 16 additions & 1 deletion src/file-system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,19 @@ const cast = <T>(value: unknown): T | undefined => {
return value as T;
};

export const NativeFileSystem = initFs();
type Fs = ReturnType<typeof initFs>;
let cachedFs: Fs | undefined;
const getFs = (): Fs => {
if (!cachedFs) {
cachedFs = initFs();
}
return cachedFs;
};

// Deferred so importing this module never touches the native file system module
// (e.g. on web, where it doesn't exist) until a consumer actually reads a property.
export const NativeFileSystem = new Proxy({} as Fs, {
get(_target, prop) {
return getFs()[prop as keyof Fs];
},
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './encrypted-storage';
export * from './error';
export * from './file-system';
export * from './navigation-mode';
export * from './native-modules';
5 changes: 5 additions & 0 deletions src/native-modules/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TurboModuleRegistry } from 'react-native';

export const getNativeModule = <T extends object = Record<string, unknown>>(
name: string
): T | null => TurboModuleRegistry.get<T>(name) ?? null;
22 changes: 19 additions & 3 deletions src/navigation-mode/NativeMxNavigation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import { TurboModuleRegistry, type TurboModule } from 'react-native';
import type { CodegenTypes } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import type { TurboModule, CodegenTypes } from 'react-native';

export interface Spec extends TurboModule {
isNavigationBarActive(): boolean;
getNavigationBarHeight(): CodegenTypes.Double;
}

export default TurboModuleRegistry.getEnforcing<Spec>('MxNavigation');
let cachedModule: Spec | undefined;
const getModule = (): Spec => {
if (!cachedModule) {
cachedModule = TurboModuleRegistry.getEnforcing<Spec>('MxNavigation');
}
return cachedModule;
};

// Resolves the native module lazily (on first property access) instead of at import time,
// so requiring this file on web, where TurboModuleRegistry is stubbed out, never crashes.
const NativeMxNavigation = new Proxy({} as Spec, {
get(_target, prop) {
return getModule()[prop as keyof Spec];
},
});

export default NativeMxNavigation;
10 changes: 8 additions & 2 deletions src/navigation-mode/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import NativeMxNavigation from './NativeMxNavigation';

// Getters keep this lazy: reading .height/.isActive is what triggers the native call,
// not importing this module.
export const AndroidNavigationBar = {
height: NativeMxNavigation.getNavigationBarHeight(),
isActive: NativeMxNavigation.isNavigationBarActive(),
get height() {
return NativeMxNavigation.getNavigationBarHeight();
},
get isActive() {
return NativeMxNavigation.isNavigationBarActive();
},
};
Loading