All files ParentProxy.ts

0% Statements 0/61
0% Branches 0/19
0% Functions 0/19
0% Lines 0/60

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157                                                                                                                                                                                                                                                                                                                         
import { ProxyError } from "@hypernetlabs/objects";
import { errAsync, ResultAsync } from "neverthrow";
import Postmate from "postmate";
 
interface IIFrameCallData<T> {
  callId: number;
  data: T;
}
 
class IFrameCallData<T> implements IIFrameCallData<T> {
  constructor(public callId: number, public data: T) {}
}
 
class IFrameCall<T, E> {
  protected promise: Promise<T>;
  protected resolveFunc: ((result: T) => void) | null;
  protected rejectFunc: ((error: E) => void) | null;
 
  constructor(public callData: IIFrameCallData<any>) {
    this.resolveFunc = null;
    this.rejectFunc = null;
 
    this.promise = new Promise((resolve, reject) => {
      this.resolveFunc = resolve;
      this.rejectFunc = reject;
    });
  }
 
  public resolve(result: T): void {
    if (this.resolveFunc != null) {
      this.resolveFunc(result);
    }
  }
 
  public reject(error: E): void {
    if (this.rejectFunc != null) {
      this.rejectFunc(error);
    }
  }
 
  public getResult(): ResultAsync<T, E> {
    return ResultAsync.fromPromise(this.promise, (e) => {
      return e as E;
    });
  }
}
 
export abstract class ParentProxy {
  protected handshake: Postmate;
  protected child: Postmate.ParentAPI | null;
  protected callId = 0;
  protected calls: IFrameCall<any, any>[] = [];
  protected active: boolean;
 
  constructor(
    protected element: HTMLElement | null,
    protected iframeUrl: string,
    protected iframeName: string,
    protected debug: boolean = false,
  ) {
    this.child = null;
    this.active = false;
 
    if (element == null) {
      element = document.body;
    }
 
    Postmate.debug = debug;
    this.handshake = new Postmate({
      container: element,
      url: iframeUrl,
      name: iframeName,
      classListArray: ["hypernet-core-iframe-style"], // Classes to add to the iframe
    });
  }
 
  protected activateResult: ResultAsync<void, ProxyError> | undefined;
 
  public activate(): ResultAsync<void, ProxyError> {
    if (this.activateResult != null) {
      return this.activateResult;
    }
    this.activateResult = ResultAsync.fromPromise(
      this.handshake,
      (e) => new ProxyError("Proxy handshake failed in parent", e),
    ).map((child) => {
      // Stash the API for future calls
      this.child = child;
 
      child.on("callSuccess", (data: IIFrameCallData<any>) => {
        // Get the matching calls
        const matchingCalls = this.calls.filter((val) => {
          return val.callData.callId == data.callId;
        });
 
        // Remove the matching calls from the source array
        this.calls = this.calls.filter((val) => {
          return val.callData.callId != data.callId;
        });
 
        // Resolve the calls - should only ever be 1
        for (const call of matchingCalls) {
          call.resolve(data.data);
        }
      });
 
      child.on("callError", (data: IIFrameCallData<any>) => {
        // Get the matching calls
        const matchingCalls = this.calls.filter((val) => {
          return val.callData.callId == data.callId;
        });
 
        // Remove the matching calls from the source array
        this.calls = this.calls.filter((val) => {
          return val.callData.callId != data.callId;
        });
 
        // Reject the calls - should only ever be 1
        for (const call of matchingCalls) {
          call.reject(data.data);
        }
      });
 
      this.active = true;
    });
 
    return this.activateResult;
  }
 
  public destroy() {
    this.child?.destroy();
    this.active = false;
  }
 
  protected _createCall<T, E>(
    callName: string,
    data: any,
  ): ResultAsync<T, E | ProxyError> {
    if (!this.active) {
      return errAsync(
        new ProxyError(
          "Proxy is not activated or has been destroyed, cannot make a call to the iframe!",
        ),
      );
    }
    const callId = this.callId++;
    const callData = new IFrameCallData(callId, data);
 
    const call = new IFrameCall<T, E>(callData);
    this.calls.push(call);
 
    this.child?.call(callName, callData);
 
    return call.getResult();
  }
}