core-plugin-api: switch to using version-bridge

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-09-10 17:51:43 +02:00
parent d6eb28c91a
commit 8b0e02e0bb
12 changed files with 9 additions and 214 deletions
+1
View File
@@ -31,6 +31,7 @@
"dependencies": {
"@backstage/config": "^0.1.9",
"@backstage/theme": "^0.2.9",
"@backstage/version-bridge": "^0.1.0",
"@material-ui/core": "^4.12.2",
"@types/react": "*",
"history": "^5.0.0",
@@ -15,7 +15,7 @@
*/
import { renderHook } from '@testing-library/react-hooks';
import { createVersionedContextForTesting } from '../../lib/versionedValues';
import { createVersionedContextForTesting } from '@backstage/version-bridge';
import { createApiRef } from './ApiRef';
import { useApi } from './useApi';
@@ -16,7 +16,7 @@
import React, { PropsWithChildren } from 'react';
import { ApiRef, ApiHolder, TypesToApiRefs } from './types';
import { useVersionedContext } from '../../lib/versionedValues';
import { useVersionedContext } from '@backstage/version-bridge';
export function useApiHolder(): ApiHolder {
const versionedHolder = useVersionedContext<{ 1: ApiHolder }>('api-context');
@@ -15,7 +15,7 @@
*/
import { renderHook } from '@testing-library/react-hooks';
import { createVersionedContextForTesting } from '../lib/versionedValues';
import { createVersionedContextForTesting } from '@backstage/version-bridge';
import { useApp } from './useApp';
describe('v1 consumer', () => {
+1 -1
View File
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { useVersionedContext } from '../lib/versionedValues';
import { useVersionedContext } from '@backstage/version-bridge';
import { AppContext as AppContextV1 } from './types';
export const useApp = (): AppContextV1 => {
@@ -15,7 +15,7 @@
*/
import { ComponentType, ReactNode } from 'react';
import { getOrCreateGlobalSingleton } from '../lib/globalObject';
import { getOrCreateGlobalSingleton } from '@backstage/version-bridge';
type DataContainer = {
map: Map<string, unknown>;
@@ -1,67 +0,0 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getGlobalSingleton, 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__'];
});
it('should return an existing value', () => {
const myThing = {};
anyGlobal['__@backstage/my-thing__'] = myThing;
expect(getOrCreateGlobalSingleton('my-thing', () => ({}))).toBe(myThing);
expect(getOrCreateGlobalSingleton('my-thing', () => ({}))).toBe(myThing);
});
it('should should create a new value', () => {
const myNewThing = {};
expect(anyGlobal['__@backstage/my-thing__']).toBe(undefined);
expect(getOrCreateGlobalSingleton('my-thing', () => myNewThing)).toBe(
myNewThing,
);
expect(anyGlobal['__@backstage/my-thing__']).toBe(myNewThing);
expect(getOrCreateGlobalSingleton('my-thing', () => ({}))).toBe(myNewThing);
});
});
@@ -1,73 +0,0 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
function getGlobalObject() {
if (typeof window !== 'undefined' && window.Math === Math) {
return window;
}
if (typeof self !== 'undefined' && self.Math === Math) {
return self;
}
// eslint-disable-next-line no-new-func
return Function('return this')();
}
const globalObject = getGlobalObject();
const makeKey = (id: string) => `__@backstage/${id}__`;
/**
* Used to provide a global singleton value, failing if it is already set.
*/
export function setGlobalSingleton(id: string, value: unknown): void {
const key = makeKey(id);
if (key in globalObject) {
throw new Error(`Global ${id} is already set`);
}
globalObject[key] = value;
}
/**
* Used to access a global singleton value, failing if it is not already set.
*/
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.
*/
export function getOrCreateGlobalSingleton<T>(
id: string,
supplier: () => T,
): T {
const key = makeKey(id);
let value = globalObject[key];
if (value) {
return value;
}
value = supplier();
globalObject[key] = value;
return value;
}
@@ -1,66 +0,0 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createContext, useContext, Context } from 'react';
import { getGlobalSingleton, setGlobalSingleton } from './globalObject';
/**
* The versioned value interface is a container for a set of values that
* can be looked up by version. It is intended to be used as a container
* for values that can be versioned independently of package versions.
*/
export type VersionedValue<Versions extends { [version: number]: any }> = {
atVersion<Version extends keyof Versions>(
version: Version,
): Versions[Version] | undefined;
};
/**
* Creates a container for a map of versioned values that implements VersionedValue.
*/
export function createVersionedValueMap<
Versions extends { [version: number]: any },
>(versions: Versions): VersionedValue<Versions> {
Object.freeze(versions);
return {
atVersion(version) {
return versions[version];
},
};
}
export function useVersionedContext<
Versions extends { [version in number]: any },
>(key: string): VersionedValue<Versions> {
const versionedValue = useContext(
getGlobalSingleton<Context<VersionedValue<Versions>>>(key),
);
if (!versionedValue) {
throw new Error(`No provider available for ${key} context`);
}
return versionedValue;
}
export function createVersionedContextForTesting(key: string) {
return {
set(versions: { [version in number]: unknown }) {
setGlobalSingleton(key, createContext(createVersionedValueMap(versions)));
},
reset() {
delete (globalThis as any)[`__@backstage/${key}__`];
},
};
}
@@ -15,7 +15,7 @@
*/
import { OldIconComponent } from '../icons/types';
import { getOrCreateGlobalSingleton } from '../lib/globalObject';
import { getOrCreateGlobalSingleton } from '@backstage/version-bridge';
export type AnyParams = { [param in string]: string } | undefined;
export type ParamKeys<Params extends AnyParams> = keyof Params extends never
@@ -17,7 +17,7 @@
import { renderHook } from '@testing-library/react-hooks';
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { createVersionedContextForTesting } from '../lib/versionedValues';
import { createVersionedContextForTesting } from '@backstage/version-bridge';
import { useRouteRef } from './useRouteRef';
import { createRouteRef } from './RouteRef';
@@ -16,7 +16,7 @@
import { useMemo } from 'react';
import { matchRoutes, useLocation } from 'react-router-dom';
import { useVersionedContext } from '../lib/versionedValues';
import { useVersionedContext } from '@backstage/version-bridge';
import {
AnyParams,
ExternalRouteRef,