Skip to content

Commit

Permalink
Merge pull request #60 from FleekHQ/giancarlo/add-items-stream
Browse files Browse the repository at this point in the history
Add items stream
  • Loading branch information
giankotarola authored Jun 10, 2020
2 parents d65f568 + 92c4137 commit 3c1113d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 69 deletions.
12 changes: 4 additions & 8 deletions public/client/space.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}

Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions public/events/add-items-subscribe.js
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 0 additions & 25 deletions public/events/add-items.js

This file was deleted.

4 changes: 2 additions & 2 deletions public/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -13,8 +13,8 @@ const registerEvents = (mainWindow) => {
registerConfigEvents(mainWindow);
registerObjectsEvents(mainWindow);
registerPathInfoEvents(mainWindow);
registerAddItemsSubscribe(mainWindow);
registerGenerateKeyPairEvents(mainWindow);
registerAddItemsEvents(mainWindow);

return () => {
stream.destroy();
Expand Down
31 changes: 31 additions & 0 deletions src/events/add-items-subscribe.js
Original file line number Diff line number Diff line change
@@ -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;
31 changes: 0 additions & 31 deletions src/events/add-items.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -14,13 +14,13 @@ const registerEvents = () => {
registerObjectsEvents();
registerPathInfoEvents();
registerGenerateKeyPairEvents();
registerAddItemsEvents();
registerTxlSubscribeEvents();
registerAddItemsSubscribeEvents();
};

export default registerEvents;
export * from './config';
export * from './objects';
export * from './path-info';
export * from './generate-key-pair';
export * from './add-items';
export * from './add-items-subscribe';

0 comments on commit 3c1113d

Please sign in to comment.