adding type classes

and basic sanity check
This commit is contained in:
Yotam Mann 2019-08-30 09:28:45 -07:00
parent 544f184b21
commit 2ff888d3b3
2 changed files with 50 additions and 0 deletions

30
Tone/fromContext.test.ts Normal file
View file

@ -0,0 +1,30 @@
import { expect } from "chai";
import { OfflineContext } from "./core/context/OfflineContext";
import { fromContext } from "./fromContext";
describe("fromContext", () => {
let context: OfflineContext;
before(() => {
context = new OfflineContext(1, 1, 44100);
context.initialize();
});
after(() => {
context.dispose();
});
it ("creates an object from a context", () => {
const tone = fromContext(context);
const osc = new tone.Oscillator();
expect(osc.context).to.equal(context);
osc.dispose();
});
it ("units are relative to the passed in context's timing", () => {
const tone = fromContext(context);
expect(tone.Time("+0.5").valueOf()).to.equal(0.5);
});
});

View file

@ -2,6 +2,11 @@ import * as Classes from "./classes";
import { Transport } from "./core/clock/Transport";
import { Context } from "./core/context/Context";
import { Destination } from "./core/context/Destination";
import { FrequencyClass } from "./core/type/Frequency";
import { MidiClass } from "./core/type/Midi";
import { TicksClass } from "./core/type/Ticks";
import { TimeClass } from "./core/type/Time";
import { TransportTimeClass } from "./core/type/TransportTime";
import { isDefined, isFunction } from "./core/util/TypeCheck";
/**
@ -14,6 +19,14 @@ type Tone = {
now: () => number;
} & typeof Classes;
/**
* Bind the TimeBaseClass to the context
*/
// tslint:disable-next-line: typedef
function bindTypeClass(context: Context, type) {
return (...args: any[]) => new type(context, ...args);
}
/**
* Return an object with all of the classes bound to the passed in context
* @param context The context to bind all of the nodes to
@ -36,5 +49,12 @@ export function fromContext(context: Context): Tone {
toneFromContext.now = () => context.now();
toneFromContext.Transport = context.transport;
toneFromContext.Destination = context.destination;
// add the type classes
toneFromContext.Midi = bindTypeClass(context, MidiClass);
toneFromContext.Time = bindTypeClass(context, TimeClass);
toneFromContext.Frequency = bindTypeClass(context, FrequencyClass);
toneFromContext.Ticks = bindTypeClass(context, TicksClass);
toneFromContext.TransportTime = bindTypeClass(context, TransportTimeClass);
// return the object
return toneFromContext as Tone;
}