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
39 changes: 32 additions & 7 deletions packages/react-native/Libraries/Settings/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,45 @@
* @format
*/

import Platform from '../Utilities/Platform';

let Settings: {
type SettingsStatic = {
get<T = unknown>(key: string): ?T,
set(settings: {[string]: unknown, ...}): void,
watchKeys(keys: string | Array<string>, callback: () => void): number,
clearWatch(watchId: number): void,
...
};

if (Platform.OS === 'ios') {
Settings = require('./Settings').default;
} else {
Settings = require('./SettingsFallback').default;
let SettingsImpl: ?SettingsStatic = null;

function getSettings(): SettingsStatic {
if (SettingsImpl != null) {
return SettingsImpl;
}
const Platform = require('../Utilities/Platform').default;
if (Platform.OS === 'ios') {
SettingsImpl = require('./Settings').default;
} else {
SettingsImpl = require('./SettingsFallback').default;
}
return SettingsImpl as SettingsStatic;
}

const Settings: SettingsStatic = {
get<T = unknown>(key: string): ?T {
return getSettings().get<T>(key);
},

set(settings: {[string]: unknown, ...}): void {
getSettings().set(settings);
},

watchKeys(keys: string | Array<string>, callback: () => void): number {
return getSettings().watchKeys(keys, callback);
},

clearWatch(watchId: number): void {
getSettings().clearWatch(watchId);
},
};

export default Settings;
101 changes: 101 additions & 0 deletions packages/react-native/Libraries/Settings/__tests__/Settings-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

describe('Settings', () => {
beforeEach(() => {
jest.resetModules();
});

it('should not throw due to circular dependency during Platform initialization', () => {
// Intercept NativePlatformConstantsIOS (which Platform.ios requires during
// load) to simulate another module requiring Settings during Platform's
// initialization phase.
jest.mock('../../Utilities/NativePlatformConstantsIOS', () => {
// Accessing Settings while Platform is loading
require('../Settings.js');
return {
getConstants() {
return {
interfaceIdiom: 'phone',
isTesting: true,
osVersion: '16.0',
systemName: 'iOS',
};
},
};
});

expect(() => {
require('../../Utilities/Platform');
}).not.toThrow();
});

it('defers accessing Platform until a method is first invoked', () => {
let platformAccessCount = 0;
jest.doMock('../../Utilities/Platform', () => {
platformAccessCount++;
return {
__esModule: true,
default: {OS: 'android'},
};
});

const Settings = require('../Settings.js').default;
expect(platformAccessCount).toBe(0);

const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
Settings.get('any');
expect(platformAccessCount).toBeGreaterThan(0);
warnSpy.mockRestore();
});

it('delegates get/set/watchKeys/clearWatch to the iOS implementation', () => {
const setValues = jest.fn();
jest.doMock('../../Utilities/Platform', () => ({
__esModule: true,
default: {OS: 'ios'},
}));
jest.doMock('../NativeSettingsManager', () => ({
__esModule: true,
default: {
getConstants: () => ({settings: {existing: 'initial'}}),
setValues,
},
}));

const Settings = require('../Settings.js').default;

expect(Settings.get('existing')).toBe('initial');
Settings.set({added: 'value'});
expect(Settings.get('added')).toBe('value');
expect(setValues).toHaveBeenCalledWith({added: 'value'});

const watchId = Settings.watchKeys('key', () => {});
expect(typeof watchId).toBe('number');
expect(() => Settings.clearWatch(watchId)).not.toThrow();
});

it('uses the fallback implementation on non-iOS platforms', () => {
jest.doMock('../../Utilities/Platform', () => ({
__esModule: true,
default: {OS: 'android'},
}));

const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
const Settings = require('../Settings.js').default;

expect(Settings.get('foo')).toBeNull();
expect(Settings.watchKeys('foo', () => {})).toBe(-1);
expect(warnSpy).toHaveBeenCalled();
warnSpy.mockRestore();
});
});
17 changes: 9 additions & 8 deletions packages/react-native/ReactNativeApi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<eda44383e13efcfb4f814115c4b44547>>
* @generated SignedSource<<28ea8d7daa304089bed1b3e135dde95a>>
*
* This file was generated by scripts/js-api/build-types/index.js.
*/
Expand Down Expand Up @@ -377,12 +377,7 @@ declare const setStyleAttributePreprocessor: <T>(
process: (nextProp: T) => unknown,
) => void
declare const Settings: typeof Settings_default
declare let Settings_default: {
clearWatch(watchId: number): void
get<T = unknown>(key: string): T | undefined
set(settings: { [$$Key$$: string]: unknown }): void
watchKeys(keys: Array<string> | string, callback: () => void): number
}
declare const Settings_default: SettingsStatic
declare const spring: typeof $$AnimatedImplementation.spring
declare const springImpl: (
value:
Expand Down Expand Up @@ -4507,6 +4502,12 @@ declare function setSurfaceProps(
displayMode?: number,
): void
declare type Settings = typeof Settings
declare type SettingsStatic = {
clearWatch(watchId: number): void
get<T = unknown>(key: string): T | undefined
set(settings: { [$$Key$$: string]: unknown }): void
watchKeys(keys: Array<string> | string, callback: () => void): number
}
declare function setWrapperComponentProvider(
provider: WrapperComponentProvider,
): void
Expand Down Expand Up @@ -6002,7 +6003,7 @@ export {
SectionListRenderItem, // 715b2086
SectionListRenderItemInfo, // 4a48a922
Separators, // 6a45f7e3
Settings, // 40b69683
Settings, // d6cf7906
Share, // e4591b32
ShareAction, // ead1004a
ShareActionSheetError, // 55e4f451
Expand Down
Loading