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


TypeScript promise.AtomicPromise類代碼示例

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


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

示例1: 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

示例2: 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

示例3: wait

function wait(el: HTMLElement): AtomicPromise<Event> {
  return AtomicPromise.race([
    new AtomicPromise<Event>(resolve => void once(el, 'load', resolve)),
    new AtomicPromise<Event>(resolve => void once(el, 'abort', resolve)),
    new AtomicPromise<Event>(resolve => void once(el, 'error', resolve)),
  ]);
}
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:7,代碼來源:content.ts

示例4: 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

示例5: tuple

 .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,代碼行數:9,代碼來源:script.ts

示例6: Event

 m2.fmap(([[, sp], seqD]) =>
   // Asynchronously wait for load completion of elements and scripts.
   void AtomicPromise.all([cp, sp])
     .then(process.either)
     .then(m => m
       .fmap(([events]) => (
         void window.dispatchEvent(new Event('pjax:load')),
         void config.sequence.load(seqD, events)))
       .extract(() => undefined))))
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:9,代碼來源:update.ts

示例7: css

 .fmap(async ([areas]) => {
   config.update.css
     ? void css(documents, config.update.ignore)
     : undefined;
   void io.document.dispatchEvent(new Event('pjax:content'));
   const seqC = await config.sequence.content(seqB, areas);
   const ssm = config.update.script
     ? await script(documents, state.scripts, config.update, Math.max(config.fetch.timeout, 1000) * 10, process)
     : await process.either<[HTMLScriptElement[], AtomicPromise<Either<Error, HTMLScriptElement[]>>]>([[], AtomicPromise.resolve(process.either([]))]);
   void focus(event.type, documents.dst);
   void scroll(event.type, documents.dst, {
     hash: event.location.dest.fragment,
     position: io.position,
   });
   void savePosition();
   void io.document.dispatchEvent(new Event('pjax:ready'));
   return [
     ssm
       .fmap(([ss, ap]) =>
         [ss, ap.then(m => m.extract())] as const),
     await config.sequence.ready(seqC),
   ] as const;
 })
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:23,代碼來源:update.ts

示例8:

 .fmap(([as, ps]) =>
   [as, AtomicPromise.all(ps)] as const)))
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:2,代碼來源:update.ts

示例9: update

export function update(
  {
    event,
    config,
    state,
  }: RouterEntity,
  response: FetchResponse,
  seq: 'fetch',
  io: {
    document: Document;
    position: () => { top: number; left: number; };
  }
): AtomicPromise<Either<Error, readonly [HTMLScriptElement[], Promise<HTMLScriptElement[]>]>> {
  const { process } = state;
  const documents = {
    src: response.document,
    dst: io.document,
  };
  return AtomicPromise.resolve(seq)
    .then(process.either)
    // fetch -> unload
    .then(m => m
      .bind(() =>
        separate(documents, config.areas)
          .extract(
            () => Left(new Error(`Failed to separate the areas.`)),
            () => m))
      .fmap(seqA => (
        void window.dispatchEvent(new Event('pjax:unload')),
        config.sequence.unload(seqA, { ...response, url: response.url.reference }))))
    .then(m => Either.sequence(m))
    .then(process.promise)
    .then(m => m
        .bind(seqB =>
          separate(documents, config.areas)
            .fmap(([area]) =>
              [seqB, area])
            .extract(
              () => Left(new Error(`Failed to separate the areas.`)),
              process.either))
        .bind(([seqB, area]) => (
          void config.update.rewrite(documents.src, area),
          separate(documents, config.areas)
            .fmap(([, areas]) =>
              [seqB, areas])
            .extract(
              () => Left(new Error(`Failed to separate the areas.`)),
              process.either))))
    .then(process.promise)
    // unload -> ready
    .then(m => m
      .fmap(([seqB, areas]) =>
        new HNil()
          .extend(() => (
            void blur(documents.dst),
            void url(
              new RouterEventLocation(response.url),
              documents.src.title,
              event.type,
              event.source,
              config.replace),
            void title(documents),
            void saveTitle(),
            void head(documents, config.update.head, config.update.ignore),
            process.either(content(documents, areas))
              .fmap(([as, ps]) =>
                [as, AtomicPromise.all(ps)] as const)))
          .extend(async p => (await p)
            .fmap(async ([areas]) => {
              config.update.css
                ? void css(documents, config.update.ignore)
                : undefined;
              void io.document.dispatchEvent(new Event('pjax:content'));
              const seqC = await config.sequence.content(seqB, areas);
              const ssm = config.update.script
                ? await script(documents, state.scripts, config.update, Math.max(config.fetch.timeout, 1000) * 10, process)
                : await process.either<[HTMLScriptElement[], AtomicPromise<Either<Error, HTMLScriptElement[]>>]>([[], AtomicPromise.resolve(process.either([]))]);
              void focus(event.type, documents.dst);
              void scroll(event.type, documents.dst, {
                hash: event.location.dest.fragment,
                position: io.position,
              });
              void savePosition();
              void io.document.dispatchEvent(new Event('pjax:ready'));
              return [
                ssm
                  .fmap(([ss, ap]) =>
                    [ss, ap.then(m => m.extract())] as const),
                await config.sequence.ready(seqC),
              ] as const;
            })
            .fmap(p =>
              p.then(([m, seqD]) =>
                m.fmap(sst =>
                  [sst, seqD] as const)))
            .extract(e => AtomicPromise.resolve(Left(e))))
          .reverse()
          .tuple()))
    .then(process.promise)
    // ready -> load
//.........這裏部分代碼省略.........
開發者ID:falsandtru,項目名稱:pjax-api,代碼行數:101,代碼來源:update.ts


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