@automerge/automerge-repo 1.0.0-alpha.4 → 1.0.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/.eslintrc +2 -2
- package/dist/DocHandle.d.ts +5 -1
- package/dist/DocHandle.d.ts.map +1 -1
- package/dist/DocHandle.js +11 -3
- package/dist/Repo.d.ts +38 -4
- package/dist/Repo.d.ts.map +1 -1
- package/dist/Repo.js +95 -3
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/network/NetworkAdapter.d.ts +2 -3
- package/dist/network/NetworkAdapter.d.ts.map +1 -1
- package/dist/network/NetworkSubsystem.d.ts +1 -3
- package/dist/network/NetworkSubsystem.d.ts.map +1 -1
- package/dist/network/NetworkSubsystem.js +0 -9
- package/dist/storage/StorageSubsystem.d.ts.map +1 -1
- package/dist/storage/StorageSubsystem.js +8 -2
- package/dist/synchronizer/CollectionSynchronizer.d.ts +2 -2
- package/dist/synchronizer/CollectionSynchronizer.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/DocHandle.ts +13 -5
- package/src/Repo.ts +130 -4
- package/src/index.ts +0 -1
- package/src/network/NetworkAdapter.ts +2 -4
- package/src/network/NetworkSubsystem.ts +14 -23
- package/src/storage/StorageSubsystem.ts +9 -2
- package/src/synchronizer/CollectionSynchronizer.ts +2 -2
- package/test/CollectionSynchronizer.test.ts +12 -11
- package/test/DocHandle.test.ts +2 -1
- package/test/Repo.test.ts +26 -5
- package/test/helpers/DummyNetworkAdapter.ts +2 -3
- package/test/helpers/generate-large-object.ts +13 -0
- package/dist/DocCollection.d.ts +0 -46
- package/dist/DocCollection.d.ts.map +0 -1
- package/dist/DocCollection.js +0 -102
- package/src/DocCollection.ts +0 -144
- package/test/DocCollection.test.ts +0 -20
package/src/DocCollection.ts
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from "eventemitter3"
|
|
2
|
-
import { DocHandle } from "./DocHandle.js"
|
|
3
|
-
import { DocumentId, type BinaryDocumentId, AutomergeUrl } from "./types.js"
|
|
4
|
-
import { type SharePolicy } from "./Repo.js"
|
|
5
|
-
import {
|
|
6
|
-
documentIdToBinary,
|
|
7
|
-
binaryToDocumentId,
|
|
8
|
-
generateAutomergeUrl,
|
|
9
|
-
isValidAutomergeUrl,
|
|
10
|
-
parseAutomergeUrl,
|
|
11
|
-
} from "./DocUrl.js"
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* A DocCollection is a collection of DocHandles. It supports creating new documents and finding
|
|
15
|
-
* documents by ID.
|
|
16
|
-
* */
|
|
17
|
-
export class DocCollection extends EventEmitter<DocCollectionEvents> {
|
|
18
|
-
#handleCache: Record<DocumentId, DocHandle<any>> = {}
|
|
19
|
-
|
|
20
|
-
/** By default, we share generously with all peers. */
|
|
21
|
-
sharePolicy: SharePolicy = async () => true
|
|
22
|
-
|
|
23
|
-
constructor() {
|
|
24
|
-
super()
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** Returns an existing handle if we have it; creates one otherwise. */
|
|
28
|
-
#getHandle<T>(
|
|
29
|
-
/** The documentId of the handle to look up or create */
|
|
30
|
-
documentId: DocumentId,
|
|
31
|
-
|
|
32
|
-
/** If we know we're creating a new document, specify this so we can have access to it immediately */
|
|
33
|
-
isNew: boolean
|
|
34
|
-
) {
|
|
35
|
-
// If we have the handle cached, return it
|
|
36
|
-
if (this.#handleCache[documentId]) return this.#handleCache[documentId]
|
|
37
|
-
|
|
38
|
-
// If not, create a new handle, cache it, and return it
|
|
39
|
-
if (!documentId) throw new Error(`Invalid documentId ${documentId}`)
|
|
40
|
-
const handle = new DocHandle<T>(documentId, { isNew })
|
|
41
|
-
this.#handleCache[documentId] = handle
|
|
42
|
-
return handle
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Returns all the handles we have cached. */
|
|
46
|
-
get handles() {
|
|
47
|
-
return this.#handleCache
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Creates a new document and returns a handle to it. The initial value of the document is
|
|
52
|
-
* an empty object `{}`. Its documentId is generated by the system. we emit a `document` event
|
|
53
|
-
* to advertise interest in the document.
|
|
54
|
-
*/
|
|
55
|
-
create<T>(): DocHandle<T> {
|
|
56
|
-
// TODO:
|
|
57
|
-
// either
|
|
58
|
-
// - pass an initial value and do something like this to ensure that you get a valid initial value
|
|
59
|
-
|
|
60
|
-
// const myInitialValue = {
|
|
61
|
-
// tasks: [],
|
|
62
|
-
// filter: "all",
|
|
63
|
-
//
|
|
64
|
-
// const guaranteeInitialValue = (doc: any) => {
|
|
65
|
-
// if (!doc.tasks) doc.tasks = []
|
|
66
|
-
// if (!doc.filter) doc.filter = "all"
|
|
67
|
-
|
|
68
|
-
// return { ...myInitialValue, ...doc }
|
|
69
|
-
// }
|
|
70
|
-
|
|
71
|
-
// or
|
|
72
|
-
// - pass a "reify" function that takes a `<any>` and returns `<T>`
|
|
73
|
-
|
|
74
|
-
// Generate a new UUID and store it in the buffer
|
|
75
|
-
const { documentId } = parseAutomergeUrl(generateAutomergeUrl())
|
|
76
|
-
const handle = this.#getHandle<T>(documentId, true) as DocHandle<T>
|
|
77
|
-
this.emit("document", { handle, isNew: true })
|
|
78
|
-
return handle
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Retrieves a document by id. It gets data from the local system, but also emits a `document`
|
|
83
|
-
* event to advertise interest in the document.
|
|
84
|
-
*/
|
|
85
|
-
find<T>(
|
|
86
|
-
/** The documentId of the handle to retrieve */
|
|
87
|
-
automergeUrl: AutomergeUrl
|
|
88
|
-
): DocHandle<T> {
|
|
89
|
-
if (!isValidAutomergeUrl(automergeUrl)) {
|
|
90
|
-
throw new Error(`Invalid AutomergeUrl: '${automergeUrl}'`)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const { documentId } = parseAutomergeUrl(automergeUrl)
|
|
94
|
-
// If we have the handle cached, return it
|
|
95
|
-
if (this.#handleCache[documentId]) {
|
|
96
|
-
if (this.#handleCache[documentId].isUnavailable()) {
|
|
97
|
-
// this ensures that the event fires after the handle has been returned
|
|
98
|
-
setTimeout(() => {
|
|
99
|
-
this.#handleCache[documentId].emit("unavailable", {
|
|
100
|
-
handle: this.#handleCache[documentId],
|
|
101
|
-
})
|
|
102
|
-
})
|
|
103
|
-
}
|
|
104
|
-
return this.#handleCache[documentId]
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const handle = this.#getHandle<T>(documentId, false) as DocHandle<T>
|
|
108
|
-
this.emit("document", { handle, isNew: false })
|
|
109
|
-
return handle
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
delete(
|
|
113
|
-
/** The documentId of the handle to delete */
|
|
114
|
-
id: DocumentId | AutomergeUrl
|
|
115
|
-
) {
|
|
116
|
-
if (isValidAutomergeUrl(id)) {
|
|
117
|
-
;({ documentId: id } = parseAutomergeUrl(id))
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const handle = this.#getHandle(id, false)
|
|
121
|
-
handle.delete()
|
|
122
|
-
|
|
123
|
-
delete this.#handleCache[id]
|
|
124
|
-
this.emit("delete-document", {
|
|
125
|
-
documentId: id,
|
|
126
|
-
})
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// events & payloads
|
|
131
|
-
interface DocCollectionEvents {
|
|
132
|
-
document: (arg: DocumentPayload) => void
|
|
133
|
-
"delete-document": (arg: DeleteDocumentPayload) => void
|
|
134
|
-
"unavailable-document": (arg: DeleteDocumentPayload) => void
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
interface DocumentPayload {
|
|
138
|
-
handle: DocHandle<any>
|
|
139
|
-
isNew: boolean
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
interface DeleteDocumentPayload {
|
|
143
|
-
documentId: DocumentId
|
|
144
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import assert from "assert"
|
|
2
|
-
import { DocCollection, BinaryDocumentId } from "../src/index.js"
|
|
3
|
-
import { TestDoc } from "./types.js"
|
|
4
|
-
import { generateAutomergeUrl, stringifyAutomergeUrl } from "../src/DocUrl.js"
|
|
5
|
-
|
|
6
|
-
const MISSING_DOCID = generateAutomergeUrl()
|
|
7
|
-
|
|
8
|
-
describe("DocCollection", () => {
|
|
9
|
-
it("can create documents which are ready to go", async () => {
|
|
10
|
-
const collection = new DocCollection()
|
|
11
|
-
const handle = collection.create<TestDoc>()
|
|
12
|
-
assert(handle.isReady() === true)
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
it("can start finding documents and they shouldn't be ready", () => {
|
|
16
|
-
const collection = new DocCollection()
|
|
17
|
-
const handle = collection.find<TestDoc>(MISSING_DOCID)
|
|
18
|
-
assert(handle.isReady() === false)
|
|
19
|
-
})
|
|
20
|
-
})
|