File size: 1,880 Bytes
05b45a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
export class PlayerState {
    constructor() {
        this.state = {
            isPlaying: false,
            isGenerating: false,
            currentTime: 0,
            duration: 0,
            volume: 1,
            speed: 1,
            progress: 0,
            error: null
        };
        this.listeners = new Set();
    }

    subscribe(listener) {
        this.listeners.add(listener);
        return () => this.listeners.delete(listener);
    }

    notify() {
        this.listeners.forEach(listener => listener(this.state));
    }

    setState(updates) {
        this.state = {
            ...this.state,
            ...updates
        };
        this.notify();
    }

    setPlaying(isPlaying) {
        this.setState({ isPlaying });
    }

    setGenerating(isGenerating) {
        this.setState({ isGenerating });
    }

    setProgress(loaded, total) {
        const progress = total > 0 ? (loaded / total) * 100 : 0;
        this.setState({ progress });
    }

    setTime(currentTime, duration) {
        this.setState({ currentTime, duration });
    }

    setVolume(volume) {
        this.setState({ volume });
    }

    setSpeed(speed) {
        this.setState({ speed });
    }

    setError(error) {
        this.setState({ error });
    }

    clearError() {
        this.setState({ error: null });
    }

    reset() {
        // Keep current speed setting but reset everything else
        const currentSpeed = this.state.speed;
        const currentVolume = this.state.volume;
        
        this.setState({
            isPlaying: false,
            isGenerating: false,
            currentTime: 0,
            duration: 0,
            progress: 0,
            error: null,
            speed: currentSpeed,
            volume: currentVolume
        });
    }

    getState() {
        return { ...this.state };
    }
}

export default PlayerState;