From de9416adf00e7788a7a76cffb5b884cd4fc138e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Trys=C5=82a?= Date: Mon, 8 Jul 2019 14:49:26 +0200 Subject: [PATCH] add BundleRegistry docs --- docs/bundleregistry.md | 148 +++++++++++++++++++++++++++++++++++++++++ website/i18n/en.json | 3 + website/sidebars.json | 1 + 3 files changed, 152 insertions(+) create mode 100644 docs/bundleregistry.md diff --git a/docs/bundleregistry.md b/docs/bundleregistry.md new file mode 100644 index 00000000000..110dc9d477f --- /dev/null +++ b/docs/bundleregistry.md @@ -0,0 +1,148 @@ +--- +id: bundleregistry +title: BundleRegistry +--- + + + +`BundleRegistry` is the JS module which provides an API to load new bundles on-demand and access their exported data. `BundleRegistry` can only be used when running in multi-bundle mode, which means the bundler has to create multiple bundles and store them in the same directory. + +To create bundles for multi-bundle mode, you need to use [Haul](https://github.com/callstack/haul/tree/next) and `multi-bundle` command instead of `bundle`. Please refer to [Haul](https://github.com/callstack/haul/tree/next) documentation for a complete guide. + +Using `BundleRegistry.loadBundle` you can send request to the native side in React Native to load new bundle either asynchronously (default) or synchronously. + +`BundleRegistry` itself is a EventEmitter, so you can use `addListener` to listen for `bundleLoaded` events, emitted when the bundle is loaded and ready to be used. + +### Methods + +- [`loadBundle`](bundleregistry.md#loadbundle) +- [`isBundleLoaded`](bundleregistry.md#isbundleloaded) +- [`getBundleExport`](bundleregistry.md#getbundleexport) +- [`addListener`](bundleregistry.md#addlistener) +- [`removeListener`](bundleregistry.md#removelistener) +- [`enableLogging`](bundleregistry.md#enablelogging) +- [`disableLogging`](bundleregistry.md#disablelogging) + +### Supported events + +- [`bundleLoaded`](bundleregistry.md#bundleloaded) + +--- + +# Reference + +## Events + +### `bundleLoaded` + +Emitted when the bundle was loaded, evaluated and it's ready to be used. + +Event payload: + +| Name | Type | Description | +| ------------------ | ------ | ------------------------------------------------- | +| bundleName | string | Name of the loaded bundle | +| loadStartTimestamp | number | Timestamp for when the bundle was started loading | + +## Methods + +### `loadBundle()` + +```javascript +static loadBundle(bundleName: string, synchronously: boolean = false) +``` + +Sends a request to load bundle specified by `bundleName` to the native side. By default th loading is asynchronous and doesn't block the main thread. You can optionally set the 2nd argument to `true` in which case the loading happens synchronously - it's useful when some bundle needs to be loaded before running anything else. + +**Example:** + +In host/initial bundle: + +```javascript +BundleRegistry.addEventListener( + 'bundleLoaded', + ({bundleName, loadStartTimestamp}) => { + console.log( + `Bundle ${bundleName} loaded in ${Date.now() - loadStartTimestamp} ms`, + ); + }, +); +BundleRegistry.loadBundle('app0'); +``` + +--- + +### `getBundleExport()` + +```javascript +static getBundleExport(bundleName: string) +``` + +Gets bundle default export for specified `bundleName`. + +**Example:** + +Entry point for `app0` bundle: + +```javascript +export default function App0() { + /* ... */ +} +``` + +In host/initial bundle: + +```javascript +const App0 = BundleRegistry.getBundleExport('app0'); +``` + +--- + +### `addListener()` + +```javascript +static addListener(eventName: string, listener: Function) +``` + +Adds event listeners. + +--- + +### `removeListener()` + +```javascript +static removeListener(eventName: string, listener: Function) +``` + +Removes event listener. + +--- + +### `enableLogging()` + +```javascript +static enableLogging() +``` + +Enables debug logging to `console`. + +--- + +### `disableLogging()` + +```javascript +static disableLogging() +``` + +Disables debug logging to `console`. + +--- diff --git a/website/i18n/en.json b/website/i18n/en.json index e527e87b86d..ff8b3bb4dac 100644 --- a/website/i18n/en.json +++ b/website/i18n/en.json @@ -53,6 +53,9 @@ "building-for-apple-tv": { "title": "Building For TV Devices" }, + "bundleregistry": { + "title": "BundleRegistry" + }, "button": { "title": "Button" }, diff --git a/website/sidebars.json b/website/sidebars.json index 07e01be7fcd..4c0999888d5 100644 --- a/website/sidebars.json +++ b/website/sidebars.json @@ -100,6 +100,7 @@ "alertios", "animated", "appregistry", + "bundleregistry", "appstate", "asyncstorage", "backhandler",