@@ -2,11 +2,14 @@ import { ParseResult, Schema } from "effect";
22import { LoroDoc , LoroList , LoroMap } from "loro-crdt" ;
33import { ActivitySchema , ActivityV1 , type ActivityV2 } from "./schema" ;
44
5+ /** 👇 Versioning management */
6+
57const Version = [ 1 , 2 , 3 ] as const ;
68export type Version = ( typeof Version ) [ number ] ;
7-
89export const VERSION = 3 satisfies Version ;
910
11+ /** 👆 Versioning management */
12+
1013const AnyLoroDocSchema = Schema . instanceOf ( LoroDoc ) ;
1114
1215export const Metadata = Schema . Struct ( { version : Schema . Number } ) ;
@@ -25,28 +28,20 @@ export class SnapshotSchema extends Schema.Class<SnapshotSchema>(
2528} ) {
2629 static readonly EmptyDoc = ( ) => {
2730 const doc = new LoroDoc < LoroSchema > ( ) ;
28-
29- const metadata = doc . getMap ( "metadata" ) ;
30- metadata . set ( "version" , VERSION ) ;
31-
31+ doc . getMap ( "metadata" ) . set ( "version" , VERSION ) ;
3232 doc . getList ( "activity" ) ;
33-
3433 return doc ;
3534 } ;
3635}
3736
3837const migrations = {
3938 1 : ( doc ) => {
40- const metadata = doc . getMap ( "metadata" ) ;
41- metadata . set ( "version" , VERSION ) ;
42-
39+ doc . getMap ( "metadata" ) . set ( "version" , VERSION ) ;
4340 doc . getList ( "activity" ) ;
44-
4541 return doc ;
4642 } ,
4743 2 : ( doc ) => {
48- const metadata = doc . getMap ( "metadata" ) ;
49- metadata . set ( "version" , 2 ) ;
44+ doc . getMap ( "metadata" ) . set ( "version" , 2 ) ;
5045
5146 const activity = doc . getList ( "activity" ) ;
5247 for ( let i = 0 ; i < activity . length ; i ++ ) {
@@ -61,9 +56,8 @@ const migrations = {
6156
6257 return doc ;
6358 } ,
64- "3" : ( doc ) => {
65- const metadata = doc . getMap ( "metadata" ) ;
66- metadata . set ( "version" , 3 ) ;
59+ 3 : ( doc ) => {
60+ doc . getMap ( "metadata" ) . set ( "version" , 3 ) ;
6761
6862 const activity = doc . getList ( "activity" ) ;
6963 for ( let i = 0 ; i < activity . length ; i ++ ) {
@@ -85,8 +79,7 @@ export const LoroDocMigration = AnyLoroDocSchema.pipe(
8579 decode : ( from , _ , ast ) => {
8680 const doc = new LoroDoc ( ) ;
8781 doc . import ( from . export ( { mode : "snapshot" } ) ) ;
88- const metadata = doc . getMap ( "metadata" ) ;
89- const currentVersion = metadata . get ( "version" ) ;
82+ const currentVersion = doc . getMap ( "metadata" ) . get ( "version" ) ;
9083
9184 if ( typeof currentVersion === "number" ) {
9285 Version . forEach ( ( version ) => {
0 commit comments