Skip to content

Commit

Permalink
Foundation work for subgraph support
Browse files Browse the repository at this point in the history
 * Switch stuff in the wasm engine to reference VCs by ID rather than index
 * Add some state for storing subgraph data
   * Each VC and foreign connectable gets tagged with a subgraph ID
   * Handle backwards compat for de/serialization
  • Loading branch information
Ameobea committed Feb 6, 2024
1 parent a661870 commit 15ddce3
Show file tree
Hide file tree
Showing 24 changed files with 384 additions and 147 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/engine.js
src/engine_bg.d.ts
*webpack*
src/vocalSynthesis
5 changes: 5 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ debug-engine:
cd .. && wasm-bindgen ./target/wasm32-unknown-unknown/debug/engine.wasm --browser --remove-producers-section --out-dir ./build && \
cp ./build/* ../src

build-engine:
cd ./engine/engine && cargo build --release --target wasm32-unknown-unknown && \
cd .. && wasm-bindgen ./target/wasm32-unknown-unknown/release/engine.wasm --browser --remove-producers-section --out-dir ./build && \
cp ./build/* ../src

build-wavetable:
cd ./engine/wavetable && cargo build --release --target wasm32-unknown-unknown && \
cp ../target/wasm32-unknown-unknown/release/wavetable.wasm ../../public
Expand Down
49 changes: 48 additions & 1 deletion engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions engine/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ crate-type = ["cdylib", "rlib"]
wasm-bindgen = { version = "=0.2.82" }
rand = "0.7"
rand_pcg = "0.2.1"
miniserde = "0.1.34"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
# Disable logging staticly in release, making all log calls into no-ops
log = { version = "0.4", features = ["release_max_level_warn"] }
uuid = { version = "1.2" }
uuid = { version = "1.2", features = ["serde"] }
fxhash = "0.2"

common = { path = "../common" }
wbg_logging = { path = "../wbg_logging" }
6 changes: 4 additions & 2 deletions engine/engine/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen(raw_module = "./vcInterop")]
extern "C" {
pub fn init_view_contexts(
active_context_ix: usize,
active_context_id: &str,
view_context_definitions: &str,
connections_json: &str,
foreign_connectables_json: &str,
active_subgraph_id: &str,
subgraphs_by_id_json: &str,
);
pub fn add_view_context(id: &str, name: &str);
pub fn delete_view_context(id: &str);
pub fn set_active_vc_ix(new_ix: usize);
pub fn set_active_vc_id(new_id: &str);
pub fn list_foreign_node_used_samples(id: &str) -> Vec<JsValue>;
/// Returns the ID of the active view context to display after initializing
pub fn initialize_default_vcm_state();
Expand Down
23 changes: 15 additions & 8 deletions engine/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ extern crate log;

use std::{ptr, str::FromStr};

use miniserde::json;
use prelude::js::js_random;
use uuid::Uuid;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -57,7 +56,8 @@ pub fn init() {
unsafe { VIEW_CONTEXT_MANAGER = Box::into_raw(vcm) };
}

/// Creates a new view context from the provided name and sets it as the main view context.
/// Creates a new view context from the provided name in the active subgraph and sets it as the main
/// view context for that subgraph.
#[wasm_bindgen]
pub fn create_view_context(vc_name: String, display_name: String) {
let uuid = uuid_v4();
Expand All @@ -66,7 +66,7 @@ pub fn create_view_context(vc_name: String, display_name: String) {
view_context.init();
view_context.hide();
let vcm = get_vcm();
vcm.add_view_context(uuid, vc_name, view_context);
vcm.add_view_context(uuid, vc_name, view_context, vcm.active_subgraph_id);
set_vc_title(uuid.to_string(), display_name)
}

Expand All @@ -90,17 +90,24 @@ pub fn delete_vc_by_id(id: &str) {
pub fn switch_view_context(uuid_str: &str) {
let uuid =
Uuid::from_str(uuid_str).expect("Invalid UUID string passed to `switch_view_context`!");
get_vcm().set_active_view_by_id(uuid);
get_vcm().set_active_view(uuid);
}

#[wasm_bindgen]
pub fn add_subgraph() {
let vcm = get_vcm();
let new_subgraph_id = vcm.add_subgraph();
vcm.set_active_subgraph(new_subgraph_id);
}

#[wasm_bindgen]
pub fn reset_vcm() {
info!("Resetting VCM...");
get_vcm().reset();
info!(
"Finished reset; current context count: {}, active_ix: {}",
"Finished reset; current context count: {}, active_id: {}",
get_vcm().contexts.len(),
get_vcm().active_context_ix
get_vcm().active_context_id
);
}

Expand Down Expand Up @@ -133,7 +140,7 @@ pub fn get_vc_connectables(vc_id: &str) -> JsValue {
#[wasm_bindgen]
pub fn set_connections(connections_json: &str) {
let connections: Vec<(ConnectionDescriptor, ConnectionDescriptor)> =
match json::from_str(connections_json) {
match serde_json::from_str(connections_json) {
Ok(conns) => conns,
Err(err) => {
error!("Failed to deserialize provided connections JSON: {:?}", err);
Expand All @@ -147,7 +154,7 @@ pub fn set_connections(connections_json: &str) {
#[wasm_bindgen]
pub fn set_foreign_connectables(foreign_connectables_json: &str) {
let foreign_connectables: Vec<ForeignConnectable> =
match json::from_str(foreign_connectables_json) {
match serde_json::from_str(foreign_connectables_json) {
Ok(conns) => conns,
Err(err) => {
error!(
Expand Down
Loading

0 comments on commit 15ddce3

Please sign in to comment.