github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/deck/static/spyglass/common.ts (about)

     1  export interface BaseMessage {
     2    type: string;
     3  }
     4  
     5  export function isBaseMessage(data: any): data is BaseMessage {
     6    return typeof data.type === 'string';
     7  }
     8  
     9  export interface ContentUpdatedMessage extends BaseMessage {
    10    type: 'contentUpdated';
    11    height: number;
    12  }
    13  
    14  export interface RequestMessage extends BaseMessage {
    15    type: 'request';
    16    data: string;
    17  }
    18  
    19  export interface RequestPageMessage extends BaseMessage {
    20    type: 'requestPage';
    21    data: string;
    22  }
    23  
    24  export interface UpdatePageMessage extends BaseMessage {
    25    type: 'updatePage';
    26    data: string;
    27  }
    28  
    29  export interface UpdateHash extends BaseMessage {
    30    type: 'updateHash';
    31    hash: string;
    32  }
    33  
    34  export interface ShowOffset extends BaseMessage {
    35    type: 'showOffset';
    36    top: number;
    37    left: number;
    38  }
    39  
    40  export interface Response extends BaseMessage {
    41    type: 'response';
    42    data: string;
    43  }
    44  
    45  export function isResponse(data: any): data is Response {
    46    return isBaseMessage(data) && data.type === 'response';
    47  }
    48  
    49  export type Message = ContentUpdatedMessage | RequestMessage | RequestPageMessage | UpdatePageMessage | UpdateHash | ShowOffset | Response;
    50  
    51  export interface TransitMessage {
    52    id: number;
    53    message: Message;
    54  }
    55  
    56  export function isTransitMessage(data: any): data is TransitMessage {
    57    return typeof data.id === 'number' && data.message && typeof data.message.type === 'string';
    58  }
    59  
    60  export function isUpdateHashMessage(data: any): data is UpdateHash {
    61    return isBaseMessage(data) && data.type === 'updateHash';
    62  }
    63  
    64  export function serialiseHashes(hashes: {[index: string]: string}): string {
    65    return Object.keys(hashes).map((i) => `${i}:${escape(hashes[i].substr(1))}`).join(';');
    66  }