Skip to content

Flip the arguments of Blocks and make the query optional#18374

Merged
sebmarkbage merged 2 commits into
facebook:masterfrom
sebmarkbage:optionalquery
Mar 24, 2020
Merged

Flip the arguments of Blocks and make the query optional#18374
sebmarkbage merged 2 commits into
facebook:masterfrom
sebmarkbage:optionalquery

Conversation

@sebmarkbage
Copy link
Copy Markdown
Contributor

The Query is an optional overload so I'll put that at the end instead. It also lines up with where the "data" argument goes into the render.

Originally I had some thought around how it would be better for types to infer the result of the first argument flowing into the second. Maybe as you're typing it out. But the current type systems and IDE wouldn't take advantage of this anyway so doesn't matter.

cc @devknoll

@facebook-github-bot facebook-github-bot added React Core Team Opened by a member of the React Core Team CLA Signed labels Mar 24, 2020
@codesandbox-ci
Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 38dc654:

Sandbox Source
frosty-darwin-qpy15 Configuration

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci Bot commented Mar 24, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 2fa207c:

Sandbox Source
youthful-sea-bk91q Configuration

@sizebot
Copy link
Copy Markdown

sizebot commented Mar 24, 2020

Details of bundled changes.

Comparing: a56309f...2fa207c

react

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react.development.js +0.4% +0.4% 102.02 KB 102.42 KB 24.93 KB 25.03 KB UMD_DEV
react-jsx-runtime.profiling.min.js 0.0% -0.3% 943 B 943 B 593 B 591 B NODE_PROFILING
react.production.min.js 🔺+0.5% 🔺+0.1% 12.23 KB 12.29 KB 4.77 KB 4.78 KB UMD_PROD
react-jsx-dev-runtime.development.js 0.0% -0.0% 26.26 KB 26.26 KB 8.05 KB 8.04 KB NODE_DEV
react-jsx-dev-runtime.production.min.js 0.0% -0.6% 433 B 433 B 315 B 313 B NODE_PROD
react-jsx-dev-runtime.profiling.min.js 0.0% -0.3% 432 B 432 B 314 B 313 B NODE_PROFILING
react-jsx-runtime.development.js 0.0% -0.0% 26.85 KB 26.85 KB 8.22 KB 8.22 KB NODE_DEV
react-jsx-runtime.production.min.js 0.0% -0.3% 944 B 944 B 594 B 592 B NODE_PROD
react.profiling.min.js +0.4% +0.2% 15.76 KB 15.83 KB 5.85 KB 5.86 KB UMD_PROFILING
react.development.js +0.6% +0.7% 63.71 KB 64.07 KB 17.2 KB 17.32 KB NODE_DEV
react.production.min.js 🔺+0.9% 🔺+0.1% 7.16 KB 7.22 KB 2.85 KB 2.85 KB NODE_PROD
React-dev.js +0.4% +0.5% 87.4 KB 87.77 KB 21.29 KB 21.4 KB FB_WWW_DEV
React-prod.js 🔺+0.8% 🔺+0.5% 17.14 KB 17.28 KB 4.47 KB 4.49 KB FB_WWW_PROD
React-profiling.js +0.8% +0.5% 17.14 KB 17.28 KB 4.47 KB 4.49 KB FB_WWW_PROFILING

React: size: 🔺+0.5%, gzip: 🔺+0.1%

Size changes (experimental)

Generated by 🚫 dangerJS against 2fa207c

@sizebot
Copy link
Copy Markdown

sizebot commented Mar 24, 2020

Details of bundled changes.

Comparing: a56309f...2fa207c

react

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react.development.js 0.0% -0.0% 98.68 KB 98.68 KB 24.3 KB 24.29 KB UMD_DEV
react.production.min.js 0.0% -0.0% 11.46 KB 11.46 KB 4.54 KB 4.54 KB UMD_PROD
react-jsx-runtime.development.js 0.0% -0.0% 26.84 KB 26.84 KB 8.22 KB 8.21 KB NODE_DEV
react-jsx-runtime.production.min.js 0.0% -0.2% 931 B 931 B 585 B 584 B NODE_PROD
react-jsx-runtime.profiling.min.js 0.0% -0.3% 930 B 930 B 585 B 583 B NODE_PROFILING
react.profiling.min.js 0.0% -0.0% 14.99 KB 14.99 KB 5.64 KB 5.63 KB UMD_PROFILING
react.development.js 0.0% -0.0% 60.52 KB 60.52 KB 16.59 KB 16.59 KB NODE_DEV
react.production.min.js 0.0% -0.0% 6.35 KB 6.35 KB 2.61 KB 2.61 KB NODE_PROD
React-dev.js +0.4% +0.5% 88.08 KB 88.45 KB 21.45 KB 21.55 KB FB_WWW_DEV
React-prod.js 🔺+0.8% 🔺+0.4% 17.2 KB 17.34 KB 4.49 KB 4.51 KB FB_WWW_PROD
react-jsx-dev-runtime.development.js 0.0% -0.0% 26.25 KB 26.25 KB 8.04 KB 8.04 KB NODE_DEV
React-profiling.js +0.8% +0.4% 17.2 KB 17.34 KB 4.49 KB 4.51 KB FB_WWW_PROFILING
react-jsx-dev-runtime.production.min.js 0.0% -0.7% 420 B 420 B 307 B 305 B NODE_PROD
react-jsx-dev-runtime.profiling.min.js 0.0% -0.7% 419 B 419 B 306 B 304 B NODE_PROFILING

React: size: 0.0%, gzip: -0.0%

Size changes (stable)

Generated by 🚫 dangerJS against 2fa207c

return [Symbol.for('react.server.block'), render];
}
let curriedQuery = () => {
return query(...args);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, if the query itself returns undefined, it will turn into null because of how JSON.stringify works on arrays. Fixing this would be kind of tricky but you're not really supposed to return undefined from a query anyway. We could warn for that same as we warn if you return undefined from render.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could warn for that same as we warn if you return undefined from render.

yeah let's warn in that case

Comment thread packages/react/src/ReactBlock.js Outdated
export function block<Args: Iterable<any>, Props, Data>(
query: BlockQueryFunction<Args, Data>,
render: BlockRenderFunction<Props, Data>,
query?: BlockQueryFunction<Args, Data>,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When declared for external users, the overloaded type is:

declare export function block<Args: Iterable<any>, Props, Data>(
  render: BlockRenderFunction<Props, Data>,
  query: BlockQueryFunction<Args, Data>,
): (...args: Args) => Block<Props>;
declare export function block<Props>(
  render: BlockRenderFunction<Props, void>,
): () => Block<Props>;

@sebmarkbage
Copy link
Copy Markdown
Contributor Author

sebmarkbage commented Mar 24, 2020

I also renamed Query to load. That's really just a convention but that's the verb we use on the importing side by convention so I used that to mirror that part of the API.

It is lower case because the significant function is the render function which is required. It gets to be the name of the component for debugging purposes. The load function is an additional extra.

}

let loadUser = block(Query, User);
let loadUser = block(User, load);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate that the order gets reversed from the grammar. User + load = loadUser.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just a naming convention that we could change, though. It's probably helpful to use different names for the result of block, since calling loadUser here doesn't directly call load.

Copy link
Copy Markdown
Member

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense

}
let loadUser = block(User);
let model = {
User: loadUser('Seb', 'Smith'),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the type signature for loadUser here would disallow args, though, right?

}

let loadUser = block(Query, User);
let loadUser = block(User, load);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just a naming convention that we could change, though. It's probably helpful to use different names for the result of block, since calling loadUser here doesn't directly call load.

@sebmarkbage sebmarkbage merged commit a317bd0 into facebook:master Mar 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants