@@ -24,6 +24,7 @@ import { processMessage } from "./protocol/message/mod";
2424import { CachedSerializer } from "./protocol/serde" ;
2525import { Schedule } from "./schedule" ;
2626import { DeadlineError , Lock , deadline } from "./utils" ;
27+ import * as cbor from "cbor-x" ;
2728
2829/**
2930 * Options for the `_saveState` method.
@@ -122,10 +123,10 @@ export class ActorInstance<S, CP, CS, V, I, AD, DB> {
122123 *
123124 * Any data that should be stored indefinitely should be held within this object.
124125 */
125- #persist! : PersistedActor < S , CP , CS > ;
126+ #persist! : PersistedActor < S , CP , CS , I > ;
126127
127128 /** Raw state without the proxy wrapper */
128- #persistRaw! : PersistedActor < S , CP , CS > ;
129+ #persistRaw! : PersistedActor < S , CP , CS , I > ;
129130
130131 #writePersistLock = new Lock < void > ( void 0 ) ;
131132
@@ -426,7 +427,7 @@ export class ActorInstance<S, CP, CS, V, I, AD, DB> {
426427 // Write to KV
427428 await this . #actorDriver. writePersistedData (
428429 this . #actorId,
429- this . #persistRaw,
430+ cbor . encode ( this . #persistRaw) ,
430431 ) ;
431432
432433 logger ( ) . debug ( "persist saved" ) ;
@@ -443,7 +444,7 @@ export class ActorInstance<S, CP, CS, V, I, AD, DB> {
443444 /**
444445 * Creates proxy for `#persist` that handles automatically flagging when state needs to be updated.
445446 */
446- #setPersist( target : PersistedActor < S , CP , CS > ) {
447+ #setPersist( target : PersistedActor < S , CP , CS , I > ) {
447448 // Set raw persist object
448449 this . #persistRaw = target ;
449450
@@ -514,11 +515,21 @@ export class ActorInstance<S, CP, CS, V, I, AD, DB> {
514515
515516 async #initialize( ) {
516517 // Read initial state
517- const persistData = ( await this . #actorDriver. readPersistedData (
518+ const persistDataBuffer = await this . #actorDriver. readPersistedData (
518519 this . #actorId,
519- ) ) as PersistedActor < S , CP , CS > ;
520-
521- if ( persistData !== undefined ) {
520+ ) ;
521+ invariant (
522+ persistDataBuffer !== undefined ,
523+ "persist data has not been set, it should be set when initialized" ,
524+ ) ;
525+ const persistData = cbor . decode ( persistDataBuffer ) as PersistedActor <
526+ S ,
527+ CP ,
528+ CS ,
529+ I
530+ > ;
531+
532+ if ( persistData . hi ) {
522533 logger ( ) . info ( "actor restoring" , {
523534 connections : persistData . c . length ,
524535 } ) ;
@@ -546,8 +557,6 @@ export class ActorInstance<S, CP, CS, V, I, AD, DB> {
546557 } else {
547558 logger ( ) . info ( "actor creating" ) ;
548559
549- const input = ( await this . #actorDriver. readInput ( this . #actorId) ) as I ;
550-
551560 // Initialize actor state
552561 let stateData : unknown = undefined ;
553562 if ( this . stateEnabled ) {
@@ -567,7 +576,7 @@ export class ActorInstance<S, CP, CS, V, I, AD, DB> {
567576 undefined ,
568577 undefined
569578 > ,
570- { input } ,
579+ { input : persistData . i } ,
571580 ) ;
572581 } else if ( "state" in this . #config) {
573582 stateData = structuredClone ( this . #config. state ) ;
@@ -578,21 +587,24 @@ export class ActorInstance<S, CP, CS, V, I, AD, DB> {
578587 logger ( ) . debug ( "state not enabled" ) ;
579588 }
580589
581- const persist : PersistedActor < S , CP , CS > = {
582- s : stateData as S ,
583- c : [ ] ,
584- e : [ ] ,
585- } ;
590+ // Save state and mark as initialized
591+ persistData . s = stateData as S ;
592+ persistData . hi = true ;
586593
587594 // Update state
588595 logger ( ) . debug ( "writing state" ) ;
589- await this . #actorDriver. writePersistedData ( this . #actorId, persist ) ;
596+ await this . #actorDriver. writePersistedData (
597+ this . #actorId,
598+ cbor . encode ( persistData ) ,
599+ ) ;
590600
591- this . #setPersist( persist ) ;
601+ this . #setPersist( persistData ) ;
592602
593603 // Notify creation
594604 if ( this . #config. onCreate ) {
595- await this . #config. onCreate ( this . actorContext , { input } ) ;
605+ await this . #config. onCreate ( this . actorContext , {
606+ input : persistData . i ,
607+ } ) ;
596608 }
597609 }
598610 }
0 commit comments