-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93c7343
commit ac52dca
Showing
7 changed files
with
380 additions
and
289 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,29 +1,82 @@ | ||
import { AutomergeStore, AutomergeStoreOptions } from "./automerge-store"; | ||
import { DocHandle } from "automerge-repo"; | ||
import type { ChangeFn, ChangeOptions, Doc } from "@automerge/automerge"; | ||
import { DocHandle, DocHandlePatchPayload } from "automerge-repo"; | ||
import type { | ||
ChangeFn, | ||
ChangeOptions, | ||
Doc, | ||
PatchCallback, | ||
} from "@automerge/automerge"; | ||
import type { PatchInfo } from "@automerge/automerge-wasm"; | ||
|
||
export class AutomergeRepoStore<T> extends AutomergeStore<T> { | ||
private patchCallbacks: Set<PatchCallback<T>> = new Set(); | ||
private subscriberCount = 0; | ||
|
||
constructor( | ||
private handle: DocHandle<T>, | ||
options: AutomergeStoreOptions = {}, | ||
) { | ||
super(handle.documentId, handle.value(), options); | ||
|
||
this._ready = false; | ||
|
||
const listener = async ({ handle }: { handle: DocHandle<T> }) => { | ||
this.doc = await handle.value(); | ||
}; | ||
|
||
handle.on("change", listener); | ||
} | ||
|
||
protected makeChange( | ||
callback: ChangeFn<T>, | ||
options: ChangeOptions<T> = {}, | ||
): Doc<T> { | ||
this.handle.change(callback, options); | ||
const { patchCallback, ...rest } = options; | ||
|
||
if (patchCallback) { | ||
if (this.subscriberCount === 0) { | ||
this.handle.once( | ||
"patch", | ||
({ patches, handle, ...patchInfo }: DocHandlePatchPayload<T>) => { | ||
patchCallback(patches, patchInfo as PatchInfo<T>); | ||
}, | ||
); | ||
} else { | ||
this.patchCallbacks.add(patchCallback); | ||
} | ||
} | ||
|
||
this.handle.change(callback, rest); | ||
|
||
return this._doc; | ||
} | ||
|
||
public subscribe( | ||
callback: (doc: T) => void, | ||
fireImmediately: boolean = true, | ||
) { | ||
if (fireImmediately) { | ||
this.handle.value().then((doc) => { | ||
this._doc = doc; | ||
callback(doc); | ||
}); | ||
} | ||
|
||
const listener = async ({ | ||
patches, | ||
handle, | ||
...patchInfo | ||
}: DocHandlePatchPayload<T>) => { | ||
this._doc = patchInfo.after; | ||
|
||
this.patchCallbacks.forEach((cb) => { | ||
cb(patches, patchInfo as PatchInfo<T>); | ||
this.patchCallbacks.delete(cb); | ||
}); | ||
|
||
callback(patchInfo.after); | ||
}; | ||
|
||
this.handle.on("patch", listener); | ||
this.subscriberCount++; | ||
|
||
return () => { | ||
this.handle.off("patch", listener); | ||
this.subscriberCount--; | ||
}; | ||
} | ||
} |
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