44 lines
1.6 KiB
TypeScript
44 lines
1.6 KiB
TypeScript
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<T extends SoundGroupNode<any>> = ReturnType<T['serializeOptions']>
|
|
export type SupportedDefinitions = SerializedNode<PlaylistGroupNode> | SerializedNode<SfxGroupNode> | SerializedNode<TabSourceNode>
|
|
|
|
|
|
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<any>[]) {
|
|
const definitions = nodes.map(node => node.serializeOptions());
|
|
localStorage.setItem('audioNodes', JSON.stringify(definitions));
|
|
}
|
|
|
|
} |