UNPKG

@appzung/react-native-code-push

Version:

React Native plugin for the CodePush service

99 lines (95 loc) 4.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; exports.withCodePush = withCodePush; var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics")); var _react = _interopRequireDefault(require("react")); var _reactNative = require("react-native"); var _CheckFrequencyEnum = require("./enums/CheckFrequency.enum.js"); var _LogLevelEnum = require("./enums/LogLevel.enum.js"); var _log = require("./internals/utils/log.js"); var _notifyAppReady = require("./notifyAppReady.js"); var _sync = require("./sync.js"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * Wraps a React component inside a "higher order" React component that knows how to synchronize your app's JavaScript bundle and image assets when it is mounted. * * Internally, the higher-order component calls `sync` inside its `componentDidMount` lifecycle handle, which in turns performs an update check, downloads the update if it exists and installs the update for you. * * @param component the React Component that will be decorated */ // @ts-ignore /** * Wraps a React component inside a "higher order" React component that knows how to synchronize your app's JavaScript bundle and image assets when it is mounted. * * Internally, the higher-order component calls `sync` inside its `componentDidMount` lifecycle handle, which in turns performs an update check, downloads the update if it exists and installs the update for you. * * @param options Options used to configure the end-user sync and update experience (e.g. when to check for updates?, show a prompt?, install the update immediately?). */ function withCodePush(optionsOrComponent) { const options = typeof optionsOrComponent === 'function' ? {} : optionsOrComponent; const WithCodePush = RootComponent => { class CodePushComponent extends _react.default.Component { constructor(props) { super(props); this.rootComponentRef = /*#__PURE__*/_react.default.createRef(); } componentDidMount() { (0, _log.log)(_LogLevelEnum.LogLevel.DEBUG, `withCodePush ${JSON.stringify(options)}`); if (options.checkFrequency === _CheckFrequencyEnum.CheckFrequency.MANUAL) { (0, _notifyAppReady.notifyAppReady)(); } else { const rootComponentInstance = this.rootComponentRef.current; let syncStatusCallback; if (rootComponentInstance && rootComponentInstance.codePushStatusDidChange) { syncStatusCallback = rootComponentInstance.codePushStatusDidChange.bind(rootComponentInstance); } let downloadProgressCallback; if (rootComponentInstance && rootComponentInstance.codePushDownloadDidProgress) { downloadProgressCallback = rootComponentInstance.codePushDownloadDidProgress.bind(rootComponentInstance); } let handleBinaryVersionMismatchCallback; if (rootComponentInstance && rootComponentInstance.codePushOnBinaryVersionMismatch) { handleBinaryVersionMismatchCallback = rootComponentInstance.codePushOnBinaryVersionMismatch.bind(rootComponentInstance); } (0, _log.log)(_LogLevelEnum.LogLevel.DEBUG, `sync on mount`); (0, _sync.sync)(options, syncStatusCallback, downloadProgressCallback, handleBinaryVersionMismatchCallback); if (options.checkFrequency === _CheckFrequencyEnum.CheckFrequency.ON_APP_RESUME) { _reactNative.AppState.addEventListener('change', newState => { if (newState === 'active') { (0, _log.log)(_LogLevelEnum.LogLevel.DEBUG, `sync on active appState`); (0, _sync.sync)(options, syncStatusCallback, downloadProgressCallback); } }); } } } render() { const props = { ...this.props }; // We can set ref property on class components only (not stateless) // Check it by render method if (RootComponent.prototype && RootComponent.prototype.render) { // @ts-ignore props.ref = this.rootComponentRef; } return /*#__PURE__*/(0, _jsxRuntime.jsx)(RootComponent, { ...props }); } } return (0, _hoistNonReactStatics.default)(CodePushComponent, RootComponent); }; if (typeof optionsOrComponent === 'function') { // Infer that the root component was directly passed to us. return WithCodePush(optionsOrComponent); } else { return WithCodePush; } } var _default = exports.default = withCodePush; //# sourceMappingURL=CodePush.js.map