Skip to content

Commit

Permalink
core-api: get or create all bridged contexts
Browse files Browse the repository at this point in the history
Co-authored-by: Juan Lulkin <[email protected]>
Signed-off-by: Patrik Oldsberg <[email protected]>
  • Loading branch information
Rugvip and Juan Lulkin committed Mar 11, 2021
1 parent 0977b7b commit 99dad12
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
11 changes: 7 additions & 4 deletions packages/core-api/src/apis/system/ApiProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import React, {
import PropTypes from 'prop-types';
import { ApiRef, ApiHolder, TypesToApiRefs } from './types';
import { ApiAggregator } from './ApiAggregator';
import { getGlobalSingleton, setGlobalSingleton } from '../../lib/globalObject';
import {
getGlobalSingleton,
getOrCreateGlobalSingleton,
} from '../../lib/globalObject';
import {
VersionedValue,
createVersionedValueMap,
Expand All @@ -42,9 +45,9 @@ type ApiProviderProps = {
};

type ApiContextType = VersionedValue<{ 1: ApiHolder }> | undefined;
const ApiContext = createContext<ApiContextType>(undefined);

setGlobalSingleton('api-context', ApiContext);
const ApiContext = getOrCreateGlobalSingleton('api-context', () =>
createContext<ApiContextType>(undefined),
);

export const ApiProvider = ({
apis,
Expand Down
11 changes: 7 additions & 4 deletions packages/core-api/src/app/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ import {
VersionedValue,
createVersionedValueMap,
} from '../lib/versionedValues';
import { getGlobalSingleton, setGlobalSingleton } from '../lib/globalObject';
import {
getGlobalSingleton,
getOrCreateGlobalSingleton,
} from '../lib/globalObject';
import { AppContext as AppContextV1 } from './types';

type AppContextType = VersionedValue<{ 1: AppContextV1 }> | undefined;
const AppContext = createContext<AppContextType | undefined>(undefined);

setGlobalSingleton('app-context', AppContext);
const AppContext = getOrCreateGlobalSingleton('app-context', () =>
createContext<AppContextType | undefined>(undefined),
);

type Props = {
appContext: AppContextV1;
Expand Down
11 changes: 7 additions & 4 deletions packages/core-api/src/routing/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ import {
VersionedValue,
createVersionedValueMap,
} from '../lib/versionedValues';
import { setGlobalSingleton, getGlobalSingleton } from '../lib/globalObject';
import {
getGlobalSingleton,
getOrCreateGlobalSingleton,
} from '../lib/globalObject';

type RoutingContextType = VersionedValue<{ 1: RouteResolver }> | undefined;
const RoutingContext = createContext<RoutingContextType>(undefined);

setGlobalSingleton('routing-context', RoutingContext);
const RoutingContext = getOrCreateGlobalSingleton('routing-context', () =>
createContext<RoutingContextType>(undefined),
);

export function useRouteRef<Optional extends boolean, Params extends AnyParams>(
routeRef: ExternalRouteRef<Params, Optional>,
Expand Down

0 comments on commit 99dad12

Please sign in to comment.