diff options
Diffstat (limited to 'devtools/client/debugger/src/utils/async-value.js')
-rw-r--r-- | devtools/client/debugger/src/utils/async-value.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/devtools/client/debugger/src/utils/async-value.js b/devtools/client/debugger/src/utils/async-value.js new file mode 100644 index 0000000000..f90b16c5ca --- /dev/null +++ b/devtools/client/debugger/src/utils/async-value.js @@ -0,0 +1,45 @@ +/* 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 <http://mozilla.org/MPL/2.0/>. */ + +// @flow + +export type FulfilledValue<+T> = {| + state: "fulfilled", + +value: T, +|}; +export type RejectedValue = {| + state: "rejected", + value: mixed, +|}; +export type PendingValue = {| + state: "pending", +|}; +export type SettledValue<+T> = FulfilledValue<T> | RejectedValue; +export type AsyncValue<+T> = SettledValue<T> | PendingValue; + +export function pending(): PendingValue { + return { state: "pending" }; +} +export function fulfilled<+T>(value: T): FulfilledValue<T> { + return { state: "fulfilled", value }; +} +export function rejected(value: mixed): RejectedValue { + return { state: "rejected", value }; +} + +export function asSettled<T>( + value: AsyncValue<T> | null +): SettledValue<T> | null { + return value && value.state !== "pending" ? value : null; +} + +export function isPending(value: AsyncValue<mixed>): boolean %checks { + return value.state === "pending"; +} +export function isFulfilled(value: AsyncValue<mixed>): boolean %checks { + return value.state === "fulfilled"; +} +export function isRejected(value: AsyncValue<mixed>): boolean %checks { + return value.state === "rejected"; +} |