Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hoc): adding hoc support #1

Merged
merged 10 commits into from
Jan 31, 2019
Merged

feat(hoc): adding hoc support #1

merged 10 commits into from
Jan 31, 2019

Conversation

mfrachet
Copy link
Contributor

  • Pull request has tests / docs demo, and is linted.

In the React universe, when we try to aggregate behaviors or avoid computation repetition, we use to rely on 3 possibilites:

  • render props (the default behavior you've provided)
  • hoc (the one I'm providing with this PR)
  • hooks (one that I will work on soon)

This PR is a suggestion to add a function withWasm that allows to wrap a component inside the Wasm one using the HoC approach.

@mbasso
Copy link
Owner

mbasso commented Jan 23, 2019

Hi @mfrachet,
thank you so much for opening this issue, I really appreciate it 😄
I like the idea of providing an HoC and also an hook!
I'll submit a review soon about a couple of things.

I'd also like to discuss the signature of the HoC, the current implementation works like this:

// define a component
const Example = ({ loading, error, data }) => {
  if (loading) return "Loading...";
  if (error) return "An error has occurred";

  const { module, instance } = data;
  return <div>1 + 2 = {instance.exports.add(1, 2)}</div>;
};

// wrap it with the HoC
const WasmExample = withWasm(Example);

// we can use it like this
<WasmExample url="/add.wasm" {...otherProps} />

I think we might also consider an alternative solution that allows us to create HoCs that use wasm modules. In this way we can create, for example, libraries or modules that exports them instead of a component that provides constants urls and so on.

// define a component
const Example = ({ loading, error, add }) => {
  if (loading) return "Loading...";
  if (error) return "An error has occurred";

  const { module, instance } = data;
  return <div>1 + 2 = {add(1, 2)}</div>;
};

// define an HoC based on our helper
// withWasm(
//     wasmPropsProvider = props => props,
//     mapProps = props => props
// )
const withAdd = withWasm(
  // callback that takes the props and returns the ones used to instantiate the Wasm component
  (/* props */) => ({
    url: '/add.wasm'
  }),

  // map Wasm-provided props to the ones to provide to the decorated component
  ({ loading, error, data }) => ({
    loading,
    error,
    add: data && data.instance.exports.add
  })
);

// wrap the component with the HoC
const WasmExample = withAdd(Example);

// we can use it like this
<WasmExample {...otherProps} />

With this signature, using the default values, we can also achieve the same behaviour that you have provided:

const WasmExample = withWasm()(Example);

<WasmExample url="/add.wasm" {...otherProps} />

What do you think about it? Does it make sense?

README.md Outdated Show resolved Hide resolved
src/withWasm.js Outdated Show resolved Hide resolved
test/withWasm.spec.js Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@mbasso
Copy link
Owner

mbasso commented Jan 31, 2019

I'll merge and publish a new release soon! Thank you!

@mbasso mbasso merged commit 10eed53 into mbasso:master Jan 31, 2019
@mbasso mbasso mentioned this pull request Feb 1, 2019
@mbasso
Copy link
Owner

mbasso commented Feb 1, 2019

[email protected] is now online 🎉 Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants