Skip to content

Commit 65a4a25

Browse files
committed
Release v1.0.0
1 parent a08f12a commit 65a4a25

15 files changed

+936
-816
lines changed

README.md

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@ Perge is a minimal p2p synchronization system for [Automerge](https://github.com
88
- [Installation](#installation)
99
- [Quick Start](#quick-start)
1010
- [API](#api)
11-
- [`Perge`](#perge)
12-
- [`constructor (actorId: string, config: PergeConfig<T> = {})`](#constructor-actorid-string-config-pergeconfigt)
13-
- [`readonly docSet: Automerge.DocSet<T>;`](#readonly-docset-automergedocsett)
14-
- [`connect(id: string, conn?: PeerJS.DataConnection): void;`](#connectid-string-conn-peerjsdataconnection-void)
15-
- [`select (id: string): (fn: Function, ...args: any[]) => Automerge.Doc<T>`](#select-id-string-fn-function-args-any--automergedoct)
16-
- [`subscribe(idOrSetHandler: string | Automerge.DocSetHandler<T>, docHandler?: DocHandler<T>): () => void`](#subscribeidorsethandler-string--automergedocsethandlert-dochandler-dochandlert---void)
11+
- [`Perge`](#perge-1)
12+
- [`constructor (actorId: string, config: PergeConfig = {})`](#constructor-actorid-string-config-pergeconfig--)
13+
- [`readonly docSet: Automerge.DocSet<any>;`](#readonly-docset-automergedocsetany)
14+
- [`readonly peer: Peer`](#readonly-peer-peer)
15+
- [`connect(id: string, conn?: PeerJS.DataConnection): Peer.DataConnection;`](#connectid-string-conn-peerjsdataconnection-peerdataconnection)
16+
- [`select<T>(id: string): (fn: Function, ...args: any[]) => Automerge.Doc<T>`](#selecttid-string-fn-function-args-any--automergedoct)
17+
- [`subscribe<T>(idOrSetHandler: string | Automerge.DocSetHandler<T>, callback?: Automerge.ChangeFn<T>): () => void`](#subscribetidorsethandler-string--automergedocsethandlert-callback-automergechangefnt---void)
1718

1819
## Installation
1920

2021
`Perge` has the following dependencies:
2122
```json
2223
{
23-
"automerge": "^0.14.0",
24+
"automerge": "^0.14.1",
2425
"peerjs": "^1.2.0"
2526
}
2627
```
@@ -78,37 +79,53 @@ perge.select('some-document-id')(
7879

7980
`Perge` is a class containing references to `Automerge.Connections`, and encodes and decodes passed messages using `PeerJS` and the `Automerge.Connection` protocol.
8081

81-
#### `constructor (actorId: string, config: PergeConfig<T> = {})`
82+
#### `constructor (actorId: string, config: PergeConfig = {})`
8283

83-
You can construct `Perge` with the following config shape. All properties are optional.
84+
| `actorId` | `string` | Required. A unique ID used to initialize the PeerJS connection. Automerge should also be initialized with with this value.
8485

85-
| Key | Type | Description |
86-
| -------------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
87-
| `actorId` | `string` | Unique ID used to initialize the PeerJS connection. Automerge should also be initialized with with this value. |
88-
| `decode` | `(msg: string) => any` | A function called on a WebRTC string message before it is passed to an `Automerge.Connection` with `receiveMsg`, defaults to `JSON.parse` |
89-
| `encode` | `(msg: any) => string` | A function called on `Automerge.DocSet` change objects before it is sent to a peer, defaults to `JSON.stringify` |
90-
| `peerInstance` | `PeerJS.Peer` | A preconfigured `PeerJS.Peer` instance. |
91-
| `docSet` | `Automerge.DocSet<T>` | An instantiated `Automerge.DocSet` to sync between clients. |
86+
You can further configure `Perge` with the following config shape. All properties are optional.
9287

93-
#### `readonly docSet: Automerge.DocSet<T>;`
88+
| Key | Type | Description |
89+
| -------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
90+
| `decode` | `(msg: string) => any` | A function called on a WebRTC string message before it is passed to an `Automerge.Connection` with `receiveMsg`, defaults to `JSON.parse` |
91+
| `encode` | `(msg: any) => string` | A function called on `Automerge.DocSet` change objects before it is sent to a peer, defaults to `JSON.stringify` |
92+
| `peer` | `PeerJS.Peer` | A preconfigured `PeerJS.Peer` instance. |
93+
| `docSet` | `Automerge.DocSet<T>` | An instantiated `Automerge.DocSet` to sync between clients. |
9494

95-
Getter that retrieves the sync'd `Automerge.DocSet`, handy to subscribe to state changes with:
95+
96+
#### `readonly docSet: Automerge.DocSet<any>;`
97+
98+
A reference to the synchronized `Automerge.DocSet`, handy to subscribe to state changes with if you don't want to use `Perge.subscribe`:
9699

97100
```js
98101
docSet.registerHandler((docId, doc) => {
99102
// REACT TO STATE UPDATES
100103
})
101104
```
102105

103-
#### `connect(id: string, conn?: PeerJS.DataConnection): void;`
106+
#### `readonly peer: Peer`
107+
108+
A reference to the underlying PeerJS instance, useful for registering lifecycle handlers.
109+
110+
```js
111+
perge.peer.on('error', (err) => {
112+
// handle
113+
})
114+
```
115+
116+
#### `connect(id: string, conn?: PeerJS.DataConnection): Peer.DataConnection;`
104117

105118
Connects to a `PeerJS` peer with the given ID and sends outgoing `Automerge.DocSet` syncronization messages using the `Automerge.Connection` protocol.
106119

120+
Returns
121+
107122
Optionally, you can pass an existing `PeerJS` connection.
108123

109-
#### `select (id: string): (fn: Function, ...args: any[]) => Automerge.Doc<T>`
124+
#### `select<T>(id: string): (fn: Function, ...args: any[]) => Automerge.Doc<T>`
110125

111-
Returns a function that applies a given `Automerge` doc method, then sets the returned document on the internal `DocSet` to broadcast changes to connected peers, for example:
126+
- [Updating Automerge Documents](https://github.com/automerge/automerge#updating-a-document)
127+
128+
Returns a function that applies a given `Automerge` document change method, then sets the returned document on the internal `DocSet` to broadcast changes to connected peers, for example:
112129

113130
```js
114131
// Selects the document with the ID 'foo'
@@ -133,6 +150,22 @@ const newDoc = Automerge.change(oldDoc, 'increase counter', doc => {
133150
perge.set.setDoc(id, newDoc)
134151
```
135152

136-
#### `subscribe(idOrSetHandler: string | Automerge.DocSetHandler<T>, docHandler?: DocHandler<T>): () => void`
153+
#### `subscribe<T>(idOrSetHandler: string | Automerge.DocSetHandler<T>, callback?: Automerge.ChangeFn<T>): () => void`
154+
155+
Subscribe to doc updates for either the entire docSet or a specific document ID. Returns a function that, when called, unsubscribes.
156+
157+
```js
158+
159+
const unsubscribeFromAll = instance.subscribe((id, doc) => {
160+
// do something with the updated doc
161+
})
137162

138-
Subscribe to doc updates for either the entire docSet or a specific document ID. Returns a function that, when called, unsubscribes.
163+
// subscribe returns an unsubscribe function
164+
const unsubscribeFromFoo = instance.subscribe('foo', (doc) => {
165+
console.log('foo', doc)
166+
if (doc.counter.value === 10) {
167+
unsubscribeFromFoo()
168+
console.log('unsubscribed from foo!')
169+
}
170+
})
171+
```

dist/index.d.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
import Peer from 'peerjs';
22
import * as Automerge from 'automerge';
3-
export interface PergeConfig<T> {
3+
export interface PergeConfig {
44
decode?: (msg: string) => any;
55
encode?: (msg: any) => string;
6-
peerInstance?: Peer;
7-
docSet?: Automerge.DocSet<T>;
6+
peer?: Peer;
7+
docSet?: Automerge.DocSet<any>;
88
}
9-
declare type DocHandler<T> = (doc: Automerge.Doc<T>) => void;
10-
export default class Perge<T> {
9+
export default class Perge {
10+
readonly peer: Peer;
11+
readonly docSet: Automerge.DocSet<any>;
1112
private _connections;
1213
private _actorId;
13-
private _docSet;
14-
private _peerInstance;
1514
private _encode;
1615
private _decode;
17-
constructor(actorId: string, config?: PergeConfig<T>);
18-
get docSet(): Automerge.DocSet<T>;
19-
connect(id: string, conn?: Peer.DataConnection): void;
20-
select(id: string): (fn: Function, ...args: any[]) => Automerge.Doc<T>;
21-
subscribe(idOrSetHandler: string | Automerge.DocSetHandler<T>, docHandler?: DocHandler<T>): () => void;
16+
constructor(actorId: string, config?: PergeConfig);
17+
connect(id: string, conn?: Peer.DataConnection): Peer.DataConnection;
18+
select<T>(id: string): (fn: Function, ...args: any[]) => Automerge.Doc<T>;
19+
subscribe<T>(idOrSetHandler: string | Automerge.DocSetHandler<T>, callback?: Automerge.ChangeFn<T>): () => void;
2220
}
23-
export {};

dist/index.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.m.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.m.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)