Skip to content

Commit

Permalink
Separate in different type file
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <marrcooos@gmail.com>
  • Loading branch information
mcandeia committed Mar 11, 2024
1 parent 3a515a2 commit c37d497
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 74 deletions.
5 changes: 4 additions & 1 deletion src/crdt/text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { DeleteAtOperation, TextFilePatchOperation } from "../realtime.ts";
import type {
DeleteAtOperation,
TextFilePatchOperation,
} from "../realtime.types.ts";
import { BinaryIndexedTree } from "./bit.ts";

const shrinkOperations = (
Expand Down
85 changes: 12 additions & 73 deletions src/realtime.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { applyReducer, type Operation } from "fast-json-patch";
import { applyReducer } from "fast-json-patch";
import { BinaryIndexedTree } from "./crdt/bit.ts";
import { apply } from "./crdt/text.ts";
import { type Env } from "./index.ts";
import { Router, Routes, createRouter } from "./router.ts";
import { createRouter, Router, Routes } from "./router.ts";
import {
File,
FilePatchResult,
FileSystemNode,
isJSONFilePatch,
isTextFileSet,
ServerEvent,
VolumePatchRequest,
VolumePatchResponse,
} from "./realtime.types.ts";

export const getObjectFor = (volume: string, ctx: { env: Env }) => {
const object = volume.startsWith("ephemeral:")
Expand All @@ -16,77 +26,6 @@ export const getObjectFor = (volume: string, ctx: { env: Env }) => {
const encoder = new TextEncoder();
const decoder = new TextDecoder();

export interface BaseFilePatch {
path: string;
}
export type TextFilePatchOperation = InsertAtOperation | DeleteAtOperation;

export interface TextFielPatchOperationBase {
at: number;
}

export interface InsertAtOperation extends TextFielPatchOperationBase {
text: string;
}

export interface DeleteAtOperation extends TextFielPatchOperationBase {
length: number;
}
export interface TextFilePatch extends BaseFilePatch {
operations: TextFilePatchOperation[];
timestamp: number;
}

export interface TextFileSet extends BaseFilePatch {
content: string | null;
}

export type FilePatch = JSONFilePatch | TextFilePatch | TextFileSet;

export const isJSONFilePatch = (patch: FilePatch): patch is JSONFilePatch => {
return (patch as JSONFilePatch).patches !== undefined;
};

export const isTextFileSet = (patch: FilePatch): patch is TextFileSet => {
return (patch as TextFileSet).content !== undefined;
};

export interface JSONFilePatch extends BaseFilePatch {
patches: Operation[];
}

export interface VolumePatchRequest {
messageId?: string;
patches: FilePatch[];
}

export interface FilePatchResult {
path: string;
accepted: boolean;
content?: string;
deleted?: boolean;
}

export interface VolumePatchResponse {
results: FilePatchResult[];
timestamp: number;
}

export interface FsEvent {
messageId?: string;
path: string;
deleted?: boolean;
timestamp: number;
}

export type ServerEvent = FsEvent;

export interface File {
content: string;
}

export type FileSystemNode = Record<string, File>;

export class RealtimeError extends Error {
public status: number;

Expand Down
72 changes: 72 additions & 0 deletions src/realtime.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { type Operation } from "fast-json-patch";

export interface BaseFilePatch {
path: string;
}
export type TextFilePatchOperation = InsertAtOperation | DeleteAtOperation;

export interface TextFielPatchOperationBase {
at: number;
}

export interface InsertAtOperation extends TextFielPatchOperationBase {
text: string;
}

export interface DeleteAtOperation extends TextFielPatchOperationBase {
length: number;
}
export interface TextFilePatch extends BaseFilePatch {
operations: TextFilePatchOperation[];
timestamp: number;
}

export interface TextFileSet extends BaseFilePatch {
content: string | null;
}

export type FilePatch = JSONFilePatch | TextFilePatch | TextFileSet;

export const isJSONFilePatch = (patch: FilePatch): patch is JSONFilePatch => {
return (patch as JSONFilePatch).patches !== undefined;
};

export const isTextFileSet = (patch: FilePatch): patch is TextFileSet => {
return (patch as TextFileSet).content !== undefined;
};

export interface JSONFilePatch extends BaseFilePatch {
patches: Operation[];
}

export interface VolumePatchRequest {
messageId?: string;
patches: FilePatch[];
}

export interface FilePatchResult {
path: string;
accepted: boolean;
content?: string;
deleted?: boolean;
}

export interface VolumePatchResponse {
results: FilePatchResult[];
timestamp: number;
}

export interface FsEvent {
messageId?: string;
path: string;
deleted?: boolean;
timestamp: number;
}

export type ServerEvent = FsEvent;

export interface File {
content: string;
}

export type FileSystemNode = Record<string, File>;

0 comments on commit c37d497

Please sign in to comment.