<commit_before>var assign = require("object-assign");
var SegmentTimeline = require("./SegmentTimeline");

var loaders = require("./loaders");
var rectClamp = require("rect-clamp");
var rectCrop = require("rect-crop");
var rectMix = require("rect-mix");
var BezierEasing = require("bezier-easing");

function SegmentKenBurns (renderChannel, data, diaporama) {
  SegmentTimeline.call(this, renderChannel, data);
  this.diaporama = diaporama;
  this.outOfSync = false;
  this.syncing = false;
  this.video = null;
  this.image = null;
  this.opts = assign({
    position: 0,
    volume: 0,
    playbackRate: 1,
    loop: false
  }, data);

  this.onVideoSeeked = this.onVideoSeeked.bind(this);
  this.onDiaporamaPause = this.onDiaporamaPause.bind(this);
  this.onDiaporamaPlay = this.onDiaporamaPlay.bind(this);
  this.onDiaporamaRateChange = this.onDiaporamaRateChange.bind(this);
  this.onDiaporamaSeeked = this.onDiaporamaSeeked.bind(this);
}

SegmentKenBurns.prototype = assign({}, SegmentTimeline.prototype, {
  toString: function () {
    return "SegmentKenBurns("+this.channel+")";
  },

  destroy: function () {
    if (this._res) this._res();
    this._res = null;
  },

  _load: function () {
    if (this._loaded) return;
    this._loaded = true;
    var self = this;
    var data = this.data;
    if (data.video) {
      this._res = loaders.video(data.video, function (result) {
        if (result.video) {
          var video = self.video = result.video;
          self.videoDuration = video.duration;
          self.videoSize = {
            width: video.videoWidth,
            height: video.videoHeight
          };
        }
        else if (result.image) {
          self.image = result.image;
        }
      });
    }
    else if (data.image) {
      this._res = loaders.image(data.image, function (img) {
        self.image = img;
      });
    }
  },

  ready: function () {
    this._load();
    return this.image || this.video;
  },

  getMediaSize: function () {
    return this.image || this.videoSize;
  },

  enter: function (ctx) {<commit_msg>Optimisation that don't load an image/video twice<commit_after>var assign = require("object-assign");
var SegmentTimeline = require("./SegmentTimeline");

var loaders = require("./loaders");
var rectClamp = require("rect-clamp");
var rectCrop = require("rect-crop");
var rectMix = require("rect-mix");
var BezierEasing = require("bezier-easing");
var hash = require("./loaders/hash");

var firstInstanceKey = <KEY>";

function SegmentKenBurns (renderChannel, data, diaporama) {
  SegmentTimeline.call(this, renderChannel, data);
  this.diaporama = diaporama;
  this.outOfSync = false;
  this.syncing = false;
  this.video = null;
  this.image = null;
  this.opts = assign({
    position: 0,
    volume: 0,
    playbackRate: 1,
    loop: false
  }, data);

  this.onVideoSeeked = this.onVideoSeeked.bind(this);
  this.onDiaporamaPause = this.onDiaporamaPause.bind(this);
  this.onDiaporamaPlay = this.onDiaporamaPlay.bind(this);
  this.onDiaporamaRateChange = this.onDiaporamaRateChange.bind(this);
  this.onDiaporamaSeeked = this.onDiaporamaSeeked.bind(this);

  this.resKey = hash(data.video || data.image);

  if (!diaporama[firstInstanceKey]) diaporama[firstInstanceKey] = {};
  if (!diaporama[firstInstanceKey][this.resKey]) {
    this.isFirstInstance = true;
    diaporama[firstInstanceKey][this.resKey] = true;
  }
}

SegmentKenBurns.prototype = assign({}, SegmentTimeline.prototype, {
  toString: function () {
    return "SegmentKenBurns("+this.channel+")";
  },

  destroy: function () {
    if (this.isFirstInstance) {
      // release the media, for the next time
      delete this.diaporama[firstInstanceKey][this.resKey];
    }
    else {
      // the resource is not from media, we can free it
      if (this._res) this._res();
    }
    this._res = null;
  },

  _computeVideoMeta: function () {
    var video = this.video;
    this.videoDuration = video.duration;
    this.videoSize = {
      width: video.videoWidth,
      height: video.videoHeight
    };
  },

  _load: function (ctx) {
    var self = this;
    var data = this.data;
    if (this._loaded) return;
    if (this.isFirstInstance) {
      var res = ctx.media.get(data.image || data.video);
      if (res) {
        if (data.image) this.image = res;
        else if (data.video) {
          if (res.video) {
            this.video = res.video;
            this._computeVideoMeta();
          }
          else if (res.image) this.image = res.image;
        }
        this._loaded = true;
      }
      return;
    }
    this._loaded = true;
    if (data.video) {
      this._res = loaders.video(data.video, function (result) {
        if (result.video) {
          self.video = result.video;
          self._computeVideoMeta();
        }
        else if (result.image) {
          self.image = result.image;
        }
      });
    }
    else if (data.image) {
      this._res = loaders.image(data.image, function (img) {
        self.image = img;
      });
    }
  },

  ready: function (ctx) {
    this._load(ctx);
    return this.image || this.video;
  },

  getMediaSize: function () {
    return this.image || this.videoSize;
  },

  enter: function (ctx) {
{'fraction_non_alphanumeric': 0.13855306083285357, 'fraction_numerical': 0.0011514104778353484, 'mean_word_length': 2.796067006554989, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '7183508', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1709, 'n_words': 348}