sounds, better files
This commit is contained in:
44
src/model/Audio/AudioNodesManager.ts
Normal file
44
src/model/Audio/AudioNodesManager.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user