Skip to content

Commit 757e5bb

Browse files
authored
Merge dd059d6 into ebb0d10
2 parents ebb0d10 + dd059d6 commit 757e5bb

File tree

9 files changed

+613
-539
lines changed

9 files changed

+613
-539
lines changed

.eslintrc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
"react/jsx-filename-extension": "off",
2424
"react/jsx-uses-react": 2,
2525
"react/jsx-uses-vars": 2,
26-
"react/react-in-jsx-scope": 2
26+
"react/react-in-jsx-scope": 2,
27+
"react-hooks/rules-of-hooks": "error"
2728
},
2829
"plugins": [
2930
"flowtype",
30-
"react"
31+
"react",
32+
"react-hooks"
3133
],
3234
"extends": [
3335
"plugin:flowtype/recommended",
@@ -55,4 +57,4 @@
5557
"onlyFilesWithFlowAnnotation": true
5658
}
5759
}
58-
}
60+
}

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"@babel/register": "^7.0.0",
7676
"babel-core": "7.0.0-bridge.0",
7777
"babel-eslint": "^9.0.0",
78-
"babel-jest": "23.6.0",
78+
"babel-jest": "^24.1.0",
7979
"babel-loader": "^8.0.0",
8080
"babel-preset-minify": "^0.4.3",
8181
"compression-webpack-plugin": "1.0.0",
@@ -88,19 +88,20 @@
8888
"eslint-plugin-import": "2.14.0",
8989
"eslint-plugin-jsx-a11y": "6.1.1",
9090
"eslint-plugin-react": "7.11.1",
91-
"flow-bin": "0.89.0",
92-
"jest": "23.6.0",
91+
"eslint-plugin-react-hooks": "^1.0.1",
92+
"flow-bin": "^0.92.1",
93+
"jest": "^24.1.0",
9394
"nyc": "10.3.0",
9495
"prettier": "1.15.3",
9596
"prettier-eslint": "8.8.2",
96-
"react": "^16.7.0",
97-
"react-dom": "^16.7.0",
98-
"react-test-renderer": "16.7.0",
97+
"react": "^16.8.0",
98+
"react-dom": "^16.8.0",
99+
"react-test-renderer": "^16.8.0",
99100
"rimraf": "2.6.3",
100101
"webpack": "3.5.5"
101102
},
102103
"peerDependencies": {
103-
"react": "^16.7.0"
104+
"react": "^16.8.0"
104105
},
105106
"npmName": "react-wasm",
106107
"npmFileMap": [
@@ -119,4 +120,4 @@
119120
"dependencies": {
120121
"@babel/runtime": "7.2.0"
121122
}
122-
}
123+
}

src/Wasm.js

Lines changed: 20 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,22 @@
11
// @flow
22

3-
import React from 'react';
4-
import compileWasm from './compileWasm';
5-
import type { WasmProps, WasmState, WasmParams } from './types';
6-
7-
export default class Wasm extends React.Component<WasmProps, WasmState> {
8-
static defaultProps = {
9-
url: null,
10-
bufferSource: null,
11-
importObject: {}
12-
};
13-
14-
state = {
15-
loading: true,
16-
error: null,
17-
data: null,
18-
// eslint-disable-next-line
19-
prevProps: {
20-
url: null,
21-
bufferSource: null
22-
}
23-
};
24-
25-
static getDerivedStateFromProps(props: WasmProps, state: WasmState) {
26-
const { url, bufferSource } = props;
27-
28-
if (
29-
url && url !== state.prevProps.url ||
30-
!url && bufferSource !== state.prevProps.bufferSource
31-
) {
32-
return {
33-
loading: true,
34-
error: null,
35-
data: null,
36-
prevProps: {
37-
url,
38-
bufferSource
39-
}
40-
};
41-
}
42-
43-
return null;
44-
}
45-
46-
componentDidMount() {
47-
const { url, bufferSource, importObject } = this.props;
48-
49-
this.loadModule({ url, bufferSource, importObject });
50-
}
51-
52-
componentDidUpdate(prevProps: WasmProps) {
53-
const { url, bufferSource, importObject } = this.props;
54-
55-
if (
56-
url && url !== prevProps.url ||
57-
!url && bufferSource !== prevProps.bufferSource
58-
) {
59-
this.loadModule({ url, bufferSource, importObject });
60-
}
61-
}
62-
63-
loadModule = ({
64-
url,
65-
bufferSource,
66-
importObject
67-
}: WasmParams) =>
68-
compileWasm({
69-
url,
70-
bufferSource,
71-
importObject
72-
})
73-
.then(({ module, instance }) => {
74-
this.setState({
75-
loading: false,
76-
error: null,
77-
data: {
78-
module,
79-
instance
80-
}
81-
});
82-
})
83-
.catch(error => {
84-
this.setState({
85-
loading: false,
86-
error,
87-
data: null
88-
});
89-
});
90-
91-
render() {
92-
const { children } = this.props;
93-
const { loading, error, data } = this.state;
94-
95-
return children({ loading, error, data });
96-
}
97-
}
3+
import useWasm from './useWasm';
4+
import type { WasmProps } from './types';
5+
6+
const Wasm = ({ url, bufferSource, importObject, children }: WasmProps) => {
7+
const state = useWasm({
8+
url,
9+
bufferSource,
10+
importObject
11+
});
12+
13+
return children(state);
14+
};
15+
16+
Wasm.defaultProps = {
17+
url: null,
18+
bufferSource: null,
19+
importObject: {}
20+
};
21+
22+
export default Wasm;

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// @flow
22

33
import Wasm from './Wasm';
4+
import useWasmDefinition from './useWasm';
45
import withWasmDefinition from './withWasm';
56

6-
export default Wasm;
7-
7+
export const useWasm = useWasmDefinition;
88
export const withWasm = withWasmDefinition;
9+
10+
export default Wasm;

src/useWasm.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// @flow
2+
3+
import { useState, useEffect } from 'react';
4+
import compileWasm from './compileWasm';
5+
import type { WasmParams } from './types';
6+
7+
const useWasm = ({
8+
url,
9+
bufferSource,
10+
importObject
11+
}: WasmParams) => {
12+
const [state, setState] = useState({
13+
loading: true,
14+
error: null,
15+
data: null
16+
});
17+
18+
const [prevProps, setPrevProps] = useState({
19+
url: null,
20+
bufferSource: null
21+
});
22+
23+
let newState = state;
24+
25+
if (
26+
(url && url !== prevProps.url) ||
27+
(!url && bufferSource !== prevProps.bufferSource)
28+
) {
29+
newState = {
30+
loading: true,
31+
error: null,
32+
data: null
33+
};
34+
35+
setState(newState);
36+
setPrevProps({
37+
url,
38+
bufferSource
39+
});
40+
}
41+
42+
useEffect(() => {
43+
compileWasm({
44+
url,
45+
bufferSource,
46+
importObject
47+
})
48+
.then(({ module, instance }) => {
49+
setState({
50+
loading: false,
51+
error: null,
52+
data: {
53+
module,
54+
instance
55+
}
56+
});
57+
})
58+
.catch(ex => {
59+
setState({
60+
loading: false,
61+
error: ex,
62+
data: null
63+
});
64+
});
65+
}, [
66+
url,
67+
!url && bufferSource,
68+
importObject,
69+
setState
70+
]);
71+
72+
return newState;
73+
}
74+
75+
export default useWasm;

src/withWasm.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import React from 'react';
2-
import Wasm from '.';
2+
import useWasm from './useWasm';
33

4-
const withWasm = (config = {}, mapToChild) => ComponentDefinition => ({
4+
const withWasm = (config = {}, mapToChild = x => x) => ComponentDefinition => ({
55
url,
66
bufferSource,
77
importObject,
88
...otherProps
9-
}) => (
10-
<Wasm
11-
url={url}
12-
bufferSource={bufferSource}
13-
importObject={importObject}
14-
{...config}
15-
>
16-
{wasmData => {
17-
const wasmProps = mapToChild ? mapToChild(wasmData) : wasmData;
9+
}) => {
10+
const state = useWasm({
11+
url,
12+
bufferSource,
13+
importObject,
14+
...config
15+
});
1816

19-
return <ComponentDefinition {...otherProps} {...wasmProps} />;
20-
}}
21-
</Wasm>
22-
);
17+
return (
18+
<ComponentDefinition
19+
{...otherProps}
20+
{...mapToChild(state)}
21+
/>
22+
);
23+
};
2324

2425
export default withWasm;

0 commit comments

Comments
 (0)