return from 'get' should be serializable

This commit is contained in:
Yotam Mann 2019-07-19 11:09:28 -04:00
parent 60736135f3
commit f8d845cede

View file

@ -6,7 +6,7 @@ import { TransportTimeClass } from "../type/TransportTime";
import "../type/Units"; import "../type/Units";
import { getDefaultsFromInstance, omitFromObject, optionsFromArguments } from "../util/Defaults"; import { getDefaultsFromInstance, omitFromObject, optionsFromArguments } from "../util/Defaults";
import { RecursivePartial } from "../util/Interface"; import { RecursivePartial } from "../util/Interface";
import { isDefined, isUndef } from "../util/TypeCheck"; import { isArray, isDefined, isNumber, isString, isUndef } from "../util/TypeCheck";
import { Context } from "./Context"; import { Context } from "./Context";
/** /**
@ -101,22 +101,10 @@ export abstract class ToneWithContext<Options extends ToneWithContextOptions> ex
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
/** /**
* Get the object's attributes. Given no arguments get * Get the object's attributes.
* will return all available object properties and their corresponding
* values. Pass in a single attribute to retrieve or an array
* of attributes. The attribute strings can also include a "."
* to access deeper properties.
* @param params the parameters to get, otherwise will return all available.
* @example * @example
* osc.get(); * osc.get();
* //returns {"type" : "sine", "frequency" : 440, ...etc} * //returns {"type" : "sine", "frequency" : 440, ...etc}
* @example
* osc.get("type");
* //returns { "type" : "sine"}
* @example
* //use dot notation to access deep properties
* synth.get(["envelope.attack", "envelope.release"]);
* //returns {"envelope" : {"attack" : 0.2, "release" : 0.4}}
*/ */
get(): Options { get(): Options {
const defaults = getDefaultsFromInstance(this) as Options; const defaults = getDefaultsFromInstance(this) as Options;
@ -126,16 +114,17 @@ export abstract class ToneWithContext<Options extends ToneWithContextOptions> ex
if (isDefined(member) && isDefined(member.value) && isDefined(member.setValueAtTime)) { if (isDefined(member) && isDefined(member.value) && isDefined(member.setValueAtTime)) {
defaults[attribute] = member.value; defaults[attribute] = member.value;
} else if (member instanceof ToneWithContext) { } else if (member instanceof ToneWithContext) {
const attributes = member.get(); defaults[attribute] = member.get();
// merge only the attributes that are in the defaults // otherwise make sure it's a serializable type
Object.keys(defaults[attribute]).forEach(key => { } else if (isArray(member) || isNumber(member) || isString(member)) {
defaults[attribute][key] = attributes[key];
});
} else {
defaults[attribute] = member; defaults[attribute] = member;
} else {
// remove all undefined and unserializable attributes
delete defaults[attribute];
} }
} }
}); });
return defaults; return defaults;
} }