Tone.js/Tone/fromContext.ts

78 lines
2.6 KiB
TypeScript
Raw Normal View History

import * as Classes from "./classes";
import { TransportClass } from "./core/clock/Transport";
import { Context } from "./core/context/Context";
import { ListenerClass } from "./core/context/Listener";
import { DestinationClass } 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";
2019-10-30 18:49:02 +00:00
import { omitFromObject } from "./core/util/Defaults";
import { DrawClass } from "./core/util/Draw";
2019-10-30 18:49:02 +00:00
type ClassesWithoutSingletons = Omit<typeof Classes, "Transport" | "Destination" | "Draw">;
/**
* The exported Tone object. Contains all of the classes that default
* to the same context and contains a singleton Transport and Destination node.
*/
type ToneObject = {
Transport: TransportClass;
Destination: DestinationClass;
Listener: ListenerClass;
Draw: DrawClass;
2019-10-30 18:49:02 +00:00
context: Context;
now: () => number;
2019-10-30 18:49:02 +00:00
immediate: () => number;
} & ClassesWithoutSingletons;
/**
* Bind the TimeBaseClass to the context
*/
function bindTypeClass(context: Context, type) {
return (...args: unknown[]) => 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
*/
export function fromContext(context: Context): ToneObject {
2019-10-30 18:49:02 +00:00
const classesWithContext: Partial<ClassesWithoutSingletons> = {};
Object.keys(omitFromObject(Classes, ["Transport", "Destination", "Draw"])).map(key => {
const cls = Classes[key];
if (isDefined(cls) && isFunction(cls.getDefaults)) {
2019-10-30 18:49:02 +00:00
classesWithContext[key] = class ToneFromContextNode extends cls {
get defaultContext(): Context {
return context;
}
};
} else {
// otherwise just copy it over
2019-10-30 18:49:02 +00:00
classesWithContext[key] = Classes[key];
}
});
2019-10-30 18:49:02 +00:00
const toneFromContext: ToneObject = {
2019-10-30 18:49:02 +00:00
...(classesWithContext as ClassesWithoutSingletons),
now: context.now.bind(context),
immediate: context.immediate.bind(context),
2019-10-30 18:49:02 +00:00
Transport: context.transport,
Destination: context.destination,
Listener: context.listener,
2019-10-30 18:49:02 +00:00
Draw: context.draw,
context,
// the type functions
Midi: bindTypeClass(context, MidiClass),
Time: bindTypeClass(context, TimeClass),
Frequency: bindTypeClass(context, FrequencyClass),
Ticks: bindTypeClass(context, TicksClass),
TransportTime: bindTypeClass(context, TransportTimeClass),
};
// return the object
2019-10-30 18:49:02 +00:00
return toneFromContext;
}