-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add network and storage adapter interfaces (#291)
- Loading branch information
1 parent
7a341b1
commit 6b19210
Showing
11 changed files
with
557 additions
and
348 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
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
77 changes: 77 additions & 0 deletions
77
packages/automerge-repo/src/network/NetworkAdapterInterface.ts
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,77 @@ | ||
/* c8 ignore start */ | ||
|
||
import { EventEmitter } from "eventemitter3" | ||
import { PeerId } from "../types.js" | ||
import { Message } from "./messages.js" | ||
import { StorageId } from "../storage/types.js" | ||
|
||
/** | ||
* Describes a peer intent to the system | ||
* storageId: the key for syncState to decide what the other peer already has | ||
* isEphemeral: to decide if we bother recording this peer's sync state | ||
* | ||
*/ | ||
export interface PeerMetadata { | ||
storageId?: StorageId | ||
isEphemeral?: boolean | ||
} | ||
|
||
/** An interface representing some way to connect to other peers | ||
* | ||
* @remarks | ||
* The {@link Repo} uses one or more `NetworkAdapter`s to connect to other peers. | ||
* Because the network may take some time to be ready the {@link Repo} will wait | ||
* until the adapter emits a `ready` event before it starts trying to use it | ||
*/ | ||
export interface NetworkAdapterInterface extends EventEmitter<NetworkAdapterEvents> { | ||
peerId?: PeerId | ||
peerMetadata?: PeerMetadata | ||
|
||
/** Called by the {@link Repo} to start the connection process | ||
* | ||
* @argument peerId - the peerId of this repo | ||
* @argument peerMetadata - how this adapter should present itself to other peers | ||
*/ | ||
connect(peerId: PeerId, peerMetadata?: PeerMetadata): void | ||
|
||
/** Called by the {@link Repo} to send a message to a peer | ||
* | ||
* @argument message - the message to send | ||
*/ | ||
send(message: Message): void | ||
|
||
/** Called by the {@link Repo} to disconnect from the network */ | ||
disconnect(): void | ||
} | ||
|
||
// events & payloads | ||
|
||
export interface NetworkAdapterEvents { | ||
/** Emitted when the network is ready to be used */ | ||
ready: (payload: OpenPayload) => void | ||
|
||
/** Emitted when the network is closed */ | ||
close: () => void | ||
|
||
/** Emitted when the network adapter learns about a new peer */ | ||
"peer-candidate": (payload: PeerCandidatePayload) => void | ||
|
||
/** Emitted when the network adapter learns that a peer has disconnected */ | ||
"peer-disconnected": (payload: PeerDisconnectedPayload) => void | ||
|
||
/** Emitted when the network adapter receives a message from a peer */ | ||
message: (payload: Message) => void | ||
} | ||
|
||
export interface OpenPayload { | ||
network: NetworkAdapterInterface | ||
} | ||
|
||
export interface PeerCandidatePayload { | ||
peerId: PeerId | ||
peerMetadata: PeerMetadata | ||
} | ||
|
||
export interface PeerDisconnectedPayload { | ||
peerId: PeerId | ||
} |
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
34 changes: 34 additions & 0 deletions
34
packages/automerge-repo/src/storage/StorageAdapterInterface.ts
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,34 @@ | ||
import { StorageKey, Chunk } from "./types.js" | ||
|
||
/** A storage adapter represents some way of storing binary data for a {@link Repo} | ||
* | ||
* @remarks | ||
* `StorageAdapter`s provide a key/value storage interface. The keys are arrays of strings | ||
* ({@link StorageKey}) and the values are binary blobs. | ||
*/ | ||
export interface StorageAdapterInterface { | ||
/** Load the single value corresponding to `key` */ | ||
load(key: StorageKey): Promise<Uint8Array | undefined> | ||
|
||
/** Save the value `data` to the key `key` */ | ||
save(key: StorageKey, data: Uint8Array): Promise<void> | ||
|
||
/** Remove the value corresponding to `key` */ | ||
remove(key: StorageKey): Promise<void> | ||
|
||
/** | ||
* Load all values with keys that start with `keyPrefix`. | ||
* | ||
* @remarks | ||
* The `keyprefix` will match any key that starts with the given array. For example: | ||
* - `[documentId, "incremental"]` will match all incremental saves | ||
* - `[documentId]` will match all data for a given document. | ||
* | ||
* Be careful! `[documentId]` would also match something like `[documentId, "syncState"]`! We | ||
* aren't using this yet but keep it in mind.) | ||
*/ | ||
loadRange(keyPrefix: StorageKey): Promise<Chunk[]> | ||
|
||
/** Remove all values with keys that start with `keyPrefix` */ | ||
removeRange(keyPrefix: StorageKey): Promise<void> | ||
} |
Oops, something went wrong.