<commit_before>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

define(["nbextensions/widgets/widgets/js/manager",
        "underscore",
        "backbone",
        "jquery",
        "base/js/utils",
        "base/js/namespace",
], function(widgetmanager, _, Backbone, $, utils, IPython){
    "use strict";

    var unpack_models = function unpack_models(value, model) {
        /**
         * Replace model ids with models recursively.
         */
        var unpacked;
        if ($.isArray(value)) {
            unpacked = [];
            _.each(value, function(sub_value, key) {
                unpacked.push(unpack_models(sub_value, model));
            });
            return Promise.all(unpacked);
        } else if (value instanceof Object) {
            unpacked = {};
            _.each(value, function(sub_value, key) {
                unpacked[key] = unpack_models(sub_value, model);
            });
            return utils.resolve_promises_dict(unpacked);
        } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") {
            // get_model returns a promise already
            return model.widget_manager.get_model(value.slice(10, value.length));
        } else {
            return Promise.resolve(value);
        }
    };

    var WidgetModel = Backbone.Model.extend({
        constructor: function (widget_manager, model_id, comm) {
            /**
             * Constructor
             *
             * Creates a WidgetModel instance.
             *
             * Parameters
             * ----------
             * widget_manager : WidgetManager instance
             * model_id : string
             *      An ID unique to this model.
             * comm : Comm instance (optional)
             */
            this.widget_manager = widget_manager;
            this.state_change = Promise.resolve();
            this._buffered_state_diff = {};
            this.pending_msgs = 0;
            this.msg_buffer = null;
            this.state_lock = null;
            this.id = model_id;
            this.views = {};
            this._resolve_received_state = {};

            if (comm !== undefined) {
                // Remember comm associated with the model.
                this.comm = comm;
                comm.model = this;

                // Hook comm messages up to model.
                comm.on_close($.proxy(this._handle_comm_closed, this));
                comm.on_msg($.proxy(this._handle_comm_msg, this));

                // Assume the comm is alive.
                this.set_comm_live(true);
            } else {
                this.set_comm_live(false);
            }

            // Listen for the events that lead to the websocket being terminated.
            var that = this;
            var died = function() {
                that.set_comm_live(false);
            };
            widget_manager.notebook.events.on('kernel_disconnected.Kernel', died);
            widget_manager.notebook.events.on('kernel_killed.Kernel', died);
            widget_manager.notebook.events.on('kernel_restarting.Kernel', died);
            widget_manager.notebook.events.on('kernel_dead.Kernel', died);

            return Backbone.Model.apply(this);
        },<commit_msg>Workaround new backbone behavior.<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

define(["nbextensions/widgets/widgets/js/manager",
        "underscore",
        "backbone",
        "jquery",
        "base/js/utils",
        "base/js/namespace",
], function(widgetmanager, _, Backbone, $, utils, IPython){
    "use strict";

    var unpack_models = function unpack_models(value, model) {
        /**
         * Replace model ids with models recursively.
         */
        var unpacked;
        if ($.isArray(value)) {
            unpacked = [];
            _.each(value, function(sub_value, key) {
                unpacked.push(unpack_models(sub_value, model));
            });
            return Promise.all(unpacked);
        } else if (value instanceof Object) {
            unpacked = {};
            _.each(value, function(sub_value, key) {
                unpacked[key] = unpack_models(sub_value, model);
            });
            return utils.resolve_promises_dict(unpacked);
        } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") {
            // get_model returns a promise already
            return model.widget_manager.get_model(value.slice(10, value.length));
        } else {
            return Promise.resolve(value);
        }
    };

    var WidgetModel = Backbone.Model.extend({
        constructor: function (widget_manager, model_id, comm) {
            /**
             * Constructor
             *
             * Creates a WidgetModel instance.
             *
             * Parameters
             * ----------
             * widget_manager : WidgetManager instance
             * model_id : string
             *      An ID unique to this model.
             * comm : Comm instance (optional)
             */
            WidgetModel.__super__.constructor.apply(this);
            this.widget_manager = widget_manager;
            this.state_change = Promise.resolve();
            this._buffered_state_diff = {};
            this.pending_msgs = 0;
            this.msg_buffer = null;
            this.state_lock = null;
            this.id = model_id;

            // Force backbone to think that the model has already been
            // synced with the server.  As of backbone 1.1, backbone
            // ignore `patch` if it thinks the model has never been
            // pushed.
            this.isNew = function() { return false; };

            this.views = {};
            this._resolve_received_state = {};

            if (comm !== undefined) {
                // Remember comm associated with the model.
                this.comm = comm;
                comm.model = this;

                // Hook comm messages up to model.
                comm.on_close($.proxy(this._handle_comm_closed, this));
                comm.on_msg($.proxy(this._handle_comm_msg, this));

                // Assume the comm is alive.
                this.set_comm_live(true);
            } else {
                this.set_comm_live(false);
            }

            // Listen for the events that lead to the websocket being terminated.
            var that = this;
            var died = function() {
                that.set_comm_live(false);
            };
            widget_manager.notebook.events.on('kernel_disconnected.Kernel', died);
            widget_manager.notebook.events.on('kernel_killed.Kernel', died);
            widget_manager.notebook.events.on('kernel_restarting.Kernel', died);
            widget_manager.notebook.events.on('kernel_dead.Kernel', died);

            return Backbone.Model.apply(this);
        },
{'fraction_non_alphanumeric': 0.1083850483475249, 'fraction_numerical': 0.002020493577716842, 'mean_word_length': 1.6299810246679316, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1778159', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1772, 'n_words': 408}