Skip to content

Commit

Permalink
Merge f29a8d1 into ba01165
Browse files Browse the repository at this point in the history
  • Loading branch information
pmkroeker committed May 10, 2019
2 parents ba01165 + f29a8d1 commit bb641eb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compat/src/index.d.ts
Expand Up @@ -9,6 +9,8 @@ export import createRef = preact.createRef;
export import Fragment = preact.Fragment;
export import createElement = preact.createElement
export import cloneElement = preact.cloneElement
export import Suspense = preact.Suspense;
export import lazy = preact.lazy;

export declare const version: string;

Expand Down Expand Up @@ -66,6 +68,8 @@ declare const _default: {
forwardRef: typeof forwardRef,
unstable_batchedUpdates: typeof unstable_batchedUpdates,
Children: Children,
Suspense: typeof Suspense,
lazy: typeof lazy,
};

export default _default;
13 changes: 13 additions & 0 deletions src/index.d.ts
Expand Up @@ -231,4 +231,17 @@ declare namespace preact {
interface PreactContext<T> extends Context<T> {}

function createContext<T>(defaultValue: T): Context<T>;

//
// Suspense/lazy
// -----------------------------------

function lazy<T, P>(loader: () => Promise<T>): (props: P) => VNode<P>;

interface SuspenseProps {
children?: ComponentChildren;
fallback: ComponentChildren;
}

abstract class Suspense extends Component<SuspenseProps> {}
}
23 changes: 23 additions & 0 deletions test/ts/lazy.tsx
@@ -0,0 +1,23 @@
import {
createElement,
Component,
} from "../..";

export interface LazyProps {
isProp: boolean;
}

interface LazyState {
forState: string;
}
export default class IsLazyComponent extends Component<LazyProps, LazyState> {
render ({ isProp }: LazyProps) {
return (
<div>{
isProp ?
'Super Lazy TRUE' :
'Super Lazy FALSE'
}</div>
)
}
}
42 changes: 42 additions & 0 deletions test/ts/suspense-test.tsx
@@ -0,0 +1,42 @@
import "mocha";
import { expect } from "chai";
import {
createElement,
Component,
Suspense,
lazy,
} from "../..";

interface LazyProps {
isProp: boolean;
}

const IsLazyFunctional = (props: LazyProps) =>
<div>{
props.isProp ?
'Super Lazy TRUE' :
'Super Lazy FALSE'
}</div>

const FallBack = () => <div>Still working...</div>;
/**
* Have to mock dynamic import as import() throws a syntax error in the test runner
*/
const componentPromise = new Promise<typeof IsLazyFunctional>(resolve=>{
setTimeout(()=>{
resolve(IsLazyFunctional);
},800);
});

/**
* For usage with import:
* const IsLazyComp = lazy<typeof import('./lazy'), LazyProps>(() => import('./lazy'));
*/
const IsLazyFunc = lazy<typeof IsLazyFunctional, LazyProps>(() => componentPromise)

// Suspense using lazy component
class SuspensefulFunc extends Component {
render() {
return <Suspense fallback={<FallBack/>}><IsLazyFunc isProp={false} /></Suspense>
}
}

0 comments on commit bb641eb

Please sign in to comment.