<commit_before>/* -*- js2-basic-offset: 2; indent-tabs-mode: nil; -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80; */
"use strict";


require("sockjs");
let Sock = window.SockJS,
    Genfun = require("genfun"),
    {addMethod} = Genfun,
    {clone, init} = require("./proto"),
    {partial, forEach, contains, without} = require("lodash");

let SocketConn = clone(),
    onOpen = new Genfun(),
    onMessage = new Genfun(),
    onClose = new Genfun();

addMethod(init, [SocketConn], function(conn, url) {
  conn.url = url;
  conn.observers = {};
  conn.socket = new Sock(url);
  conn.socket.onopen = partial(notifyObservers, conn, onOpen);
  conn.socket.onmessage = partial(notifyObservers, conn, onMessage);
  conn.socket.onclose = partial(notifyObservers, conn, onClose);
});

addMethod(onOpen, [], function() {});
addMethod(onMessage, [], function() {});
addMethod(onClose, [], function() {});

function notifyObservers(conn, handler, msg) {
  if (msg.type === "message") {
    let data = JSON.parse(msg.data),
        observers = conn.observers[data.namespace] ||
          (console.warn("Unknown namespace: ", data.namespace),
           []);
    forEach(observers, function(obs) {
      return handler.call(conn, obs, data.data);
    });
  } else {
    forEach(conn.observers, function(arr) {
      forEach(arr, function(obs) {
        return handler.call(conn, obs);
      });
    });
  }<commit_msg>Set connection state in sockConn for observability<commit_after>/* -*- js2-basic-offset: 2; indent-tabs-mode: nil; -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80; */
"use strict";


require("sockjs");
let Sock = window.SockJS,
    Genfun = require("genfun"),
    {addMethod} = Genfun,
    {clone, init} = require("./proto"),
    {partial, forEach, contains, without} = require("lodash");

let SocketConn = clone(),
    onOpen = new Genfun(),
    onMessage = new Genfun(),
    onClose = new Genfun();

addMethod(init, [SocketConn], function(conn, url) {
  conn.url = url;
  conn.state = can.compute("close");
  conn.observers = {};
  conn.socket = new Sock(url);
  conn.socket.onopen = partial(notifyObservers, conn, onOpen);
  conn.socket.onmessage = partial(notifyObservers, conn, onMessage);
  conn.socket.onclose = partial(notifyObservers, conn, onClose);
});

addMethod(onOpen, [], function() {});
addMethod(onMessage, [], function() {});
addMethod(onClose, [], function() {});

function notifyObservers(conn, handler, msg) {
  if (msg.type === "message") {
    let data = JSON.parse(msg.data),
        observers = conn.observers[data.namespace] ||
          (console.warn("Unknown namespace: ", data.namespace),
           []);
    forEach(observers, function(obs) {
      return handler.call(conn, obs, data.data);
    });
  } else {
    conn.state(msg.type);
    forEach(conn.observers, function(arr) {
      forEach(arr, function(obs) {
        return handler.call(conn, obs);
      });
    });
  }
{'fraction_non_alphanumeric': 0.17761806981519507, 'fraction_numerical': 0.004106776180698152, 'mean_word_length': 3.617693522906793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2246360', 'n_tokens_mistral': 961, 'n_tokens_neox': 961, 'n_words': 207}