-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP initial work * rename Rpc to LocalPeers * Handle deviceInfo internally, id -> deviceId * Tests for stream error handling * remove unnecessary constructor * return replication stream * Attach protomux instance to peer info * rename and re-organize * revert changes outside scope of PR * WIP initial work * Tie everything together * rename getProjectInstance * feat: client.listLocalPeers() & `local-peers` evt * feat: add $sync API methods For now this simplifies the API (because we are only supporting local sync, not remote sync over the internet) to: - `project.$sync.getState()` - `project.$sync.start()` - `project.$sync.stop()` - Events - `sync-state` It's currently not possible to stop local discovery, nor is it possible to stop sync of the metadata namespaces (auth, config, blobIndex). The start and stop methods stop the sync of the data and blob namespaces. Fixes #134. Stacked on #360, #358 and #356.
- Loading branch information
1 parent
ab77e51
commit ca95b2f
Showing
4 changed files
with
115 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { TypedEmitter } from 'tiny-typed-emitter' | ||
import { SyncState } from './sync-state.js' | ||
import { PeerSyncController } from './peer-sync-controller.js' | ||
|
||
export const kSyncReplicate = Symbol('replicate sync') | ||
|
||
/** | ||
* @typedef {object} SyncEvents | ||
* @property {(syncState: import('./sync-state.js').State) => void} sync-state | ||
*/ | ||
|
||
/** | ||
* @extends {TypedEmitter<SyncEvents>} | ||
*/ | ||
export class SyncApi extends TypedEmitter { | ||
syncState | ||
#coreManager | ||
#capabilities | ||
/** @type {Map<import('protomux'), PeerSyncController>} */ | ||
#peerSyncControllers = new Map() | ||
/** @type {Set<'local' | 'remote'>} */ | ||
#dataSyncEnabled = new Set() | ||
|
||
/** | ||
* | ||
* @param {object} opts | ||
* @param {import('../core-manager/index.js').CoreManager} opts.coreManager | ||
* @param {import("../capabilities.js").Capabilities} opts.capabilities | ||
* @param {number} [opts.throttleMs] | ||
*/ | ||
constructor({ coreManager, throttleMs = 200, capabilities }) { | ||
super() | ||
this.#coreManager = coreManager | ||
this.#capabilities = capabilities | ||
this.syncState = new SyncState({ coreManager, throttleMs }) | ||
this.syncState.on('state', this.emit.bind(this, 'sync-state')) | ||
} | ||
|
||
getState() { | ||
return this.syncState.getState() | ||
} | ||
|
||
/** | ||
* Start syncing data cores | ||
*/ | ||
start() { | ||
if (this.#dataSyncEnabled.has('local')) return | ||
this.#dataSyncEnabled.add('local') | ||
for (const peerSyncController of this.#peerSyncControllers.values()) { | ||
peerSyncController.enableDataSync() | ||
} | ||
} | ||
|
||
/** | ||
* Stop syncing data cores (metadata cores will continue syncing in the background) | ||
*/ | ||
stop() { | ||
if (!this.#dataSyncEnabled.has('local')) return | ||
this.#dataSyncEnabled.delete('local') | ||
for (const peerSyncController of this.#peerSyncControllers.values()) { | ||
peerSyncController.disableDataSync() | ||
} | ||
} | ||
|
||
/** | ||
* @param {import('protomux')<import('@hyperswarm/secret-stream')>} protomux A protomux instance | ||
*/ | ||
[kSyncReplicate](protomux) { | ||
if (this.#peerSyncControllers.has(protomux)) return | ||
|
||
const peerSyncController = new PeerSyncController({ | ||
protomux, | ||
coreManager: this.#coreManager, | ||
syncState: this.syncState, | ||
capabilities: this.#capabilities, | ||
}) | ||
if (this.#dataSyncEnabled.has('local')) { | ||
peerSyncController.enableDataSync() | ||
} | ||
this.#peerSyncControllers.set(protomux, peerSyncController) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.