<commit_before>typedef void OnChangeFn(ResourceCollection<T> rc);
typedef void OnLoadFn(ResourceCollection<T> rc);
typedef T DeserializeFn<T>(Object rawData);
typedef Object SerializeFn<T>(T modelObj);

class ResourceCollection<T> implements Collection<T> {
  final Resource<T> resource;
  bool loaded = false;
  OnChangeFn onChangeFn = null;
  OnLoadFn onLoadFn = null;
  DeserializeFn<T> deserializeFn = null;
  SerializeFn<T> serializeFn = JSON.stringify;

  Collection<T> _collection = [];

  ResourceCollection(this.resource);

  void _ensureLoadStarted() {
    if (loaded == false) {
      _load();
    }
  }

  void _load() {
    resource.query({}, (data) {
      _collection = data.map((e) => (deserializeFn != null) ? deserializeFn(e) : e);
      loaded = true;
      if (onLoadFn != null) {
        onLoadFn(this);
      }
      if (onChangeFn != null) {
        onChangeFn(this);
      }
    });
  }

  void add(T value) {
    _collection.add(value);
    if (onChangeFn != null) {
      onChangeFn(this);
    }
    resource.post(serializeFn(value), null);
  }

  bool every(bool f(T element)) {
    _ensureLoadStarted();
    return _collection.every(f);
  }

  Collection<T> filter(bool f(T element)) {
    _ensureLoadStarted();
    return _collection.filter(f);
  }

  void forEach(void f(T element)) {
    _ensureLoadStarted();<commit_msg>ResourceCollection implements List<T> instead of Collection<T> because dart-web-components' _ListWatcher<T> only watches Lists<commit_after>typedef void OnChangeFn(ResourceCollection<T> rc);
typedef void OnLoadFn(ResourceCollection<T> rc);
typedef T DeserializeFn<T>(Object rawData);
typedef Object SerializeFn<T>(T modelObj);

class ResourceCollection<T> implements List<T> {
  final Resource<T> resource;
  bool loaded = false;
  OnChangeFn onChangeFn = null;
  OnLoadFn onLoadFn = null;
  DeserializeFn<T> deserializeFn = null;
  SerializeFn<T> serializeFn = JSON.stringify;

  List<T> _collection = [];
  List<T> get collection => new List.from(_collection);

  ResourceCollection(this.resource);

  void _ensureLoadStarted() {
    if (loaded == false) {
      _load();
    }
  }

  void _load() {
    resource.query({}, (data) {
      _collection = data.map((e) => (deserializeFn != null) ? deserializeFn(e) : e);
      loaded = true;
      if (onLoadFn != null) {
        onLoadFn(this);
      }
      if (onChangeFn != null) {
        onChangeFn(this);
      }
    });
  }

  T operator [](int index) {
    _ensureLoadStarted();
    return _collection[index];
  }

  void add(T value) {
    _collection.add(value);
    if (onChangeFn != null) {
      onChangeFn(this);
    }
    resource.post(serializeFn(value), null);
  }

  bool every(bool f(T element)) {
    _ensureLoadStarted();
    return _collection.every(f);
  }

  Collection<T> filter(bool f(T element)) {
    _ensureLoadStarted();
    return _collection.filter(f);
  }

  void forEach(void f(T element)) {
    _ensureLoadStarted();
{'fraction_non_alphanumeric': 0.13245257452574527, 'fraction_numerical': 0.0, 'mean_word_length': 3.5083969465648854, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 32, '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': '5189238', 'n_tokens_mistral': 984, 'n_tokens_neox': 957, 'n_words': 214}