azure-devops-ui
Version:
React components for building web UI in Azure DevOps
1 lines • 2.61 kB
JavaScript
import"../../CommonImports";import"../../Core/core.css";import*as React from"react";import{ObservableLike}from"../../Core/Observable";class ObserverBase extends React.Component{constructor(e){super(e),this.subscriptions={};var s={values:{},oldProps:{}};for(const t in e)s.values[t]=getPropValue(e[t]);this.state=s}static getDerivedStateFromProps(e,s){s=updateSubscriptionsAndState(s.oldProps,e,s);return null!=s?Object.assign(Object.assign({},s),{oldProps:e}):{oldProps:e}}render(){var e,s={};for(const t in this.state.values)"children"!==t&&(s[t]=this.state.values[t]);return"function"==typeof this.props.children?(0,this.props.children)(s):(e=React.Children.only(this.props.children),React.cloneElement(e,Object.assign(Object.assign({},e.props),s),e.props.children))}componentDidMount(){this.updateSubscriptionsAndStateAfterRender()}componentDidUpdate(){this.updateSubscriptionsAndStateAfterRender(),this.props.onUpdate&&this.props.onUpdate()}componentWillUnmount(){for(const e in this.subscribedProps)this.unsubscribe(e,this.subscribedProps)}subscribe(r,i){if("children"!==r){let e,s=i[r],t;s&&void 0!==s.observableValue&&(s=(e=s).observableValue,t=e.action),ObservableLike.isObservable(s)&&(i=this.onValueChanged.bind(this,r,s,e),ObservableLike.subscribe(s,i,t),this.subscriptions[r]={delegate:i,action:t})}}unsubscribe(e,s){var t;"children"!==e&&(s=getObservableValue(s[e]),ObservableLike.isObservable(s))&&(t=this.subscriptions[e],ObservableLike.unsubscribe(s,t.delegate,t.action),delete this.subscriptions[e])}updateSubscriptionsAndStateAfterRender(){var e=updateSubscriptionsAndState(this.subscribedProps,this.props,this.state,this);null!=e&&this.setState(e),this.subscribedProps=Object.assign({},this.props)}onValueChanged(t,r,e,i,s){let a=!0;t in this.subscriptions&&(a=e&&e.filter?e.filter(i,s):a)&&this.setState((e,s)=>({values:Object.assign(Object.assign({},e.values),{[t]:r.value||i})}))}}function getObservableValue(e){return e&&void 0!==e.observableValue?e.observableValue:e}function getPropValue(e){return ObservableLike.getValue(getObservableValue(e))}function updateSubscriptionsAndState(e,s,t,r){var i=Object.assign({},t);let a=!1;if(e)for(const u in e){var n=getObservableValue(e[u]),o=getObservableValue(s[u]);n!==o&&(r&&r.unsubscribe(u,e),void 0===o)&&(delete i.values[u],a=!0)}for(const c in s){var b=e&&getObservableValue(e[c]),l=getObservableValue(s[c]);b!==l&&(r&&r.subscribe(c,s),t.values[c]!==getPropValue(l))&&(i.values[c]=getPropValue(l),a=!0)}return a?i:null}class Observer extends ObserverBase{}class UncheckedObserver extends ObserverBase{}export{Observer,UncheckedObserver};