UNPKG

3.47 kBJavaScriptView Raw
1/*
2 * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __extends } from "tslib";
17import * as React from "react";
18import { isNodeEnv } from "./utils";
19/**
20 * An abstract component that Blueprint components can extend
21 * in order to add some common functionality like runtime props validation.
22 *
23 * @deprecated componentWillReceiveProps is deprecated in React 16.9; use AbstractPureComponent2 instead
24 */
25// eslint-disable-next-line @typescript-eslint/ban-types
26var AbstractPureComponent = /** @class */ (function (_super) {
27 __extends(AbstractPureComponent, _super);
28 function AbstractPureComponent(props, context) {
29 var _this = _super.call(this, props, context) || this;
30 // Not bothering to remove entries when their timeouts finish because clearing invalid ID is a no-op
31 _this.timeoutIds = [];
32 /**
33 * Clear all known timeouts.
34 */
35 _this.clearTimeouts = function () {
36 if (_this.timeoutIds.length > 0) {
37 for (var _i = 0, _a = _this.timeoutIds; _i < _a.length; _i++) {
38 var timeoutId = _a[_i];
39 window.clearTimeout(timeoutId);
40 }
41 _this.timeoutIds = [];
42 }
43 };
44 if (!isNodeEnv("production")) {
45 _this.validateProps(_this.props);
46 }
47 return _this;
48 }
49 AbstractPureComponent.prototype.componentWillReceiveProps = function (nextProps) {
50 if (!isNodeEnv("production")) {
51 this.validateProps(nextProps);
52 }
53 };
54 AbstractPureComponent.prototype.componentWillUnmount = function () {
55 this.clearTimeouts();
56 };
57 /**
58 * Set a timeout and remember its ID.
59 * All stored timeouts will be cleared when component unmounts.
60 *
61 * @returns a "cancel" function that will clear timeout when invoked.
62 */
63 AbstractPureComponent.prototype.setTimeout = function (callback, timeout) {
64 var handle = window.setTimeout(callback, timeout);
65 this.timeoutIds.push(handle);
66 return function () { return window.clearTimeout(handle); };
67 };
68 /**
69 * Ensures that the props specified for a component are valid.
70 * Implementations should check that props are valid and usually throw an Error if they are not.
71 * Implementations should not duplicate checks that the type system already guarantees.
72 *
73 * This method should be used instead of React's
74 * [propTypes](https://facebook.github.io/react/docs/reusable-components.html#prop-validation) feature.
75 * Like propTypes, these runtime checks run only in development mode.
76 */
77 AbstractPureComponent.prototype.validateProps = function (_props) {
78 // implement in subclass
79 };
80 return AbstractPureComponent;
81}(React.PureComponent));
82export { AbstractPureComponent };
83//# sourceMappingURL=abstractPureComponent.js.map
\No newline at end of file