Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat isolation #32

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/chat/Chat.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { fly } from 'svelte/transition';
import { UserStore } from '@lib/user';
import type { Message, TextMessage } from '@lib/message';
import { onMount } from 'svelte';

let message: string;
let messagesDiv: HTMLDivElement = null;
Expand Down Expand Up @@ -43,6 +44,10 @@
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}, 500);
}

onMount(() => {
chatStore.newChat($jamStore.id);
});
</script>

<div
Expand All @@ -52,8 +57,8 @@
<div
bind:this={messagesDiv}
class="messages">
{#if $chatStore.length > 0}
{#each $chatStore as message}
{#if $chatStore[$jamStore.id] && $chatStore[$jamStore.id].length > 0}
{#each $chatStore[$jamStore.id] as message}
<ChatBubble {message} />
{/each}
{:else}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const agent = {
list: async () => {
const {
data: { rooms },
} = await api.get<{ rooms: JamRoom[]; }>('/jams');
} = await api.get<{ rooms: JamRoom[] }>('/jams');

return rooms;
},
Expand Down
3 changes: 2 additions & 1 deletion src/lib/audio/mic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export function noteFromPitch(
octaveLength = 12
): number | null {
const A4 = { freq: 440, midi: 69 };
const noteNum = octaveLength * (Math.log(frequency / A4.freq) / Math.log(2));
const noteNum =
octaveLength * (Math.log(frequency / A4.freq) / Math.log(2));
const midiNote = Math.round(noteNum) + A4.midi;
if (midiNote < 0 || midiNote > 127) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/audio/midi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Instrument {
}

// FIXME - _velocity defined but never used
noteOn(note: number) {
noteOn(note: number, _velocity: number) {
this.player.play(note.toString());
}
}
Expand Down
31 changes: 23 additions & 8 deletions src/lib/jam/chat.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import type { TextMessage } from '@lib/message';
import { createStorage } from '../storage';

const createChatStorage = (init: TextMessage[] = []) => {
const { subscribe, update } = createStorage<TextMessage[]>(
'CHAT_STORE',
init
);
interface ChatHistory {
[jamId: string]: TextMessage[];
}

function saveMessage(...message: TextMessage[]) {
const createChatStorage = () => {
const { subscribe, update } = createStorage<ChatHistory>('CHAT_STORE', {});

function newChat(jamId: string) {
update(($storage) => {
if ($storage[jamId] === undefined) {
$storage[jamId] = []
}

return $storage
});
}

function saveMessage(jamId: string, ...message: TextMessage[]) {
update(($storage) => {
return [...$storage, ...message];
if ($storage[jamId])
return {
...$storage,
[jamId]: [...$storage[jamId], ...message],
};
});
}

return { subscribe, saveMessage };
return { subscribe, newChat, saveMessage };
};

export const chatStore = createChatStorage();
5 changes: 4 additions & 1 deletion src/lib/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,8 @@ export const switchTheme = (name: ThemeName) => {
};

export const applyTheme = (theme: Theme) => {
return Object.entries(theme.vars).reduce((rules, [prop, val]) => `${rules}${prop}:${val};`, '');
return Object.entries(theme.vars).reduce(
(rules, [prop, val]) => `${rules}${prop}:${val};`,
''
);
};
8 changes: 6 additions & 2 deletions src/pages/Jam.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
// TODO -- this can be done at the variable declaration level
// easier to track
// FIXME - webkitAudioContext doesn't exist in type `Window` (d.ts file needed)
audioContext = new (window.AudioContext || window.webkitAudioContext)({
audioContext = new (window.AudioContext ||
globalThis.webkitAudioContext)({
latencyHint: 'interactive',
});
try {
Expand Down Expand Up @@ -161,7 +162,10 @@
displayMsg.displayName = 'You';
}

chatStore.saveMessage({ ...message, payload: displayMsg });
chatStore.saveMessage($jamStore.id, {
...message,
payload: displayMsg,
});
break;
}
case 'midi': {
Expand Down