Skip to content

Commit

Permalink
Removed state change from Carousel.running.
Browse files Browse the repository at this point in the history
Fixed setState in PromiseUtil.
Updated to [email protected].
  • Loading branch information
sjohnsonaz committed Jun 9, 2020
1 parent 2039dca commit 10c37fd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 51 deletions.
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@cascade/promise-util": "0.0.1"
},
"peerDependencies": {
"artistry": "^0.9.3",
"artistry": "^0.9.7",
"react": "16.11.0",
"react-dom": "16.11.0"
},
Expand All @@ -36,7 +36,7 @@
"@types/mocha": "5.2.7",
"@types/react": "16.9.9",
"@types/react-dom": "16.9.2",
"artistry": "^0.9.3",
"artistry": "^0.9.7",
"autoprefixer": "9.7.4",
"chai": "4.2.0",
"css-loader": "3.4.2",
Expand Down
52 changes: 18 additions & 34 deletions src/scripts/components/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ export interface ICarouselProps {
}

export interface ICarouselState {
height: string;
activeIndex: number;
previousActiveIndex: number;
running: boolean;
animating: boolean;
selected: boolean;
height?: string;
activeIndex?: number;
previousActiveIndex?: number;
animating?: boolean;
selected?: boolean;
}

export default class Carousel extends React.Component<ICarouselProps, ICarouselState> {
Expand All @@ -31,10 +30,10 @@ export default class Carousel extends React.Component<ICarouselProps, ICarouselS
height: undefined,
activeIndex: this.props.activeIndex,
previousActiveIndex: this.props.activeIndex,
running: false,
animating: false,
selected: true
};
running: boolean = false;
runCount: number = 0;
transitionCount: number = 0;

Expand All @@ -43,8 +42,7 @@ export default class Carousel extends React.Component<ICarouselProps, ICarouselS
this.transitionCount++;
this.transitionCount %= 2;
if (!this.transitionCount) {
let running = this.state.running;
if (!running) {
if (!this.running) {
this.setState({
animating: false,
height: undefined,
Expand All @@ -55,25 +53,6 @@ export default class Carousel extends React.Component<ICarouselProps, ICarouselS
}
}

async startRun() {
await setState({ running: true }, this);
}

async stopRun() {
if (this.state.running) {
if (this.props.staticHeight) {
await setState({
running: false,
animating: false,
height: undefined,
previousActiveIndex: this.state.activeIndex
}, this);
} else {
await setState({ running: false }, this);
}
}
}

async componentWillReceiveProps(nextProps?: ICarouselProps) {
let { activeIndex } = nextProps;
let { activeIndex: previousActiveIndex } = this.props;
Expand Down Expand Up @@ -105,11 +84,7 @@ export default class Carousel extends React.Component<ICarouselProps, ICarouselS
let runCount = this.runCount;

// Start run
await setState({ runCount: runCount }, this);
await this.startRun();
if (runCount !== this.runCount) {
return;
}
this.running = true;

let node: HTMLDivElement;
if (!this.props.staticHeight) {
Expand Down Expand Up @@ -164,7 +139,16 @@ export default class Carousel extends React.Component<ICarouselProps, ICarouselS
}

// Stop run
await this.stopRun();
if (this.running) {
if (this.props.staticHeight) {
await setState({
animating: false,
height: undefined,
previousActiveIndex: this.state.activeIndex
}, this);
}
this.running = false;
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/scripts/components/Closeable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export interface ICloseableProps {
}

export interface ICloseableState {
closed: boolean;
running: boolean;
animating: boolean;
height: string;
closed?: boolean;
running?: boolean;
animating?: boolean;
height?: string;
}

export default class Closeable extends React.Component<ICloseableProps, ICloseableState> {
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/util/PromiseUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function waitAnimation(time?: number) {
});
}

export function setState<T>(state: T, thisArg: React.Component): Promise<void> {
export function setState<T>(state: T, thisArg: React.Component<any, T>): Promise<void> {
return new Promise((resolve) => {
thisArg.setState(state, () => {
resolve();
Expand Down

0 comments on commit 10c37fd

Please sign in to comment.