Skip to content

Commit

Permalink
feat(code): hoist on init (#11)
Browse files Browse the repository at this point in the history
Allow components to subscribe to init, and hoist statics when dynamic import has finished loading.
  • Loading branch information
adam-26 committed Mar 1, 2018
2 parents 2c052a8 + bb774b0 commit ed1de4c
Show file tree
Hide file tree
Showing 5 changed files with 999 additions and 21 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -754,6 +754,17 @@ const ChunkComponent = chunk({...});
ChunkComponent.getChunkLoader();
```
#### `hoistOnInit(getComponent: () => node): void`
This is a static method that can be used to subscribe components to which statics will be hoisted on initialization.
Note: this requires `hoistStatics: true`
```js
const ChunkComponent = chunk({...});

ChunkComponent.hoistOnInit(() => MyParentHOC);
```
### `WrappedComponent`
Expand Down
14 changes: 14 additions & 0 deletions __tests__/test.js
Expand Up @@ -325,6 +325,20 @@ describe('chunk', () => {
delete MyComponent.myStatic;
});

test('hoists statics on init', async () => {
function HOC() {}

const jestFn = jest.fn();
MyComponent.myStatic = jestFn;
let ChunkMyComponent = chunk(createLoader(400, () => MyComponent), { hoistStatics: true })();
ChunkMyComponent.hoistOnInit(() => HOC);

await preloadAll();

expect(HOC.myStatic).toBe(jestFn);
delete MyComponent.myStatic;
});

test('does not apply statics on error', async () => {
let ChunkMyComponent = chunk(createLoader(400, () => { throw new Error(); }), {
hoistStatics: true
Expand Down

0 comments on commit ed1de4c

Please sign in to comment.