How to use the @shopify/async.createResolver function in @shopify/async

To help you get started, we’ve selected a few @shopify/async examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Shopify / quilt / packages / react-async / src / provider.tsx View on Github external
export function createAsyncContext({
  id,
  load,
}: Options): AsyncContextType {
  const resolver = createResolver({id, load});
  const Context = React.createContext(null);

  // Just like a "normal" value returned from `createContext`, rendering
  // the value itself is not supported. This component is just a placeholder
  // to provide a more useful error.
  function Root() {
    throw new Error(
      'Do not attempt to render the result of calling `createAsyncContext()` directly. Render its `.Provider` component instead.',
    );
  }

  function Provider(props: ProviderProps) {
    const {load, resolved} = useAsync(resolver, {
      assets: AssetTiming.Immediate,
    });
github Shopify / quilt / packages / react-graphql / src / async / query.ts View on Github external
export function createAsyncQuery({
  id,
  load,
}: Options): AsyncDocumentNode<
  Data,
  Variables,
  DeepPartial
> {
  const resolver = createResolver({id, load});

  function usePreload() {
    return useAsync(resolver, {assets: AssetTiming.NextPage}).load;
  }

  function usePrefetch(options: VariableOptions) {
    const load = usePreload();
    return useBackgroundQuery(load, options);
  }

  function useKeepFresh(
    options: VariableOptions & Pick,
  ) {
    const load = usePreload();
    return useBackgroundQuery(load, {pollInterval: 10_000, ...options});
  }
github Shopify / quilt / packages / react-async / src / component.tsx View on Github external
usePreload: useCustomPreload = noopUse,
  usePrefetch: useCustomPrefetch = noopUse,
  useKeepFresh: useCustomKeepFresh = noopUse,
}: Options<
  Props,
  PreloadOptions,
  PrefetchOptions,
  KeepFreshOptions
>): AsyncComponentType<
  ComponentType,
  Props,
  PreloadOptions,
  PrefetchOptions,
  KeepFreshOptions
> {
  const resolver = createResolver({id, load});
  const componentName = displayName || displayNameFromId(resolver.id);
  const deferred = defer != null;
  const progressivelyHydrated = deferHydration != null;
  const scriptTiming =
    deferred || progressivelyHydrated
      ? AssetTiming.CurrentPage
      : AssetTiming.Immediate;
  const stylesTiming = deferred
    ? AssetTiming.CurrentPage
    : AssetTiming.Immediate;

  function Async(props: Props) {
    const {resolved: Component, load, loading, error} = useAsync(resolver, {
      scripts: scriptTiming,
      styles: stylesTiming,
      immediate: !deferred,

@shopify/async

Primitives for loading parts of an application asynchronously

MIT
Latest version published 28 days ago

Package Health Score

93 / 100
Full package analysis

Similar packages