UNPKG

1.96 kBJavaScriptView Raw
1import * as Classes from "./classes.js";
2import { FrequencyClass } from "./core/type/Frequency.js";
3import { MidiClass } from "./core/type/Midi.js";
4import { TicksClass } from "./core/type/Ticks.js";
5import { TimeClass } from "./core/type/Time.js";
6import { TransportTimeClass } from "./core/type/TransportTime.js";
7import { isDefined, isFunction } from "./core/util/TypeCheck.js";
8import { omitFromObject } from "./core/util/Defaults.js";
9/**
10 * Bind the TimeBaseClass to the context
11 */
12function bindTypeClass(context, type) {
13 return (...args) => new type(context, ...args);
14}
15/**
16 * Return an object with all of the classes bound to the passed in context
17 * @param context The context to bind all of the nodes to
18 */
19export function fromContext(context) {
20 const classesWithContext = {};
21 Object.keys(omitFromObject(Classes, ["Transport", "Destination", "Draw"])).map((key) => {
22 const cls = Classes[key];
23 if (isDefined(cls) && isFunction(cls.getDefaults)) {
24 classesWithContext[key] = class ToneFromContextNode extends cls {
25 get defaultContext() {
26 return context;
27 }
28 };
29 }
30 else {
31 // otherwise just copy it over
32 classesWithContext[key] = Classes[key];
33 }
34 });
35 const toneFromContext = Object.assign(Object.assign({}, classesWithContext), { now: context.now.bind(context), immediate: context.immediate.bind(context), Transport: context.transport, Destination: context.destination, Listener: context.listener, Draw: context.draw, context,
36 // the type functions
37 Midi: bindTypeClass(context, MidiClass), Time: bindTypeClass(context, TimeClass), Frequency: bindTypeClass(context, FrequencyClass), Ticks: bindTypeClass(context, TicksClass), TransportTime: bindTypeClass(context, TransportTimeClass) });
38 // return the object
39 return toneFromContext;
40}
41//# sourceMappingURL=fromContext.js.map
\No newline at end of file