Files
react-native/Libraries/Lists/VirtualizedListInjection.js
T
Tim Yung 494c47360f RN: Sort Imports via ESLint
Summary:
Applies the autofix from the newly introduced `lint/sort-imports` ESLint rule.

Changelog:
[Internal]

Reviewed By: cortinico, skinsshark

Differential Revision: D39907798

fbshipit-source-id: 17f5f11b08a5b4bb66286816b78eb26e07e829b8
2022-09-30 14:28:48 -07:00

74 lines
2.2 KiB
JavaScript

/**
* 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';
import type VirtualizedList from './VirtualizedList';
import type {Props as VirtualizedListProps} from './VirtualizedListProps';
import invariant from 'invariant';
import * as React from 'react';
export type ListImplementation = React.ComponentType<VirtualizedListProps> &
interface {};
/**
* Global override to the VirtualizedList implementation used when imported
*/
let injection: ?ListImplementation;
let retrieved = false;
export function inject(listImplementation: ListImplementation): void {
invariant(
!retrieved,
'VirtualizedListInjection.inject() called after the injection was already retrieved',
);
injection = listImplementation;
}
export function getOrDefault(
defaultImplementation: Class<VirtualizedList>,
): Class<VirtualizedList> {
retrieved = true;
return injection
? verifyVirtualizedList(injection, defaultImplementation)
: defaultImplementation;
}
function verifyVirtualizedList(
injectedImplementation: ListImplementation,
defaultImplementation: Class<VirtualizedList>,
): Class<VirtualizedList> {
// The original VirtualizedList marks method as "private by convention" by
// prefixing with underscore. These methods may still be called at runtime
// by tests or other internals, so they cannot be truly private, and will be
// included in the Flow type of VirtualizedList.
// Allow the injection to have different private methods by allowing a loose
// Flow type, but check at runtime that the set of public properties matches.
if (__DEV__) {
for (const field of Object.keys(defaultImplementation)) {
if (isPublicField(field)) {
invariant(
injectedImplementation.hasOwnProperty(field),
`VirtualizedList injection missing field: "${field}"`,
);
}
}
}
// $FlowExpectedError
return injectedImplementation;
}
function isPublicField(fieldName: string): boolean {
// Respect JSTransform public methods by double underscore (D33982339)
return fieldName.length > 0 && (fieldName[0] !== '_' || fieldName[1] === '_');
}