@@ -11,7 +11,8 @@ import {
1111
1212import { PortContext } from './context'
1313import { EntityAction , EntityActionType } from 'shared/types'
14- import { registerCRDTService } from 'renderer-protocol/services/crdtService'
14+
15+ import { rendererProtocol } from './../../../renderer-protocol/rpcClient'
1516
1617function getPayload ( payloadType : EAType , payload : Payload ) : any {
1718 switch ( payloadType ) {
@@ -59,62 +60,60 @@ function getPayload(payloadType: EAType, payload: Payload): any {
5960}
6061
6162export function registerEngineApiServiceServerImplementation ( port : RpcServerPort < PortContext > ) {
62- codegen . registerService ( port , EngineApiServiceDefinition , async ( port , ctx ) => {
63- const crdtService = registerCRDTService ( ctx . rendererPort )
64-
65- return {
66- async sendBatch ( req : ManyEntityAction , ctx ) {
67- const actions : EntityAction [ ] = [ ]
63+ codegen . registerService ( port , EngineApiServiceDefinition , async ( ) => ( {
64+ async sendBatch ( req : ManyEntityAction , ctx ) {
65+ const actions : EntityAction [ ] = [ ]
6866
69- for ( const action of req . actions ) {
70- const actionType = eaTypeToStr ( action . type )
71- if ( actionType && action . payload ) {
72- actions . push ( {
73- type : actionType ,
74- tag : action . tag ,
75- payload : getPayload ( action . type , action . payload as any )
76- } )
77- }
67+ for ( const action of req . actions ) {
68+ const actionType = eaTypeToStr ( action . type )
69+ if ( actionType && action . payload ) {
70+ actions . push ( {
71+ type : actionType ,
72+ tag : action . tag ,
73+ payload : getPayload ( action . type , action . payload as any )
74+ } )
7875 }
76+ }
7977
80- if ( actions . length ) {
81- ctx . sendBatch ( actions )
82- }
78+ if ( actions . length ) {
79+ ctx . sendBatch ( actions )
80+ }
8381
84- const events : EventData [ ] = ctx . events
82+ const events : EventData [ ] = ctx . events
8583
86- if ( events . length ) {
87- ctx . events = [ ]
88- }
84+ if ( events . length ) {
85+ ctx . events = [ ]
86+ }
8987
90- return { events }
91- } ,
88+ return { events }
89+ } ,
9290
93- async subscribe ( req , ctx ) {
94- ctx . subscribedEvents . add ( req . eventId )
95- return { }
96- } ,
97- async unsubscribe ( req , ctx ) {
98- ctx . subscribedEvents . delete ( req . eventId )
99- return { }
100- } ,
101- async crdtSendToRenderer ( req , ctx ) {
102- return crdtService . sendCrdt ( {
103- sceneId : ctx . sceneData . id ,
104- payload : req . data ,
105- sceneNumber : ctx . sceneData . sceneNumber
106- } )
107- } ,
91+ async subscribe ( req , ctx ) {
92+ ctx . subscribedEvents . add ( req . eventId )
93+ return { }
94+ } ,
95+ async unsubscribe ( req , ctx ) {
96+ ctx . subscribedEvents . delete ( req . eventId )
97+ return { }
98+ } ,
99+ async crdtSendToRenderer ( req , ctx ) {
100+ const protocol = await rendererProtocol
101+ return protocol . crdtService . sendCrdt ( {
102+ sceneId : ctx . sceneData . id ,
103+ payload : req . data ,
104+ sceneNumber : ctx . sceneData . sceneNumber
105+ } )
106+ } ,
108107
109- async crdtGetMessageFromRenderer ( _ , ctx ) {
110- const response = await crdtService . pullCrdt ( {
111- sceneId : ctx . sceneData . id ,
112- sceneNumber : ctx . sceneData . sceneNumber
113- } )
114- return { data : [ response . payload ] }
115- }
108+ async crdtGetMessageFromRenderer ( _ , ctx ) {
109+ const protocol = await rendererProtocol
110+ const response = await protocol . crdtService . pullCrdt ( {
111+ sceneId : ctx . sceneData . id ,
112+ sceneNumber : ctx . sceneData . sceneNumber
113+ } )
114+ return { data : [ response . payload ] }
116115 }
117- } )
116+ } ) )
118117}
119118function eaTypeToStr ( type : EAType ) : EntityActionType | null {
120119 switch ( type ) {
0 commit comments