diff --git a/public/client/space.proto b/public/client/space.proto index 674d0b71..ef1fd5c7 100644 --- a/public/client/space.proto +++ b/public/client/space.proto @@ -12,7 +12,7 @@ service SpaceApi { rpc Subscribe(google.protobuf.Empty) returns (stream FileEventResponse) {} rpc TxlSubscribe(google.protobuf.Empty) returns (stream TextileEventResponse) {} rpc OpenFile(OpenFileRequest) returns (OpenFileResponse) {} - rpc AddItems(AddItemsRequest) returns (AddItemsResponse) {} + rpc AddItems(AddItemsRequest) returns (stream AddItemsResponse) {} rpc CreateFolder(CreateFolderRequest) returns (CreateFolderResponse) {} } @@ -80,16 +80,12 @@ message AddItemsRequest { message AddItemResult { string sourcePath= 1; string bucketPath = 2; -} - -message AddItemError { - string sourcePath = 1; - string error = 2; + string error = 3; } message AddItemsResponse { - repeated AddItemResult results = 1; - repeated AddItemError errors = 2; + AddItemResult result = 1; + uint32 progress = 2; } message CreateFolderRequest { diff --git a/public/events/add-items-subscribe.js b/public/events/add-items-subscribe.js new file mode 100644 index 00000000..a1bace20 --- /dev/null +++ b/public/events/add-items-subscribe.js @@ -0,0 +1,32 @@ +const { ipcMain } = require('electron'); + +const client = require('../client'); +const { listDirectories } = require('./objects'); + +const EVENT_PREFIX = 'addItemsSuscribe'; +const SUBSCRIBE_START_EVENT = `${EVENT_PREFIX}:start`; +const SUBSCRIBE_ERROR_EVENT = `${EVENT_PREFIX}:error`; +const SUBSCRIBE_SUCCESS_EVENT = `${EVENT_PREFIX}:success`; + +const registerAddItemsSubscribe = (mainWindow) => { + let eventStream; + + ipcMain.on(SUBSCRIBE_START_EVENT, (event, payload) => { + eventStream = client.AddItems(payload); + + eventStream.on('data', (event) => { + mainWindow.webContents.send(SUBSCRIBE_SUCCESS_EVENT, event); + listDirectories(mainWindow); + }); + + eventStream.on('error', (error) => { + mainWindow.webContents.send(SUBSCRIBE_ERROR_EVENT, error); + }); + + eventStream.on('end', () => { + eventStream.destroy(); + }) + }); +}; + +module.exports = registerAddItemsSubscribe; diff --git a/public/events/add-items.js b/public/events/add-items.js deleted file mode 100644 index ea989e96..00000000 --- a/public/events/add-items.js +++ /dev/null @@ -1,25 +0,0 @@ -const { ipcMain } = require('electron'); - -const client = require('../client'); - -const EVENT_PREFIX = 'upload'; -const START_EVENT = `${EVENT_PREFIX}:start`; -const SUCCESS_EVENT = `${EVENT_PREFIX}:success`; -const ERROR_EVENT = `${EVENT_PREFIX}:error`; - -const registerAddItemsEvents = (mainWindow) => { - ipcMain.on(START_EVENT, (event, payload) => { - client.AddItems(payload, (err, res) => { - if (err) { - mainWindow.webContents.send(ERROR_EVENT, { - message: err.message, - }); - return; - } - - mainWindow.webContents.send(SUCCESS_EVENT, res); - }); - }); -}; - -module.exports = registerAddItemsEvents; diff --git a/public/events/index.js b/public/events/index.js index a45bb666..f745cbe2 100644 --- a/public/events/index.js +++ b/public/events/index.js @@ -3,8 +3,8 @@ const registerConfigEvents = require('./config'); const registerPathInfoEvents = require('./path-info'); const registerObjectsEvents = require('./objects').default; const registerGenerateKeyPairEvents = require('./generate-key-pair'); -const registerAddItemsEvents = require('./add-items'); const registerTxlSubscribe = require('./txl-subscribe'); +const registerAddItemsSubscribe = require('./add-items-subscribe'); const registerEvents = (mainWindow) => { const stream = registerEventStream(mainWindow); @@ -13,8 +13,8 @@ const registerEvents = (mainWindow) => { registerConfigEvents(mainWindow); registerObjectsEvents(mainWindow); registerPathInfoEvents(mainWindow); + registerAddItemsSubscribe(mainWindow); registerGenerateKeyPairEvents(mainWindow); - registerAddItemsEvents(mainWindow); return () => { stream.destroy(); diff --git a/src/events/add-items-subscribe.js b/src/events/add-items-subscribe.js new file mode 100644 index 00000000..f85ef378 --- /dev/null +++ b/src/events/add-items-subscribe.js @@ -0,0 +1,31 @@ +import { ipcRenderer } from 'electron'; +import { SET_UPLOAD_ERROR_STATE } from '@reducers/storage'; + +import store from '../store'; + +const EVENT_PREFIX = 'addItemsSuscribe'; +const SUBSCRIBE_START_EVENT = `${EVENT_PREFIX}:start`; +const SUBSCRIBE_ERROR_EVENT = `${EVENT_PREFIX}:error`; +const SUBSCRIBE_SUCCESS_EVENT = `${EVENT_PREFIX}:success`; + +const registerAddItemsSubscribeEvents = () => { + ipcRenderer.on(SUBSCRIBE_SUCCESS_EVENT, (event, payload) => { + // eslint-disable-next-line no-console + console.log('upload completed: ', payload); + }); + + ipcRenderer.on(SUBSCRIBE_ERROR_EVENT, (event, payload) => { + // eslint-disable-next-line no-console + console.error('Error on adding item: ', payload); + store.dispatch({ + payload, + type: SET_UPLOAD_ERROR_STATE, + }); + }); +}; + +export const addItems = (payload) => { + ipcRenderer.send(SUBSCRIBE_START_EVENT, payload); +}; + +export default registerAddItemsSubscribeEvents; diff --git a/src/events/add-items.js b/src/events/add-items.js deleted file mode 100644 index b8404433..00000000 --- a/src/events/add-items.js +++ /dev/null @@ -1,31 +0,0 @@ -import { ipcRenderer } from 'electron'; -import { SET_UPLOAD_ERROR_STATE } from '@reducers/storage'; - -import store from '../store'; - -const EVENT_PREFIX = 'upload'; -const START_EVENT = `${EVENT_PREFIX}:start`; -const SUCCESS_EVENT = `${EVENT_PREFIX}:success`; -const ERROR_EVENT = `${EVENT_PREFIX}:error`; - -const registerUploadEvents = () => { - ipcRenderer.on(SUCCESS_EVENT, () => { - // eslint-disable-next-line no-console - console.log('upload completed'); - }); - - ipcRenderer.on(ERROR_EVENT, (event, payload) => { - // eslint-disable-next-line no-console - console.error('Error on AddItems call: ', payload); - store.dispatch({ - payload, - type: SET_UPLOAD_ERROR_STATE, - }); - }); -}; - -export const addItems = (payload) => { - ipcRenderer.send(START_EVENT, payload); -}; - -export default registerUploadEvents; diff --git a/src/events/index.js b/src/events/index.js index 32d99e33..ce3025a8 100644 --- a/src/events/index.js +++ b/src/events/index.js @@ -4,8 +4,8 @@ import registerConfigEvents from './config'; import registerObjectsEvents from './objects'; import registerPathInfoEvents from './path-info'; import registerGenerateKeyPairEvents from './generate-key-pair'; -import registerAddItemsEvents from './add-items'; import registerTxlSubscribeEvents from './txl-subscribe'; +import registerAddItemsSubscribeEvents from './add-items-subscribe'; const registerEvents = () => { registerShortcuts(); @@ -14,8 +14,8 @@ const registerEvents = () => { registerObjectsEvents(); registerPathInfoEvents(); registerGenerateKeyPairEvents(); - registerAddItemsEvents(); registerTxlSubscribeEvents(); + registerAddItemsSubscribeEvents(); }; export default registerEvents; @@ -23,4 +23,4 @@ export * from './config'; export * from './objects'; export * from './path-info'; export * from './generate-key-pair'; -export * from './add-items'; +export * from './add-items-subscribe';