Skip to content

Commit 3989cac

Browse files
authored
Merge pull request automerge#439 from automerge/type-patchcallback
Add TypeScript type for PatchCallback
2 parents 430d842 + 2d072d8 commit 3989cac

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

automerge-js/src/index.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ import { STATE, HEADS, TRACE, OBJECT_ID, READ_ONLY, FROZEN } from "./constants"
77
import { AutomergeValue, Text, Counter } from "./types"
88
export { AutomergeValue, Text, Counter, Int, Uint, Float64 } from "./types"
99

10-
import { type API } from "@automerge/automerge-wasm";
10+
import { type API, type Patch } from "@automerge/automerge-wasm";
1111
import { ApiHandler, UseApi } from "./low_level"
1212

1313
import { Actor as ActorId, Prop, ObjID, Change, DecodedChange, Heads, Automerge, MaterializeValue } from "@automerge/automerge-wasm"
1414
import { JsSyncState as SyncState, SyncMessage, DecodedSyncMessage } from "@automerge/automerge-wasm"
1515

16-
export type ChangeOptions = { message?: string, time?: number, patchCallback?: Function }
17-
export type ApplyOptions = { patchCallback?: Function }
16+
export type ChangeOptions<T> = { message?: string, time?: number, patchCallback?: PatchCallback<T> }
17+
export type ApplyOptions<T> = { patchCallback?: PatchCallback<T> }
1818

1919
export type Doc<T> = { readonly [P in keyof T]: T[P] }
2020

2121
export type ChangeFn<T> = (doc: T) => void
2222

23+
export type PatchCallback<T> = (patch: Patch, before: Doc<T>, after: Doc<T>) => void
24+
2325
export interface State<T> {
2426
change: DecodedChange
2527
snapshot: T
@@ -32,25 +34,25 @@ export function use(api: API) {
3234
import * as wasm from "@automerge/automerge-wasm"
3335
use(wasm)
3436

35-
export type InitOptions = {
37+
export type InitOptions<T> = {
3638
actor?: ActorId,
3739
freeze?: boolean,
38-
patchCallback?: Function,
40+
patchCallback?: PatchCallback<T>,
3941
};
4042

4143

42-
interface InternalState {
44+
interface InternalState<T> {
4345
handle: Automerge,
4446
heads: Heads | undefined,
4547
freeze: boolean,
46-
patchCallback: Function | undefined,
48+
patchCallback?: PatchCallback<T>
4749
}
4850

4951
export function getBackend<T>(doc: Doc<T>) : Automerge {
5052
return _state(doc).handle
5153
}
5254

53-
function _state<T>(doc: Doc<T>, checkroot = true) : InternalState {
55+
function _state<T>(doc: Doc<T>, checkroot = true) : InternalState<T> {
5456
const state = Reflect.get(doc,STATE)
5557
if (state === undefined || (checkroot && _obj(doc) !== "_root")) {
5658
throw new RangeError("must be the document root")
@@ -90,15 +92,15 @@ function _readonly<T>(doc: Doc<T>) : boolean {
9092
return Reflect.get(doc,READ_ONLY) !== false
9193
}
9294

93-
function importOpts(_actor?: ActorId | InitOptions) : InitOptions {
95+
function importOpts<T>(_actor?: ActorId | InitOptions<T>) : InitOptions<T> {
9496
if (typeof _actor === 'object') {
9597
return _actor
9698
} else {
9799
return { actor: _actor }
98100
}
99101
}
100102

101-
export function init<T>(_opts?: ActorId | InitOptions) : Doc<T>{
103+
export function init<T>(_opts?: ActorId | InitOptions<T>) : Doc<T>{
102104
let opts = importOpts(_opts)
103105
let freeze = !!opts.freeze
104106
let patchCallback = opts.patchCallback
@@ -131,7 +133,7 @@ export function from<T extends Record<string, unknown>>(initialState: T | Doc<T>
131133
return change(init(actor), (d) => Object.assign(d, initialState))
132134
}
133135

134-
export function change<T>(doc: Doc<T>, options: string | ChangeOptions | ChangeFn<T>, callback?: ChangeFn<T>): Doc<T> {
136+
export function change<T>(doc: Doc<T>, options: string | ChangeOptions<T> | ChangeFn<T>, callback?: ChangeFn<T>): Doc<T> {
135137
if (typeof options === 'function') {
136138
return _change(doc, {}, options)
137139
} else if (typeof callback === 'function') {
@@ -144,7 +146,7 @@ export function change<T>(doc: Doc<T>, options: string | ChangeOptions | ChangeF
144146
}
145147
}
146148

147-
function progressDocument<T>(doc: Doc<T>, heads: Heads, callback?: Function): Doc<T> {
149+
function progressDocument<T>(doc: Doc<T>, heads: Heads, callback?: PatchCallback<T>): Doc<T> {
148150
let state = _state(doc)
149151
let nextState = { ... state, heads: undefined };
150152
// @ts-ignore
@@ -154,7 +156,7 @@ function progressDocument<T>(doc: Doc<T>, heads: Heads, callback?: Function): Do
154156
return nextDoc
155157
}
156158

157-
function _change<T>(doc: Doc<T>, options: ChangeOptions, callback: ChangeFn<T>): Doc<T> {
159+
function _change<T>(doc: Doc<T>, options: ChangeOptions<T>, callback: ChangeFn<T>): Doc<T> {
158160

159161

160162
if (typeof callback !== "function") {
@@ -192,7 +194,7 @@ function _change<T>(doc: Doc<T>, options: ChangeOptions, callback: ChangeFn<T>):
192194
}
193195
}
194196

195-
export function emptyChange<T>(doc: Doc<T>, options: ChangeOptions) {
197+
export function emptyChange<T>(doc: Doc<T>, options: ChangeOptions<T>) {
196198
if (options === undefined) {
197199
options = {}
198200
}
@@ -214,7 +216,7 @@ export function emptyChange<T>(doc: Doc<T>, options: ChangeOptions) {
214216
return progressDocument(doc, heads)
215217
}
216218

217-
export function load<T>(data: Uint8Array, _opts?: ActorId | InitOptions) : Doc<T> {
219+
export function load<T>(data: Uint8Array, _opts?: ActorId | InitOptions<T>) : Doc<T> {
218220
const opts = importOpts(_opts)
219221
const actor = opts.actor
220222
const patchCallback = opts.patchCallback
@@ -320,7 +322,7 @@ export function getAllChanges<T>(doc: Doc<T>) : Change[] {
320322
return state.handle.getChanges([])
321323
}
322324

323-
export function applyChanges<T>(doc: Doc<T>, changes: Change[], opts?: ApplyOptions) : [Doc<T>] {
325+
export function applyChanges<T>(doc: Doc<T>, changes: Change[], opts?: ApplyOptions<T>) : [Doc<T>] {
324326
const state = _state(doc)
325327
if (!opts) { opts = {} }
326328
if (state.heads) {
@@ -378,7 +380,7 @@ export function generateSyncMessage<T>(doc: Doc<T>, inState: SyncState) : [ Sync
378380
return [ outState, message ]
379381
}
380382

381-
export function receiveSyncMessage<T>(doc: Doc<T>, inState: SyncState, message: SyncMessage, opts?: ApplyOptions) : [ Doc<T>, SyncState, null ] {
383+
export function receiveSyncMessage<T>(doc: Doc<T>, inState: SyncState, message: SyncMessage, opts?: ApplyOptions<T>) : [ Doc<T>, SyncState, null ] {
382384
const syncState = ApiHandler.importSyncState(inState)
383385
if (!opts) { opts = {} }
384386
const state = _state(doc)

0 commit comments

Comments
 (0)