Skip to content

Commit 71e36fd

Browse files
committed
refactor(websocket): Extract MakePatchFn type
1 parent bc21468 commit 71e36fd

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

websocket/patch.ts

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { suggestUnDupTitle } from "./suggestUnDupTitle.ts";
66
import type { Result } from "option-t/plain_result";
77
import type { Socket } from "socket.io-client";
88

9-
export type PatchOptions = PushOptions;
10-
119
export interface PatchMetadata extends Page {
1210
/** Number of retry attempts for page modification
1311
*
@@ -17,6 +15,30 @@ export interface PatchMetadata extends Page {
1715
attempts: number;
1816
}
1917

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+
2042
/** Modify an entire Scrapbox page by computing and sending only the differences
2143
*
2244
* This function handles the entire page modification process:
@@ -26,32 +48,20 @@ export interface PatchMetadata extends Page {
2648
* 4. Handles errors (e.g., duplicate titles)
2749
* 5. Retries on conflicts
2850
*
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
3955
*
4056
* 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
4359
* - On duplicate title: Automatically suggests non-conflicting title
4460
*/
4561
export const patch = (
4662
project: string,
4763
title: string,
48-
update: (
49-
lines: BaseLine[],
50-
metadata: PatchMetadata,
51-
) =>
52-
| (string | { text: string })[]
53-
| undefined
54-
| Promise<(string | { text: string })[] | undefined>,
64+
update: MakePatchFn,
5565
options?: PatchOptions,
5666
): Promise<Result<string, PushError | Socket.DisconnectReason>> =>
5767
push(

0 commit comments

Comments
 (0)