aboutsummaryrefslogtreecommitdiffstats
path: root/src/electron/ipc-api/sessionStorage.ts
blob: 39e84d42bf78bbd9da7bfa1dba1148051517b460 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { ipcMain, Session, session } from 'electron';

import { TODOS_PARTITION_ID } from '../../config';

const debug = require('../../preload-safe-debug')(
  'Ferdium:ipcApi:sessionStorage',
);

function deduceSession(serviceId: string | undefined | null): Session {
  if (serviceId) {
    return session.fromPartition(
      serviceId === TODOS_PARTITION_ID
        ? TODOS_PARTITION_ID
        : `persist:service-${serviceId}`,
    );
  }
  return session.defaultSession;
}

export default async () => {
  ipcMain.on('clear-storage-data', (_event, { serviceId, targetsToClear }) => {
    try {
      const serviceSession = deduceSession(serviceId);
      serviceSession.flushStorageData();
      if (targetsToClear) {
        debug('Clearing targets:', targetsToClear);
        serviceSession.clearStorageData(targetsToClear);
      } else {
        debug('Clearing all targets');
        serviceSession.clearStorageData();
      }
    } catch (error) {
      debug(error);
    }
  });

  ipcMain.handle('clear-cache', (_event, { serviceId }) => {
    const serviceSession = deduceSession(serviceId);
    return serviceSession.clearCache();
  });
};