importCanonical method

Future<Stylesheet?> importCanonical(
  1. AsyncImporter importer,
  2. Uri canonicalUrl, {
  3. Uri? originalUrl,
})

Tries to load the canonicalized canonicalUrl using importer.

If importer can import canonicalUrl, returns the imported Stylesheet. Otherwise returns null.

If passed, the originalUrl represents the URL that was canonicalized into canonicalUrl. It's used to resolve a relative canonical URL, which importers may return for legacy reasons.

Caches the result of the import and uses cached results if possible.

Implementation

Future<Stylesheet?> importCanonical(AsyncImporter importer, Uri canonicalUrl,
    {Uri? originalUrl}) async {
  return await putIfAbsentAsync(_importCache, canonicalUrl, () async {
    var loadTime = DateTime.now();
    var result = await importer.load(canonicalUrl);
    if (result == null) return null;

    _loadTimes[canonicalUrl] = loadTime;
    _resultsCache[canonicalUrl] = result;
    return Stylesheet.parse(result.contents, result.syntax,
        // For backwards-compatibility, relative canonical URLs are resolved
        // relative to [originalUrl].
        url: originalUrl == null
            ? canonicalUrl
            : originalUrl.resolveUri(canonicalUrl));
  });
}