Skip to main content
Module

x/capi/mod.ts>$.EncodeBuffer

[WIP] A framework for crafting interactions with Substrate chains
Latest
class $.EncodeBuffer
Re-export
import { $ } from "https://deno.land/x/capi@v0.1.1-beta.1/mod.ts";
const { EncodeBuffer } = $;

Constructors

new
EncodeBuffer(init: number | Uint8Array, context?)

Creates a new EncodeBuffer with a specified initial size/buffer

Properties

array: Uint8Array
asyncCount: number
asyncPromise
asyncResolve: (value: void | Promise<void>) => void
finishedArrays: (Uint8Array | EncodeBuffer)[]
finishedSize: number
index: number
queuedArrays: Uint8Array[]
view: DataView

Methods

Pushes the data written in array to finishedArrays. Leaves the buffer in an invalid state -- array and index must be updated.

_finishInto(fullArray: Uint8Array, index: number): number

Copies all data from finishedArrays into fullArray

_setArray(array: Uint8Array)

Sets array and updates view

createCursor(length: number): EncodeBuffer & { close(): void; }

Creates a sub-buffer that can be written into later to insert data into the middle of the array. .close() must be called after the cursor is done being written into. The cursor should not be used after .close() is called. If the cursor will be written into asynchronously, the buffer must be held open with .waitFor().

finish(): Uint8Array

Finishes the current array, and returns a Uint8Array containing everything written. The EncodeBuffer is left in an undefined state, and should not be used afterwards. Throws if asynchronous writes are still pending.

finishAsync(): Promise<Uint8Array>

Finishes the current array, and returns a Uint8Array containing everything written. The EncodeBuffer is left in an undefined state, and should not be used afterwards.

insertArray(buffer: Uint8Array)

Inserts a Uint8Array at the current position in the buffer. This does not consume any of the pre-allocated space.

Finishes the current array and resumes writing on the previous array. Must be called after .pushAlloc().

pushAlloc(size: number)

Allocates more space in the EncodeBuffer. .popAlloc() must be called after this space is used.

stealAlloc(length: number): Uint8Array

Consumes length allocated bytes without writing anything, and returns the skipped subarray. Anything written into the returned array will not affect the buffer, except if it is later reincorporated e.g. via .insertArray(). Rather niche.

waitFor(fn: () => Promise<void>)

Immediately invokes the callback, and holds the buffer open until the returned promise resolves.

waitForBuffer(buffer: EncodeBuffer, fn: () => void)

Invokes the callback once buffer's async tasks finish, and holds this buffer open until the callback returns.

writeAsync(length: number, fn: (buffer: EncodeBuffer) => Promise<void>)

Creates a sub-buffer that can be written into asynchronously. The buffer passed to the callback should not be used after the returned promise resolves.