Skip to content

Commit

Permalink
feat: disable subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jul 1, 2024
1 parent 8588f5d commit 4b29d85
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 103 deletions.
1 change: 1 addition & 0 deletions src/lib/ApiConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const ApiConnection = ({ children }) => (
const wsLink = new WebSocketLink({
uri: publicRuntimeConfig.GRAPHQL_API.replace(/https/, 'wss').replace(/http/, 'ws'),
options: {
lazy: (publicRuntimeConfig.DISABLE_SUBSCRIPTIONS.toLowerCase() === 'true'),
reconnect: true,
connectionParams: {
authToken: auth.apiToken,
Expand Down
84 changes: 44 additions & 40 deletions src/pages/backups.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,50 +117,54 @@ export const PageBackups = ({ router }) => {
);
}

subscribeToMore({
document: BackupsSubscription,
variables: { environment: environment.id },
updateQuery: (prevStore, { subscriptionData }) => {
if (!subscriptionData.data) return prevStore;
const prevBackups = prevStore.environment.backups;
const incomingBackup = subscriptionData.data.backupChanged;
const existingIndex = prevBackups.findIndex(prevBackup => prevBackup.id === incomingBackup.id);
let newBackups;

// New backup.
if (existingIndex === -1) {
// Don't add new deleted backups.
if (incomingBackup.deleted !== '0000-00-00 00:00:00') {
return prevStore;
if (!(publicRuntimeConfig.DISABLE_SUBSCRIPTIONS.toLowerCase() === 'true')) {
subscribeToMore({
document: BackupsSubscription,
variables: { environment: environment.id },
updateQuery: (prevStore, { subscriptionData }) => {
if (!subscriptionData.data) return prevStore;
const prevBackups = prevStore.environment.backups;
const incomingBackup = subscriptionData.data.backupChanged;
const existingIndex = prevBackups.findIndex(prevBackup => prevBackup.id === incomingBackup.id);
let newBackups;

// New backup.
if (existingIndex === -1) {
// Don't add new deleted backups.
if (incomingBackup.deleted !== '0000-00-00 00:00:00') {
return prevStore;
}

newBackups = [incomingBackup, ...prevBackups];
}

newBackups = [incomingBackup, ...prevBackups];
}
// Existing backup.
else {
// Updated backup
if (incomingBackup.deleted === '0000-00-00 00:00:00') {
newBackups = Object.assign([...prevBackups], {
[existingIndex]: incomingBackup,
});
}
// Deleted backup
// Existing backup.
else {
newBackups = R.remove(existingIndex, 1, prevBackups);
// Updated backup
if (incomingBackup.deleted === '0000-00-00 00:00:00') {
newBackups = Object.assign([...prevBackups], {
[existingIndex]: incomingBackup,
});
}
// Deleted backup
else {
newBackups = R.remove(existingIndex, 1, prevBackups);
}
}
}

const newStore = {
...prevStore,
environment: {
...prevStore.environment,
backups: newBackups,
},
};

return newStore;
},
});
const newStore = {
...prevStore,
environment: {
...prevStore.environment,
backups: newBackups,
},
};

return newStore;
},
});
} else {
subscribeToMore()
}

return (
<>
Expand Down
67 changes: 35 additions & 32 deletions src/pages/deployments.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,39 +118,42 @@ export const PageDeployments = ({ router }) => {
/>
);
}
if (!(publicRuntimeConfig.DISABLE_SUBSCRIPTIONS.toLowerCase() === 'true')) {
subscribeToMore({
document: DeploymentsSubscription,
variables: { environment: environment.id },
updateQuery: (prevStore, { subscriptionData }) => {
if (!subscriptionData.data) return prevStore;
const prevDeployments = prevStore.environment.deployments;
const incomingDeployment = subscriptionData.data.deploymentChanged;
const existingIndex = prevDeployments.findIndex(prevDeployment => prevDeployment.id === incomingDeployment.id);
let newDeployments;

// New deployment.
if (existingIndex === -1) {
newDeployments = [incomingDeployment, ...prevDeployments];
}
// Updated deployment
else {
newDeployments = Object.assign([...prevDeployments], {
[existingIndex]: incomingDeployment,
});
}

subscribeToMore({
document: DeploymentsSubscription,
variables: { environment: environment.id },
updateQuery: (prevStore, { subscriptionData }) => {
if (!subscriptionData.data) return prevStore;
const prevDeployments = prevStore.environment.deployments;
const incomingDeployment = subscriptionData.data.deploymentChanged;
const existingIndex = prevDeployments.findIndex(prevDeployment => prevDeployment.id === incomingDeployment.id);
let newDeployments;

// New deployment.
if (existingIndex === -1) {
newDeployments = [incomingDeployment, ...prevDeployments];
}
// Updated deployment
else {
newDeployments = Object.assign([...prevDeployments], {
[existingIndex]: incomingDeployment,
});
}

const newStore = {
...prevStore,
environment: {
...prevStore.environment,
deployments: newDeployments,
},
};

return newStore;
},
});
const newStore = {
...prevStore,
environment: {
...prevStore.environment,
deployments: newDeployments,
},
};

return newStore;
},
});
} else {
subscribeToMore()
}

return (
<>
Expand Down
66 changes: 35 additions & 31 deletions src/pages/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,38 +120,42 @@ export const PageTasks = ({ router, renderAddTasks }) => {
);
}

subscribeToMore({
document: TasksSubscription,
variables: { environment: environment.id },
updateQuery: (prevStore, { subscriptionData }) => {
if (!subscriptionData.data) return prevStore;
const prevTasks = prevStore.environment.tasks;
const incomingTask = subscriptionData.data.taskChanged;
const existingIndex = prevTasks.findIndex(prevTask => prevTask.id === incomingTask.id);
let newTasks;

// New task.
if (existingIndex === -1) {
newTasks = [incomingTask, ...prevTasks];
}
// Updated task
else {
newTasks = Object.assign([...prevTasks], {
[existingIndex]: incomingTask,
});
}

const newStore = {
...prevStore,
environment: {
...prevStore.environment,
tasks: newTasks,
},
};
if (!(publicRuntimeConfig.DISABLE_SUBSCRIPTIONS.toLowerCase() === 'true')) {
subscribeToMore({
document: TasksSubscription,
variables: { environment: environment.id },
updateQuery: (prevStore, { subscriptionData }) => {
if (!subscriptionData.data) return prevStore;
const prevTasks = prevStore.environment.tasks;
const incomingTask = subscriptionData.data.taskChanged;
const existingIndex = prevTasks.findIndex(prevTask => prevTask.id === incomingTask.id);
let newTasks;

// New task.
if (existingIndex === -1) {
newTasks = [incomingTask, ...prevTasks];
}
// Updated task
else {
newTasks = Object.assign([...prevTasks], {
[existingIndex]: incomingTask,
});
}

return newStore;
},
});
const newStore = {
...prevStore,
environment: {
...prevStore.environment,
tasks: newTasks,
},
};

return newStore;
},
});
} else {
subscribeToMore()
}

return (
<>
Expand Down

0 comments on commit 4b29d85

Please sign in to comment.