<commit_before>///<reference path='refs.ts'/>
module TDev.RT {
    //? A web socket message
    //@ stem("msg") ctx(general) dbgOnly
    export class WebSocketMessage
        extends RTValue {
        private stringData:string;
        private binaryData:any;
        private err: string;

        constructor() {
            super()
        }

        static mk(data: any) {
            var msg = new WebSocketMessage();
            if (typeof data == "string")
                msg.stringData = data
            else
                msg.binaryData = data
            return msg;
        }

        static mkError(err: string) {
            var msg = new WebSocketMessage();
            msg.err = err;
            return msg;
        }

        //? Indicates if this message is an error
        public is_error(): boolean {
            return !!this.err;
        }

        //? Gets the error if any.
        public error(): string {
            return this.err;
        }

        //? Gets the message as a string
        public string(): string {
            return this.stringData;
        }

        private _json: JsonObject;
        //? Gets the message as a Json payload
        public json(): JsonObject {
            if (!this.stringData) return undefined
            if (!this._json) this._json = JsonObject.mk(this.stringData);
            return this._json;
        }

        //? Gets the message as a Buffer
        public buffer(): Buffer {
            if (this.binaryData instanceof Buffer) return this.binaryData
            if (this.binaryData instanceof ArrayBuffer) {
                this.binaryData = Buffer.fromTypedArray(new Uint8Array(this.binaryData))
                return this.binaryData
            }
            return undefined
        }

        public toString(): string {
            if (this.err) return "error: " + this.err;
            if (this.binaryData && this.binaryData.toString)
                return this.binaryData.toString()
            return JSON.stringify(this.stringData)
        }

        //? Displays the message on the wall
        public post_to_wall(s: IStackFrame): void {
            super.post_to_wall(s);
        }
    }

    //? A web socket
    //@ stem("ws") ctx(general) dbgOnly
    export class WebSocket_
        extends RTValue {

        private msgs = [];

        constructor (private ws: WebSocket, private rt: Runtime) {
            super()
        }

        static mk(ws: WebSocket, rt : Runtime) {
            var w = new WebSocket_(ws, rt);
            w.attachEvents()
            return w;
        }
<commit_msg>make websocket disposable<commit_after>///<reference path='refs.ts'/>
module TDev.RT {
    //? A web socket message
    //@ stem("msg") ctx(general) dbgOnly
    export class WebSocketMessage
        extends RTValue {
        private stringData:string;
        private binaryData:any;
        private err: string;

        constructor() {
            super()
        }

        static mk(data: any) {
            var msg = new WebSocketMessage();
            if (typeof data == "string")
                msg.stringData = data
            else
                msg.binaryData = data
            return msg;
        }

        static mkError(err: string) {
            var msg = new WebSocketMessage();
            msg.err = err;
            return msg;
        }

        //? Indicates if this message is an error
        public is_error(): boolean {
            return !!this.err;
        }

        //? Gets the error if any.
        public error(): string {
            return this.err;
        }

        //? Gets the message as a string
        public string(): string {
            return this.stringData;
        }

        private _json: JsonObject;
        //? Gets the message as a Json payload
        public json(): JsonObject {
            if (!this.stringData) return undefined
            if (!this._json) this._json = JsonObject.mk(this.stringData);
            return this._json;
        }

        //? Gets the message as a Buffer
        public buffer(): Buffer {
            if (this.binaryData instanceof Buffer) return this.binaryData
            if (this.binaryData instanceof ArrayBuffer) {
                this.binaryData = Buffer.fromTypedArray(new Uint8Array(this.binaryData))
                return this.binaryData
            }
            return undefined
        }

        public toString(): string {
            if (this.err) return "error: " + this.err;
            if (this.binaryData && this.binaryData.toString)
                return this.binaryData.toString()
            return JSON.stringify(this.stringData)
        }

        //? Displays the message on the wall
        public post_to_wall(s: IStackFrame): void {
            super.post_to_wall(s);
        }
    }

    //? A web socket
    //@ stem("ws") ctx(general) dbgOnly
    export class WebSocket_
        extends RTDisposableValue {

        private msgs = [];

        constructor (private ws: WebSocket, rt : Runtime) {
            super(rt)
        }

        public dispose() {
            this.close();
            super.dispose();
        }

        static mk(ws: WebSocket, rt : Runtime) {
            var w = new WebSocket_(ws, rt);
            w.attachEvents()
            return w;
        }

{'fraction_non_alphanumeric': 0.0944822373393802, 'fraction_numerical': 0.0003779289493575208, 'mean_word_length': 1.4873120300751879, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7343621', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1348, 'n_words': 397}