All files / state/src/effect transfer.ts

100% Statements 28/28
87.5% Branches 7/8
100% Functions 4/4
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 291x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 5x 5x 5x 5x 1x  
import { transferHandlers } from 'comlink';
import { Effect } from './effect';
 
/**
 * Custom addition to the {@link transferHandlers}, allowing values of type
 * {@link Store} to be transparently transferred between endpoints.
 */
transferHandlers.set('effect', {
  canHandle: (value: unknown): value is typeof Effect => {
    return (value as Function | undefined)?.prototype instanceof Effect;
  },
  deserialize: (value: string) => {
    // @ts-expect-error missing implementation
    const effect = class extends Effect {};
 
    Object.defineProperty(effect.prototype, 'function', {
      // eslint-disable-next-line @typescript-eslint/no-implied-eval
      value: new Function(`return (${value});`)()
    });
 
    return effect;
  },
  serialize: (value: typeof Effect) => {
    const method = value.prototype.function.toString();
    const effect = method.replace(/^[^(]+/, 'function');
    return [effect, []];
  }
});