Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- componentDidUpdate(prevProps, prevState) {
- const {state, props} = this;
- const current_id = props.item && props.item.id;
- const prev_id = prevProps.item && prevProps.item.id;
- if (current_id !== prev_id) {
- this.stop_polling();
- this.setState(this.get_initial_state(), () => {
- this.start_polling();
- });
- } else {
- const prev_poll = this.should_poll(prevProps, prevState);
- const next_poll = this.should_poll(props, state);
- if (prev_poll !== next_poll) {
- if (next_poll) {
- this.start_polling();
- } else {
- this.stop_polling();
- }
- }
- }
- }
- start_polling = () => {
- if (this.should_poll(this.props, this.state)) {
- this.poll();
- }
- };
- poll = () => {
- let still_polling = true;
- if (this.cancel_poll_request) {
- this.cancel_poll_request();
- }
- const cancel_poll_promise = new Promise((resolve) => {
- this.cancel_poll_request = resolve; });
- cancel_poll_promise.then(() => {
- still_polling = false;
- this.cancel_poll_request = undefined;
- clearTimeout(this.poll_timeout);
- });
- const items_promise = client.get_items(this.props.item.id,
- cancel_poll_promise);
- items_promise.then((request) => {
- const next_items = [];
- let something_changed = false;
- for (const new_item of request.response) {
- const old_item = this.state.items.find(
- old_item =>
- old_item.id === new_item.id);
- if (old_item) {
- next_items.push(old_item);
- } else {
- something_changed = true;
- next_items.push(new_item);
- }
- }
- if (something_changed) {
- const next_state = {items: next_items};
- this.setState(next_state);
- }
- }).then(() => {
- if (still_polling) {
- this.poll_timeout = setTimeout(this.poll, 100s);
- }
- }).catch(this.props.notifications.request_error);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement