mirror of
https://github.com/denoland/deno.git
synced 2025-03-04 01:44:26 -05:00

This commit adds an ability to "ref" or "unref" pending ops. Up to this point Deno had a notion of "async ops" and "unref async ops"; the former keep event loop alive, while the latter do not block event loop from finishing. It was not possible to change between op types after dispatching, one had to decide which type to use before dispatch. Instead of storing ops in two separate "FuturesUnordered" collections, now ops are stored in a single collection, with supplemental "HashSet" storing ids of promises that were "unrefed". Two APIs were added to "Deno.core": "Deno.core.refOp(promiseId)" which allows to mark promise id to be "refed" and keep event loop alive (the default behavior) "Deno.core.unrefOp(promiseId)" which allows to mark promise id as "unrefed" which won't block event loop from exiting
119 lines
3.8 KiB
TypeScript
119 lines
3.8 KiB
TypeScript
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// deno-lint-ignore-file no-explicit-any
|
|
|
|
/// <reference no-default-lib="true" />
|
|
/// <reference lib="esnext" />
|
|
|
|
declare namespace Deno {
|
|
declare namespace core {
|
|
/** Call an op in Rust, and synchronously receive the result. */
|
|
function opSync(
|
|
opName: string,
|
|
a?: any,
|
|
b?: any,
|
|
): any;
|
|
|
|
/** Call an op in Rust, and asynchronously receive the result. */
|
|
function opAsync(
|
|
opName: string,
|
|
a?: any,
|
|
b?: any,
|
|
): Promise<any>;
|
|
|
|
/** Mark following promise as "ref", ie. event loop won't exit
|
|
* until all "ref" promises are resolved. All async ops are "ref" by default. */
|
|
function refOp(promiseId: number): void;
|
|
|
|
/** Mark following promise as "unref", ie. event loop will exit
|
|
* if there are only "unref" promises left. */
|
|
function unrefOps(promiseId: number): void;
|
|
|
|
/**
|
|
* Retrieve a list of all registered ops, in the form of a map that maps op
|
|
* name to internal numerical op id.
|
|
*/
|
|
function ops(): Record<string, number>;
|
|
|
|
/**
|
|
* Retrieve a list of all open resources, in the form of a map that maps
|
|
* resource id to the resource name.
|
|
*/
|
|
function resources(): Record<string, string>;
|
|
|
|
/**
|
|
* Close the resource with the specified op id. Throws `BadResource` error
|
|
* if resource doesn't exist in resource table.
|
|
*/
|
|
function close(rid: number): void;
|
|
|
|
/**
|
|
* Try close the resource with the specified op id; if resource with given
|
|
* id doesn't exist do nothing.
|
|
*/
|
|
function tryClose(rid: number): void;
|
|
|
|
/**
|
|
* Read from a (stream) resource that implements read()
|
|
*/
|
|
function read(rid: number, buf: Uint8Array): Promise<number>;
|
|
|
|
/**
|
|
* Write to a (stream) resource that implements write()
|
|
*/
|
|
function write(rid: number, buf: Uint8Array): Promise<number>;
|
|
|
|
/**
|
|
* Shutdown a resource
|
|
*/
|
|
function shutdown(rid: number): Promise<void>;
|
|
|
|
/** Get heap stats for current isolate/worker */
|
|
function heapStats(): Record<string, number>;
|
|
|
|
/** Encode a string to its Uint8Array representation. */
|
|
function encode(input: string): Uint8Array;
|
|
|
|
/**
|
|
* Set a callback that will be called when the WebAssembly streaming APIs
|
|
* (`WebAssembly.compileStreaming` and `WebAssembly.instantiateStreaming`)
|
|
* are called in order to feed the source's bytes to the wasm compiler.
|
|
* The callback is called with the source argument passed to the streaming
|
|
* APIs and an rid to use with the wasm streaming ops.
|
|
*
|
|
* The callback should eventually invoke the following ops:
|
|
* - `op_wasm_streaming_feed`. Feeds bytes from the wasm resource to the
|
|
* compiler. Takes the rid and a `Uint8Array`.
|
|
* - `op_wasm_streaming_abort`. Aborts the wasm compilation. Takes the rid
|
|
* and an exception. Invalidates the resource.
|
|
* - `op_wasm_streaming_set_url`. Sets a source URL for the wasm module.
|
|
* Takes the rid and a string.
|
|
* - To indicate the end of the resource, use `Deno.core.close()` with the
|
|
* rid.
|
|
*/
|
|
function setWasmStreamingCallback(
|
|
cb: (source: any, rid: number) => void,
|
|
): void;
|
|
|
|
/**
|
|
* Set a callback that will be called after resolving ops and before resolving
|
|
* macrotasks.
|
|
*/
|
|
function setNextTickCallback(
|
|
cb: () => void,
|
|
): void;
|
|
|
|
/** Check if there's a scheduled "next tick". */
|
|
function hasNextTickScheduled(): bool;
|
|
|
|
/** Set a value telling the runtime if there are "next ticks" scheduled */
|
|
function setHasNextTickScheduled(value: bool): void;
|
|
|
|
/**
|
|
* Set a callback that will be called after resolving ops and "next ticks".
|
|
*/
|
|
function setMacrotaskCallback(
|
|
cb: () => bool,
|
|
): void;
|
|
}
|
|
}
|