<commit_before>// Copyright (c) 2015, <NAME>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

library event_kit.base;

import 'dart:async';

import 'package:disposable/disposable.dart';

/// A utility class to be used when implementing event-based APIs that allows
/// for handlers registered via [on] to be invoked with calls to [emit].
/// Instances of this class are intended to be used internally by classes
/// that expose an event-based API.
class Emitter implements Disposable {
  final Map<String, StreamController> _emitterMap =
      new Map<String, StreamController>();

  bool _isDisposed = false;

  @override
  bool get isDisposed => _isDisposed;

  StreamController _getOrCreateEmitter(String eventName) {
    var emitter = _emitterMap[eventName];
    if (emitter == null) {
      emitter = new StreamController.broadcast(sync: true);
      _emitterMap[eventName] = emitter;
    }
    return emitter;
  }

  /// Registers the given event [handler] to be invoked whenever events by
  /// the given [eventName] are emitted via [emit].
  ///
  /// Returns a [Disposable] on which `.dispose()` can be called to unsubscribe.
  Disposable on(String eventName, Function handler) {
    StreamController emitter = _getOrCreateEmitter(eventName);
    final subscription = emitter.stream.listen((List arguments) {
      switch (arguments.length) {
        case 0:
          handler();
          break;
        case 1:
          handler(arguments[0]);
          break;
        case 2:
          handler(arguments[0], arguments[1]);
          break;
        case 3:
          handler(arguments[0], arguments[1], arguments[2]);
          break;
        case 4:
          handler(arguments[0], arguments[1], arguments[2], arguments[3]);
          break;
        case 5:
          handler(arguments[0], arguments[1], arguments[2], arguments[3],
              arguments[4]);
          break;
        default:
          throw new UnsupportedError('exceeded max arguments size');
      }
    });
    return new Disposable.from(subscription);
  }

  /// Invokes handlers registered via [on] for the given [eventname].
  ///
  /// Callbacks will be invoked with [arguments] as arguments.
  void emit(String eventName, [List arguments = const []]) {
    StreamController emitter = _getOrCreateEmitter(eventName);
    emitter.add(arguments);
  }

  /// Clears out any existing subscribers.
  void clear() {
    for (final emitter in _emitterMap.values) {
      emitter.close();
    }
    _emitterMap.clear();
  }

  @override
  void dispose() {
    _isDisposed = true;
    clear();
  }
}

<commit_msg>Fix a typo in the documentation.<commit_after>// Copyright (c) 2015, <NAME>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

library event_kit.base;

import 'dart:async';

import 'package:disposable/disposable.dart';

/// A utility class to be used when implementing event-based APIs that allows
/// for handlers registered via [on] to be invoked with calls to [emit].
/// Instances of this class are intended to be used internally by classes
/// that expose an event-based API.
class Emitter implements Disposable {
  final Map<String, StreamController> _emitterMap =
      new Map<String, StreamController>();

  bool _isDisposed = false;

  @override
  bool get isDisposed => _isDisposed;

  StreamController _getOrCreateEmitter(String eventName) {
    var emitter = _emitterMap[eventName];
    if (emitter == null) {
      emitter = new StreamController.broadcast(sync: true);
      _emitterMap[eventName] = emitter;
    }
    return emitter;
  }

  /// Registers the given event [handler] to be invoked whenever events by
  /// the given [eventName] are emitted via [emit].
  ///
  /// Returns a [Disposable] on which `.dispose()` can be called to unsubscribe.
  Disposable on(String eventName, Function handler) {
    StreamController emitter = _getOrCreateEmitter(eventName);
    final subscription = emitter.stream.listen((List arguments) {
      switch (arguments.length) {
        case 0:
          handler();
          break;
        case 1:
          handler(arguments[0]);
          break;
        case 2:
          handler(arguments[0], arguments[1]);
          break;
        case 3:
          handler(arguments[0], arguments[1], arguments[2]);
          break;
        case 4:
          handler(arguments[0], arguments[1], arguments[2], arguments[3]);
          break;
        case 5:
          handler(arguments[0], arguments[1], arguments[2], arguments[3],
              arguments[4]);
          break;
        default:
          throw new UnsupportedError('exceeded max arguments size');
      }
    });
    return new Disposable.from(subscription);
  }

  /// Invokes handlers registered via [on] for the given [eventName].
  ///
  /// Callbacks will be invoked with [arguments] as arguments.
  void emit(String eventName, [List arguments = const []]) {
    StreamController emitter = _getOrCreateEmitter(eventName);
    emitter.add(arguments);
  }

  /// Clears out any existing subscribers.
  void clear() {
    for (final emitter in _emitterMap.values) {
      emitter.close();
    }
    _emitterMap.clear();
  }

  @override
  void dispose() {
    _isDisposed = true;
    clear();
  }
}


{'fraction_non_alphanumeric': 0.10774976657329599, 'fraction_numerical': 0.009337068160597572, 'mean_word_length': 2.7533286615276804, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '1047288', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1453, 'n_words': 493}