@@ -8,46 +8,16 @@ import { WebsocketProvider } from 'y-websocket'
8
8
import ws from 'ws'
9
9
10
10
const notebook = new YNotebook ( )
11
- const cells = notebook . ydoc . getArray ( 'cells' )
12
-
13
- function append_text ( ) {
14
- const cell = notebook . getCell ( 0 )
15
- const youtput = cell . youtputs . get ( 0 )
16
- const text = youtput . get ( 'text' )
17
- text . insert ( 1 , [ ' World!' ] )
18
- }
19
-
20
- function callback2 ( event ) {
21
- if ( event . transaction . local ) {
22
- return
23
- }
24
-
25
- const cell = notebook . getCell ( 0 )
26
- if ( cell . youtputs . length > 0 ) {
27
- setTimeout ( append_text , 100 )
28
- }
29
- }
30
-
31
- function callback1 ( event ) {
32
- if ( event . transaction . local ) {
33
- return
34
- }
35
-
36
- if ( cells . length > 0 ) {
37
- const cell = notebook . getCell ( 0 )
38
- if ( cell . youtputs . length > 0 ) {
39
- setTimeout ( append_text , 100 )
40
- }
41
- else {
42
- cell . youtputs . observe ( callback2 )
43
- }
44
- }
45
- }
46
-
47
- cells . observe ( callback1 )
48
11
49
12
const wsProvider = new WebsocketProvider (
50
13
'ws://localhost:1234' , 'my-roomname' ,
51
14
notebook . ydoc ,
52
15
{ WebSocketPolyfill : ws }
53
16
)
17
+
18
+ wsProvider . on ( 'sync' , ( isSynced ) => {
19
+ const cell = notebook . getCell ( 0 )
20
+ const youtput = cell . youtputs . get ( 0 )
21
+ const text = youtput . get ( 'text' )
22
+ text . insert ( 1 , [ ' World!' ] )
23
+ } )
0 commit comments