Skip to content

Commit

Permalink
feat(hoc): replace dependency HOC implementation with non-curried fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
protoman92 committed Sep 25, 2018
1 parent 80710a0 commit 78b567e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 59 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-base-utilities-js",
"version": "1.4.3",
"version": "1.4.4",
"description": "Utilities for React applications.",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
Expand Down
105 changes: 51 additions & 54 deletions src/hoc.dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,70 +48,67 @@ export function withDependency<
}>,
Props extends DependencyHOCProps<Dependency>
>(
targetComponent: TargetDependencyHOCComponent<Dependency, Props, State>,
options: DependencyHOCOptions<Dependency, Props>
): (
targetComponent: TargetDependencyHOCComponent<Dependency, Props, State>
) => ComponentType<FactorifiedDependencyHOCProps<Props>> {
return targetComponent => {
type PureProps = Omit<Props, 'dependency'>;
type WrapperProps = PureProps & DependencyFactoryHOCProps;
let {createDependency, dependencyHooks} = options;
let displayName = getComponentDisplayName(targetComponent);

return class DependencyWrapper extends Component<WrapperProps, State> {
public static displayName = `${displayName}_DependencyWrapper`;

private readonly dependency: Dependency;
private readonly subscription?: Subscription;

public constructor(props: WrapperProps) {
super(props);

if (dependencyHooks && dependencyHooks.beforeDependencyCreated) {
dependencyHooks.beforeDependencyCreated();
}

let dependency = createDependency(props);
this.dependency = dependency;

/* istanbul ignore else */
if (dependency.stateStream) {
this.subscription = new Subscription();
}
}
): ComponentType<FactorifiedDependencyHOCProps<Props>> {
type PureProps = Omit<Props, 'dependency'>;
type WrapperProps = PureProps & DependencyFactoryHOCProps;
let {createDependency, dependencyHooks} = options;
let displayName = getComponentDisplayName(targetComponent);

return class DependencyWrapper extends Component<WrapperProps, State> {
public static displayName = `${displayName}_DependencyWrapper`;

private readonly dependency: Dependency;
private readonly subscription?: Subscription;

public componentDidMount() {
let {dependency, subscription} = this;
public constructor(props: WrapperProps) {
super(props);

/* istanbul ignore else */
if (dependency.stateStream && subscription) {
subscription.add(
dependency.stateStream.subscribe(s => this.setState(s))
);
}
if (dependencyHooks && dependencyHooks.beforeDependencyCreated) {
dependencyHooks.beforeDependencyCreated();
}

public componentWillUnmount() {
/* istanbul ignore else */
if (this.dependency.performCleanUp) {
this.dependency.performCleanUp();
}
let dependency = createDependency(props);
this.dependency = dependency;

/* istanbul ignore else */
if (this.subscription) {
this.subscription.unsubscribe();
}
/* istanbul ignore else */
if (dependency.stateStream) {
this.subscription = new Subscription();
}
}

public render() {
let actualProps = Object.assign(
Objects.deleteKeys(this.props, 'dependencyFactory'),
{dependency: this.dependency},
this.state
public componentDidMount() {
let {dependency, subscription} = this;

/* istanbul ignore else */
if (dependency.stateStream && subscription) {
subscription.add(
dependency.stateStream.subscribe(s => this.setState(s))
);
}
}

return React.createElement(targetComponent, actualProps as any);
public componentWillUnmount() {
/* istanbul ignore else */
if (this.dependency.performCleanUp) {
this.dependency.performCleanUp();
}
};

/* istanbul ignore else */
if (this.subscription) {
this.subscription.unsubscribe();
}
}

public render() {
let actualProps = Object.assign(
Objects.deleteKeys(this.props, 'dependencyFactory'),
{dependency: this.dependency},
this.state
);

return React.createElement(targetComponent, actualProps as any);
}
};
}
11 changes: 7 additions & 4 deletions test/hoc.dependency.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ describe('View model HOC should work correctly', () => {
});

// tslint:disable-next-line:variable-name
let HOCTestComponent = withDependency<State, Dependency, DependencyProps>({
dependencyHooks: instance(dependencyHooks),
createDependency: props => (props.dependencyFactory as any)(),
})(DependencyTestComponent);
let HOCTestComponent = withDependency<State, Dependency, DependencyProps>(
DependencyTestComponent,
{
dependencyHooks: instance(dependencyHooks),
createDependency: props => (props.dependencyFactory as any)(),
}
);

componentIndex = 1000;

Expand Down

0 comments on commit 78b567e

Please sign in to comment.