@@ -6,8 +6,6 @@ import { suggestUnDupTitle } from "./suggestUnDupTitle.ts";
6
6
import type { Result } from "option-t/plain_result" ;
7
7
import type { Socket } from "socket.io-client" ;
8
8
9
- export type PatchOptions = PushOptions ;
10
-
11
9
export interface PatchMetadata extends Page {
12
10
/** Number of retry attempts for page modification
13
11
*
@@ -17,6 +15,30 @@ export interface PatchMetadata extends Page {
17
15
attempts : number ;
18
16
}
19
17
18
+ /**
19
+ * Function used in {@linkcode patch} to generate a patch from the current page state
20
+ *
21
+ * This function is used to generate a patch from the current page state.
22
+ * It receives the current page lines and metadata and returns the new page content.
23
+ * The function can be synchronous or asynchronous.
24
+ *
25
+ * @param lines - Current page lines
26
+ * @param metadata - Current page metadata
27
+ * @returns one of the following or a {@linkcode Promise} resolving to one:
28
+ * - `(string | { text: string; })[]`: New page content
29
+ * - `[]`: Delete the page
30
+ * - `undefined`: Abort modification
31
+ */
32
+ export type MakePatchFn = (
33
+ lines : BaseLine [ ] ,
34
+ metadata : PatchMetadata ,
35
+ ) =>
36
+ | ( string | { text : string } ) [ ]
37
+ | undefined
38
+ | Promise < ( string | { text : string } ) [ ] | undefined > ;
39
+
40
+ export type PatchOptions = PushOptions ;
41
+
20
42
/** Modify an entire Scrapbox page by computing and sending only the differences
21
43
*
22
44
* This function handles the entire page modification process:
@@ -26,29 +48,20 @@ export interface PatchMetadata extends Page {
26
48
* 4. Handles errors (e.g., duplicate titles)
27
49
* 5. Retries on conflicts
28
50
*
29
- * @param project - Project ID containing the target page
30
- * @param title - Title of the page to modify
31
- * @param update - Function to generate new content:
32
- * - Input: Current page lines and metadata
33
- * - Return values:
34
- * - `string[]`: New page content
35
- * - `undefined`: Abort modification
36
- * - `[]`: Delete the page
37
- * Can be async (returns `Promise`)
38
- * @param options - Optional WebSocket configuration
51
+ * @param project Project ID containing the target page
52
+ * @param title Title of the page to modify
53
+ * @param update Function to generate new content
54
+ * @param options Optional WebSocket configuration
39
55
*
40
56
* Special cases:
41
- * - If update returns undefined: Operation is cancelled
42
- * - If update returns [] : Page is deleted
57
+ * - If ` update` returns ` undefined` : Operation is cancelled
58
+ * - If ` update` returns `[]` : Page is deleted
43
59
* - On duplicate title: Automatically suggests non-conflicting title
44
60
*/
45
61
export const patch = (
46
62
project : string ,
47
63
title : string ,
48
- update : (
49
- lines : BaseLine [ ] ,
50
- metadata : PatchMetadata ,
51
- ) => string [ ] | undefined | Promise < string [ ] | undefined > ,
64
+ update : MakePatchFn ,
52
65
options ?: PatchOptions ,
53
66
) : Promise < Result < string , PushError | Socket . DisconnectReason > > =>
54
67
push (
0 commit comments