Skip to content

Commit 8cf7b83

Browse files
authored
Merge pull request #177 from takker99:push
fix(patch): Wrong loop escape conditions
2 parents 063961a + d7022fc commit 8cf7b83

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

browser/websocket/push.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,23 @@ export const push = async (
6565
| [PinChange],
6666
options?: PushOptions,
6767
): Promise<Result<string, RetryError>> => {
68-
const [
69-
page_,
70-
projectId,
71-
userId,
72-
] = await Promise.all([
73-
pull(project, title),
74-
getProjectId(project),
75-
getUserId(),
76-
]);
77-
78-
let page: PushMetadata = { ...page_, projectId, userId };
79-
8068
const injectedSocket = options?.socket;
8169
const socket = injectedSocket ?? await socketIO();
8270
await connect(socket);
8371

8472
try {
73+
let page: PushMetadata = await Promise.all([
74+
pull(project, title),
75+
getProjectId(project),
76+
getUserId(),
77+
]).then(([page_, projectId, userId]) => ({ ...page_, projectId, userId }));
78+
8579
const { request } = wrap(socket);
86-
// loop for create Diff
8780
let attempts = 0;
8881
let changes: Change[] | [DeletePageChange] | [PinChange] = [];
8982
let reason: "NotFastForwardError" | "DuplicateTitleError" | undefined;
83+
84+
// loop for create Diff
9085
while (
9186
options?.maxAttempts === undefined || attempts < options.maxAttempts
9287
) {
@@ -99,10 +94,10 @@ export const push = async (
9994

10095
const data: PageCommit = {
10196
kind: "page",
102-
projectId,
97+
projectId: page.projectId,
10398
pageId: page.id,
10499
parentId: page.commitId,
105-
userId,
100+
userId: page.userId,
106101
changes,
107102
cursor: null,
108103
freeze: true,
@@ -132,14 +127,20 @@ export const push = async (
132127
}
133128
if (name === "TimeoutError" || name === "SocketIOError") {
134129
await sleep(3000);
135-
// go back to the diff loop
136-
break;
130+
// go back to the push loop
131+
continue;
137132
}
138133
if (name === "NotFastForwardError") {
139-
page = { ...await pull(project, title), projectId, userId };
134+
await sleep(1000);
135+
page = {
136+
...await pull(project, title),
137+
projectId: page.projectId,
138+
userId: page.userId,
139+
};
140140
}
141141
reason = name;
142-
// go back to the push loop
142+
// go back to the diff loop
143+
break;
143144
}
144145
}
145146
return {

0 commit comments

Comments
 (0)