import { PlaylistGroupNode, PlaylistGroupNodeOptions } from "./PlaylistGroupNode"; import { SfxGroupNode, SfxGroupNodeOptions } from "./SfxGroupNode"; import { SoundGroupNode } from "./SoundGroupNode"; import { TabSourceNode, TabSourceNodeOptions } from "./TabSourceNode"; import { SoundboardContext } from "./types"; type SerializedNode> = ReturnType export type SupportedDefinitions = SerializedNode | SerializedNode | SerializedNode export class AudioNodeManager { deserializeNodes(definition: string, ctx: SoundboardContext) { const definitions = JSON.parse(definition) as SupportedDefinitions[]; return definitions.map((definition) => this.deserializeNode(definition, ctx)); } deserializeNode(definition: SupportedDefinitions, ctx: SoundboardContext) { if (definition.type === 'playlist') { return new PlaylistGroupNode(definition, ctx); } else if (definition.type === 'sfx') { return new SfxGroupNode(definition, ctx); } else if (definition.type === 'tabSource') { return new TabSourceNode(definition, ctx); } else { throw new Error('Invalid node type'); } } restore(ctx: SoundboardContext) { const nodes = localStorage.getItem('audioNodes'); if (nodes) { return this.deserializeNodes(nodes, ctx); } return []; } store(nodes: SoundGroupNode[]) { const definitions = nodes.map(node => node.serializeOptions()); localStorage.setItem('audioNodes', JSON.stringify(definitions)); } }