<commit_before>import Ember from 'ember-metal/core';

import merge from "ember-metal/merge";
import create from 'ember-metal/platform/create';
import { get } from "ember-metal/property_get";
import { set } from "ember-metal/property_set";
import {
  addObserver,
  removeObserver
} from "ember-metal/observer";
import Stream from "ember-metal/streams/stream";
import { isStream, read } from "ember-metal/streams/utils";

function KeyStream(source, key) {
  Ember.assert("KeyStream error: source must be a stream", isStream(source));
  Ember.assert("KeyStream error: key must be a non-empty string", typeof key === 'string' && key.length > 0);
  Ember.assert("KeyStream error: key must not have a '.'", key.indexOf('.') === -1);

  this.init();
  this.source = source;
  this.obj = undefined;
  this.key = key;
}

KeyStream.prototype = create(Stream.prototype);

merge(KeyStream.prototype, {
  compute() {
    var obj = read(this.source);
    if (obj) {
      return get(obj, this.key);
    }
  },

  becameActive() {
    if (this.obj && typeof this.obj === 'object') {
      addObserver(this.obj, this.key, this, this.notify);
    }

    if (nextObj) {
      return get(nextObj, this.key);
    }
  },

  setValue(value) {
    if (this.obj) {
      set(this.obj, this.key, value);
    }
  },

  setSource(nextSource) {
    Ember.assert("KeyStream error: source must be a stream", isStream(nextSource));

    var prevSource = this.source;

    if (nextSource !== prevSource) {
      this.update(function() {
        this.dependency.replace(nextSource);
        this.source = nextSource;
        this.obj = read(nextSource);
      });
    }

    this.notify();
  },

  _didChange: function() {
    this.notify();
  },

  _super$destroy: Stream.prototype.destroy,

  destroy() {
    if (this._super$destroy()) {
      if (isStream(this.source)) {
        this.source.unsubscribe(this._didChange, this);
      }

      if (this.obj && typeof this.obj === 'object') {
        removeObserver(this.obj, this.key, this, this._didChange);
      }

      this.source = undefined;
      this.obj = undefined;
      return true;
    }
  }
});

export default KeyStream;<commit_msg>Fix some bugs in KeyStream<commit_after>import Ember from 'ember-metal/core';

import merge from "ember-metal/merge";
import create from 'ember-metal/platform/create';
import { get } from "ember-metal/property_get";
import { set } from "ember-metal/property_set";
import {
  addObserver,
  removeObserver
} from "ember-metal/observer";
import Stream from "ember-metal/streams/stream";
import { isStream, read } from "ember-metal/streams/utils";

function KeyStream(source, key) {
  Ember.assert("KeyStream error: source must be a stream", isStream(source));
  Ember.assert("KeyStream error: key must be a non-empty string", typeof key === 'string' && key.length > 0);
  Ember.assert("KeyStream error: key must not have a '.'", key.indexOf('.') === -1);

  this.init();
  this.source = source;
  this.dependency = this.addDependency(source);
  this.observedObject = undefined;
  this.key = key;
}

KeyStream.prototype = create(Stream.prototype);

merge(KeyStream.prototype, {
  compute() {
    var object = read(this.source);
    if (object) {
      return get(object, this.key);
    }
  },

  becameActive() {
    var object = read(this.source);
    if (object && typeof object === 'object') {
      addObserver(object, this.key, this, this.notify);
      this.observedObject = object;
    }

  becameInactive() {
    if (this.observedObject) {
      removeObserver(this.observedObject, this.key, this, this.notify);
      this.observedObject = undefined;
    }
  },

  setValue(value) {
    var object = read(this.source);
    if (object) {
      set(object, this.key, value);
    }
  },

  setSource(nextSource) {
    Ember.assert("KeyStream error: source must be a stream", isStream(nextSource));

    var prevSource = this.source;

    if (nextSource !== prevSource) {
      this.update(function() {
        this.dependency.replace(nextSource);
        this.source = nextSource;
      });
    }

    this.notify();
  },

  _didChange: function() {
    this.notify();
  },

  _super$destroy: Stream.prototype.destroy,

  destroy() {
    if (this._super$destroy()) {
      if (isStream(this.source)) {
        this.source.unsubscribe(this._didChange, this);
      }

      if (this.obj && typeof this.obj === 'object') {
        removeObserver(this.obj, this.key, this, this._didChange);
      }

      this.source = undefined;
      return true;
    }
  }
});

export default KeyStream;
{'fraction_non_alphanumeric': 0.14812376563528637, 'fraction_numerical': 0.0008777704630239193, 'mean_word_length': 3.1248868778280543, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6029892', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1427, 'n_words': 331}