intermediate: sync

This commit is contained in:
Julian Krauser 2025-02-27 15:08:56 +01:00
parent 24920606ea
commit 5857c7e0d4
4 changed files with 43 additions and 36 deletions

View file

@ -7,10 +7,11 @@ export const useConnectionStore = defineStore("connection", {
state: () => { state: () => {
return { return {
connection: undefined as undefined | Socket, connection: undefined as undefined | Socket,
connected: false as boolean,
}; };
}, },
getters: { getters: {
connectionStatus: (state) => !!state.connection, connectionStatus: (state) => state.connected,
}, },
actions: { actions: {
connectClient(): void { connectClient(): void {
@ -27,16 +28,21 @@ export const useConnectionStore = defineStore("connection", {
}); });
this.connection.on("connect", () => { this.connection.on("connect", () => {
this.connected = true;
notificationStore.push("Socket-Erfolg", `Verbindung aufgebaut`, "success"); notificationStore.push("Socket-Erfolg", `Verbindung aufgebaut`, "success");
}); });
this.connection.on("connect_error", (err) => { this.connection.on("connect_error", (err) => {
this.connected = false;
this.handleError(err, true); this.handleError(err, true);
}); });
this.connection.on("disconnecting", () => { this.connection.on("disconnecting", () => {
this.connected = false;
this.connection?.disconnect(); this.connection?.disconnect();
}); });
this.connection.on("disconnect", () => { this.connection.on("disconnect", () => {
this.connected = false;
this.$reset(); this.$reset();
this.connectClient();
}); });
this.connection.on("warning", (msg: string) => { this.connection.on("warning", (msg: string) => {
notificationStore.push("Socket-Warnung", msg, "warning"); notificationStore.push("Socket-Warnung", msg, "warning");

View file

@ -6,12 +6,11 @@ import { computed, ref } from "vue";
export const useMissionDetailStore = defineStore("missionDetail", () => { export const useMissionDetailStore = defineStore("missionDetail", () => {
const connectionStore = useConnectionStore(); const connectionStore = useConnectionStore();
const initialized = ref(false);
const ydoc = ref(new Y.Doc()); const ydoc = ref(new Y.Doc());
const awareness = ref(new Awareness(ydoc.value)); const awareness = ref(new Awareness(ydoc.value));
const editor = ref(ydoc.value.getText("editor"));
const title = computed({ const title = computed({
get() { get() {
return ydoc.value.getMap("form").get("title") ?? ""; return ydoc.value.getMap("form").get("title") ?? "";
@ -20,30 +19,35 @@ export const useMissionDetailStore = defineStore("missionDetail", () => {
ydoc.value.getMap("form").set("title", val); ydoc.value.getMap("form").set("title", val);
}, },
}); });
const editor = ref(ydoc.value.getText("editor"));
function init(missionId: string) { function init(missionId: string) {
connectionStore.connection?.on("package-sync", (update) => { if (!connectionStore.connection) return;
Y.applyUpdate(ydoc.value, new Uint8Array(update));
});
connectionStore.connection?.on("package-mission", (initial) => {
Y.applyUpdate(ydoc.value, new Uint8Array(initial));
});
connectionStore.connection?.emit("mission:join", missionId);
}
function destroy() {
connectionStore?.connection?.emit("mission:leave");
ydoc.value = new Y.Doc(); ydoc.value = new Y.Doc();
awareness.value = new Awareness(ydoc.value); awareness.value = new Awareness(ydoc.value);
editor.value = ydoc.value.getText("editor"); editor.value = ydoc.value.getText("editor");
ydoc.value.on("update", (update) => {
connectionStore.connection?.emit("mission:sync", Array.from(update));
});
connectionStore.connection.on("package-sync", (update) => {
Y.applyUpdate(ydoc.value, new Uint8Array(update));
});
connectionStore.connection.on("package-mission", (initial) => {
Y.applyUpdate(ydoc.value, new Uint8Array(initial));
});
connectionStore.connection.emit("mission:join", missionId, initialized.value);
initialized.value = true;
}
function destroy() {
connectionStore.connection?.emit("mission:leave");
connectionStore.connection?.off("package-mission");
connectionStore.connection?.off("package-sync");
} }
ydoc.value.on("update", (update) => { return { ydoc, awareness, title, editor, init, destroy };
console.log(connectionStore.connection);
connectionStore.connection?.emit("mission:sync", Array.from(update));
});
return { ydoc, awareness, editor, title, init, destroy };
}); });

View file

@ -10,12 +10,12 @@
</div> </div>
<div class="flex flex-col sm:flex-row gap-2"> <div class="flex flex-col sm:flex-row gap-2">
<div class="grow"> <div class="grow">
<label for="title">Einsatzbeginn</label> <label for="start">Einsatzbeginn</label>
<input type="datetime-local" id="title" /> <input type="datetime-local" id="start" />
</div> </div>
<div class="grow"> <div class="grow">
<label for="title">Einsatzende</label> <label for="end">Einsatzende</label>
<input type="datetime-local" id="title" /> <input type="datetime-local" id="end" />
</div> </div>
<div class="w-full sm:w-fit min-w-fit"> <div class="w-full sm:w-fit min-w-fit">
<p>Dauer</p> <p>Dauer</p>
@ -27,11 +27,11 @@
</div> </div>
</div> </div>
<div> <div>
<label for="title">Stichwort</label> <label for="mission_short">Stichwort</label>
<input type="text" id="title" /> <input type="text" id="mission_short" />
</div> </div>
<div> <div>
<label for="title">Einsatzort</label> <label for="location">Einsatzort</label>
<input type="text" id="title" /> <input type="text" id="title" />
</div> </div>
<div> <div>
@ -40,12 +40,12 @@
</div> </div>
<div class="flex flex-col sm:flex-row gap-2"> <div class="flex flex-col sm:flex-row gap-2">
<div class="w-full"> <div class="w-full">
<label for="title">Anzahl getretteter Personen</label> <label for="rescued">Anzahl getretteter Personen</label>
<input type="number" id="title" value="0" /> <input type="number" id="rescued" value="0" />
</div> </div>
<div class="w-full"> <div class="w-full">
<label for="title">Anzahl geborgener Personen</label> <label for="recovered">Anzahl geborgener Personen</label>
<input type="number" id="title" value="0" /> <input type="number" id="recovered" value="0" />
</div> </div>
</div> </div>
<div class="flex flex-col"> <div class="flex flex-col">
@ -72,9 +72,6 @@
<p>Kontaktdaten</p> <p>Kontaktdaten</p>
</div> </div>
</div> </div>
<!-- <div class="flex flex-row gap-4">
<button primary class="!w-fit">Knopf</button>
</div> -->
</template> </template>
<script setup lang="ts"> <script setup lang="ts">

View file

@ -71,7 +71,6 @@ export default defineComponent({
}, },
connectionStatus() { connectionStatus() {
if (this.connectionStatus) { if (this.connectionStatus) {
console.log("hi");
this.init(this.id); this.init(this.id);
} }
}, },
@ -85,6 +84,7 @@ export default defineComponent({
mounted() { mounted() {
this.manageHash(); this.manageHash();
this.connectClient(); this.connectClient();
this.init(this.id);
this.getAvailableForces(); this.getAvailableForces();
}, },
beforeUnmount() { beforeUnmount() {