<commit_before>'use strict';

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
      define(['can/util/library', 'can/model', 'can/map', 'feathers-websocket-client'], factory);
  } else if (typeof exports === 'object') {
      module.exports = factory(require('can/util/library'), require('can/model'), require('can/map'), require('feathers-websocket-client'));
  } else if(typeof window !== 'undefined' && root && root.can && root.FeathersClient) {
      factory(root.can, root.can.Model, root.can.Map, root.FeathersClient);
  }
}(this, function (can, Model, Map, feathersClient) {
  // The socket will be set up here to respond to changes.
  var connection = new Map({
    ready:new can.Deferred()
  });

  // Creates a resource event handler function for a given event
  var makeHandler = function(ev) {
    var self = this;
    return function(data) {
      var id = data[self.id];
      // Check if this id is locked and contains e.g. a 'created'
      // Which means that we are getting the duplicate event from the
      // Socket remove the lock but don't dispatch the event
      if(self._locks[id] === ev + 'd') {
        delete self._locks[id];
        // If we are currently updating or removing we ignore all
        // other resource events for this model instance
      } else if(self._locks[id] !== ev) {
        // Mapping from CanJS to Feathers event name
        var modelEvent = ev === 'remove' ? 'destroyed' : ev + 'd';

        // Don't trigger the create event on the model if it's
        // already in the model store, but still trigger other events.
        if (!(ev === 'create' && self.store[id])) {
          // Trigger the event from the resource as a model event
          var model = self.model(data);
          if(model[modelEvent]) {
            model[modelEvent]();
          }
        }
      }
    };
  };



  var SocketModel = Model.extend({
    // Here we store locks that prevent dispatching
    // created, updated and removed events multiple times
    _locks: {},
    connect: can.Deferred(),

    setup: function () {
      var self = this;
      Model.setup.apply(this, arguments);

      // Resource is required.
      if (!this.resource) {
        return false;
      }

      var socket = connection.attr('socket');

      // Create the Feathers client for this model.
      this.client = feathersClient(this.resource, socket);

      this.client.on('created', makeHandler.call(self, 'create'));
      this.client.on('updated', makeHandler.call(self, 'update'));
      this.client.on('removed', makeHandler.call(self, 'remove'));

      // Processes request for create, update, and destroy
      this.send = function () {
        var self = this,
          deferred = can.Deferred(),
          args = can.makeArray(arguments),
          name = args.shift();

        // Name doesn't use id.
        if (name == 'create') {
          args.splice(0, 1);
        }

        // Add the success callback which just resolves or fails the Deferred
        args.push(function (error, data) {
          if (error) {
            return deferred.reject(error);
          }
          self._locks[data[self.id]] = name + 'd';
          deferred.resolve(data);
        });

        // Send the request.
        this.client[name].apply(this.client, args);

        return deferred;
      }


      this.create = function (attrs, params) {
        return this.send('create', null, attrs, params || {});
      },

      this.update = function (id, attrs, params) {
        this._locks[id] = 'update';
        return this.send('update', id, attrs, params || {});
      },

      this.destroy = function (id, attrs, params) {
        this._locks[id] = 'remove';
        return this.send('remove', id, params || {});
      }
    },

    // Uses this.makeFind() to create the Model's findAll().
    makeFindAll: function(){
      var self = this;
      return function(params, success, error){
        // A can.Deferred to send back.
        var def = can.Deferred();
        // Hook up success and error handlers
        def.then(success, error);
        // When the request comes back...
        self.client.find(params || {}, function(err, data){
          // If there was an error, reject the deferred.
          if (err) {
            def.reject(err);

          // Success, resolve the deferred with the data.
          } else {
            def.resolve(self.models(data));
          }
        });
        return def;
      };
    },

    // Uses this.makeFind() to create the Model's findOne().
    makeFindOne: function(){
      var self = this;
      return function(params, success, error){
        // A can.Deferred to send back.
        var def = can.Deferred();
        // Hook up success and error handlers
        def.then(success, error);
        // When the request comes back...
        self.client.get(params.id, params, function(err, data){
          // If there was an error, reject the deferred.
          if (err) {
            def.reject(err);

          // Success, resolve the deferred with the data.
          } else {
            def.resolve(self.model(data));
          }
        });
        return def;
      };
    }

  }, {});




  can.Feathers = {
    Model: SocketModel,

    model: function (resource) {
      return SocketModel.extend({
        resource: resource
      }, {});
    },

    connect: function(socket) {
      // socket is required.
      if (!socket) {
        console.error('You must pass a socket to can.Feathers.connect(socket).');
        return false;
      }
<commit_msg>Preparing for live-changing the socket.<commit_after>'use strict';

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
      define(['can/util/library', 'can/model', 'can/map', 'feathers-websocket-client'], factory);
  } else if (typeof exports === 'object') {
      module.exports = factory(require('can/util/library'), require('can/model'), require('can/map'), require('feathers-websocket-client'));
  } else if(typeof window !== 'undefined' && root && root.can && root.Feathers.client) {
      factory(root.can, root.can.Model, root.can.Map, root.Feathers.client);
  }
}(this, function (can, Model, Map, feathersClient) {

  // A list of all of the clients for every Model.
  var clients = [];

  var connection = new Map({
    // When the socket changes, all connected Models need
    // to be updated to use the new socket. This deferred
    // will be resolved once they have all updated.
    ready:new can.Deferred(),
    // The socket is set up here to respond to changes.
    socket:{}
  });

  // Creates a resource event handler function for a given event
  var makeHandler = function(ev) {
    var self = this;
    return function(data) {
      var id = data[self.id];

      // Check if this id is locked and contains e.g. a 'created'
      // Which means that we are getting the duplicate event from the
      // Socket remove the lock but don't dispatch the event
      if(self._locks[id] === ev + 'd') {
        delete self._locks[id];

      // If we are currently updating or removing we ignore all
      // other resource events for this model instance
      } else if(self._locks[id] !== ev) {

        // Mapping from CanJS to Feathers event name
        var modelEvent = ev === 'remove' ? 'destroyed' : ev + 'd';

        // Only trigger 'create' if the model isn't in the store already.
        if (!(ev === 'create' && self.store[id])) {

          // Trigger the event from the resource as a model event
          var model = self.model(data);
          if(model[modelEvent]) {
            model[modelEvent]();
          }
        }
      }
    };
  };


  var SocketModel = Model.extend({
    // Here we store locks that prevent dispatching
    // created, updated and removed events multiple times
    _locks: {},
    connect: can.Deferred(),

    setup: function () {
      var self = this;
      Model.setup.apply(this, arguments);

      // Resource is required.
      if (!this.resource) {
        return false;
      }

      connection.bind('socket', function(ev, socket){
        var socket = connection.attr('socket');
        console.log(ev);
        console.log(socket);
      });

      var socket = connection.attr('socket');

      // Create the Feathers client for this model.
      clients.push(this.client = feathersClient(this.resource, socket));

      this.client.on('created', makeHandler.call(self, 'create'));
      this.client.on('updated', makeHandler.call(self, 'update'));
      this.client.on('removed', makeHandler.call(self, 'remove'));

      // Processes request for create, update, and destroy
      this.send = function () {
        var self = this,
          deferred = can.Deferred(),
          args = can.makeArray(arguments),
          name = args.shift();

        // Name doesn't use id.
        if (name == 'create') {
          args.splice(0, 1);
        }

        // Add the success callback which just resolves or fails the Deferred
        args.push(function (error, data) {
          if (error) {
            return deferred.reject(error);
          }
          self._locks[data[self.id]] = name + 'd';
          deferred.resolve(data);
        });

        connection.attr('ready').resolve();

        connection.attr('ready').then(function(){
          // Send the request.
          self.client[name].apply(self.client, args);
        });

        return deferred;
      }

      this.create = function (attrs, params) {
        return this.send('create', null, attrs, params || {});
      },

      this.update = function (id, attrs, params) {
        this._locks[id] = 'update';
        return this.send('update', id, attrs, params || {});
      },

      this.destroy = function (id, attrs, params) {
        this._locks[id] = 'remove';
        return this.send('remove', id, params || {});
      }
    },

    // A utility function used by this.makeFindAll() and this.makeFindOne()
    makeFind:function(name){
      var self = this;
      return function(params, success, error){
        // A can.Deferred to send back.
        var def = can.Deferred();
        // The method to be used for converting to models.
        var method = 'models';
        // Add params to args.
        var args = [params || {}];

        // Add the callback to args.
        args[1] = function(err, data){
          if (err) {
            return def.reject(err);
          }
          def.resolve(self[method](data));
        };
        // Add the id for get requests.
        if (name === 'get') {
          args.unshift(params.id);
          method = 'model';
        }
        // Hook up success and error handlers
        def.then(success, error);

        connection.attr('ready').then(function(){
          // Send the request.
          self.client[name].apply(self.client, args);
        });
        return def;
      };
    },


    // Uses this.makeFind() to create the Model's findAll().
    makeFindAll: function(){
      return this.makeFind('find');
    },

    // Uses this.makeFind() to create the Model's findOne().
    makeFindOne: function(){
      return this.makeFind('get');
    }

  }, {});




  can.Feathers = {
    Model: SocketModel,

    model: function (resource) {
      return SocketModel.extend({
        resource: resource
      }, {});
    },

    connect: function(socket) {
      // socket is required.
      if (!socket) {
        console.error('You must pass a socket to can.Feathers.connect(socket).');
        return false;
      }

{'fraction_non_alphanumeric': 0.1359425058446619, 'fraction_numerical': 0.00043293791670274485, 'mean_word_length': 2.0775379696243004, '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': '2705949', 'n_tokens_mistral': 3292, 'n_tokens_neox': 3200, 'n_words': 992}