当前位置: 首页>>代码示例>>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;未经允许,请勿转载。