Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
"typescript": "tsc -p tsconfig.json"
},
"dependencies": {
"create-react-context": "^0.1.5",
"tickedoff": "^1.0.1"
"create-react-context": "^0.1.5"
},
"peerDependencies": {
"prop-types": "^15.5.0",
"react": "^15.0.0 || ^16.0.0"
},
"devDependencies": {
Expand Down
10 changes: 6 additions & 4 deletions src/unstated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import * as React from 'react';
export class Container<State extends object> {
state: State;
setState<K extends keyof State>(
state: ((prevState: Readonly<State>) => (Partial<State> | State | null)) | (Partial<State> | State | null),
state:
| ((prevState: Readonly<State>) => Partial<State> | State | null)
| (Partial<State> | State | null),
callback?: () => void
): void;
subscribe(fn: Function): void;
unsubscribe(fn: Function): void;
): Promise<void>;
subscribe(fn: () => any): void;
unsubscribe(fn: () => any): void;
}

export interface ContainerType<State extends object> {
Expand Down
38 changes: 11 additions & 27 deletions src/unstated.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// @flow
import React, { type Node } from 'react';
import createReactContext from 'create-react-context';
import PropTypes from 'prop-types';
import defer from 'tickedoff';

type Listener = (cb?: () => void) => void;
type Listener = () => mixed;

const StateContext = createReactContext(null);

Expand All @@ -19,8 +17,8 @@ export class Container<State: {}> {
setState(
updater: $Shape<State> | ((prevState: $Shape<State>) => $Shape<State>),
callback?: () => void
) {
defer(() => {
): Promise<void> {
return Promise.resolve().then(() => {
let nextState;

if (typeof updater === 'function') {
Expand All @@ -36,23 +34,12 @@ export class Container<State: {}> {

this.state = Object.assign({}, this.state, nextState);

let completed = 0;
let total = this._listeners.length;
let promises = this._listeners.map(listener => listener());

this._listeners.forEach(fn => {
if (!callback) {
fn();
return;
return Promise.all(promises).then(() => {
if (callback) {
return callback();
Copy link

Choose a reason for hiding this comment

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

If something inside the callback function throws then the error might get swallowed.

}

let safeCallback = callback;

fn(() => {
completed++;
if (completed < total) {
safeCallback();
}
});
});
});
}
Expand Down Expand Up @@ -85,11 +72,6 @@ export class Subscribe<Containers: ContainersType> extends React.Component<
SubscribeProps<Containers>,
SubscribeState
> {
static propTypes = {
to: PropTypes.array.isRequired,
children: PropTypes.func.isRequired
};

state = {};
instances: Array<ContainerType> = [];

Expand All @@ -103,8 +85,10 @@ export class Subscribe<Containers: ContainersType> extends React.Component<
});
}

onUpdate: Listener = cb => {
this.setState(DUMMY_STATE, cb);
onUpdate: Listener = () => {
return new Promise(resolve => {
Copy link

@xat xat Apr 25, 2018

Choose a reason for hiding this comment

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

why not just return this.setState(DUMMY_STATE); (instead of creating a new promise)?

Copy link
Owner Author

Choose a reason for hiding this comment

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

because then we dont wait for the callback

this.setState(DUMMY_STATE, resolve);
});
};

_createInstances(
Expand Down