Future reloadWorkingFile()

in packages/sampler/lib/model.dart [149:191]


  Future<void> reloadWorkingFile({bool notify = true}) async {
    if (_workingFile == null) {
      assert(_workingFileWatcher == null);
      // No working file, nothing to reload.
      return;
    }
    // Only reload if the contents have actually changed.
    final String contents = await workingFile!.readAsString();
    if (_workingFileDigest != null &&
        _computeDigest(contents) == _workingFileDigest!) {
      return;
    }
    await _loadWorkingFile(contents: contents);
    if (_currentElement != null && _elements != null) {
      // Try to find the old element.
      final Iterable<SourceElement> oldMatches = _elements!.where(
          (SourceElement element) =>
              element.elementName == _currentElement!.elementName);
      if (oldMatches.isNotEmpty) {
        _currentElement = oldMatches.first;
      } else {
        throw SnippetException(
            'Unable to find element ${_currentElement!.elementName} during reload.');
      }
    } else {
      _currentElement = null;
      _currentSample = null;
    }
    if (_currentElement != null && _currentSample != null) {
      final Iterable<CodeSample> samples = _currentElement!.samples
          .where((CodeSample sample) => sample.index == _currentSample!.index);
      if (samples.length != 1) {
        throw SnippetException(
            'Unable to find sample ${_currentSample!.index} on ${_currentElement!.elementName} during reload.');
      }
      _currentSample = samples.single;
    } else {
      _currentSample = null;
    }
    if (notify) {
      notifyListeners();
    }
  }