@@ -11,8 +11,7 @@ import {
1111
1212import { PortContext } from './context'
1313import { EntityAction , EntityActionType } from 'shared/types'
14-
15- import { rendererProtocol } from './../../../renderer-protocol/rpcClient'
14+ import { registerCRDTService } from 'renderer-protocol/services/crdtService'
1615
1716function getPayload ( payloadType : EAType , payload : Payload ) : any {
1817 switch ( payloadType ) {
@@ -60,60 +59,62 @@ function getPayload(payloadType: EAType, payload: Payload): any {
6059}
6160
6261export function registerEngineApiServiceServerImplementation ( port : RpcServerPort < PortContext > ) {
63- codegen . registerService ( port , EngineApiServiceDefinition , async ( ) => ( {
64- async sendBatch ( req : ManyEntityAction , ctx ) {
65- const actions : EntityAction [ ] = [ ]
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 [ ] = [ ]
6668
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- } )
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+ }
7578 }
76- }
7779
78- if ( actions . length ) {
79- ctx . sendBatch ( actions )
80- }
80+ if ( actions . length ) {
81+ ctx . sendBatch ( actions )
82+ }
8183
82- const events : EventData [ ] = ctx . events
84+ const events : EventData [ ] = ctx . events
8385
84- if ( events . length ) {
85- ctx . events = [ ]
86- }
86+ if ( events . length ) {
87+ ctx . events = [ ]
88+ }
8789
88- return { events }
89- } ,
90+ return { events }
91+ } ,
9092
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- } ,
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+ } ,
107108
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 ] }
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+ }
115116 }
116- } ) )
117+ } )
117118}
118119function eaTypeToStr ( type : EAType ) : EntityActionType | null {
119120 switch ( type ) {
0 commit comments