UNPKG

3.36 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 AbstractComponent2 instead
24 */
25var AbstractComponent = /** @class */ (function (_super) {
26 __extends(AbstractComponent, _super);
27 function AbstractComponent(props, context) {
28 var _this = _super.call(this, props, context) || this;
29 // Not bothering to remove entries when their timeouts finish because clearing invalid ID is a no-op
30 _this.timeoutIds = [];
31 /**
32 * Clear all known timeouts.
33 */
34 _this.clearTimeouts = function () {
35 if (_this.timeoutIds.length > 0) {
36 for (var _i = 0, _a = _this.timeoutIds; _i < _a.length; _i++) {
37 var timeoutId = _a[_i];
38 window.clearTimeout(timeoutId);
39 }
40 _this.timeoutIds = [];
41 }
42 };
43 if (!isNodeEnv("production")) {
44 _this.validateProps(_this.props);
45 }
46 return _this;
47 }
48 AbstractComponent.prototype.componentWillReceiveProps = function (nextProps) {
49 if (!isNodeEnv("production")) {
50 this.validateProps(nextProps);
51 }
52 };
53 AbstractComponent.prototype.componentWillUnmount = function () {
54 this.clearTimeouts();
55 };
56 /**
57 * Set a timeout and remember its ID.
58 * All stored timeouts will be cleared when component unmounts.
59 *
60 * @returns a "cancel" function that will clear timeout when invoked.
61 */
62 AbstractComponent.prototype.setTimeout = function (callback, timeout) {
63 var handle = window.setTimeout(callback, timeout);
64 this.timeoutIds.push(handle);
65 return function () { return window.clearTimeout(handle); };
66 };
67 /**
68 * Ensures that the props specified for a component are valid.
69 * Implementations should check that props are valid and usually throw an Error if they are not.
70 * Implementations should not duplicate checks that the type system already guarantees.
71 *
72 * This method should be used instead of React's
73 * [propTypes](https://facebook.github.io/react/docs/reusable-components.html#prop-validation) feature.
74 * Like propTypes, these runtime checks run only in development mode.
75 */
76 AbstractComponent.prototype.validateProps = function (_) {
77 // implement in subclass
78 };
79 return AbstractComponent;
80}(React.Component));
81export { AbstractComponent };
82//# sourceMappingURL=abstractComponent.js.map
\No newline at end of file