version-bridge: remove getGlobalSingleton

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-09-11 13:03:05 +02:00
parent 921569b500
commit 00fee0b11d
7 changed files with 16 additions and 71 deletions
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import React, { Context, useContext } from 'react';
import React from 'react';
import {
useApi,
createApiRef,
@@ -26,7 +26,7 @@ import { ApiProvider } from './ApiProvider';
import { ApiRegistry } from './ApiRegistry';
import { render } from '@testing-library/react';
import { withLogCollector } from '@backstage/test-utils-core';
import { getGlobalSingleton, VersionedValue } from '@backstage/version-bridge';
import { useVersionedContext } from '@backstage/version-bridge';
describe('ApiProvider', () => {
type Api = () => string;
@@ -184,13 +184,10 @@ describe('ApiProvider', () => {
});
describe('v1 consumer', () => {
const ApiContext =
getGlobalSingleton<Context<VersionedValue<{ 1: ApiHolder }>>>(
'api-context',
);
function useMockApiV1<T>(apiRef: ApiRef<T>): T {
const impl = useContext(ApiContext)?.atVersion(1)?.get(apiRef);
const impl = useVersionedContext<{ 1: ApiHolder }>('api-context')
?.atVersion(1)
?.get(apiRef);
if (!impl) {
throw new Error('no impl');
}
@@ -14,20 +14,16 @@
* limitations under the License.
*/
import React, { useContext, Context } from 'react';
import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { getGlobalSingleton, VersionedValue } from '@backstage/version-bridge';
import { useVersionedContext } from '@backstage/version-bridge';
import { AppContext as AppContextV1 } from './types';
import { AppContextProvider } from './AppContext';
describe('v1 consumer', () => {
const AppContext =
getGlobalSingleton<Context<VersionedValue<{ 1: AppContextV1 }>>>(
'app-context',
);
function useMockAppV1(): AppContextV1 {
const impl = useContext(AppContext)?.atVersion(1);
const impl =
useVersionedContext<{ 1: AppContextV1 }>('app-context')?.atVersion(1);
if (!impl) {
throw new Error('no impl');
}
@@ -14,16 +14,11 @@
* limitations under the License.
*/
import React, {
PropsWithChildren,
ReactElement,
useContext,
Context,
} from 'react';
import React, { PropsWithChildren, ReactElement } from 'react';
import { MemoryRouter, Routes } from 'react-router-dom';
import { render } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import { VersionedValue, getGlobalSingleton } from '@backstage/version-bridge';
import { useVersionedContext } from '@backstage/version-bridge';
import {
childDiscoverer,
routeElementDiscoverer,
@@ -330,16 +325,13 @@ describe('discovery', () => {
});
describe('v1 consumer', () => {
const RoutingContext =
getGlobalSingleton<Context<VersionedValue<{ 1: RouteResolver }>>>(
'routing-context',
);
function useMockRouteRefV1(
routeRef: AnyRouteRef,
location: string,
): RouteFunc<any> | undefined {
const resolver = useContext(RoutingContext)?.atVersion(1);
const resolver = useVersionedContext<{
1: RouteResolver;
}>('routing-context')?.atVersion(1);
if (!resolver) {
throw new Error('no impl');
}
-3
View File
@@ -25,9 +25,6 @@ export function createVersionedValueMap<
},
>(versions: Versions): VersionedValue<Versions>;
// @public
export function getGlobalSingleton<T>(id: string): T;
// @public
export function getOrCreateGlobalSingleton<T>(id: string, supplier: () => T): T;
@@ -14,33 +14,10 @@
* limitations under the License.
*/
import { getGlobalSingleton, getOrCreateGlobalSingleton } from './globalObject';
import { getOrCreateGlobalSingleton } from './globalObject';
const anyGlobal = global as any;
describe('getGlobalSingleton', () => {
beforeEach(() => {
delete anyGlobal['__@backstage/my-thing__'];
});
it('should return an existing value', () => {
const myThing = {};
const myOtherThing = {};
anyGlobal['__@backstage/my-thing__'] = myThing;
expect(getGlobalSingleton('my-thing')).toBe(myThing);
expect(getGlobalSingleton('my-thing')).toBe(myThing);
anyGlobal['__@backstage/my-thing__'] = myOtherThing;
expect(getGlobalSingleton('my-thing')).toBe(myOtherThing);
});
it('should throw if the value is not set', () => {
expect(() => getGlobalSingleton('my-thing')).toThrow(
'Global my-thing is not set',
);
});
});
describe('getOrCreateGlobalSingleton', () => {
beforeEach(() => {
delete anyGlobal['__@backstage/my-thing__'];
@@ -30,20 +30,6 @@ const globalObject = getGlobalObject();
const makeKey = (id: string) => `__@backstage/${id}__`;
/**
* Used to access a global singleton value, failing if it is not already set.
*
* @public
*/
export function getGlobalSingleton<T>(id: string): T {
const key = makeKey(id);
if (!(key in globalObject)) {
throw new Error(`Global ${id} is not set`);
}
return globalObject[key];
}
/**
* Serializes access to a global singleton value, with the first caller creating the value.
*
+1 -1
View File
@@ -14,7 +14,7 @@
* limitations under the License.
*/
export { getGlobalSingleton, getOrCreateGlobalSingleton } from './globalObject';
export { getOrCreateGlobalSingleton } from './globalObject';
export {
createVersionedContextForTesting,
useVersionedContext,