<commit_before>import Service, { inject as service } from '@ember/service';
import { htmlSafe } from '@ember/string';
import { tracked } from '@glimmer/tracking';

import { rawTimeout, task } from 'ember-concurrency';
import { buildWaiter } from 'ember-test-waiters';

const SPEED = 200;

let waiter = buildWaiter('progress-bar');

export default class ProgressService extends Service {
  @service router;

  count = 0;

  @tracked _style = '';

  get style() {
    return htmlSafe(this._style);
  }

  handle(thenable) {
    this.counterTask.perform(thenable);
  }

  increaseCounter() {
    this.count += 1;
    this.updateTask.perform();
  }

  decreaseCounter() {
    this.count -= 1;
  }

  @task(function* (promise) {
    try {
      this.increaseCounter();
      yield promise;
    } finally {
      this.decreaseCounter();
    }
  })
  counterTask;

  @(task(function* () {
    let token = waiter.beginAsync();

    let progress = 0;
    this._style = `width: 0%`;

    while (this.count !== 0) {
      yield rawTimeout(SPEED);

      let currentAmount;
      if (progress >= 0 && progress < 0.2) {
        currentAmount = 0.1;
      } else if (progress >= 0.2 && progress < 0.5) {
        currentAmount = 0.04;
      } else if (progress >= 0.5 && progress < 0.8) {
        currentAmount = 0.02;
      } else if (progress >= 0.8 && progress < 0.99) {
        currentAmount = 0.005;
      } else {
        currentAmount = 0;
      }

      progress += currentAmount;
      if (progress > 0.998) {
        progress = 0.998;
      }

      this._style = `transition: width ${SPEED}ms linear; width: ${progress * 100}%`;
    }

    this._style = `transition: width ${SPEED}ms linear; width: 100%`;
    yield rawTimeout(SPEED);
    this._style = `transition: opacity ${SPEED * 2}ms linear; width: 100%; opacity: 0`;
<commit_msg>services/progress: Use derived state instead of `count` property<commit_after>import Service, { inject as service } from '@ember/service';
import { htmlSafe } from '@ember/string';
import { tracked } from '@glimmer/tracking';

import { rawTimeout, task } from 'ember-concurrency';
import { buildWaiter } from 'ember-test-waiters';

const SPEED = 200;

let waiter = buildWaiter('progress-bar');

export default class ProgressService extends Service {
  @service router;

  @tracked _style = '';

  get style() {
    return htmlSafe(this._style);
  }

  handle(thenable) {
    this.counterTask.perform(thenable);
  }

  @task(function* (promise) {
    this.updateTask.perform();
    yield promise;
  })
  counterTask;

  @(task(function* () {
    let token = waiter.beginAsync();

    let progress = 0;
    this._style = `width: 0%`;

    while (this.counterTask.isRunning) {
      yield rawTimeout(SPEED);

      let currentAmount;
      if (progress >= 0 && progress < 0.2) {
        currentAmount = 0.1;
      } else if (progress >= 0.2 && progress < 0.5) {
        currentAmount = 0.04;
      } else if (progress >= 0.5 && progress < 0.8) {
        currentAmount = 0.02;
      } else if (progress >= 0.8 && progress < 0.99) {
        currentAmount = 0.005;
      } else {
        currentAmount = 0;
      }

      progress += currentAmount;
      if (progress > 0.998) {
        progress = 0.998;
      }

      this._style = `transition: width ${SPEED}ms linear; width: ${progress * 100}%`;
    }

    this._style = `transition: width ${SPEED}ms linear; width: 100%`;
    yield rawTimeout(SPEED);
    this._style = `transition: opacity ${SPEED * 2}ms linear; width: 100%; opacity: 0`;

{'fraction_non_alphanumeric': 0.14435919295254335, 'fraction_numerical': 0.0312588803637397, 'mean_word_length': 2.67816091954023, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '7625027', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1102, 'n_words': 292}