|
| 1 | +# `@remote-dom/compat` |
| 2 | + |
| 3 | +The `@remote-dom/compat` package provides helpers for adapting between Remote DOM and [`remote-ui`, the previous version of this project](https://github.com/Shopify/remote-dom/discussions/267). These utilities are offered to help you transition to Remote DOM, while continuing to support existing code that expects `remote-ui`-style APIs. |
| 4 | + |
| 5 | +## Progressive migration from `remote-ui`’s `RemoteChannel` to Remote DOM’s `RemoteConnection` |
| 6 | + |
| 7 | +The `RemoteChannel` and `RemoteConnection` types from `remote-ui` and Remote DOM serve the same purpose: they describe the minimal interface that a remote environment needs to communicate with a host. In Remote DOM, the `RemoteConnection` type has been enhanced in backwards-incompatible ways, in order to support [method calling](#remote-methods), batched updates, and more. |
| 8 | + |
| 9 | +In `remote-ui`, you typically get a `RemoteChannel` function by accessing the `receiver` property on a `RemoteReceiver`, like this: |
| 10 | + |
| 11 | +```ts |
| 12 | +import {createRemoteReceiver} from '@remote-ui/core'; |
| 13 | + |
| 14 | +const receiver = createRemoteReceiver(); |
| 15 | +const channel = receiver.receive; |
| 16 | + |
| 17 | +// Do something with the channel, typically by sending it to a remote environment: |
| 18 | +sendChannelToRemoteEnvironment(channel); |
| 19 | +``` |
| 20 | + |
| 21 | +You can migrate to use a Remote DOM [`RemoteReceiver`](#remotereceiver), [`DOMRemoteReceiver`](#domremotereceiver), or [`SignalRemoteReceiver`](/packages/signals/README.md#signalremotereceiver) class, while still supporting the `RemoteChannel` API, by using the `adaptToLegacyRemoteChannel()` function: |
| 22 | + |
| 23 | +You can adapt a `RemoteConnection` to a `RemoteChannel` using this library’s `adaptToLegacyRemoteChannel()` function. This function takes a `RemoteConnection` and returns a `RemoteChannel`, which allows you to use a Remote DOM receiver class on the host, even if the remote environment is using `remote-ui`. This same technique works regardless of whether you are using the [`RemoteReceiver`](#remotereceiver), [`DOMRemoteReceiver`](#domremotereceiver), or [`SignalRemoteReceiver`](/packages/signals/README.md#signalremotereceiver) class. |
| 24 | + |
| 25 | +```ts |
| 26 | +import {DOMRemoteReceiver} from '@remote-dom/core/receivers'; |
| 27 | +import {adaptToLegacyRemoteChannel} from '@remote-dom/compat'; |
| 28 | + |
| 29 | +const receiver = new DOMRemoteReceiver(); |
| 30 | +const channel = adaptToLegacyRemoteChannel(receiver.connection); |
| 31 | + |
| 32 | +// Same as before: do something with the channel |
| 33 | +sendChannelToRemoteEnvironment(channel); |
| 34 | +``` |
| 35 | + |
| 36 | +If you use `remote-ui`’s React bindings to render your UI on the host, you will also need to update that code to make use of the new Remote DOM versions of those bindings (available for [Preact](/packages/preact/README.md#host) and [React](/packages/react/README.md#host)). With this change made, though, you can now seamlessly support code written with `remote-ui` or Remote DOM, by using the more powerful Remote DOM receiver classes on the host and adapting them for legacy code. |
0 commit comments