"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SharedObject = void 0;
const EventEmitter_1 = require("./EventEmitter");
class SharedObject {
    constructor(obj) {
        this._clients = [];
        this._obj = obj;
    }
    connect() {
        const proxy = new Proxy(this._obj, {
            get: (target, name) => {
                const value = target[name];
                if (typeof value == 'function') {
                    return (...args) => {
                        const r = value.call(target, ...args);
                        for (const c of this._clients) {
                            if (c !== client) {
                                c.emitter.emit(name, ...args);
                            }
                        }
                        return r;
                    };
                }
                return value;
            },
        });
        const emitter = new EventEmitter_1.EventEmitter_();
        const disconnect = () => {
            const i = this._clients.indexOf(client);
            this._clients.splice(i, 1);
        };
        const client = { proxy, emitter, disconnect };
        this._clients.push(client);
        return client;
    }
}
exports.SharedObject = SharedObject;
//# sourceMappingURL=SharedObject.js.map