@appzung/react-native-code-push
Version:
React Native plugin for the CodePush service
93 lines (89 loc) • 4.2 kB
JavaScript
import hoistStatics from 'hoist-non-react-statics';
import React from 'react';
import { AppState } from 'react-native';
import { CheckFrequency } from "./enums/CheckFrequency.enum.js";
import { LogLevel } from "./enums/LogLevel.enum.js";
import { log } from "./internals/utils/log.js";
import { notifyAppReady } from "./notifyAppReady.js";
import { sync } from "./sync.js";
/**
* 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?).
*/
import { jsx as _jsx } from "react/jsx-runtime";
export function withCodePush(optionsOrComponent) {
const options = typeof optionsOrComponent === 'function' ? {} : optionsOrComponent;
const WithCodePush = RootComponent => {
class CodePushComponent extends React.Component {
constructor(props) {
super(props);
this.rootComponentRef = /*#__PURE__*/React.createRef();
}
componentDidMount() {
log(LogLevel.DEBUG, `withCodePush ${JSON.stringify(options)}`);
if (options.checkFrequency === CheckFrequency.MANUAL) {
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);
}
log(LogLevel.DEBUG, `sync on mount`);
sync(options, syncStatusCallback, downloadProgressCallback, handleBinaryVersionMismatchCallback);
if (options.checkFrequency === CheckFrequency.ON_APP_RESUME) {
AppState.addEventListener('change', newState => {
if (newState === 'active') {
log(LogLevel.DEBUG, `sync on active appState`);
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__*/_jsx(RootComponent, {
...props
});
}
}
return hoistStatics(CodePushComponent, RootComponent);
};
if (typeof optionsOrComponent === 'function') {
// Infer that the root component was directly passed to us.
return WithCodePush(optionsOrComponent);
} else {
return WithCodePush;
}
}
export default withCodePush;
//# sourceMappingURL=CodePush.js.map
;