Skip to content

Commit

Permalink
Removing the need to use JSX
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipe Versehgi committed Apr 23, 2019
1 parent e9ae8ad commit 10b74a8
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions lib/plug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as React from 'react';
import { Component } from 'react';
import { Subscription } from 'rxjs';
import { TStream, ILifecycle, IPlugState } from './interfaces/plug';

export const plug = <T extends {}>(stream: TStream, lifecycleHooks: Partial<ILifecycle> = {},
) => (WrappedComponent: any) => {
const factory = React.createFactory(WrappedComponent);

return class PluggedComponent extends Component<T, IPlugState> {

_streamSubscription!: Subscription;

constructor(props: any) {
super(props);

this.state = {
hasEmmited: false,
};
}

public componentDidMount() {
this._streamSubscription = stream(this.props).subscribe((data: any) =>
this.setState({
hasEmmited: true,
innerData: data,
}),
);

if (lifecycleHooks.componentDidMount) {
lifecycleHooks.componentDidMount();
}
}

public componentWillUnmount() {
this._streamSubscription.unsubscribe();

if (lifecycleHooks.componentWillUnmount) {
lifecycleHooks.componentWillUnmount();
}
}

public render() {
const { hasEmmited, innerData } = this.state;

if (hasEmmited) {
return factory({...this.props, ...innerData});
}

return null;
}
}
}

0 comments on commit 10b74a8

Please sign in to comment.