Skip to content

Commit

Permalink
[refactor] updated the shardRepo interface and it's implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
MridulDhiman committed Jan 4, 2025
1 parent e0fd6c2 commit dc078ed
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
5 changes: 2 additions & 3 deletions src/interfaces/IShardRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export interface IShardRepository {
findById: (id: string) => Promise<Shard | null>
save(doc: Shard): Promise<void>
getFiles(id: string): Promise<File[] | null>
getAllCollaborativeRoomIds(): Promise<string[] | null>
getLastSyncTimestamp(id: string): Promise<Date | null>
updateLastSyncTimestamp(id: string) : Promise<"OK" | null>
updateLastSyncTimestamp(id: string): Promise<"OK" | null>
getAllCollaborativeRooms() : Promise<Shard[] | null>
}
15 changes: 7 additions & 8 deletions src/repositories/shardRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,18 @@ export interface ShardDocument extends Omit<Shard, "id">, Document {
return room.files;
}

async getAllCollaborativeRoomIds(): Promise<string[] | null> {
const roomsDoc = await this.model.find({ mode: "collaboration" }, "_id");
async getAllCollaborativeRooms(): Promise<Shard[] | null> {
const roomsDoc = await this.model.find({ mode: "collaboration" }, "_id,lastSyncTimestamp");
if (!roomsDoc) {
return null;
}

const ids: string[] = [];

for (let doc of roomsDoc) {
const id = doc?._id as string;
ids.push(id);
let rooms : Shard[] = [];

for (let room of roomsDoc) {
rooms.push(this.toEntity(room));
}
return ids;
return rooms;
}

async getLastSyncTimestamp(id: string): Promise<Date | null> {
Expand Down
13 changes: 5 additions & 8 deletions src/services/redis/editorStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ export class EditorStateManager {
try {
// flush cache data to the DB

const roomIds = await this.shardRepo.getAllCollaborativeRoomIds();
if (roomIds && roomIds.length > 0) {
for (let id of roomIds) {
const lastSyncTimestamp = await this.shardRepo.getLastSyncTimestamp(id);

if (lastSyncTimestamp) {
const rooms = await this.shardRepo.getAllCollaborativeRooms();
if (rooms && rooms.length > 0) {
for (let room of rooms) {
const id = room.id;
const lastSyncTimestamp = room.lastSyncTimestamp;
const redisKey = `project:${id}:changes`;
const changedFiles = await this.kvStore.zrangebyscore(redisKey, lastSyncTimestamp.getTime(), "+inf");
if (changedFiles.length > 0) {
Expand All @@ -54,9 +53,7 @@ export class EditorStateManager {
code: fileState.code
});
}

}
}

await this.shardRepo.updateLastSyncTimestamp(id);
}
Expand Down

0 comments on commit dc078ed

Please sign in to comment.