Skip to content

Commit

Permalink
fix: refactor run into safeAwait
Browse files Browse the repository at this point in the history
  • Loading branch information
hmerritt committed Sep 9, 2024
1 parent 4fb7f65 commit 35dfd93
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion scripts/bootstrap/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as core from "./core";
/**
* Internal adrift version.
*/
export const adriftVersion = "0.11.528";
export const adriftVersion = "0.11.529";

/**
* Bumps the adrift `patch` version number using the total commit count.
Expand Down
6 changes: 3 additions & 3 deletions src/lib/global/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { EnvKeys, EnvObj } from "./env";
import type { FeatureFlags, FeatureOptions } from "./featureFlags";
import type { LogStoreType } from "./log";
import type { RunFn } from "./utils";
import type { SafeAwaitFn } from "./utils";

type LogFn = (logLevel: any, ...args: any[]) => void;
type LognFn = (namespace: string, logLevel: any, ...args: any[]) => void;
Expand All @@ -14,7 +14,7 @@ declare global {
var debugn: LognFn;
var logStore: LogStoreType;
var env: EnvObj;
var run: RunFn;
var safeAwait: SafeAwaitFn;
var envGet: (key: EnvKeys) => any;
var feature: FeatureFn;
var getNumberOfEventListeners: () => number;
Expand All @@ -27,7 +27,7 @@ declare global {
debugn: LognFn;
logStore: LogStoreType;
env: EnvObj;
run: RunFn;
safeAwait: SafeAwaitFn;
envGet: (key: EnvKeys) => any;
feature: FeatureFn;
getNumberOfEventListeners: () => number;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/global/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { injectDevTools } from "./devTools";
import { injectEnv } from "./env";
import { injectFeature } from "./featureFlags";
import { injectLog } from "./log";
import { injectRun } from "./utils";
import { injectSafeAwait } from "./utils";
import { versionString } from "./version";

export const globalInit = () => {
// Inject global functions.
injectRun();
injectSafeAwait();
injectEnv();
injectLog();
injectFeature();
Expand Down
16 changes: 8 additions & 8 deletions src/lib/global/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ export const parseEnv = (value: any, isJson = false) => {
*
* @example const [error, result] = await run(myPromise())
*/
export const run = async <T, E = Error>(
promise: Promise<T>
): Promise<[null, T] | [E, null]> => {
export const safeAwait = async <T, E = Error>(
promise: Promise<T> | T
): Promise<[T, null] | [null, E]> => {
try {
const result = await promise;
return [null, result];
return [result, null];
} catch (error) {
return [error as E, null];
return [null, error as E];
}
};

export type RunFn = typeof run;
export type SafeAwaitFn = typeof safeAwait;

export const injectRun = () => {
setGlobalValue("run", run);
export const injectSafeAwait = () => {
setGlobalValue("safeAwait", safeAwait);
};

0 comments on commit 35dfd93

Please sign in to comment.