Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring getAppInstanceResources Listener #189

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions public/app/listeners/getAppInstanceResources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const GET_APP_INSTANCE_RESOURCES_CHANNEL = require('../config/channels');

const getAppInstanceResources = (mainWindow, db) => async (data = {}) => {
const defaultResponse = [];
const { userId, appInstanceId, spaceId, subSpaceId, type } = data;
try {
// tools live on the parent
const tool = spaceId === subSpaceId;

let appInstanceResourcesHandle;

// if not a tool, we need to go one step further into the phase
if (!tool) {
appInstanceResourcesHandle = db
.get('spaces')
.find({ id: spaceId })
.get('phases')
.find({ id: subSpaceId })
.get('items')
.filter(item => item.appInstance)
.map(item => item.appInstance)
.find({ id: appInstanceId })
.get('resources');
} else {
appInstanceResourcesHandle = db
.get('spaces')
.find({ id: spaceId })
.get('items')
.filter(item => item.appInstance)
.map(item => item.appInstance)
.find({ id: appInstanceId })
.get('resources');
}

// only filter by type if provided
if (type) {
appInstanceResourcesHandle.filter({ type });
}

// only filter by user if provided
if (userId) {
appInstanceResourcesHandle.filter({ user: userId });
}

const appInstanceResources = appInstanceResourcesHandle.value();

const response = appInstanceResources || defaultResponse;

// response is sent back to channel specific for this app instance
mainWindow.webContents.send(
`${GET_APP_INSTANCE_RESOURCES_CHANNEL}_${appInstanceId}`,
{
appInstanceId,
payload: response,
}
);
} catch (e) {
console.error(e);
// error is sent back to channel specific for this app instance
mainWindow.webContents.send(
`${GET_APP_INSTANCE_RESOURCES_CHANNEL}_${appInstanceId}`,
{
appInstanceId,
payload: defaultResponse,
}
);
}
};

module.exports = getAppInstanceResources;
2 changes: 2 additions & 0 deletions public/app/listeners/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const getDeveloperMode = require('./getDeveloperMode');
const setDeveloperMode = require('./setDeveloperMode');
const clearUserInput = require('./clearUserInput');
const showClearUserInputPrompt = require('./showClearUserInputPrompt');
const getAppInstanceResources = require('./getAppInstanceResources');

module.exports = {
loadSpace,
Expand All @@ -40,4 +41,5 @@ module.exports = {
setDeveloperMode,
clearUserInput,
showClearUserInputPrompt,
getAppInstanceResources,
};
71 changes: 5 additions & 66 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const {
setDeveloperMode,
clearUserInput,
showClearUserInputPrompt,
getAppInstanceResources,
} = require('./app/listeners');
const isMac = require('./app/utils/isMac');

Expand Down Expand Up @@ -362,72 +363,10 @@ app.on('ready', async () => {
);

// called when getting AppInstanceResources
ipcMain.on(GET_APP_INSTANCE_RESOURCES_CHANNEL, (event, data = {}) => {
const defaultResponse = [];
const { userId, appInstanceId, spaceId, subSpaceId, type } = data;
try {
// tools live on the parent
const tool = spaceId === subSpaceId;

let appInstanceResourcesHandle;

// if not a tool, we need to go one step further into the phase
if (!tool) {
appInstanceResourcesHandle = db
.get('spaces')
.find({ id: spaceId })
.get('phases')
.find({ id: subSpaceId })
.get('items')
.filter(item => item.appInstance)
.map(item => item.appInstance)
.find({ id: appInstanceId })
.get('resources');
} else {
appInstanceResourcesHandle = db
.get('spaces')
.find({ id: spaceId })
.get('items')
.filter(item => item.appInstance)
.map(item => item.appInstance)
.find({ id: appInstanceId })
.get('resources');
}

// only filter by type if provided
if (type) {
appInstanceResourcesHandle.filter({ type });
}

// only filter by user if provided
if (userId) {
appInstanceResourcesHandle.filter({ user: userId });
}

const appInstanceResources = appInstanceResourcesHandle.value();

const response = appInstanceResources || defaultResponse;

// response is sent back to channel specific for this app instance
mainWindow.webContents.send(
`${GET_APP_INSTANCE_RESOURCES_CHANNEL}_${appInstanceId}`,
{
appInstanceId,
payload: response,
}
);
} catch (e) {
console.error(e);
// error is sent back to channel specific for this app instance
mainWindow.webContents.send(
`${GET_APP_INSTANCE_RESOURCES_CHANNEL}_${appInstanceId}`,
{
appInstanceId,
payload: defaultResponse,
}
);
}
});
ipcMain.on(
GET_APP_INSTANCE_RESOURCES_CHANNEL,
getAppInstanceResources(mainWindow, db)
);

// called when creating an AppInstanceResource
ipcMain.on(POST_APP_INSTANCE_RESOURCE_CHANNEL, (event, payload = {}) => {
Expand Down