Skip to content

Commit

Permalink
Merge pull request crucialfelix#68 from crucialfelix/feature/fix-synt…
Browse files Browse the repository at this point in the history
…hdef-not-in-store

fix: synthdef not in store
  • Loading branch information
crucialfelix authored Nov 23, 2019
2 parents d97c847 + 9fb8911 commit 2a2c7fb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
49 changes: 26 additions & 23 deletions packages/lang/src/SynthDefCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class SynthDefCompiler {
this.store = new Map();
}

async boot() {
async boot(): Promise<SCLang> {
if (!this.lang) {
this.lang = await boot();
}
Expand All @@ -54,27 +54,34 @@ export default class SynthDefCompiler {
* Returns an object with each compiled synthdef
* as a SynthDefResultType.
*/
async compile(defs: object): Promise<SynthDefResultMapType> {
const defsList = _.toPairs(defs);
const compiledDefs = await Promise.all(_.map(defsList, ([defName, spec]) => this._compileOne(defName, spec)));
return _.fromPairs(_.map(compiledDefs, result => [result.name, result]));
async compile(defs: Record<string, SynthDefCompileRequest>): Promise<SynthDefResultMapType> {
const results: SynthDefResultMapType = {};

const compiling = _.map(defs, async (request, defName) => {
const result = await this._compileOne(defName, request);
results[defName] = result;
});
await Promise.all(compiling);

return results;
}

/**
* Compile SynthDefs and send them to the server.
*
* @returns a Promise for {defName: SynthDefResult, ...}
*/
async compileAndSend(defs: object, server: Server): Promise<SynthDefResultMapType> {
async compileAndSend(defs: Record<string, SynthDefCompileRequest>, server: Server): Promise<SynthDefResultMapType> {
// compile...
const compiledDefs = await this.compile(defs);

// send...
const commands = _.map(compiledDefs, ({ name }) => this.sendCommand(name));
const commands = _.map(compiledDefs, (compileResult, name) => this.sendCommand(name));
await Promise.all(commands.map(cmd => server.callAndResponse(cmd)));
return compiledDefs;
}

set(defName: string, data: SynthDefResultType) {
set(defName: string, data: SynthDefResultType): SynthDefResultType {
this.store.set(defName, data);
return data;
}
Expand All @@ -83,15 +90,15 @@ export default class SynthDefCompiler {
return this.store.get(defName);
}

allSendCommands() {
allSendCommands(): msg.CallAndResponse[] {
const commands: msg.CallAndResponse[] = [];
this.store.forEach((value, defName) => {
commands.push(this.sendCommand(defName));
});
return commands;
}

sendCommand(defName: string) {
sendCommand(defName: string): msg.CallAndResponse {
const data = this.get(defName);
if (!data) {
throw new Error(`SynthDef not in store: ${defName}`);
Expand All @@ -117,23 +124,19 @@ export default class SynthDefCompiler {

private async _compileOne(defName: string, spec: SynthDefCompileRequest): Promise<SynthDefResultType> {
// path or source
let result: SynthDefResultType;
if ("source" in spec) {
return this.compileSource(spec.source).then((result: SynthDefResultType) => {
this.set(defName, result);
return result;
});
result = await this.compileSource(spec.source);
} else if ("path" in spec) {
result = await this.compilePath(spec.path);
} else {
throw new Error(`Spec to SynthDefCompiler not recognized ${defName} ${JSON.stringify(spec)}`);
}

// if watch then add a watcher

if ("path" in spec) {
return this.compilePath(spec.path).then((result: SynthDefResultType) => {
this.set(result.name, result);
return result;
});
}
this.set(defName, result);
return result;

throw new Error(`Spec to SynthDefCompiler not recognized ${defName} ${JSON.stringify(spec)}`);
// TODO: if watch then add a watcher
}

/**
Expand Down
16 changes: 13 additions & 3 deletions packages/server-plus/src/ServerPlus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export default class ServerPlus extends Server {
): Promise<Synth> {
const [def, g] = await Promise.all([Promise.resolve(synthDef), Promise.resolve(group)]);
const nodeId = this.state.nextNodeID();
const sn = msg.synthNew(def.name, nodeId, addAction, g ? g.id : 0, args);
const sn = msg.synthNew(def.synthDefResult.name, nodeId, addAction, g ? g.id : 0, args);
this.send.msg(sn);
await whenNodeGo(this, String(nodeId), nodeId);
return new Synth(this, nodeId);
Expand Down Expand Up @@ -302,16 +302,26 @@ export default class ServerPlus extends Server {

private async _compileSynthDef(defName: string, sourceCode?: string, path?: string): Promise<SynthDef> {
await this.synthDefCompiler.boot();
let compileRequest: SynthDefCompileRequest;

if (sourceCode) {
compileRequest = { source: sourceCode };
} else if (path) {
compileRequest = { path: path };
} else {
throw new Error(`Neither sourceCode nor path supplied for compileSynthDef ${defName}`);
}

const defs = await this.synthDefCompiler.compileAndSend(
{
[defName]: sourceCode ? { source: sourceCode } : { path: path },
[defName]: compileRequest,
},
this,
);
// what if defName does not match synthDefResult.name ?
const synthDefResult = defs[defName];
if (!synthDefResult) {
throw new Error(`SynthDefResult not found ${defName} in compile return values`);
throw new Error(`SynthDefResult not found ${defName} in synthDefCompiler return values: ${defs}`);
}
return new SynthDef(this, defName, synthDefResult, sourceCode, path);
}
Expand Down

0 comments on commit 2a2c7fb

Please sign in to comment.