2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-08-17 16:34:30 -04:00
|
|
|
let logDebug = false;
|
2019-02-03 10:27:53 +11:00
|
|
|
let logSource = "JS";
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-09-04 12:23:38 -07:00
|
|
|
// @internal
|
2019-02-03 10:27:53 +11:00
|
|
|
export function setLogDebug(debug: boolean, source?: string): void {
|
2018-08-17 16:34:30 -04:00
|
|
|
logDebug = debug;
|
2019-02-03 10:27:53 +11:00
|
|
|
if (source) {
|
|
|
|
logSource = source;
|
|
|
|
}
|
2018-08-17 16:34:30 -04:00
|
|
|
}
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2018-10-15 07:29:50 +11:00
|
|
|
/** Debug logging for deno.
|
2018-09-14 16:48:12 +08:00
|
|
|
* Enable with the `--log-debug` or `-D` command line flag.
|
2018-09-04 12:23:38 -07:00
|
|
|
* @internal
|
|
|
|
*/
|
2019-03-10 04:30:38 +11:00
|
|
|
export function log(...args: unknown[]): void {
|
2018-08-17 16:34:30 -04:00
|
|
|
if (logDebug) {
|
2020-01-21 01:30:30 +11:00
|
|
|
// if we destructure `console` off `globalThis` too early, we don't bind to
|
2019-09-16 01:04:05 +10:00
|
|
|
// the right console, therefore we don't log anything out.
|
2020-01-21 01:30:30 +11:00
|
|
|
globalThis.console.log(`DEBUG ${logSource} -`, ...args);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 12:23:38 -07:00
|
|
|
// @internal
|
2019-11-14 05:42:34 +11:00
|
|
|
export function assert(cond: unknown, msg = "assert"): asserts cond {
|
2018-07-06 11:20:35 -04:00
|
|
|
if (!cond) {
|
2018-07-16 23:25:50 -04:00
|
|
|
throw Error(msg);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 07:29:50 +11:00
|
|
|
/** A `Resolvable` is a Promise with the `reject` and `resolve` functions
|
2018-09-04 12:23:38 -07:00
|
|
|
* placed as methods on the promise object itself. It allows you to do:
|
|
|
|
*
|
2018-10-15 07:29:50 +11:00
|
|
|
* const p = createResolvable<number>();
|
|
|
|
* // ...
|
|
|
|
* p.resolve(42);
|
2018-09-04 12:23:38 -07:00
|
|
|
*
|
2018-10-15 07:29:50 +11:00
|
|
|
* It'd be prettier to make `Resolvable` a class that inherits from `Promise`,
|
2018-09-04 12:23:38 -07:00
|
|
|
* rather than an interface. This is possible in ES2016, however typescript
|
|
|
|
* produces broken code when targeting ES5 code.
|
|
|
|
*
|
2018-10-15 07:29:50 +11:00
|
|
|
* At the time of writing, the GitHub issue is closed in favour of a proposed
|
|
|
|
* solution that is awaiting feedback.
|
|
|
|
*
|
|
|
|
* @see https://github.com/Microsoft/TypeScript/issues/15202
|
|
|
|
* @see https://github.com/Microsoft/TypeScript/issues/15397
|
2018-09-04 12:23:38 -07:00
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
|
2019-12-24 18:59:46 -08:00
|
|
|
export type ResolveFunction<T> = (value?: T | PromiseLike<T>) => void;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
export type RejectFunction = (reason?: any) => void;
|
|
|
|
|
2018-08-15 09:40:30 -07:00
|
|
|
export interface ResolvableMethods<T> {
|
2019-12-24 18:59:46 -08:00
|
|
|
resolve: ResolveFunction<T>;
|
|
|
|
reject: RejectFunction;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
2018-08-15 09:40:30 -07:00
|
|
|
|
2018-09-04 12:23:38 -07:00
|
|
|
// @internal
|
2018-08-15 20:57:36 -04:00
|
|
|
export type Resolvable<T> = Promise<T> & ResolvableMethods<T>;
|
2018-08-15 09:40:30 -07:00
|
|
|
|
2018-09-04 12:23:38 -07:00
|
|
|
// @internal
|
2018-07-06 11:20:35 -04:00
|
|
|
export function createResolvable<T>(): Resolvable<T> {
|
2019-12-24 18:59:46 -08:00
|
|
|
let resolve: ResolveFunction<T>;
|
|
|
|
let reject: RejectFunction;
|
|
|
|
const promise = new Promise<T>((res, rej): void => {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
|
|
|
}) as Resolvable<T>;
|
|
|
|
promise.resolve = resolve!;
|
|
|
|
promise.reject = reject!;
|
|
|
|
return promise;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
2018-08-19 21:04:27 +02:00
|
|
|
|
2018-09-04 12:23:38 -07:00
|
|
|
// @internal
|
2018-08-19 21:04:27 +02:00
|
|
|
export function notImplemented(): never {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
}
|
|
|
|
|
2019-04-19 17:39:54 -07:00
|
|
|
// @internal
|
|
|
|
export function immutableDefine(
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
o: any,
|
|
|
|
p: string | number | symbol,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
value: any
|
|
|
|
): void {
|
|
|
|
Object.defineProperty(o, p, {
|
|
|
|
value,
|
|
|
|
configurable: false,
|
|
|
|
writable: false
|
|
|
|
});
|
|
|
|
}
|