Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 45 additions & 23 deletions src/unstated.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,46 @@ export type SubscribeProps<Containers: ContainersType> = {
) => Node
};

type SubscribeState = {};
type SubscribeUpdaterProps<Containers: ContainersType> = {
instances: Array<ContainerType>,
children: (
...instances: $TupleMap<Containers, <C>(Class<C> | C) => C>
) => Node
};
type SubscribeUpdaterState = {};

const DUMMY_STATE = {};

export class Subscribe<Containers: ContainersType> extends React.Component<
SubscribeProps<Containers>,
SubscribeState
class SubscribeUpdater<Containers: ContainersType> extends React.Component<
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of splitting the Subscribe component in two components, but because of the context API, it looked like the cleaner method to handle the lifecycle of the instances (subscribe to new instances, unsubscribe from previous one when it render, etc).

SubscribeUpdaterProps<Containers>,
SubscribeUpdaterState
> {
state = {};
instances: Array<ContainerType> = [];
unmounted = false;

componentDidMount() {
this._subscribe(this.props.instances);
}

componentWillUnmount() {
this.unmounted = true;
this._unsubscribe();
this._unsubscribe(this.props.instances);
}

componentDidUpdate(prevProps: SubscribeUpdaterProps<Containers>) {
this._unsubscribe(prevProps.instances);
this._subscribe(this.props.instances);
}

_unsubscribe() {
this.instances.forEach(container => {
_subscribe(instances: Array<ContainerType>) {
instances.forEach(container => {
container.unsubscribe(this.onUpdate);
container.subscribe(this.onUpdate);
});
}

_unsubscribe(instances: Array<ContainerType>) {
instances.forEach(container => {
container.unsubscribe(this.onUpdate);
});
}
Expand All @@ -97,20 +118,26 @@ export class Subscribe<Containers: ContainersType> extends React.Component<
});
};

render() {
return this.props.children.apply(null, this.props.instances);
}
}

export class Subscribe<Containers: ContainersType> extends React.Component<
SubscribeProps<Containers>
> {
_createInstances(
map: ContainerMapType | null,
containers: ContainersType
): Array<ContainerType> {
this._unsubscribe();

if (map === null) {
throw new Error(
'You must wrap your <Subscribe> components with a <Provider>'
);
}

let safeMap = map;
let instances = containers.map(ContainerItem => {
return containers.map(ContainerItem => {
let instance;

if (
Expand All @@ -127,25 +154,20 @@ export class Subscribe<Containers: ContainersType> extends React.Component<
}
}

instance.unsubscribe(this.onUpdate);
instance.subscribe(this.onUpdate);

return instance;
});

this.instances = instances;
return instances;
}

render() {
return (
<StateContext.Consumer>
{map =>
this.props.children.apply(
null,
this._createInstances(map, this.props.to)
)
}
{map => (
<SubscribeUpdater
instances={this._createInstances(map, this.props.to)}
>
{this.props.children}
</SubscribeUpdater>
)}
</StateContext.Consumer>
);
}
Expand Down