Skip to content

Commit 5a2aaf2

Browse files
committed
🎨 makeChangeで書き換えた
1 parent 0858bf5 commit 5a2aaf2

File tree

5 files changed

+68
-95
lines changed

5 files changed

+68
-95
lines changed

browser/websocket/patch.test.ts renamed to browser/websocket/diffToChanges.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// <reference lib="deno.unstable" />
2-
import { diffToChanges } from "./patch.ts";
2+
import { diffToChanges } from "./diffToChanges.ts";
33
import { assertEquals } from "../../deps/testing.ts";
44

55
Deno.test("diffToChanges()", async ({ step }) => {
File renamed without changes.

browser/websocket/makeChanges.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { diffToChanges } from "./patch.ts";
1+
import { diffToChanges } from "./diffToChanges.ts";
22
import type { Line } from "../../deps/scrapbox.ts";
33
import {
44
Block,

browser/websocket/room.ts

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import {
55
wrap,
66
} from "../../deps/socket.ts";
77
import { getProjectId, getUserId } from "./id.ts";
8-
import { diffToChanges } from "./patch.ts";
98
import { applyCommit } from "./applyCommit.ts";
9+
import { toTitleLc } from "../../title.ts";
10+
import { makeChanges } from "./makeChanges.ts";
1011
import type { Line } from "../../deps/scrapbox.ts";
1112
import { ensureEditablePage, pushCommit } from "./_fetch.ts";
1213
export type { CommitNotification };
@@ -48,9 +49,13 @@ export async function joinPageRoom(
4849
]);
4950

5051
// 接続したページの情報
51-
let parentId = page.commitId;
52-
let created = page.persistent;
53-
let lines = page.lines;
52+
let head = {
53+
persistent: page.persistent,
54+
lines: page.lines,
55+
image: page.image,
56+
commitId: page.commitId,
57+
linksLc: page.links.map((link) => toTitleLc(link)),
58+
};
5459
const pageId = page.id;
5560

5661
const io = await socketIO();
@@ -63,55 +68,36 @@ export async function joinPageRoom(
6368
// subscribe the latest commit
6469
(async () => {
6570
for await (const { id, changes } of response("commit")) {
66-
parentId = id;
67-
lines = applyCommit(lines, changes, { updated: id, userId });
71+
head.commitId = id;
72+
head.lines = applyCommit(head.lines, changes, { updated: id, userId });
6873
}
6974
})();
7075

7176
return {
7277
patch: async (update: (before: Line[]) => string[] | Promise<string[]>) => {
73-
const tryPush = async () => {
74-
const pending = update(lines);
75-
const newLines = pending instanceof Promise ? await pending : pending;
76-
const changes: Change[] = [
77-
...diffToChanges(lines, newLines, { userId }),
78-
];
79-
80-
// 変更後のlinesを計算する
81-
const changedLines = applyCommit(lines, changes, {
82-
userId,
83-
});
84-
// タイトルの変更チェック
85-
// 空ページの場合もタイトル変更commitを入れる
86-
if (lines[0].text !== changedLines[0].text || !created) {
87-
changes.push({ title: changedLines[0].text });
88-
}
89-
// サムネイルの変更チェック
90-
const oldDescriptions = lines.slice(1, 6).map((line) => line.text);
91-
const newDescriptions = changedLines.slice(1, 6).map((lines) =>
92-
lines.text
93-
);
94-
if (oldDescriptions.join("\n") !== newDescriptions.join("\n")) {
95-
changes.push({ descriptions: newDescriptions });
96-
}
97-
98-
// pushする
99-
const { commitId } = await pushCommit(request, changes, {
100-
parentId,
101-
projectId,
102-
pageId,
103-
userId,
104-
});
105-
106-
// pushに成功したら、localにも変更を反映する
107-
parentId = commitId;
108-
created = true;
109-
lines = changedLines;
110-
};
111-
11278
for (let i = 0; i < 3; i++) {
11379
try {
114-
await tryPush();
80+
const pending = update(head.lines);
81+
const newLines = pending instanceof Promise ? await pending : pending;
82+
const changes = makeChanges(head.lines, newLines, {
83+
userId,
84+
head,
85+
});
86+
87+
const { commitId } = await pushCommit(request, changes, {
88+
parentId: head.commitId,
89+
projectId,
90+
pageId,
91+
userId,
92+
});
93+
94+
// pushに成功したら、localにも変更を反映する
95+
head.commitId = commitId;
96+
head.persistent = true;
97+
head.lines = applyCommit(head.lines, changes, {
98+
updated: commitId,
99+
userId,
100+
});
115101
break;
116102
} catch (_e: unknown) {
117103
if (i === 2) {
@@ -122,9 +108,13 @@ export async function joinPageRoom(
122108
);
123109
try {
124110
const page = await ensureEditablePage(project, title);
125-
parentId = page.commitId;
126-
created = page.persistent;
127-
lines = page.lines;
111+
head = {
112+
persistent: page.persistent,
113+
lines: page.lines,
114+
image: page.image,
115+
commitId: page.commitId,
116+
linksLc: page.links.map((link) => toTitleLc(link)),
117+
};
128118
} catch (e: unknown) {
129119
throw e;
130120
}

browser/websocket/shortcuts.ts

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Change, socketIO, wrap } from "../../deps/socket.ts";
1+
import { socketIO, wrap } from "../../deps/socket.ts";
22
import { getProjectId, getUserId } from "./id.ts";
3-
import { diffToChanges } from "./patch.ts";
4-
import { applyCommit } from "./applyCommit.ts";
3+
import { makeChanges } from "./makeChanges.ts";
54
import { pinNumber } from "./pin.ts";
65
import type { Line } from "../../deps/scrapbox.ts";
6+
import { toTitleLc } from "../../title.ts";
77
import { ensureEditablePage, pushCommit, pushWithRetry } from "./_fetch.ts";
88

99
/** 指定したページを削除する
@@ -68,53 +68,32 @@ export async function patch(
6868
getUserId(),
6969
]);
7070

71-
let persistent = page.persistent;
72-
let lines = page.lines;
73-
let parentId = page.commitId;
71+
let head = {
72+
persistent: page.persistent,
73+
lines: page.lines,
74+
image: page.image,
75+
commitId: page.commitId,
76+
linksLc: page.links.map((link) => toTitleLc(link)),
77+
};
7478
const pageId = page.id;
7579

7680
const io = await socketIO();
7781
try {
7882
const { request } = wrap(io);
7983

80-
const tryPush = async () => {
81-
const pending = update(lines);
82-
const newLines = pending instanceof Promise ? await pending : pending;
83-
const changes: Change[] = [
84-
...diffToChanges(lines, newLines, { userId }),
85-
];
86-
87-
// 変更後のlinesを計算する
88-
const changedLines = applyCommit(lines, changes, {
89-
userId,
90-
});
91-
// タイトルの変更チェック
92-
// 空ページの場合もタイトル変更commitを入れる
93-
if (lines[0].text !== changedLines[0].text || !persistent) {
94-
changes.push({ title: changedLines[0].text });
95-
}
96-
// サムネイルの変更チェック
97-
const oldDescriptions = lines.slice(1, 6).map((line) => line.text);
98-
const newDescriptions = changedLines.slice(1, 6).map((lines) =>
99-
lines.text
100-
);
101-
if (oldDescriptions.join("\n") !== newDescriptions.join("\n")) {
102-
changes.push({ descriptions: newDescriptions });
103-
}
104-
105-
// pushする
106-
await pushCommit(request, changes, {
107-
parentId,
108-
projectId,
109-
pageId,
110-
userId,
111-
});
112-
};
113-
11484
// 3回retryする
11585
for (let i = 0; i < 3; i++) {
11686
try {
117-
await tryPush();
87+
const pending = update(head.lines);
88+
const newLines = pending instanceof Promise ? await pending : pending;
89+
const changes = makeChanges(head.lines, newLines, { userId, head });
90+
91+
await pushCommit(request, changes, {
92+
parentId: head.commitId,
93+
projectId,
94+
pageId,
95+
userId,
96+
});
11897
break;
11998
} catch (_e: unknown) {
12099
if (i === 2) {
@@ -125,9 +104,13 @@ export async function patch(
125104
);
126105
try {
127106
const page = await ensureEditablePage(project, title);
128-
parentId = page.commitId;
129-
persistent = page.persistent;
130-
lines = page.lines;
107+
head = {
108+
persistent: page.persistent,
109+
lines: page.lines,
110+
image: page.image,
111+
commitId: page.commitId,
112+
linksLc: page.links.map((link) => toTitleLc(link)),
113+
};
131114
} catch (e: unknown) {
132115
throw e;
133116
}

0 commit comments

Comments
 (0)