huridocs/uwazi

View on GitHub
app/api/queue.v2/infrastructure/specs/Signals.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
type Signal = { promise: Promise<void>; cbHolder: { finish: Function } };

function createRecord() {
  const cbHolder: { finish: Function } = { finish: () => {} };
  const promise = new Promise<void>(resolve => {
    cbHolder.finish = resolve;
  });

  return {
    promise,
    cbHolder,
  };
}

export function createSignals() {
  const jobSignals: Record<string, Signal> = {};

  return {
    signal(index: string) {
      if (!jobSignals[index]) {
        jobSignals[index] = createRecord();
      }

      jobSignals[index].cbHolder.finish();
    },

    async signaled(index: string) {
      if (!jobSignals[index]) {
        jobSignals[index] = createRecord();
      }

      return jobSignals[index].promise;
    },
  };
}