1- import type { StructureItem , StructureNode , StructureShell , Symbol } from '@hey-api/codegen-core' ;
1+ import type {
2+ StructureItem ,
3+ StructureNode ,
4+ StructureShell ,
5+ Symbol ,
6+ SymbolMeta ,
7+ } from '@hey-api/codegen-core' ;
28import type { IR } from '@hey-api/shared' ;
39import { applyNaming } from '@hey-api/shared' ;
410
@@ -15,6 +21,15 @@ export interface ContractItem {
1521
1622export const source = globalThis . Symbol ( 'orpc' ) ;
1723
24+ function createShellMeta ( node : StructureNode ) : SymbolMeta {
25+ return {
26+ category : 'contract' ,
27+ resource : 'container' ,
28+ resourceId : node . getPath ( ) . join ( '.' ) ,
29+ tool : 'orpc' ,
30+ } ;
31+ }
32+
1833function createContractSymbol (
1934 plugin : OrpcPlugin [ 'Instance' ] ,
2035 item : StructureItem & { data : ContractItem } ,
@@ -100,16 +115,17 @@ function buildContainerObject(
100115 const contractSymbol = symbols . get ( operation . id ) ! ;
101116 const name = item . location [ item . location . length - 1 ] ! ;
102117 const propName = applyNaming ( name , plugin . config . contracts . contractName ) ;
103- obj . prop ( propName , $ ( contractSymbol ) ) ;
118+ obj . prop ( propName , contractSymbol ) ;
104119 }
105120
106121 for ( const child of node . children . values ( ) ) {
107122 if ( child . shell ) {
108123 const childShell = child . shell . define ( child ) ;
109- const childSymbol = ( childShell . node as ReturnType < typeof $ . const > ) . symbol ;
124+ const childNode = childShell . node as ReturnType < typeof $ . const > ;
125+ const childSymbol = childNode . symbol ;
110126 if ( childSymbol ) {
111127 const propName = applyNaming ( child . name , plugin . config . contracts . segmentName ) ;
112- obj . prop ( propName , $ ( childSymbol ) ) ;
128+ obj . prop ( propName , childSymbol ) ;
113129 }
114130 }
115131 }
@@ -118,26 +134,29 @@ function buildContainerObject(
118134}
119135
120136export function createShell ( plugin : OrpcPlugin [ 'Instance' ] ) : StructureShell {
137+ const cache = new Map < string | number , ReturnType < typeof $ . const > > ( ) ;
138+
121139 return {
122140 define : ( node ) => {
141+ const resourceId = node . getPath ( ) . join ( '.' ) ;
142+ const cached = cache . get ( resourceId ) ;
143+ if ( cached ) {
144+ return { dependencies : [ ] , node : cached } ;
145+ }
123146 const symbol = plugin . symbol (
124147 applyNaming (
125148 node . name ,
126149 node . isRoot ? plugin . config . contracts . containerName : plugin . config . contracts . segmentName ,
127150 ) ,
128151 {
129- meta : {
130- category : 'contract' ,
131- resource : 'container' ,
132- resourceId : node . getPath ( ) . join ( '.' ) ,
133- tool : plugin . name ,
134- } ,
152+ meta : createShellMeta ( node ) ,
135153 } ,
136154 ) ;
137155
138- const placeholder = $ . const ( symbol ) . export ( ) . assign ( $ . object ( ) ) ;
156+ const o = $ . const ( symbol ) . export ( ) . assign ( $ . object ( ) ) ;
157+ cache . set ( resourceId , o ) ;
139158
140- return { dependencies : [ ] , node : placeholder } ;
159+ return { dependencies : [ ] , node : o } ;
141160 } ,
142161 } ;
143162}
0 commit comments