public async Task RefreshEnvironmentsAsync(CancellationToken cancellationToken = default(CancellationToken)) { using (_disposeToken.Link(ref cancellationToken)) { var session = _rSession; await TaskUtilities.SwitchToBackgroundThread(); var envs = new List <REnvironment>(); bool success = false; try { var traceback = (await session.TracebackAsync(cancellationToken: cancellationToken)) .Skip(1) // skip global - it's in the search path already .Select(frame => new REnvironment(frame)) .Reverse(); if (traceback.Any()) { envs.AddRange(traceback); envs.Add(null); } var searchPath = (await session.EvaluateAsync <string[]>("as.list(search())", REvaluationKind.BaseEnv, cancellationToken)) .Except(_hiddenEnvironments) .Select(name => new REnvironment(name)); envs.AddRange(searchPath); success = true; } catch (RException) { } catch (OperationCanceledException) { } await _mainThread.SwitchToAsync(cancellationToken); var oldSelection = _selectedEnvironment; _environments.ReplaceWith(success ? envs : _errorEnvironments); IREnvironment newSelection = null; if (oldSelection != null) { newSelection = _environments?.FirstOrDefault(env => env?.Name == oldSelection.Name); } SelectedEnvironment = newSelection ?? _environments.FirstOrDefault(); } }
public async Task RefreshEnvironmentsAsync() { await TaskUtilities.SwitchToBackgroundThread(); var envs = new List <REnvironment>(); bool success = false; try { var traceback = (await _rSession.TracebackAsync()) .Skip(1) // skip global - it's in the search path already .Select(frame => new REnvironment(frame)) .Reverse(); if (traceback.Any()) { envs.AddRange(traceback); envs.Add(null); } var searchPath = (await _rSession.EvaluateAsync <string[]>("as.list(search())", REvaluationKind.BaseEnv)) .Except(_hiddenEnvironments) .Select(name => new REnvironment(name)); envs.AddRange(searchPath); success = true; } catch (RException) { } catch (MessageTransportException) { } catch (OperationCanceledException) { } VsAppShell.Current.DispatchOnUIThread(() => { var oldSelection = _selectedEnvironment; _environments.ReplaceWith(success ? envs : _errorEnvironments); IREnvironment newSelection = null; if (oldSelection != null) { newSelection = _environments?.FirstOrDefault(env => env?.Name == oldSelection.Name); } SelectedEnvironment = newSelection ?? _environments.FirstOrDefault(); }); }