Skip to content

Commit

Permalink
refactor: rename <Async> to <Resource> (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jul 14, 2022
1 parent ffa1790 commit fe4ad61
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 45 deletions.
4 changes: 2 additions & 2 deletions packages/qwik-city/runtime/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { Component } from '@builder.io/qwik';
import type { HTMLAttributes } from '@builder.io/qwik';
import { Resource } from '@builder.io/qwik';
import { ResourceReturn } from '@builder.io/qwik';

// @public (undocumented)
export const Content: Component< {}>;
Expand Down Expand Up @@ -188,7 +188,7 @@ export const useContentMenu: () => ContentMenu | undefined;
export const useDocumentHead: () => Required<ResolvedDocumentHead>;

// @public (undocumented)
export const useEndpoint: <T = unknown>() => Resource<T>;
export const useEndpoint: <T = unknown>() => ResourceReturn<T>;

// @public (undocumented)
export const useLocation: () => RouteLocation;
Expand Down
4 changes: 2 additions & 2 deletions packages/qwik-city/runtime/src/app/routes/products/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Async, component$, Host, useStore } from '@builder.io/qwik';
import { Resource, component$, Host, useStore } from '@builder.io/qwik';
import { useEndpoint, useLocation, EndpointHandler, DocumentHead } from '~qwik-city-runtime';
import os from 'os';

Expand All @@ -10,7 +10,7 @@ export default component$(() => {

return (
<Host>
<Async
<Resource
resource={resource}
onPending={() => <p>Loading</p>}
onResolved={(product) => {
Expand Down
32 changes: 16 additions & 16 deletions packages/qwik/src/core/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ export interface AriaAttributes {
'aria-valuetext'?: string | undefined;
}

// Warning: (ae-forgotten-export) The symbol "AsyncProps" needs to be exported by the entry point index.d.ts
//
// @alpha (undocumented)
export const Async: <T>(props: AsyncProps<T>) => JSXNode;

// @public
export const component$: <PROPS extends {}>(onMount: OnRenderFn<PROPS>, options?: ComponentOptions) => Component<PROPS>;

Expand Down Expand Up @@ -380,8 +375,10 @@ export interface Ref<T> {
// @alpha
export const render: (parent: Element | Document, jsxNode: JSXNode<unknown> | FunctionComponent<any>, allowRerender?: boolean) => Promise<void>;

// Warning: (ae-forgotten-export) The symbol "AsyncProps" needs to be exported by the entry point index.d.ts
//
// @alpha (undocumented)
export type Resource<T> = ResourcePending<T> | ResourceResolved<T> | ResourceRejected<T>;
export const Resource: <T>(props: AsyncProps<T>) => JSXNode;

// @alpha (undocumented)
export interface ResourceCtx<T> {
Expand Down Expand Up @@ -429,6 +426,9 @@ export interface ResourceResolved<T> {
state: 'resolved';
}

// @alpha (undocumented)
export type ResourceReturn<T> = ResourcePending<T> | ResourceResolved<T> | ResourceRejected<T>;

// @alpha (undocumented)
export type ServerFn<T> = () => ValueOrPromise<T>;

Expand Down Expand Up @@ -518,16 +518,16 @@ export const useHostElement: () => Element;
export const useLexicalScope: <VARS extends any[]>() => VARS;

// Warning: (ae-incompatible-release-tags) The symbol "useMount$" is marked as @public, but its signature references "MountFn" which is marked as @alpha
// Warning: (ae-incompatible-release-tags) The symbol "useMount$" is marked as @public, but its signature references "Resource" which is marked as @alpha
// Warning: (ae-incompatible-release-tags) The symbol "useMount$" is marked as @public, but its signature references "ResourceReturn" which is marked as @alpha
//
// @public
export const useMount$: <T>(first: ServerFn<T>) => Resource<T>;
export const useMount$: <T>(first: ServerFn<T>) => ResourceReturn<T>;

// Warning: (ae-incompatible-release-tags) The symbol "useMountQrl" is marked as @public, but its signature references "MountFn" which is marked as @alpha
// Warning: (ae-incompatible-release-tags) The symbol "useMountQrl" is marked as @public, but its signature references "Resource" which is marked as @alpha
// Warning: (ae-incompatible-release-tags) The symbol "useMountQrl" is marked as @public, but its signature references "ResourceReturn" which is marked as @alpha
//
// @public
export const useMountQrl: <T>(mountQrl: QRL<ServerFn<T>>) => Resource<T>;
export const useMountQrl: <T>(mountQrl: QRL<ServerFn<T>>) => ResourceReturn<T>;

// @alpha
export const useOn: (event: string, eventQrl: QRL<(ev: Event) => void>) => void;
Expand All @@ -546,10 +546,10 @@ export const useRef: <T extends Element = Element>(current?: T | undefined) => R
// Warning: (ae-forgotten-export) The symbol "ResourceFn" needs to be exported by the entry point index.d.ts
//
// @alpha (undocumented)
export const useResource$: <T>(generatorFn: ResourceFn<T>) => Resource<T>;
export const useResource$: <T>(generatorFn: ResourceFn<T>) => ResourceReturn<T>;

// @alpha (undocumented)
export const useResourceQrl: <T>(qrl: QRL<ResourceFn<T>>) => Resource<T>;
export const useResourceQrl: <T>(qrl: QRL<ResourceFn<T>>) => ResourceReturn<T>;

// Warning: (ae-forgotten-export) The symbol "UseResumeOptions" needs to be exported by the entry point index.d.ts
//
Expand All @@ -571,16 +571,16 @@ export const useScopedStylesQrl: (styles: QRL<string>) => void;
export const useSequentialScope: <T>() => SequentialScope<T>;

// Warning: (ae-incompatible-release-tags) The symbol "useServerMount$" is marked as @public, but its signature references "MountFn" which is marked as @alpha
// Warning: (ae-incompatible-release-tags) The symbol "useServerMount$" is marked as @public, but its signature references "Resource" which is marked as @alpha
// Warning: (ae-incompatible-release-tags) The symbol "useServerMount$" is marked as @public, but its signature references "ResourceReturn" which is marked as @alpha
//
// @public
export const useServerMount$: <T>(first: ServerFn<T>) => Resource<T>;
export const useServerMount$: <T>(first: ServerFn<T>) => ResourceReturn<T>;

// Warning: (ae-incompatible-release-tags) The symbol "useServerMountQrl" is marked as @public, but its signature references "MountFn" which is marked as @alpha
// Warning: (ae-incompatible-release-tags) The symbol "useServerMountQrl" is marked as @public, but its signature references "Resource" which is marked as @alpha
// Warning: (ae-incompatible-release-tags) The symbol "useServerMountQrl" is marked as @public, but its signature references "ResourceReturn" which is marked as @alpha
//
// @public
export const useServerMountQrl: <T>(mountQrl: QRL<ServerFn<T>>) => Resource<T>;
export const useServerMountQrl: <T>(mountQrl: QRL<ServerFn<T>>) => ResourceReturn<T>;

// Warning: (ae-forgotten-export) The symbol "UseStoreOptions" needs to be exported by the entry point index.d.ts
//
Expand Down
4 changes: 2 additions & 2 deletions packages/qwik/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ export type {
MountFn as ServerFn,
UseEffectOptions,
UseEffectRunOptions,
Resource,
ResourceReturn,
ResourceCtx,
ResourcePending,
ResourceRejected,
ResourceResolved,
} from './use/use-watch';
export { useWatch$, useWatchQrl } from './use/use-watch';
export { useResource$, useResourceQrl, Async } from './use/use-resource';
export { useResource$, useResourceQrl, Resource } from './use/use-resource';
export { useClientEffect$, useClientEffectQrl } from './use/use-watch';
export { useServerMount$, useServerMountQrl } from './use/use-watch';
export { useMount$, useMountQrl } from './use/use-watch';
Expand Down
14 changes: 7 additions & 7 deletions packages/qwik/src/core/use/use-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSequentialScope } from './use-store.public';
import { $, QRL } from '../import/qrl.public';
import { assertQrl } from '../import/qrl-class';
import {
Resource,
ResourceReturn,
ResourceDescriptor,
ResourceFn,
runResource,
Expand All @@ -21,15 +21,15 @@ import { getInvokeContext } from './use-core';
/**
* @alpha
*/
export const useResourceQrl = <T>(qrl: QRL<ResourceFn<T>>): Resource<T> => {
const { get, set, i, ctx } = useSequentialScope<Resource<T>>();
export const useResourceQrl = <T>(qrl: QRL<ResourceFn<T>>): ResourceReturn<T> => {
const { get, set, i, ctx } = useSequentialScope<ResourceReturn<T>>();
if (get != null) {
return get;
}
assertQrl(qrl);

const containerState = ctx.$renderCtx$.$containerState$;
const result: Resource<T> = {
const result: ResourceReturn<T> = {
promise: undefined as never,
resolved: undefined as never,
error: undefined as never,
Expand All @@ -56,7 +56,7 @@ export const useResourceQrl = <T>(qrl: QRL<ResourceFn<T>>): Resource<T> => {
/**
* @alpha
*/
export const useResource$ = <T>(generatorFn: ResourceFn<T>): Resource<T> => {
export const useResource$ = <T>(generatorFn: ResourceFn<T>): ResourceReturn<T> => {
return useResourceQrl<T>($(generatorFn));
};

Expand All @@ -70,7 +70,7 @@ export const useIsServer = () => {
* @alpha
*/
export interface AsyncProps<T> {
resource: Resource<T>;
resource: ResourceReturn<T>;
onResolved: (value: T) => JSXNode;
onPending?: () => JSXNode;
onRejected?: (reason: any) => JSXNode;
Expand All @@ -79,7 +79,7 @@ export interface AsyncProps<T> {
/**
* @alpha
*/
export const Async = <T>(props: AsyncProps<T>): JSXNode => {
export const Resource = <T>(props: AsyncProps<T>): JSXNode => {
const isBrowser = !qDev || !useIsServer();
if (isBrowser) {
if (props.onRejected) {
Expand Down
16 changes: 8 additions & 8 deletions packages/qwik/src/core/use/use-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type MountFn<T> = () => ValueOrPromise<T>;
/**
* @alpha
*/
export type Resource<T> = ResourcePending<T> | ResourceResolved<T> | ResourceRejected<T>;
export type ResourceReturn<T> = ResourcePending<T> | ResourceResolved<T> | ResourceRejected<T>;

/**
* @alpha
Expand Down Expand Up @@ -109,7 +109,7 @@ export type WatchDescriptor = DescriptorBase<WatchFn>;
* @alpha
*/
export interface ResourceDescriptor<T> extends DescriptorBase<ResourceFn<T>> {
r: Resource<T>;
r: ResourceReturn<T>;
}

/**
Expand Down Expand Up @@ -408,8 +408,8 @@ export const useClientEffect$ = /*#__PURE__*/ implicit$FirstArg(useClientEffectQ
* @public
*/
// </docs>
export const useServerMountQrl = <T>(mountQrl: QRL<MountFn<T>>): Resource<T> => {
const { get, set, ctx } = useSequentialScope<Resource<T>>();
export const useServerMountQrl = <T>(mountQrl: QRL<MountFn<T>>): ResourceReturn<T> => {
const { get, set, ctx } = useSequentialScope<ResourceReturn<T>>();
if (get) {
return get;
}
Expand Down Expand Up @@ -500,8 +500,8 @@ export const useServerMount$ = /*#__PURE__*/ implicit$FirstArg(useServerMountQrl
* @public
*/
// </docs>
export const useMountQrl = <T>(mountQrl: QRL<MountFn<T>>): Resource<T> => {
const { get, set, ctx } = useSequentialScope<Resource<T>>();
export const useMountQrl = <T>(mountQrl: QRL<MountFn<T>>): ResourceReturn<T> => {
const { get, set, ctx } = useSequentialScope<ResourceReturn<T>>();
if (get) {
return get;
}
Expand All @@ -511,8 +511,8 @@ export const useMountQrl = <T>(mountQrl: QRL<MountFn<T>>): Resource<T> => {
return resource;
};

const createResourceFromPromise = <T>(promise: Promise<T>): Resource<T> => {
const resource: Resource<T> = {
const createResourceFromPromise = <T>(promise: Promise<T>): ResourceReturn<T> => {
const resource: ResourceReturn<T> = {
state: 'pending',
error: undefined,
resolved: undefined,
Expand Down
6 changes: 3 additions & 3 deletions starters/apps/e2e/src/components/resource/resource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
useResource$,
Resource,
useWatch$,
Async,
createContext,
useContextProvider,
useContext,
useStyles$,
ResourceReturn,
} from '@builder.io/qwik';

export interface WeatherData {
Expand Down Expand Up @@ -89,7 +89,7 @@ export const ResourceApp = component$(() => {
);
});

export const Results = component$((props: { result: Resource<number> }) => {
export const Results = component$((props: { result: ResourceReturn<number> }) => {
useStyles$(`
.logs {
white-space: pre;
Expand All @@ -99,7 +99,7 @@ export const Results = component$((props: { result: Resource<number> }) => {

return (
<Host>
<Async
<Resource
resource={props.result}
onPending={() => <div class="resource1">loading resource 1...</div>}
onRejected={(reason) => <div class="resource1">error {reason}</div>}
Expand Down
8 changes: 4 additions & 4 deletions starters/apps/e2e/src/components/resource/weather.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useResource$,
Resource,
useWatch$,
Async,
ResourceReturn,
} from '@builder.io/qwik';

export interface WeatherData {
Expand Down Expand Up @@ -66,7 +66,7 @@ export const Weather = component$(() => {
);
});

export const WeatherResults = component$((props: { weather: Resource<WeatherData> }) => {
export const WeatherResults = component$((props: { weather: ResourceReturn<WeatherData> }) => {
console.log('rerender');
return (
<Host>
Expand All @@ -88,11 +88,11 @@ export const WeatherResults = component$((props: { weather: Resource<WeatherData
});

export const WeatherResults2 = component$(
(props: { weather: Resource<WeatherData | undefined> }) => {
(props: { weather: ResourceReturn<WeatherData | undefined> }) => {
console.log('rerender');
return (
<Host>
<Async
<Resource
resource={props.weather}
onPending={() => <div>loading data...</div>}
onRejected={(reason) => <div>error {reason}</div>}
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"**/lib",
// "starters/servers",
// "starters/apps",
"starters/**/root.tsx",
"starters/**/entry.netlify.ts",
"starters/**/entry.cloudflare.tsx",
"starters/**/entry.express.tsx",
Expand Down

0 comments on commit fe4ad61

Please sign in to comment.