UNPKG

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