當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript either.Right函數代碼示例

本文整理匯總了TypeScript中spica/either.Right函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript Right函數的具體用法?TypeScript Right怎麽用?TypeScript Right使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Right函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: fetch

async function fetch(
  script: HTMLScriptElement,
  timeout: number,
): Promise<Either<Error, FetchData>> {
  if (!script.hasAttribute('src')) return Right<FetchData>([script, script.text]);
  if (script.type.toLowerCase() === 'module') return Right<FetchData>([script, '']);
  return AtomicPromise.race([
    window.fetch(script.src, {
      headers: new Headers({
        Accept: 'application/javascript',
      }),
      integrity: script.integrity,
    }),
    wait(timeout).then(() => AtomicPromise.reject(new Error(`${script.src}: Timeout.`))),
  ])
    .then(
      async res =>
        res.ok
          ? Right<FetchData>([script, await res.text()])
          : script.matches('[src][async]')
            ? retry(script)
                .then(
                  () => Right<FetchData>([script, '']),
                  () => Left(new Error(`${script.src}: ${res.statusText}`)))
            : Left(new Error(res.statusText)),
      (error: Error) => Left(error));
}
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:27,代碼來源:script.ts

示例2: run

 function run(responses: Either<Error, FetchData>[]): Either<Error, AtomicPromise<Result>> {
   return responses
     .reduce(
       (results, m) => m.bind(() => results),
       responses
         .reduce((results, m) =>
           results
             .bind(cancellation.either)
             .bind(([sp, ap]) => m
               .fmap(([script, code]) =>
                 io.evaluate(script, code, selector.logger, skip, AtomicPromise.all(sp), cancellation))
               .bind(m =>
                 m.extract(
                   p => Right(tuple([concat(sp, [p]), ap])),
                   p => Right(tuple([sp, concat(ap, [p])])))))
       , Right<Error, [AtomicPromise<Either<Error, HTMLScriptElement>>[], AtomicPromise<Either<Error, HTMLScriptElement>>[]]>([[], []])))
     .fmap(([sp, ap]) =>
       AtomicPromise.all(sp)
         .then(m => Either.sequence(m))
         .then(sm =>
           sm.fmap(ss => tuple([
             ss,
             Promise.all(ap)
               .then(m => Either.sequence(m))
           ]))));
 }
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:26,代碼來源:script.ts

示例3: evaluate

 function evaluate(): AtomicPromise<Either<Error, HTMLScriptElement>> {
   if (script.matches('[type="module"][src]')) {
     return AtomicPromise.resolve(import(script.src))
       .catch((reason: Error) =>
         reason.message.startsWith('Failed to load ') && script.matches('[src][async]')
           ? retry(script).catch(() => AtomicPromise.reject(reason))
           : AtomicPromise.reject(reason))
       .then(
         () => (
           void script.dispatchEvent(new Event('load')),
           Right(script)),
         reason => (
           void script.dispatchEvent(new Event('error')),
           Left(new FatalError(reason instanceof Error ? reason.message : reason + ''))));
   }
   else {
     try {
       if (new URL(standardize(window.location.href)).path !== new URL(standardize(window.location.href)).path) throw new FatalError('Expired.');
       if (skip.has(new URL(standardize(window.location.href)).reference)) throw new FatalError('Expired.');
       void (0, eval)(code);
       script.hasAttribute('src') && void script.dispatchEvent(new Event('load'));
       return AtomicPromise.resolve(Right(script));
     }
     catch (reason) {
       script.hasAttribute('src') && void script.dispatchEvent(new Event('error'));
       return AtomicPromise.resolve(Left(new FatalError(reason instanceof Error ? reason.message : reason + '')));
     }
   }
 }
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:29,代碼來源:script.ts

示例4: route

export async function route(
  entity: RouterEntity,
  io: {
    document: Document;
  }
): Promise<RouterResult> {
  return Right<Error, void>(undefined)
    .bind(entity.state.process.either)
    .bind(() =>
      match(io.document, entity.config.areas)
        ? Right(undefined)
        : Left(new Error(`Failed to match areas.`)))
    .fmap(() =>
      fetch(entity.event.request, entity.config, entity.state.process))
    .fmap(async p => (await p)
      .fmap(([res, seq]) =>
        update(entity, res, seq, {
          document: io.document,
          position: loadPosition
        }))
      .extract<Left<Error>>(Left))
    .extract<Left<Error>>(Left);

  function match(
    document: Document,
    areas: string[]
  ): boolean {
    return separate({ src: document, dst: document }, areas)
      .extract(
        () => false,
        () => true);
  }
}
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:33,代碼來源:api.ts

示例5: concat

 .fmap(([ss2, ap2]) =>
   AtomicPromise.all([as1, Right<Error, HTMLScriptElement[]>(ss2), ap2])
     .then(sst =>
       sst.reduce((m1, m2) =>
         m1.bind(s1 =>
           m2.fmap(s2 =>
             concat(s1, s2))))))
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:7,代碼來源:script.ts

示例6: compare

 m.bind(link =>
   !link.has(dst) && compare(src, dst)
     ? (
       void link.set(dst, concat(link.get(null) || [], [src])),
       void link.delete(null),
       Left(link))
     : Right(link))
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:7,代碼來源:sync.ts

示例7: retry

 async res =>
   res.ok
     ? Right<FetchData>([script, await res.text()])
     : script.matches('[src][async]')
       ? retry(script)
           .then(
             () => Right<FetchData>([script, '']),
             () => Left(new Error(`${script.src}: ${res.statusText}`)))
       : Left(new Error(res.statusText)),
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:9,代碼來源:script.ts

示例8: URL

 .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

示例9: concat

 .reduce<Map<T | null, T[]>>((link, src) =>
   dsts.length === 0
     ? link.set(null, concat(link.get(null) || [], [src]))
     : dsts
         .reduce<Either<typeof link, typeof link>>((m, dst) =>
           m.bind(link =>
             !link.has(dst) && compare(src, dst)
               ? (
                 void link.set(dst, concat(link.get(null) || [], [src])),
                 void link.delete(null),
                 Left(link))
               : Right(link))
         , Right(link))
         .fmap(link =>
           link.set(null, concat(link.get(null) || [], [src])))
         .extract(link =>
           link)
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:17,代碼來源:sync.ts

示例10: Right

 p => Right(tuple([sp, concat(ap, [p])])))))
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:1,代碼來源:script.ts


注:本文中的spica/either.Right函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。