UNPKG

4.4 kBJavaScriptView Raw
1/*
2 * Copyright 2019 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// eslint-disable-next-line @typescript-eslint/ban-types
24var AbstractPureComponent2 = /** @class */ (function (_super) {
25 __extends(AbstractPureComponent2, _super);
26 function AbstractPureComponent2(props, context) {
27 var _this = _super.call(this, props, context) || this;
28 // Not bothering to remove entries when their timeouts finish because clearing invalid ID is a no-op
29 _this.timeoutIds = [];
30 _this.requestIds = [];
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 /**
44 * Clear all known animation frame requests.
45 */
46 _this.cancelAnimationFrames = function () {
47 if (_this.requestIds.length > 0) {
48 for (var _i = 0, _a = _this.requestIds; _i < _a.length; _i++) {
49 var requestId = _a[_i];
50 window.cancelAnimationFrame(requestId);
51 }
52 _this.requestIds = [];
53 }
54 };
55 if (!isNodeEnv("production")) {
56 _this.validateProps(_this.props);
57 }
58 return _this;
59 }
60 AbstractPureComponent2.prototype.componentDidUpdate = function (_prevProps, _prevState, _snapshot) {
61 if (!isNodeEnv("production")) {
62 this.validateProps(this.props);
63 }
64 };
65 AbstractPureComponent2.prototype.componentWillUnmount = function () {
66 this.clearTimeouts();
67 this.cancelAnimationFrames();
68 };
69 /**
70 * Request an animation frame and remember its ID.
71 * All pending requests will be canceled when component unmounts.
72 *
73 * @returns a "cancel" function that will cancel the request when invoked.
74 */
75 AbstractPureComponent2.prototype.requestAnimationFrame = function (callback) {
76 var handle = window.requestAnimationFrame(callback);
77 this.requestIds.push(handle);
78 return function () { return window.cancelAnimationFrame(handle); };
79 };
80 /**
81 * Set a timeout and remember its ID.
82 * All pending timeouts will be cleared when component unmounts.
83 *
84 * @returns a "cancel" function that will clear timeout when invoked.
85 */
86 AbstractPureComponent2.prototype.setTimeout = function (callback, timeout) {
87 var handle = window.setTimeout(callback, timeout);
88 this.timeoutIds.push(handle);
89 return function () { return window.clearTimeout(handle); };
90 };
91 /**
92 * Ensures that the props specified for a component are valid.
93 * Implementations should check that props are valid and usually throw an Error if they are not.
94 * Implementations should not duplicate checks that the type system already guarantees.
95 *
96 * This method should be used instead of React's
97 * [propTypes](https://facebook.github.io/react/docs/reusable-components.html#prop-validation) feature.
98 * Like propTypes, these runtime checks run only in development mode.
99 */
100 AbstractPureComponent2.prototype.validateProps = function (_props) {
101 // implement in subclass
102 };
103 return AbstractPureComponent2;
104}(React.PureComponent));
105export { AbstractPureComponent2 };
106//# sourceMappingURL=abstractPureComponent2.js.map
\No newline at end of file