@@ -12,7 +12,6 @@ import {
1212 Schema ,
1313 Stream ,
1414} from "effect" ;
15- import diff from "fast-diff" ;
1615import { LoroDoc , LoroText , type Frontiers } from "loro-crdt" ;
1716
1817export type Doc = LoroDoc < {
@@ -22,7 +21,7 @@ export type Doc = LoroDoc<{
2221export class LoroNoteManager {
2322 #noteId: string ;
2423 #noteKey: string ;
25- # doc: Doc ;
24+ doc : Doc ;
2625 #text: LoroText ;
2726 #onUpdate: ( snapshot : string ) => void | Promise < void > ;
2827 #eventSource: EventSource | null = null ;
@@ -42,12 +41,12 @@ export class LoroNoteManager {
4241 ) {
4342 this . #noteId = noteId ;
4443 this . #noteKey = noteKey ;
45- this . # doc = new LoroDoc ( ) ;
46- this . #text = this . # doc. getText ( "content" ) ;
44+ this . doc = new LoroDoc ( ) ;
45+ this . #text = this . doc . getText ( "content" ) ;
4746 this . #onUpdate = onUpdate ?? Function . constVoid ;
4847
4948 // Initialize frontiers
50- this . #lastFrontiers = this . # doc. frontiers ( ) ;
49+ this . #lastFrontiers = this . doc . frontiers ( ) ;
5150
5251 // 1. Init Hubs
5352 this . #outgoingHub = Effect . runSync ( PubSub . unbounded < Uint8Array > ( ) ) ;
@@ -66,7 +65,7 @@ export class LoroNoteManager {
6665 this . #persistenceFiber = Effect . runFork ( persistenceStream ) ;
6766
6867 // Subscribe to changes
69- this . # doc. subscribe ( ( event ) => {
68+ this . doc . subscribe ( ( event ) => {
7069 // Notify content listeners
7170 const content = this . getContent ( ) ;
7271 this . #contentListeners. forEach ( ( listener ) => {
@@ -78,9 +77,9 @@ export class LoroNoteManager {
7877
7978 // Publish local ops for sync
8079 if ( event . by === "local" ) {
81- const frontiers = this . # doc. frontiers ( ) ;
80+ const frontiers = this . doc . frontiers ( ) ;
8281 try {
83- const update = this . # doc. export ( {
82+ const update = this . doc . export ( {
8483 mode : "shallow-snapshot" ,
8584 frontiers : this . #lastFrontiers,
8685 } ) ;
@@ -121,7 +120,7 @@ export class LoroNoteManager {
121120 async init ( encryptedSnapshot ?: string ) {
122121 if ( encryptedSnapshot ) {
123122 await this . loadEncryptedSnapshot ( encryptedSnapshot ) ;
124- this . #lastFrontiers = this . # doc. frontiers ( ) ;
123+ this . #lastFrontiers = this . doc . frontiers ( ) ;
125124 }
126125 }
127126
@@ -163,7 +162,7 @@ export class LoroNoteManager {
163162 } ) . pipe (
164163 Stream . runForEach ( ( update ) =>
165164 Effect . sync ( ( ) => {
166- this . # doc. import ( update ) ;
165+ this . doc . import ( update ) ;
167166 } ) ,
168167 ) ,
169168 ) ;
@@ -222,50 +221,11 @@ export class LoroNoteManager {
222221 return this . #text. toString ( ) ;
223222 }
224223
225- /**
226- * Update text content using diffs
227- */
228- updateContent ( newContent : string ) {
229- const currentContent = this . #text. toString ( ) ;
230- if ( currentContent === newContent ) return ;
231-
232- console . debug ( "[Loro] Updating content with diff..." ) ;
233-
234- // Calculate diff
235- const diffs = diff ( currentContent , newContent ) ;
236-
237- let index = 0 ;
238- for ( const [ type , text ] of diffs ) {
239- switch ( type ) {
240- // DELETE
241- case - 1 : {
242- this . #text. delete ( index , text . length ) ;
243- break ;
244- }
245-
246- // EQUAL
247- case 0 : {
248- index += text . length ;
249- break ;
250- }
251-
252- // INSERT
253- case 1 : {
254- this . #text. insert ( index , text ) ;
255- index += text . length ;
256- break ;
257- }
258- }
259- }
260-
261- this . commit ( ) ;
262- }
263-
264224 /**
265225 * Get encrypted snapshot for storage
266226 */
267227 async getEncryptedSnapshot ( ) : Promise < string > {
268- const snapshot = this . # doc. export ( {
228+ const snapshot = this . doc . export ( {
269229 mode : "snapshot" ,
270230 } ) as Uint8Array < ArrayBuffer > ;
271231 const encrypted = await encryptData ( snapshot , this . #noteKey) ;
@@ -281,7 +241,7 @@ export class LoroNoteManager {
281241 Either . getOrThrow ,
282242 ) as Uint8Array < ArrayBuffer > ;
283243 const decrypted = await decryptData ( encryptedBytes , this . #noteKey) ;
284- this . # doc. import ( decrypted ) ;
244+ this . doc . import ( decrypted ) ;
285245 } catch ( error ) {
286246 console . error ( "Failed to load encrypted snapshot:" , error ) ;
287247 throw error ;
@@ -292,13 +252,13 @@ export class LoroNoteManager {
292252 * Apply update from another peer
293253 */
294254 applyUpdate ( update : Uint8Array ) {
295- this . # doc. import ( update ) ;
255+ this . doc . import ( update ) ;
296256 }
297257
298258 /**
299259 * Commit current changes
300260 */
301261 commit ( ) {
302- this . # doc. commit ( ) ;
262+ this . doc . commit ( ) ;
303263 }
304264}
0 commit comments