mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-05-20 10:42:46 +00:00
avoid repeating websocket creation
This commit is contained in:
@@ -11,7 +11,10 @@
|
||||
let networkSessionInputs;
|
||||
let joinLink;
|
||||
|
||||
$: if(session) {
|
||||
let cachedSessionId;
|
||||
|
||||
$: if(!cachedSessionId && session) {
|
||||
cachedSessionId = session.session_id;
|
||||
console.log("NetworkSessionWrapper ready, now setting up sessionEvents")
|
||||
sessionEvents = networkSessionStateEvents(session);
|
||||
joinLink = api.createJoinLink(session.session_id);
|
||||
@@ -25,9 +28,7 @@
|
||||
{#if !session}
|
||||
<h3>no session</h3>
|
||||
{:else}
|
||||
{#if sessionEvents}
|
||||
events: {JSON.stringify($sessionEvents)}
|
||||
{/if}
|
||||
{JSON.stringify(session)}
|
||||
{#if session.state === SessionState.PENDING}
|
||||
<h3>waiting for other player...</h3>
|
||||
<CopyToClipboard text={joinLink}></CopyToClipboard>
|
||||
|
||||
@@ -60,7 +60,7 @@ const networkEvents = (session: NetworkSession) => readable([], function(set) {
|
||||
// TODO: Hotfix, would be better to have clean serialization in the backend...
|
||||
data = data.map(({event, ...rest}) => ({...rest, event: JSON.parse(event)}))
|
||||
console.debug("Parsed events: ", data)
|
||||
events.set(data)
|
||||
events.set([...get(events), ...data]);
|
||||
}
|
||||
ws.onerror = err => {
|
||||
console.error("ws error: ", err)
|
||||
@@ -85,11 +85,12 @@ const networkEvents = (session: NetworkSession) => readable([], function(set) {
|
||||
})
|
||||
|
||||
export const networkSessionStateEvents = (session: NetworkSession): Readable<unknown[]> => derived(networkEvents(session), $sessionEvents => {
|
||||
const sessionEvents = $sessionEvents.filter(({topic}) => topic === 'session');
|
||||
const sessionEvents = $sessionEvents.filter(({topic}) => topic === 'session').map(({event}) => event);
|
||||
if (!sessionEvents.length) {
|
||||
return [];
|
||||
}
|
||||
const latestSessionEvent = sessionEvents[sessionEvents.length-1];
|
||||
console.debug("updating current session: ", latestSessionEvent.session)
|
||||
sessionStore.set(latestSessionEvent.session);
|
||||
return sessionEvents;
|
||||
});
|
||||
|
||||
@@ -86,15 +86,9 @@ async fn handle_session_create(
|
||||
.body(Body::from(e))
|
||||
.unwrap());
|
||||
}
|
||||
let session = session_create_res.unwrap();
|
||||
error!("session created by player {:?}: {:?}", player, session);
|
||||
let reason = format!("player {:?} created session", player);
|
||||
let session_created = SessionEvent::Created(SessionEventPayload {
|
||||
session,
|
||||
actor: player,
|
||||
reason
|
||||
});
|
||||
let serialized = json!(session_created);
|
||||
let session_event = session_create_res.unwrap();
|
||||
error!("session created: {:?}", session_event);
|
||||
let serialized = json!(session_event);
|
||||
return build_success_res(&serialized.to_string());
|
||||
}
|
||||
|
||||
@@ -116,15 +110,10 @@ async fn handle_session_join(
|
||||
.body(Body::from(e))
|
||||
.unwrap());
|
||||
}
|
||||
let session = session_join_res.unwrap();
|
||||
info!("player {:?} successfully joined session: {:?}", player, session);
|
||||
let session_event = session_join_res.unwrap();
|
||||
info!("player {:?} successfully joined session: {:?}", player, session_event);
|
||||
let reason = format!("player {:?} joined session", player);
|
||||
let session_joined = SessionEvent::Joined(SessionEventPayload {
|
||||
actor: player,
|
||||
session,
|
||||
reason
|
||||
});
|
||||
let serialized = json!(session_joined);
|
||||
let serialized = json!(session_event);
|
||||
return build_success_res(&serialized.to_string());
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::kafka::{
|
||||
KafkaTopicManager,
|
||||
};
|
||||
use crate::actor::Player;
|
||||
use crate::event::{SessionEvent, SessionEventPayload};
|
||||
use crate::session::{Session, SessionState};
|
||||
|
||||
pub struct SessionManager {
|
||||
@@ -37,7 +38,7 @@ impl SessionManager {
|
||||
.map_or_else(|| None, |s| Some(s.clone()))
|
||||
}
|
||||
|
||||
pub async fn create_session(&mut self, player: Player) -> Result<Session, String> {
|
||||
pub async fn create_session(&mut self, player: Player) -> Result<SessionEvent, String> {
|
||||
info!("called to create new session by player {:?}", player);
|
||||
let add_partition_res = self.topic_manager.add_partition().await;
|
||||
if let Err(e) = add_partition_res {
|
||||
@@ -62,14 +63,19 @@ impl SessionManager {
|
||||
);
|
||||
}
|
||||
info!("successfully persisted create session event.");
|
||||
Ok(session)
|
||||
let session_created = SessionEvent::Created(SessionEventPayload {
|
||||
session,
|
||||
actor: player,
|
||||
reason: format!("session created")
|
||||
});
|
||||
Ok(session_created)
|
||||
}
|
||||
|
||||
pub async fn join_session(
|
||||
&mut self,
|
||||
session_id: String,
|
||||
player: Player,
|
||||
) -> Result<Session, String> {
|
||||
) -> Result<SessionEvent, String> {
|
||||
let updated_session = {
|
||||
let session = self.sessions.iter_mut().find(|s| s.session_id == session_id);
|
||||
if let None = session {
|
||||
@@ -107,12 +113,17 @@ impl SessionManager {
|
||||
}
|
||||
};
|
||||
println!("sessions = {:?}", self.sessions);
|
||||
Ok(updated_session.clone())
|
||||
let session_joined_event = SessionEvent::Joined(SessionEventPayload {
|
||||
session: updated_session,
|
||||
reason: "session joined".to_owned(),
|
||||
actor: player
|
||||
});
|
||||
Ok(session_joined_event)
|
||||
}
|
||||
|
||||
fn write_to_producer<T>(&mut self, session_event: T) -> Result<(), String>
|
||||
where
|
||||
T: SessionEvent,
|
||||
T: SessionAwareEvent,
|
||||
{
|
||||
let session_id = session_event.session_id();
|
||||
let session_producer = match self.session_producers.get_mut(session_id) {
|
||||
@@ -233,7 +244,7 @@ impl SessionReader {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SessionEvent : Serialize {
|
||||
pub trait SessionAwareEvent: Serialize {
|
||||
fn session_id(&self) -> &str;
|
||||
}
|
||||
|
||||
@@ -254,7 +265,7 @@ impl SessionCreatedEvent {
|
||||
}
|
||||
}
|
||||
|
||||
impl SessionEvent for SessionCreatedEvent {
|
||||
impl SessionAwareEvent for SessionCreatedEvent {
|
||||
fn session_id(&self) -> &str {
|
||||
&self.session.session_id
|
||||
}
|
||||
@@ -277,7 +288,7 @@ impl SessionJoinedEvent {
|
||||
}
|
||||
}
|
||||
|
||||
impl SessionEvent for SessionJoinedEvent {
|
||||
impl SessionAwareEvent for SessionJoinedEvent {
|
||||
fn session_id(&self) -> &str {
|
||||
&self.session.session_id
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user