当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript cache.Cache类代码示例

本文整理汇总了TypeScript中spica/cache.Cache的典型用法代码示例。如果您正苦于以下问题:TypeScript Cache类的具体用法?TypeScript Cache怎么用?TypeScript Cache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Cache类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: URL

 .fmap(xhr => {
   const responseURL: URL<StandardURL> = new URL(standardize(xhr.responseURL));
   assert(responseURL.origin === new URL(window.location.origin).origin);
   if (method === 'GET') {
     const cc = new Map<string, string>(
       xhr.getResponseHeader('Cache-Control')
         ? xhr.getResponseHeader('Cache-Control')!.trim().split(/\s*,\s*/)
             .filter(v => v.length > 0)
             .map(v => v.split('=').concat('') as [string, string])
         : []);
     for (const path of new Set([requestURL.path, responseURL.path])) {
       if (xhr.getResponseHeader('ETag') && !cc.has('no-store')) {
         void caches.set(path, {
           etag: xhr.getResponseHeader('ETag')!,
           expiry: cc.has('max-age') && !cc.has('no-cache')
             ? Date.now() + +cc.get('max-age')! * 1000 || 0
             : 0,
           xhr,
         });
       }
       else {
         void caches.delete(path);
       }
     }
   }
   return (overriddenDisplayURL: URL<StandardURL>, overriddenRequestURL: URL<StandardURL>) =>
     new FetchResponse(
       responseURL.path === overriddenRequestURL.path
         ? overriddenDisplayURL
         : overriddenRequestURL.path === requestURL.path || !key
             ? responseURL
             : overriddenDisplayURL,
       xhr);
 })
开发者ID:falsandtru,项目名称:pjax-api,代码行数:34,代码来源:xhr.ts

示例2: XMLHttpRequest

  return new AtomicPromise<Either<Error, FetchResponse>>(resolve => {
    if (key && memory.has(key)) return resolve(Right(memory.get(key)!(displayURL, requestURL)));
    const xhr = new XMLHttpRequest();
    void xhr.open(method, requestURL.path, true);
    for (const [name, value] of headers) {
      void xhr.setRequestHeader(name, value);
    }

    xhr.responseType = 'document';
    xhr.timeout = timeout;
    void xhr.send(body);

    void xhr.addEventListener("abort", () =>
      void resolve(Left(new Error(`Failed to request a page by abort.`))));

    void xhr.addEventListener("error", () =>
      void resolve(Left(new Error(`Failed to request a page by error.`))));

    void xhr.addEventListener("timeout", () =>
      void resolve(Left(new Error(`Failed to request a page by timeout.`))));

    void xhr.addEventListener("load", () =>
      void verify(xhr, method)
        .fmap(xhr => {
          const responseURL: URL<StandardURL> = new URL(standardize(xhr.responseURL));
          assert(responseURL.origin === new URL(window.location.origin).origin);
          if (method === 'GET') {
            const cc = new Map<string, string>(
              xhr.getResponseHeader('Cache-Control')
                ? xhr.getResponseHeader('Cache-Control')!.trim().split(/\s*,\s*/)
                    .filter(v => v.length > 0)
                    .map(v => v.split('=').concat('') as [string, string])
                : []);
            for (const path of new Set([requestURL.path, responseURL.path])) {
              if (xhr.getResponseHeader('ETag') && !cc.has('no-store')) {
                void caches.set(path, {
                  etag: xhr.getResponseHeader('ETag')!,
                  expiry: cc.has('max-age') && !cc.has('no-cache')
                    ? Date.now() + +cc.get('max-age')! * 1000 || 0
                    : 0,
                  xhr,
                });
              }
              else {
                void caches.delete(path);
              }
            }
          }
          return (overriddenDisplayURL: URL<StandardURL>, overriddenRequestURL: URL<StandardURL>) =>
            new FetchResponse(
              responseURL.path === overriddenRequestURL.path
                ? overriddenDisplayURL
                : overriddenRequestURL.path === requestURL.path || !key
                    ? responseURL
                    : overriddenDisplayURL,
              xhr);
        })
        .fmap(f => {
          if (key) {
            void memory.set(key, f);
          }
          return f(displayURL, requestURL);
        })
        .extract(
          err => void resolve(Left(err)),
          res => void resolve(Right(res))));

    void cancellation.register(() => void xhr.abort());
  });
开发者ID:falsandtru,项目名称:pjax-api,代码行数:69,代码来源:xhr.ts

示例3: match

function match(pattern: string, segment: string): boolean {
  assert(segment === '/' || !segment.startsWith('/'));
  if (segment[0] === '.' && [...'?*'].includes(pattern[0])) return false;
  const id = `${pattern}:${segment}`;
  return cache.has(id)
    ? cache.get(id)!
    : cache.set(id, match(optimize(pattern), segment));

  function match(pattern: string, segment: string): boolean {
    const [p = '', ...ps] = [...pattern];
    const [s = '', ...ss] = [...segment];
    assert(typeof p === 'string');
    assert(typeof s === 'string');
    switch (p) {
      case '':
        return s === '';
      case '?':
        return s !== ''
            && s !== '/'
            && match(ps.join(''), ss.join(''));
      case '*':
        return s === '/'
          ? match(ps.join(''), segment)
          : Sequence
              .zip(
                Sequence.cycle([ps.join('')]),
                Sequence.from(segment)
                  .tails()
                  .map(ss => ss.join('')))
              .filter(uncurry(match))
              .take(1)
              .extract()
              .length > 0;
      default:
        return s === p
            && match(ps.join(''), ss.join(''));
    }
  }
  
  function optimize(pattern: string): string {
    const pat = pattern.replace(/\*(\?+)\*?/g, '$1*');
    return pat === pattern
      ? pat
      : optimize(pat);
  }
}
开发者ID:falsandtru,项目名称:pjax-api,代码行数:46,代码来源:router.ts

示例4: Left

 .bind(xhr => {
   const url = new URL(standardize(xhr.responseURL));
   switch (true) {
     case !xhr.responseURL:
       return Left(new Error(`Failed to get the response URL.`));
     case url.origin !== new URL(window.location.origin).origin:
       return Left(new Error(`Redirected to another origin.`));
     case !/2..|304/.test(`${xhr.status}`):
       return Left(new Error(`Failed to validate the status of response.`));
     case !xhr.responseXML:
       return method === 'GET' && xhr.status === 304 && caches.has(url.path)
         ? Right(caches.get(url.path)!.xhr)
         : Left(new Error(`Failed to get the response body.`));
     case !match(xhr.getResponseHeader('Content-Type'), 'text/html'):
       return Left(new Error(`Failed to validate the content type of response.`));
     default:
       return Right(xhr);
   }
 });
开发者ID:falsandtru,项目名称:pjax-api,代码行数:19,代码来源:xhr.ts

示例5: f

 .fmap(f => {
   if (key) {
     void memory.set(key, f);
   }
   return f(displayURL, requestURL);
 })
开发者ID:falsandtru,项目名称:pjax-api,代码行数:6,代码来源:xhr.ts


注:本文中的spica/cache.Cache类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。