33 *
44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
6+ *
7+ * @flow
68 */
79
810import {
@@ -11,14 +13,64 @@ import {
1113 REACT_FORWARD_REF_TYPE ,
1214} from 'shared/ReactSymbols' ;
1315
16+ type BlockQueryFunction < Args : Iterable < any > , Data > = ( ...args : Args ) => Data ;
17+ type BlockRenderFunction < Props , Data > = (
18+ props : Props ,
19+ data : Data ,
20+ ) => React$Node ;
21+
22+ type Thenable < T , R > = {
23+ then ( resolve : ( T ) => mixed , reject : ( mixed ) => mixed ) : R ,
24+ } ;
25+
26+ type Initializer < Props , Payload , Data > = (
27+ payload : Payload ,
28+ ) =>
29+ | [ Data , BlockRenderFunction < Props , Data > ]
30+ | Thenable < [ Data , BlockRenderFunction < Props , Data > ] , mixed > ;
31+
32+ export type UninitializedBlockComponent < Props , Payload , Data > = {
33+ $$typeof : Symbol | number ,
34+ _status : - 1 ,
35+ _data : Payload ,
36+ _fn : Initializer < Props , Payload, Data> ,
37+ } ;
38+
39+ export type PendingBlockComponent < Props , Data > = {
40+ $$typeof : Symbol | number ,
41+ _status : 0 ,
42+ _data : Thenable < [ Data , BlockRenderFunction < Props , Data > ] , mixed > ,
43+ _fn : null ,
44+ } ;
45+
46+ export type ResolvedBlockComponent < Props , Data > = {
47+ $$typeof : Symbol | number ,
48+ _status : 1 ,
49+ _data : Data ,
50+ _fn : BlockRenderFunction < Props , Data> ,
51+ } ;
52+
53+ export type RejectedBlockComponent = {
54+ $$typeof : Symbol | number ,
55+ _status : 2 ,
56+ _data : mixed ,
57+ _fn : null ,
58+ } ;
59+
60+ export type BlockComponent < Props , Payload , Data > =
61+ | UninitializedBlockComponent < Props , Payload , Data >
62+ | PendingBlockComponent < Props , Data >
63+ | ResolvedBlockComponent < Props , Data >
64+ | RejectedBlockComponent ;
65+
1466opaque type Block < Props > : React$AbstractComponent <
1567 Props ,
1668 null ,
1769> = React$AbstractComponent < Props , null > ;
1870
19- export default function block < Args , Props , Data > (
20- query : ( ... args : Args ) = > Data ,
21- render : ( props : Props , data : Data ) => React$Node ,
71+ export default function block < Args : Iterable < any > , Props, Data> (
72+ query : BlockQueryFunction < Args , Data > ,
73+ render : BlockRenderFunction < Props , Data > ,
2274) : ( ...args : Args ) = > Block < Props > {
2375 if ( __DEV__ ) {
2476 if ( typeof query !== 'function' ) {
@@ -63,14 +115,19 @@ export default function block<Args, Props, Data>(
63115 ) ;
64116 }
65117 }
118+ function initializer ( args ) {
119+ let data = query . apply ( null , args ) ;
120+ return [ data , render ] ;
121+ }
66122 return function ( ) : Block < Props > {
67- let args = arguments ;
68- return {
123+ let args : Args = arguments ;
124+ let blockComponent : UninitializedBlockComponent < Props , Args , Data > = {
69125 $$typeof : REACT_BLOCK_TYPE ,
70- query : function ( ) {
71- return query . apply ( null , args ) ;
72- } ,
73- render : render ,
126+ _status : - 1 ,
127+ _data : args ,
128+ _fn : initializer ,
74129 } ;
130+ // $FlowFixMe
131+ return blockComponent ;
75132 } ;
76133}
0 commit comments