From 0349918272be85099409262a5fbe1499d68f95d6 Mon Sep 17 00:00:00 2001 From: wangjianhui Date: Thu, 20 Dec 2018 17:48:27 +0800 Subject: [PATCH] modify collab server --- frontend/src/components/dir-view/dir-view.js | 24 ++++++++++++-- .../dirent-list-view/dirent-list-view.js | 3 -- frontend/src/repo-wiki-mode.js | 20 ++++++++++- frontend/src/utils/collab-server.js | 33 ++++++++----------- 4 files changed, 54 insertions(+), 26 deletions(-) diff --git a/frontend/src/components/dir-view/dir-view.js b/frontend/src/components/dir-view/dir-view.js index b115384716..e5402ef33c 100644 --- a/frontend/src/components/dir-view/dir-view.js +++ b/frontend/src/components/dir-view/dir-view.js @@ -51,6 +51,7 @@ class DirView extends React.Component { // eg: http://127.0.0.1:8000/library/repo_id/repo_name/**/**/\ let location = decodeURIComponent(window.location.href); let repoID = this.props.repoID; + collabServer.watchRepo(repoID, this.onRepoUpdateEvent); seafileAPI.getRepoInfo(repoID).then(res => { let repoInfo = new RepoInfo(res.data); this.setState({ @@ -70,7 +71,27 @@ class DirView extends React.Component { } }); } - + + componentWillUnmount() { + collabServer.unwatchRepo(this.props.repoID); + } + + onRepoUpdateEvent = () => { + let repoID = this.props.repoID; + let { path, dirID } = this.state; + seafileAPI.dirMetaData(repoID, path).then((res) => { + if (res.data.id !== dirID) { + toaster.notify( + + {gettext('This folder has been updated. ')} + {gettext('Refresh')} + , + {duration: 3600} + ); + } + }) + } + updateDirentList = (filePath) => { let repoID = this.state.repoID; this.setState({isDirentListLoading: true}); @@ -83,7 +104,6 @@ class DirView extends React.Component { direntList: direntList, dirID: res.headers.oid, }); - collabServer.watchRepo(this.state.repoID, this.state.path, this.state.dirID); }).catch(() => { this.setState({pathExist: false}); }); diff --git a/frontend/src/components/dirent-list-view/dirent-list-view.js b/frontend/src/components/dirent-list-view/dirent-list-view.js index 9e6d4e815f..647cd1c387 100644 --- a/frontend/src/components/dirent-list-view/dirent-list-view.js +++ b/frontend/src/components/dirent-list-view/dirent-list-view.js @@ -7,9 +7,6 @@ import ModalPortal from '../modal-portal'; import CreateFile from '../../components/dialog/create-file-dialog'; import '../../css/tip-for-new-md.css'; -import io from 'socket.io-client'; -import { seafileAPI } from '../../utils/seafile-api'; -import toaster from '../../components/toast'; const propTypes = { path: PropTypes.string.isRequired, diff --git a/frontend/src/repo-wiki-mode.js b/frontend/src/repo-wiki-mode.js index 997e8f2b04..a81dc55895 100644 --- a/frontend/src/repo-wiki-mode.js +++ b/frontend/src/repo-wiki-mode.js @@ -63,6 +63,7 @@ class Wiki extends Component { } componentDidMount() { + collabServer.watchRepo(repoID, this.onRepoUpdateEvent); seafileAPI.getRepoInfo(repoID).then(res => { this.setState({ libNeedDecrypt: res.data.lib_need_decrypt, @@ -82,6 +83,24 @@ class Wiki extends Component { }); } + componentWillUnmount() { + collabServer.unwatchRepo(repoID); + } + + onRepoUpdateEvent = () => { + let { path, dirID } = this.state; + seafileAPI.dirMetaData(repoID, path).then((res) => { + if (res.data.id !== dirID) { + toaster.notify( + + {gettext('This folder has been updated. ')} + {gettext('Refresh')} + , + {duration: 3600} + ); + } + }) + } deleteItemAjaxCallback(path, isDir) { let node = this.state.treeData.getNodeByPath(path); @@ -282,7 +301,6 @@ class Wiki extends Component { isDirentListLoading: false, dirID: res.headers.oid, }); - collabServer.watchRepo(repoID, this.state.path, this.state.dirID); }); } diff --git a/frontend/src/utils/collab-server.js b/frontend/src/utils/collab-server.js index 3612e20ec1..7fab59da6c 100644 --- a/frontend/src/utils/collab-server.js +++ b/frontend/src/utils/collab-server.js @@ -1,14 +1,11 @@ -import React from 'react'; import io from 'socket.io-client'; -import toaster from '../components/toast'; -import { seafileAPI } from './seafile-api'; -import { gettext, name, username, contactEmail, seafileCollabServer } from './constants'; +import { name, username, contactEmail, seafileCollabServer } from './constants'; const socket = io(seafileCollabServer); class CollabServer { - watchRepo(repoID, path, dirID) { + watchRepo(repoID, fn) { socket.emit('repo_update', { request: 'watch_update', repo_id: repoID, @@ -19,23 +16,19 @@ class CollabServer { }, }); - socket.on('repo_update', () => { - seafileAPI.dirMetaData(repoID, path).then((res) => { - if (res.data.id !== dirID) { - toaster.notify( - - {gettext('This folder has been updated. ')} - {gettext('Refresh')} - , - {duration: 3600} - ); - } - }) - }) + socket.on('repo_update', fn) } - unwatchRepo() { - socket.off() + unwatchRepo(repoID) { + socket.emit('repo_update', { + request: 'unwatch_update', + repo_id: repoID, + user: { + name: name, + username: username, + contact_email: contactEmail, + }, + }); } }