Starting to implement push
This commit is contained in:
48
src/sync.ts
48
src/sync.ts
@ -4,9 +4,9 @@ import PullGetLogRequest from "./com/pullgetlogrequest";
|
||||
import PullGetLogResponse from "./com/pullgetlogresponse";
|
||||
import { ServiceProvider as ServiceProviderClient } from "./com/service_client";
|
||||
import { ServiceProvider as ServiceProviderServer } from "./com/service_server";
|
||||
import { PullObjectRequest, PullObjectResponse, Sync as SyncClient, SyncInitRequest, SyncInitResponse } from "./com/sync_client";
|
||||
import { PullObjectRequest, PullObjectResponse, PushObjectRequest, PushObjectResponse, Sync as SyncClient, SyncInitRequest, SyncInitResponse } from "./com/sync_client";
|
||||
import { Sync as SyncServer } from "./com/sync_server";
|
||||
import Repository from "./repo";
|
||||
import Repository, { ObjectTypes } from "./repo";
|
||||
|
||||
if (typeof btoa === 'undefined') {
|
||||
global.btoa = function (str) {
|
||||
@ -84,13 +84,13 @@ export async function startSyncClient(stream: ISimpleStream, repo: Repository) {
|
||||
let head = await repo.readHead();
|
||||
let remoteContainsHead = false;
|
||||
|
||||
let response = await service.SyncInit({
|
||||
let syncResponse = await service.SyncInit({
|
||||
clientCommit: head?.id as string
|
||||
})
|
||||
|
||||
if (response.needPull) {
|
||||
if (syncResponse.needPull) {
|
||||
let commitsToDownload = new Set<string>();
|
||||
let toAnalyse: string[] = [response.remoteCommit];
|
||||
let toAnalyse: string[] = [syncResponse.remoteCommit];
|
||||
while (toAnalyse.length > 0) {
|
||||
let current = toAnalyse.pop() as string;
|
||||
|
||||
@ -168,18 +168,32 @@ export async function startSyncClient(stream: ISimpleStream, repo: Repository) {
|
||||
|
||||
if (head) {
|
||||
if (remoteContainsHead) {
|
||||
await repo.writeHead(response.remoteCommit);
|
||||
await repo.writeHead(syncResponse.remoteCommit);
|
||||
} else {
|
||||
let commit = await repo.mergeCommits(head.id, response.remoteCommit);
|
||||
let commit = await repo.mergeCommits(head.id, syncResponse.remoteCommit);
|
||||
await repo.writeHead(commit);
|
||||
}
|
||||
} else if (response.remoteCommit) {
|
||||
await repo.writeHead(response.remoteCommit);
|
||||
} else if (syncResponse.remoteCommit) {
|
||||
await repo.writeHead(syncResponse.remoteCommit);
|
||||
}
|
||||
}
|
||||
|
||||
if (response.needPush) {
|
||||
if (syncResponse.needPush) {
|
||||
//TODO: Push
|
||||
// Steps:
|
||||
// 1. Figure out missing commits
|
||||
// 2. Push missing Objects of these commits
|
||||
// 3. Push missing commits
|
||||
let commitsToPush = new Set<string>();
|
||||
let objectsToPush = new Set<string>();
|
||||
|
||||
let localHead = await repo.readHead()
|
||||
if (localHead) { // If there is nothing to push, don't push
|
||||
// Find matching point
|
||||
let match = undefined;
|
||||
|
||||
if (!syncResponse.remoteCommit) { }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -274,7 +288,18 @@ class SyncRemote extends SyncServer<never> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async PushObject({ id, type, data }: PushObjectRequest): Promise<PushObjectResponse> {
|
||||
if (type in ObjectTypes) {
|
||||
let newID = await this.repo.writeObject(data, type as any)
|
||||
if (newID !== id) //TODO: Maybe cleanup wrong object?
|
||||
throw new Error("Invalid ID!");
|
||||
return {
|
||||
success: true
|
||||
}
|
||||
} else {
|
||||
throw new Error("Invalid type!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function startSyncRemote(stream: ISimpleStream, repo: Repository) {
|
||||
@ -284,6 +309,5 @@ export async function startSyncRemote(stream: ISimpleStream, repo: Repository) {
|
||||
const sess = prov.getSession(stream.send.bind(stream));
|
||||
stream.on("data", (data) => sess.onMessage(data).catch(no));
|
||||
stream.on("close", () => { sess.close(); yes() });
|
||||
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user