Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: standalone presenter mode (#552)
* feat: use BroadcastChannel instead of server in static build * feat: sync drawings while using static build * refactor: refactor shared and drawing states * feat: manage drawing persist mode * fix: use persist mode only for drawings
- Loading branch information
Showing
11 changed files
with
152 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
<script setup lang="ts"> | ||
import { serverState } from '../env' | ||
import { sharedState } from '../state/shared' | ||
</script> | ||
|
||
<template> | ||
<div | ||
v-if="serverState.cursor" | ||
v-if="sharedState.cursor" | ||
class="absolute top-0 left-0 right-0 bottom-0 pointer-events-none text-xl" | ||
> | ||
<ph:cursor-fill | ||
class="absolute" | ||
:style="{ left: `${serverState.cursor.x}%`, top: `${serverState.cursor.y}%` }" | ||
:style="{ left: `${sharedState.cursor.x}%`, top: `${sharedState.cursor.y}%` }" | ||
/> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import serverDrawingState from 'server-reactive:drawings?diff' | ||
import { createSyncState } from './syncState' | ||
|
||
export type DrawingsState = Record<number, string | undefined> | ||
|
||
const { init, onPatch, patch, state } = createSyncState<DrawingsState>(serverDrawingState, {}, __SLIDEV_FEATURE_DRAWINGS_PERSIST__) | ||
export { init as initDrawingState, onPatch, patch, state as drawingState } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import serverState from 'server-reactive:nav' | ||
import { createSyncState } from './syncState' | ||
|
||
export interface SharedState { | ||
page: number | ||
clicks: number | ||
cursor?: { | ||
x: number | ||
y: number | ||
} | ||
} | ||
|
||
const { init, onPatch, patch, state } = createSyncState<SharedState>(serverState, { | ||
page: 1, | ||
clicks: 0, | ||
}) | ||
export { init as initSharedState, onPatch, patch, state as sharedState } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { reactive, toRaw, watch } from 'vue' | ||
|
||
export function createSyncState<State extends object>(serverState: State, defaultState: State, persist = false) { | ||
const onPatchCallbacks: ((state: State) => void)[] = [] | ||
let patching = false | ||
let updating = false | ||
let patchingTimeout: NodeJS.Timeout | ||
let updatingTimeout: NodeJS.Timeout | ||
|
||
const state = __DEV__ | ||
? reactive<State>(serverState) as State | ||
: reactive<State>(defaultState) as State | ||
|
||
function onPatch(fn: (state: State) => void) { | ||
onPatchCallbacks.push(fn) | ||
} | ||
|
||
function patch<K extends keyof State>(key: K, value: State[K]) { | ||
clearTimeout(patchingTimeout) | ||
patching = true | ||
state[key] = value | ||
patchingTimeout = setTimeout(() => patching = false, 0) | ||
} | ||
|
||
function onUpdate(patch: Partial<State>) { | ||
if (!patching) { | ||
clearTimeout(updatingTimeout) | ||
updating = true | ||
Object.entries(patch).forEach(([key, value]) => { | ||
state[key as keyof State] = value as State[keyof State] | ||
}) | ||
updatingTimeout = setTimeout(() => updating = false, 0) | ||
} | ||
} | ||
|
||
function init(channelKey: string) { | ||
let stateChannel: BroadcastChannel | ||
if (!__DEV__ && !persist) { | ||
stateChannel = new BroadcastChannel(channelKey) | ||
stateChannel.addEventListener('message', (event: MessageEvent<Partial<State>>) => onUpdate(event.data)) | ||
} | ||
else if (!__DEV__ && persist) { | ||
window.addEventListener('storage', (event) => { | ||
if (event && event.key === channelKey && event.newValue) | ||
onUpdate(JSON.parse(event.newValue) as Partial<State>) | ||
}) | ||
} | ||
|
||
function onDrawingStateChanged() { | ||
if (!persist && stateChannel && !updating) | ||
stateChannel.postMessage(toRaw(state)) | ||
else if (persist && !updating) | ||
window.localStorage.setItem(channelKey, JSON.stringify(state)) | ||
if (!patching) | ||
onPatchCallbacks.forEach((fn: (state: State) => void) => fn(state)) | ||
} | ||
|
||
watch(state, onDrawingStateChanged, { deep: true }) | ||
if (!__DEV__ && persist) { | ||
const serialzedState = window.localStorage.getItem(channelKey) | ||
if (serialzedState) | ||
onUpdate(JSON.parse(serialzedState) as Partial<State>) | ||
} | ||
} | ||
|
||
return { init, onPatch, patch, state } | ||
} |