Last active
July 20, 2022 09:40
-
-
Save Gurey/8301022f8357b3a7d74cefca4b598646 to your computer and use it in GitHub Desktop.
Wait for Firebase emulators to finish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { client as Client, connection as Connection } from 'websocket' | |
export interface LogMessage { | |
type: string | |
utf8Data: string | |
} | |
export interface LogMessageContent { | |
level: string | |
data: Data | |
timestamp: number | |
message: string | |
} | |
export interface Data { | |
metadata: Metadata | |
} | |
export interface Metadata { | |
emulator: Emulator | |
function: Emulator | |
message: string | |
} | |
export interface Emulator { | |
name: string | |
} | |
let con: Connection | |
const listeners: (() => void)[] = [] | |
export function initFirebaseListener(waitTimeMs: number = 100) { | |
return new Promise((resolve) => { | |
if (con) { | |
resolve(true) | |
} | |
const socket = new Client() | |
socket.connect('ws://localhost:4500/') | |
socket.on('connect', (connection) => { | |
con = connection | |
let debounce: NodeJS.Timeout | |
con.on('message', (message) => { | |
const data = message as any as LogMessage | |
const messageContent = JSON.parse(data.utf8Data) as LogMessageContent | |
if (messageContent.message.startsWith('Beginning execution of "')) { | |
if (debounce) { | |
clearTimeout(debounce) | |
} | |
} else if (messageContent.message.startsWith('Finished "')) { | |
debounce = setTimeout( | |
() => listeners.forEach((listener) => listener()), | |
waitTimeMs | |
) | |
} | |
}) | |
resolve(true) | |
}) | |
}) | |
} | |
export function closeSocket() { | |
con.close() | |
} | |
export function waitForFirebaseToFinish() { | |
return new Promise((resolve) => { | |
listeners.push(() => resolve(true)) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment