Sha256: 33b938a3e87893ca4dea7cc7da522a56f28b35475d9b665ad3043297573db3cb

Contents?: true

Size: 1.23 KB

Versions: 5

Compression:

Stored size: 1.23 KB

Contents

var queue = [];
var current = null;
var identifier = JSON.stringify({ channel: '<%= channel %>' });
var socket = new WebSocket("ws://" + window.location.hostname + "/<%= url %>");

// TOOD: This is a temporary implementation
socket.onopen = function(event) {
  const msg = { command: 'subscribe', identifier: identifier };
  socket.send(JSON.stringify(msg));
};

socket.onmessage = function(event) {
  const msg = JSON.parse(event.data);
  if (msg.type === "ping") {
    return;
  }

  if (msg.type === "confirm_subscription") {
    execute_next();
    return;
  }

  if (msg.message && current) {
    current.resolve(msg.message.result);
    current = null;
    execute_next();
  } else {
    console.dir(msg);
  }
};

function execute_next() {
  if (socket.readyState != '1' || queue.length === 0 || current) {
    return;
  }

  current = queue.shift();
  socket.send(JSON.stringify({
    command: 'message',
    identifier: identifier,
    data: JSON.stringify({ action: 'execute', ...current.data }),
  }));
}

function graphQLFetcher(graphQLParams) {
  var resolve;
  var promise = new Promise((success) => {
    resolve = success;
  });

  var item = { data: graphQLParams, promise, resolve };

  queue.push(item);
  execute_next();
  return promise;
};

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails-graphql-1.0.2 lib/rails/graphql/railties/app/views/_cable.js.erb
rails-graphql-1.0.1 lib/rails/graphql/railties/app/views/_cable.js.erb
rails-graphql-1.0.0 lib/rails/graphql/railties/app/views/_cable.js.erb
rails-graphql-1.0.0.rc2 lib/rails/graphql/railties/app/views/_cable.js.erb
rails-graphql-1.0.0.rc1 lib/rails/graphql/railties/app/views/_cable.js.erb