Skip to content

Commit

Permalink
chore(web-vis): extract wasm vector APIs into a wrapper with listener
Browse files Browse the repository at this point in the history
  • Loading branch information
arazabishov committed Nov 12, 2023
1 parent b75076a commit ae9abb9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 53 deletions.
8 changes: 6 additions & 2 deletions web-vis/www/src/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./styles.css";
import { WasmDecorator, VectorVis } from "./vector";
import { VectorVis, Vector } from "./vector";
import { WasmDecorator } from "./wasm";

class VectorComponent extends HTMLElement {
constructor(vectorVis) {
Expand Down Expand Up @@ -263,7 +264,10 @@ const addVectorToGrid = (vector, nextSibling) => {
};

const addVector = (button) => {
const vector = wasmDecorator.add(64);
const vecId = wasmDecorator.pushVec();
const vector = new Vector(vecId, wasmDecorator);
vector.setSize(64);

addVectorToGrid(vector, button);
};
addVectorButton.onClick = addVector;
Expand Down
52 changes: 1 addition & 51 deletions web-vis/www/src/vector.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,4 @@
import { RrbVec } from "./rrbvec.js";
import * as wasm from "web-vis";

export class WasmDecorator {
constructor(listener) {
this.listener = listener;
}

add(initialSize) {
const vecId = wasm.len();
wasm.push_vec();

const vector = new Vector(vecId, this);
if (initialSize !== undefined) {
vector.setSize(initialSize);
}

this.listener();

return vector;
}

setSize(id, size) {
wasm.set_vec_size(id, size);
this.listener();
}

splitOffVec(id, index) {
const other = wasm.split_off_vec(id, index);
this.listener();

return other;
}

getVecSize(id) {
return wasm.get_vec_size(id);
}

get(id) {
return wasm.get(id);
}

concatenatAll() {
wasm.concatenat_all();
this.listener();
}

len() {
return wasm.len();
}
}

export class Vector {
constructor(id, wasmDecorator) {
Expand All @@ -61,7 +11,7 @@ export class Vector {
}

setSize(size) {
this.wasmDecorator.setSize(this._id, size);
this.wasmDecorator.setVecSize(this._id, size);
}

splitAt(index) {
Expand Down
45 changes: 45 additions & 0 deletions web-vis/www/src/wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as wasm from "web-vis";

export class WasmDecorator {
constructor(listener) {
this.listener = listener;
}

pushVec() {
const vecId = wasm.len();

wasm.push_vec();
this.listener();

return vecId;
}

setVecSize(id, size) {
wasm.set_vec_size(id, size);
this.listener();
}

splitOffVec(id, index) {
const other = wasm.split_off_vec(id, index);
this.listener();

return other;
}

concatenatAll() {
wasm.concatenat_all();
this.listener();
}

getVecSize(id) {
return wasm.get_vec_size(id);
}

get(id) {
return wasm.get(id);
}

len() {
return wasm.len();
}
}

0 comments on commit ae9abb9

Please sign in to comment.