From e05fe77352ac76522468560639195b7f26fcd4d1 Mon Sep 17 00:00:00 2001 From: liaoxuezhi Date: Wed, 28 Aug 2019 15:32:21 +0800 Subject: [PATCH] =?UTF-8?q?Task=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=20checkApi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderers/Tasks.tsx | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/renderers/Tasks.tsx b/src/renderers/Tasks.tsx index 9e30b935..e2e8be4f 100644 --- a/src/renderers/Tasks.tsx +++ b/src/renderers/Tasks.tsx @@ -5,7 +5,8 @@ import cx from 'classnames'; import getExprProperties from '../utils/filter-schema'; import {Api, Payload} from '../types'; import update = require('react-addons-update'); -import {isEffectiveApi} from '../utils/api'; +import {isEffectiveApi, isApiOutdated} from '../utils/api'; +import { ScopedContext, IScopedContext } from '../Scoped'; export interface TaskProps extends RendererProps { className?: string; @@ -81,6 +82,10 @@ export default class Task extends React.Component { this.tick = this.tick.bind(this); } + componentDidMount() { + this.tick(!!this.props.checkApi); + } + componentWillReceiveProps(nextProps: TaskProps) { const props = this.props; @@ -91,17 +96,26 @@ export default class Task extends React.Component { } } - componentDidMount() { - this.tick(!!this.props.checkApi); + componentDidUpdate(prevProps: TaskProps) { + const props = this.props; + + if (isApiOutdated(prevProps.checkApi, props.checkApi, prevProps.data, props.data)) { + this.tick(true); + } } componentWillUnmount() { clearTimeout(this.timer); } + reload() { + this.tick(true); + } + tick(force = false) { const {loadingStatusCode, data, interval, checkApi, env} = this.props; const items = this.state.items; + clearTimeout(this.timer); // 如果每个 task 都完成了, 则不需要取查看状态. if (!force && !items.some(item => item.status === loadingStatusCode)) { @@ -298,4 +312,18 @@ export default class Task extends React.Component { test: /(^|\/)tasks$/, name: 'tasks', }) -export class TaskRenderer extends Task {} +export class TaskRenderer extends Task { + static contextType = ScopedContext; + + componentWillMount() { + // super.componentWillMount(); + const scoped = this.context as IScopedContext; + scoped.registerComponent(this); + } + + componentWillUnmount() { + super.componentWillUnmount(); + const scoped = this.context as IScopedContext; + scoped.unRegisterComponent(this); + } +}