Beispiel #1
0
        private IChangeToken GetOrAddChangeToken(string filePath)
        {
            ChangeTokenInfo tokenInfo;

            if (!_matchInfoCache.TryGetValue(filePath, out tokenInfo))
            {
                var cancellationTokenSource = new CancellationTokenSource();
                var cancellationChangeToken = new CancellationChangeToken(cancellationTokenSource.Token);
                tokenInfo = new ChangeTokenInfo(cancellationTokenSource, cancellationChangeToken);
                tokenInfo = _matchInfoCache.GetOrAdd(filePath, tokenInfo);
            }

            IChangeToken changeToken = tokenInfo.ChangeToken;

            if (_pollForChanges)
            {
                // The expiry of CancellationChangeToken is controlled by this type and consequently we can cache it.
                // PollingFileChangeToken on the other hand manages its own lifetime and consequently we cannot cache it.
                changeToken = new CompositeFileChangeToken(
                    new[]
                {
                    changeToken,
                    new PollingFileChangeToken(new FileInfo(filePath))
                });
            }

            return(changeToken);
        }
        private IChangeToken GetOrAddWildcardChangeToken(string pattern)
        {
            ChangeTokenInfo tokenInfo;

            if (!_wildcardTokenLookup.TryGetValue(pattern, out tokenInfo))
            {
                var cancellationTokenSource = new CancellationTokenSource();
                var cancellationChangeToken = new CancellationChangeToken(cancellationTokenSource.Token);
                var matcher = new Matcher(StringComparison.OrdinalIgnoreCase);
                matcher.AddInclude(pattern);
                tokenInfo = new ChangeTokenInfo(cancellationTokenSource, cancellationChangeToken, matcher);
                tokenInfo = _wildcardTokenLookup.GetOrAdd(pattern, tokenInfo);
            }

            IChangeToken changeToken = tokenInfo.ChangeToken;

            if (_pollForChanges)
            {
                // The expiry of CancellationChangeToken is controlled by this type and consequently we can cache it.
                // PollingFileChangeToken on the other hand manages its own lifetime and consequently we cannot cache it.
                changeToken = new CompositeFileChangeToken(
                    new[]
                {
                    changeToken,
                    new PollingWildCardChangeToken(_root, pattern)
                });
            }

            return(changeToken);
        }