Skip to content

Commit eb807a8

Browse files
authored
Merge pull request #90 from tsirysndr/feat/graphql-subscriptions
feat: use graphql subscriptions
2 parents f07c690 + 718a4ea commit eb807a8

31 files changed

+1302
-100
lines changed

Cargo.lock

Lines changed: 24 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "music-player"
3-
version = "0.2.0-alpha.5"
3+
version = "0.2.0-alpha.6"
44
edition = "2021"
55
repository = "https://github.com/tsirysndr/music-player"
66
license = "MIT"
@@ -40,7 +40,7 @@ version = "0.1.9"
4040

4141
[dependencies.music-player-playback]
4242
path = "playback"
43-
version = "0.1.5"
43+
version = "0.1.6"
4444

4545
[dependencies.music-player-scanner]
4646
path = "scanner"
@@ -76,15 +76,15 @@ version = "0.1.1"
7676

7777
[dependencies.music-player-webui]
7878
path = "webui"
79-
version = "0.1.6"
79+
version = "0.1.7"
8080

8181
[dependencies.music-player-discovery]
8282
path = "discovery"
8383
version = "0.1.0"
8484

8585
[dependencies.music-player-graphql]
8686
path = "graphql"
87-
version = "0.1.2"
87+
version = "0.1.3"
8888

8989
[dependencies.music-player-types]
9090
path = "types"

graphql/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "music-player-graphql"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
edition = "2021"
55
repository = "https://github.com/tsirysndr/music-player"
66
license = "MIT"
@@ -49,3 +49,7 @@ sea-orm = { version = "0.9.3", features = ["runtime-tokio-rustls", "sqlx-sqlite"
4949
rand = "0.8.5"
5050
cuid = "1.2.0"
5151
chrono = "0.4.23"
52+
futures-util = "0.3.25"
53+
futures-channel = "0.3.25"
54+
once_cell = "1.16.0"
55+
slab = "0.4.7"

graphql/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use async_graphql::{EmptySubscription, Schema};
2-
use schema::{Mutation, Query};
1+
use async_graphql::Schema;
2+
use schema::{Mutation, Query, Subscription};
33

44
pub mod schema;
5+
pub mod simple_broker;
56

6-
pub type MusicPlayerSchema = Schema<Query, Mutation, EmptySubscription>;
7+
pub type MusicPlayerSchema = Schema<Query, Mutation, Subscription>;

graphql/src/schema/mod.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use async_graphql::MergedObject;
1+
use async_graphql::{Enum, MergedObject, MergedSubscription};
22

33
use self::{
44
library::{LibraryMutation, LibraryQuery},
55
mixer::{MixerMutation, MixerQuery},
6-
playback::{PlaybackMutation, PlaybackQuery},
7-
playlist::{PlaylistMutation, PlaylistQuery},
8-
tracklist::{TracklistMutation, TracklistQuery},
6+
playback::{PlaybackMutation, PlaybackQuery, PlaybackSubscription},
7+
playlist::{PlaylistMutation, PlaylistQuery, PlaylistSubscription},
8+
tracklist::{TracklistMutation, TracklistQuery, TracklistSubscription},
99
};
1010

1111
pub mod addons;
@@ -35,3 +35,20 @@ pub struct Mutation(
3535
PlaylistMutation,
3636
TracklistMutation,
3737
);
38+
39+
#[derive(MergedSubscription, Default)]
40+
pub struct Subscription(
41+
PlaybackSubscription,
42+
PlaylistSubscription,
43+
TracklistSubscription,
44+
);
45+
46+
#[derive(Enum, Eq, PartialEq, Copy, Clone)]
47+
pub enum MutationType {
48+
Created,
49+
Cleared,
50+
Deleted,
51+
Renamed,
52+
Moved,
53+
Updated,
54+
}

graphql/src/schema/objects/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod artist;
33
pub mod current_track;
44
pub mod folder;
55
pub mod lyrics;
6+
pub mod player_state;
67
pub mod playlist;
78
pub mod search_result;
89
pub mod track;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use async_graphql::*;
2+
3+
#[derive(Default, Clone)]
4+
pub struct PlayerState {
5+
pub index: u32,
6+
pub position_ms: u32,
7+
pub is_playing: bool,
8+
}
9+
10+
#[Object]
11+
impl PlayerState {
12+
async fn index(&self) -> u32 {
13+
self.index
14+
}
15+
16+
async fn position_ms(&self) -> u32 {
17+
self.position_ms
18+
}
19+
20+
async fn is_playing(&self) -> bool {
21+
self.is_playing
22+
}
23+
}

graphql/src/schema/playback.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
use std::sync::Arc;
22

33
use async_graphql::*;
4+
use futures_util::{lock::Mutex, Stream};
45
use music_player_playback::player::PlayerCommand;
5-
use music_player_tracklist::Tracklist;
6-
use tokio::sync::{mpsc::UnboundedSender, Mutex};
6+
use music_player_tracklist::{PlaybackState, Tracklist};
7+
use tokio::sync::mpsc::UnboundedSender;
78

8-
use super::objects::current_track::CurrentlyPlayingSong;
9+
use crate::simple_broker::SimpleBroker;
10+
11+
use super::objects::{
12+
current_track::CurrentlyPlayingSong, player_state::PlayerState, track::Track,
13+
};
914

1015
#[derive(Default)]
1116
pub struct PlaybackQuery;
@@ -50,7 +55,7 @@ impl PlaybackQuery {
5055
})
5156
}
5257

53-
async fn get_playback_state(&self, ctx: &Context<'_>) -> bool {
58+
async fn get_player_state(&self, ctx: &Context<'_>) -> PlayerState {
5459
let _tracklist = ctx.data::<Arc<Mutex<Tracklist>>>().unwrap();
5560
todo!()
5661
}
@@ -133,3 +138,33 @@ impl PlaybackMutation {
133138
Ok(true)
134139
}
135140
}
141+
142+
#[derive(Clone)]
143+
pub struct PositionMilliseconds {
144+
pub position_ms: u32,
145+
}
146+
147+
#[Object]
148+
impl PositionMilliseconds {
149+
async fn position_ms(&self) -> u32 {
150+
self.position_ms
151+
}
152+
}
153+
154+
#[derive(Default)]
155+
pub struct PlaybackSubscription;
156+
157+
#[Subscription]
158+
impl PlaybackSubscription {
159+
async fn player_state(&self) -> impl Stream<Item = PlayerState> {
160+
SimpleBroker::<PlayerState>::subscribe()
161+
}
162+
163+
async fn currently_playing_song(&self) -> impl Stream<Item = Track> {
164+
SimpleBroker::<Track>::subscribe()
165+
}
166+
167+
async fn track_time_position(&self) -> impl Stream<Item = PositionMilliseconds> {
168+
SimpleBroker::<PositionMilliseconds>::subscribe()
169+
}
170+
}

0 commit comments

Comments
 (0)