Skip to content

Commit 186f455

Browse files
authored
Merge pull request #14 from takker99/patch-with-metadata
👍 Enable to use metadata of the page on `update()`
2 parents c3632de + fbcdd70 commit 186f455

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

browser/websocket/room.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CommitNotification, socketIO, wrap } from "../../deps/socket.ts";
22
import { getProjectId, getUserId } from "./id.ts";
33
import { applyCommit } from "./applyCommit.ts";
44
import { makeChanges } from "./makeChanges.ts";
5-
import { pull } from "./pull.ts";
5+
import { HeadData, pull } from "./pull.ts";
66
import type { Line } from "../../deps/scrapbox.ts";
77
import { pushCommit } from "./_fetch.ts";
88
export type { CommitNotification };
@@ -15,7 +15,9 @@ export interface JoinPageRoomResult {
1515
*
1616
* @param update 書き換え後の本文を作成する函数。引数には現在の本文が渡される
1717
*/
18-
patch: (update: (before: Line[]) => string[]) => Promise<void>;
18+
patch: (
19+
update: (before: Line[], metadata?: HeadData) => string[],
20+
) => Promise<void>;
1921
/** ページの更新情報を購読する */
2022
listenPageUpdate: () => AsyncGenerator<CommitNotification, void, unknown>;
2123
/** ページの操作を終了する。これを呼び出すと他のmethodsは使えなくなる
@@ -62,10 +64,15 @@ export async function joinPageRoom(
6264
})();
6365

6466
return {
65-
patch: async (update: (before: Line[]) => string[] | Promise<string[]>) => {
67+
patch: async (
68+
update: (
69+
before: Line[],
70+
metadata?: HeadData,
71+
) => string[] | Promise<string[]>,
72+
) => {
6673
for (let i = 0; i < 3; i++) {
6774
try {
68-
const pending = update(head.lines);
75+
const pending = update(head.lines, head);
6976
const newLines = pending instanceof Promise ? await pending : pending;
7077
const changes = makeChanges(head.lines, newLines, {
7178
userId,

browser/websocket/shortcuts.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { socketIO, wrap } from "../../deps/socket.ts";
22
import { getProjectId, getUserId } from "./id.ts";
33
import { makeChanges } from "./makeChanges.ts";
4-
import { pull } from "./pull.ts";
4+
import { HeadData, pull } from "./pull.ts";
55
import { pinNumber } from "./pin.ts";
66
import type { Line } from "../../deps/scrapbox.ts";
77
import { pushCommit, pushWithRetry } from "./_fetch.ts";
@@ -55,7 +55,7 @@ export async function deletePage(
5555
export async function patch(
5656
project: string,
5757
title: string,
58-
update: (lines: Line[]) => string[] | Promise<string[]>,
58+
update: (lines: Line[], metadata?: HeadData) => string[] | Promise<string[]>,
5959
): Promise<void> {
6060
const [
6161
head_,
@@ -76,7 +76,7 @@ export async function patch(
7676
// 3回retryする
7777
for (let i = 0; i < 3; i++) {
7878
try {
79-
const pending = update(head.lines);
79+
const pending = update(head.lines, head);
8080
const newLines = pending instanceof Promise ? await pending : pending;
8181
const changes = makeChanges(head.lines, newLines, { userId, head });
8282

0 commit comments

Comments
 (0)