/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at . */
// @flow
import {
getResourcePair,
getResourceValues,
type ResourceState,
type Id,
type ResourceBound,
type ResourceIdentity
} from "./core";
export type ResourceMap = (
R,
ResourceIdentity
) => Mapped;
export function hasResource(
state: ResourceState,
id: Id
): boolean %checks {
return !!getResourcePair(state, id);
}
export function getResourceIds(
state: ResourceState
): Array> {
return Object.keys(getResourceValues(state));
}
export function getResource(
state: ResourceState,
id: Id
): R {
const pair = getResourcePair(state, id);
if (!pair) {
throw new Error(`Resource ${id} does not exist`);
}
return pair.value;
}
export function getMappedResource(
state: ResourceState,
id: Id,
map: ResourceMap
): Mapped {
const pair = getResourcePair(state, id);
if (!pair) {
throw new Error(`Resource ${id} does not exist`);
}
return map(pair.value, pair.identity);
}