1.0.0 • Published 10 months ago
@esfx/struct-type v1.0.0
@esfx/struct-type
Define structured types using ArrayBuffer
and SharedArrayBuffer
.
Overview
Installation
npm i @esfx/struct-type
Usage
import { StructType, int32 } from "@esfx/struct-type";
// simple types
const Point = StructType([
{ name: "x", type: int32 },
{ name: "y", type: int32 },
]);
// complex types
const Line = StructType([
{ name: "p1", type: Point },
{ name: "p2", type: Point },
]);
// inherited types
const Point3D = StructType(Point, [
{ name: "z", type: int32 }
]);
// create instances
const p1 = new Point({ x: 1, y: 2 }); // by field name
const p2 = new Point([3, 4]); // by field ordinal
// copy contents
const buffer = new ArrayBuffer(16);
const l = new Line([p1, p2]);
l.writeTo(buffer);
// create from a buffer
const l2 = new Line(buffer);
// read from field names
console.log(l1.p1.x); // 1
console.log(l1.p1.y); // 2
console.log(l1.p2.x); // 3
console.log(l1.p2.y); // 4
// read from field ordinals
console.log(l1[0][0]); // 1
console.log(l1[0][1]); // 2
console.log(l1[1][0]); // 3
console.log(l1[1][1]); // 4
Using with Workers
import { StructType, int32 } from "@esfx/struct-type";
import { Worker, isMainThread, parentPort, workerData } from "worker_threads";
// NOTE: For TypeScript, the `as const` is needed in the
// declaration below to infer the correct field names.
// For JavaScript, remove the `as const` and type annotations.
const ThreadData = StructType([
{ name: "itemsRemaining", type: int32 },
]);
if (isMainThread) {
// this is running on the main thread...
const data = new ThreadData(/*shared*/ true); // allocate struct using a SharedArrayBuffer
data.itemsRemaining = 5;
const worker = new Worker(__filename, { workerData: data.buffer });
worker.on("message", message => {
if (message === "done") {
console.log(data.itemsRemaining); // 0
}
});
}
else if (parentPort) {
// this is running in a background worker...
const data = new ThreadData(workerData); // allocate struct using the SharedArrayBuffer
while (data.itemsRemaining) {
// do some work...
data.itemsRemaining--;
}
parentPort.postMessage("done");
}
API
You can read more about the API here.
1.1.0-alpha.202307260322
10 months ago
1.1.0-alpha.202302091715
1 year ago
1.1.0-alpha.202306051228
11 months ago
1.1.0-alpha.202306061406
11 months ago
1.1.0-alpha.202306080253
11 months ago
1.1.0-alpha.202302071935
1 year ago
1.1.0-alpha.202306061316
11 months ago
1.1.0-alpha.202305082104
1 year ago
1.1.0-alpha.202211221509
1 year ago
1.1.0-alpha.202210252139
2 years ago
1.0.0
2 years ago
1.0.0-dev.7
2 years ago
1.0.0-pre.42
2 years ago
1.0.0-dev.8
2 years ago
1.0.0-pre.41
2 years ago
1.0.0-dev.5
2 years ago
1.0.0-pre.44
2 years ago
1.0.0-dev.6
2 years ago
1.0.0-pre.43
2 years ago
1.0.0-dev.4
2 years ago
1.0.0-prenative.0
2 years ago
1.0.0-prenative.1
2 years ago
1.0.0-dev.0
2 years ago
1.0.0-pre.40
2 years ago
1.0.0-pre.31
2 years ago
1.0.0-pre.33
2 years ago
1.0.0-pre.35
2 years ago
1.0.0-pre.34
2 years ago
1.0.0-pre.37
2 years ago
1.0.0-pre.36
2 years ago
1.0.0-pre.39
2 years ago
1.0.0-pre.38
2 years ago
1.1.0-alpha.202211291446
1 year ago
1.0.0-pre.25
3 years ago
1.0.0-pre.23
3 years ago
1.0.0-pre.19
3 years ago
1.0.0-pre.17
3 years ago
1.0.0-pre.16
3 years ago
1.0.0-pre.11
5 years ago
1.0.0-pre.9
5 years ago
1.0.0-pre.8
5 years ago