From da15407e53ead25eef99cb5385f8e514f3e1caa7 Mon Sep 17 00:00:00 2001 From: YarikMix <43493788+YarikMix@users.noreply.github.com> Date: Sat, 25 May 2024 17:46:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BF=D0=BE=D0=B4=20=D1=83=D0=B2=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D0=BC=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BE=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=D0=B3=D0=BB=D0=B0=D1=88=D0=B5=D0=BD=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=B2=20=D0=B7=D0=B0=D0=BC=D0=B5=D1=82=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/src/modules/stores/NotesStore.ts | 99 ++++++++++++++++--------- 1 file changed, 62 insertions(+), 37 deletions(-) diff --git a/public/src/modules/stores/NotesStore.ts b/public/src/modules/stores/NotesStore.ts index 95441f18..2de79284 100644 --- a/public/src/modules/stores/NotesStore.ts +++ b/public/src/modules/stores/NotesStore.ts @@ -79,7 +79,8 @@ class NotesStore extends BaseStore { fullScreen: false }; - private ws + private editorWS + private invitesWS public socket_id constructor() { @@ -87,6 +88,58 @@ class NotesStore extends BaseStore { this.registerEvents(); } + async init () { + await this.fetchNotes(true); + await this.fetchTags() + + this.invitesWS = new WebSocketConnection(`api/note/subscribe/on_invites`) + + this.invitesWS.onOpen(() => { + console.log("invitesWS.onOpen") + // this.editorWS.sendMessage(JSON.stringify({ + // type: "opened", + // note_id: note.id, + // user_id: AppUserStore.state.user_id, + // username: AppUserStore.state.username, + // image_path: AppUserStore.state.avatarUrl + // })) + }) + + this.invitesWS.onMessage((event) => { + console.log("invitesWS.onMessage") + + let data = JSON.parse(event.data) + + console.log(data) + }) + + return this.state; + } + + exit () { + AppNotesStore.ClearCallbacks() + AppNoteStore.ClearCallbacks() + + this.SetState(state => ({ + ...state, + notes: [], + tags: [], + selectedTags: [], + selectedNote: null, + selectedNoteSynced: null, + selectedNoteCollaborators: [], + query: '', + offset: 0, + count: 10, + fetching: false, + noteNotFound: false, + fullScreen: false + })); + + this.invitesWS.close() + this.invitesWS = null + } + private registerEvents(){ AppDispatcher.register(async (action) => { switch (action.type){ @@ -198,28 +251,6 @@ class NotesStore extends BaseStore { } }); } - - exit () { - AppNotesStore.ClearCallbacks() - AppNoteStore.ClearCallbacks() - - this.SetState(state => ({ - ...state, - notes: [], - tags: [], - selectedTags: [], - selectedNote: null, - selectedNoteSynced: null, - selectedNoteCollaborators: [], - query: '', - offset: 0, - count: 10, - fetching: false, - noteNotFound: false, - fullScreen: false - })); - } - syncNotes = () => { const notes = this.state.notes; notes.forEach((note, index) => { @@ -272,15 +303,15 @@ class NotesStore extends BaseStore { } closeWS = () => { - if (this.ws && this.state.selectedNote) { - this.ws.sendMessage(JSON.stringify({ + if (this.editorWS && this.state.selectedNote) { + this.editorWS.sendMessage(JSON.stringify({ type: "closed", note_id: this.state.selectedNote.id, user_id: AppUserStore.state.user_id })) - this.ws.close() - this.ws = null + this.editorWS.close() + this.editorWS = null this.socket_id = null } } @@ -305,10 +336,10 @@ class NotesStore extends BaseStore { blocks: note.data.content }) - this.ws = new WebSocketConnection(`note/${note.id}/subscribe_on_updates`) + this.editorWS = new WebSocketConnection(`note/${note.id}/subscribe_on_updates`) - this.ws.onOpen(() => { - this.ws.sendMessage(JSON.stringify({ + this.editorWS.onOpen(() => { + this.editorWS.sendMessage(JSON.stringify({ type: "opened", note_id: note.id, user_id: AppUserStore.state.user_id, @@ -317,7 +348,7 @@ class NotesStore extends BaseStore { })) }) - this.ws.onMessage((event) => { + this.editorWS.onMessage((event) => { let data = JSON.parse(event.data) // Если socket_id совпадает, то ничего обновлять не надо @@ -386,12 +417,6 @@ class NotesStore extends BaseStore { history.pushState(null, null, '/notes/' + id) } - async init () { - await this.fetchNotes(true); - await this.fetchTags() - return this.state; - } - async searchNotes ({query, selectedTags}) { this.SetState(state => ({ ...state,