"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createClientConnection = void 0;
const events_1 = require("events");
function createClientConnection(url) {
    if (typeof EventSource === 'undefined') {
        throw new Error(`Because of typeof EventSource === 'undefined', So materials hot updating feature is disabled`);
    }
    else {
        const source = new EventSource(url);
        const evtEmitter = new events_1.EventEmitter();
        source.addEventListener('message', function (ev) {
            let data = {};
            try {
                data = JSON.parse(ev.data);
                console.log('data', data);
                evtEmitter.emit('message', data);
            }
            catch (e) { }
        });
        return {
            addHandler: (handler) => {
                evtEmitter.on('message', handler);
                return () => {
                    evtEmitter.off('message', handler);
                };
            },
            close: () => {
                source.close();
                evtEmitter.removeAllListeners();
            }
        };
    }
}
exports.createClientConnection = createClientConnection;
//# sourceMappingURL=sse.js.map