loro-repo 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,6 +44,8 @@ await repo.unloadDoc("note:welcome");
44
44
  ## Using the API
45
45
 
46
46
  - **Create a repo** with `await LoroRepo.create<Meta>({ transportAdapter?, storageAdapter?, assetTransportAdapter?, docFrontierDebounceMs? })`; metadata is hydrated automatically.
47
+ - **Swap or attach a transport later** by calling `await repo.setTransportAdapter(adapter)` (useful when booting offline, then enabling realtime once connectivity/auth is ready).
48
+ - **Check adapter availability** with `repo.hasTransport()` / `repo.hasStorage()` before calling `joinMetaRoom` / `joinDocRoom`.
47
49
  - **Define your metadata contract** once via the generic `Meta`. All metadata helpers (`upsertDocMeta`, `getDocMeta`, `listDoc`, `watch`) stay type-safe.
48
50
  - **Choose sync lanes** with `repo.sync({ scope: "meta" | "doc" | "full", docIds?: string[] })` to pull remote changes on demand.
49
51
  - **Work with documents** using `openPersistedDoc(docId)` for repo-managed docs (persisted snapshots + frontier tracking) and `openDetachedDoc(docId)` for isolated snapshots; call `joinDocRoom`/`handle.joinRoom` for live sync, or `unloadDoc`/`flush` to persist and drop cached docs.
package/dist/index.cjs CHANGED
@@ -1585,6 +1585,11 @@ var SyncRunner = class {
1585
1585
  this.getMetaFlock = options.getMetaFlock;
1586
1586
  this.replaceMetaFlock = options.mergeFlock;
1587
1587
  }
1588
+ setTransport(transport) {
1589
+ if (this.transport === transport) return;
1590
+ this.leaveRooms();
1591
+ this.transport = transport;
1592
+ }
1588
1593
  async ready() {
1589
1594
  if (!this.readyPromise) this.readyPromise = this.initialize();
1590
1595
  await this.readyPromise;
@@ -1754,6 +1759,16 @@ var SyncRunner = class {
1754
1759
  get metaFlock() {
1755
1760
  return this.getMetaFlock();
1756
1761
  }
1762
+ leaveRooms() {
1763
+ if (this.metaRoomSubscription) {
1764
+ this.metaRoomSubscription.base.unsubscribe();
1765
+ this.metaRoomSubscription = void 0;
1766
+ }
1767
+ for (const record of this.docSubscriptions.values()) record.base.unsubscribe();
1768
+ this.docSubscriptions.clear();
1769
+ this.unsubscribeMetaFlock?.();
1770
+ this.unsubscribeMetaFlock = void 0;
1771
+ }
1757
1772
  };
1758
1773
 
1759
1774
  //#endregion
@@ -1916,7 +1931,6 @@ var MetaPersister = class {
1916
1931
  const DEFAULT_DOC_FRONTIER_DEBOUNCE_MS = 1e3;
1917
1932
  const DEFAULT_DELETED_DOC_KEEP_MS = 720 * 60 * 60 * 1e3;
1918
1933
  var LoroRepo = class LoroRepo {
1919
- options;
1920
1934
  _destroyed = false;
1921
1935
  transport;
1922
1936
  storage;
@@ -1933,7 +1947,6 @@ var LoroRepo = class LoroRepo {
1933
1947
  deletedDocKeepMs;
1934
1948
  purgeWatchHandle;
1935
1949
  constructor(options) {
1936
- this.options = options;
1937
1950
  this.transport = options.transportAdapter;
1938
1951
  this.storage = options.storageAdapter;
1939
1952
  this.assetTransport = options.assetTransportAdapter;
@@ -2026,6 +2039,25 @@ var LoroRepo = class LoroRepo {
2026
2039
  await this.syncRunner.sync(options);
2027
2040
  }
2028
2041
  /**
2042
+ * Sets (or replaces) the transport adapter used for syncing and realtime rooms.
2043
+ *
2044
+ * Swapping transports will leave any joined meta/doc rooms managed by the repo.
2045
+ */
2046
+ async setTransportAdapter(transport) {
2047
+ if (this._destroyed) throw new Error("Repo has been destroyed");
2048
+ if (this.transport === transport) return;
2049
+ const previous = this.transport;
2050
+ this.transport = transport;
2051
+ this.syncRunner.setTransport(transport);
2052
+ await previous?.close();
2053
+ }
2054
+ hasTransport() {
2055
+ return Boolean(this.transport);
2056
+ }
2057
+ hasStorage() {
2058
+ return Boolean(this.storage);
2059
+ }
2060
+ /**
2029
2061
  * Start syncing the metadata (Flock) room. It will establish a realtime connection to the transport adaptor.
2030
2062
  * All changes on the room will be synced to the Flock, and all changes on the Flock will be synced to the room.
2031
2063
  *