$(async function () {
    var liveDataRst;
    window.addEventListener(
        'flutterInAppWebViewPlatformReady',
        async function (event) {
            const data = await callDart('Dart_GetLiveData', 0);
            liveDataRst = JSON.parse(data);
            main(liveDataRst);
        },
    );

    async function main(rst) {
        startLive(rst, onBeforeReconnect);
    }

    async function onBeforeReconnect() {
        var result = await callDart('Dart_CheckLiveState', liveDataRst.DeviceCode);
        return result;
    }

    function callDart(method) {
        var args = Array.from(arguments).slice(1);
        return window.flutter_inappwebview.callHandler(method, ...args);
    }
});
