@@ -81,7 +81,7 @@ export function createGhostVRefInInstance (prototypeId: VRef,
8181
8282export function createMaterializeGhostEvent ( resolver : Resolver , ghostId : VRef ) : ?Action {
8383 try {
84- return createMaterializeGhostPathAction ( resolver , ghostId . getGhostPath ( ) , undefined , true ) ;
84+ return createMaterializeGhostReferenceAction ( resolver , ghostId , undefined , true ) ;
8585 } catch ( error ) {
8686 throw wrapError ( error , `During createMaterializeGhostEvent(), with:` ,
8787 "\n\tghostId:" , ...dumpObject ( ghostId ) ) ;
@@ -100,15 +100,22 @@ export function createMaterializeGhostPathAction (resolver: Resolver, ghostObjec
100100 typeName : string , isEvent : ?boolean ) : ?Action {
101101 const actions = [ ] ;
102102 invariantify ( ghostObjectPath . isGhost ( ) , "materializeGhostPathAction.ghostObjectPath.isGhost" ) ;
103- _createMaterializeGhostAction ( resolver , resolver . getState ( ) , ghostObjectPath , typeName , isEvent ,
104- actions ) ;
105- return ! actions . length ? undefined
106- : actions . length === 1 ? actions [ 0 ]
107- : transacted ( { actions } ) ;
103+ _createMaterializeGhostAction ( resolver , resolver . getState ( ) , ghostObjectPath , typeName ,
104+ isEvent , undefined , actions ) ;
105+ return ! actions . length ? undefined : actions . length === 1 ? actions [ 0 ] : transacted ( { actions } ) ;
106+ }
107+
108+ export function createMaterializeGhostReferenceAction ( resolver : Resolver , ghostId : VRef ,
109+ typeName : string , isEvent : ?boolean ) : ?Action {
110+ const actions = [ ] ;
111+ invariantify ( ghostId . isGhost ( ) , "materializeGhostPathAction.ghostObjectPath.isGhost" ) ;
112+ _createMaterializeGhostAction ( resolver , resolver . getState ( ) , ghostId . getGhostPath ( ) , typeName ,
113+ isEvent , ghostId , actions ) ;
114+ return ! actions . length ? undefined : actions . length === 1 ? actions [ 0 ] : transacted ( { actions } ) ;
108115}
109116
110117/**
111- * Like createMaterializeGhostPathAction but allows creationg of
118+ * Like createMaterializeGhostEvent but allows creationg of
112119 * a transient for non-ghost resources as well.
113120 *
114121 * @export
@@ -117,19 +124,18 @@ export function createMaterializeGhostPathAction (resolver: Resolver, ghostObjec
117124 * @param {string } typeName
118125 * @returns {?Action }
119126 */
120- export function createMaterializeTransientAction ( resolver : Resolver , ghostObjectPath : GhostPath ,
121- typeName : string ) : ?Action {
127+ export function createMaterializeTransientAction ( resolver : Resolver , id : VRef , typeName : string ) :
128+ ?Action {
122129 const actions = [ ] ;
123- _createMaterializeGhostAction ( resolver , resolver . getState ( ) , ghostObjectPath , typeName , false ,
124- actions ) ;
125- return ! actions . length ? undefined
126- : actions . length === 1 ? actions [ 0 ]
127- : transacted ( { actions } ) ;
130+ _createMaterializeGhostAction ( resolver , resolver . getState ( ) , id . getGhostPath ( ) , typeName ,
131+ false , id , actions ) ;
132+ return ! actions . length ? undefined : actions . length === 1 ? actions [ 0 ] : transacted ( { actions } ) ;
128133}
129134
130135function _createMaterializeGhostAction ( resolver : Resolver , state : State ,
131- ghostObjectPath : GhostPath , typeName : string , isEvent : boolean ,
132- outputActions : Array < Action > ) : { id : string , actualType : string , ghostPath : GhostPath } {
136+ ghostObjectPath : GhostPath , typeName : string , isEvent : boolean , knownId : ?VRef ,
137+ outputActions : Array < Action > ,
138+ ) : { id: string , actualType : string , ghostPath : GhostPath } {
133139 // TODO(iridian): This whole segment needs to be re-evaluated now
134140 // with the introduction of the "ghostOwnlings"/"ghostOwner" coupling
135141 // introduction. Specifically: owners would not need to be
@@ -161,28 +167,34 @@ function _createMaterializeGhostAction (resolver: Resolver, state: State,
161167 // the referred resource doesn't exist. As it stands there is no
162168 // theoretical way to determine the actual partition id reliably,
163169 // either. Create an inactive reference for the resource.
164- const id = vRef ( ghostRawId ) ;
165- // TODO(iridian): Add inactive partition checks: throw if this partition is in fact active.
170+ const id = knownId || vRef ( ghostRawId ) ;
171+ if ( knownId && ! knownId . isInactive ( ) ) {
172+ throw new Error ( "Cannot materialize a non-existent resource (partition is active)" ) ;
173+ }
174+ // Make also the resource inactive, not the partition reference
175+ // prototype. This way only transient merging will activate it.
166176 id . setInactive ( ) ;
167177 actualType = resolver . schema . inactiveType . name ;
168178 outputActions . push ( created ( {
169179 id, typeName : actualType ,
170180 meta : { noSubMaterialize : ! isEvent } ,
171181 } ) ) ;
172- return { id, actualType, ghostPath : id . getGhostPath } ;
182+ return { id, actualType, ghostPath : id . getGhostPath ( ) } ;
173183 }
174184 // A regular non-root ghost Resource with no transient.
175185 // Still possibly inside an inactive partition.
176186 const { id : ghostPrototype , actualType : prototypeTypeName , ghostPath : prototypePath }
177187 = _createMaterializeGhostAction ( resolver , state , ghostObjectPath . previousStep ( ) , typeName ,
178- false , outputActions ) ;
188+ false , undefined , outputActions ) ;
179189 actualType = isInactiveTypeName ( prototypeTypeName ) ? typeName : prototypeTypeName ;
180190 const ghostPath = prototypePath
181191 . withNewStep ( ghostHostPrototypeRawId , ghostHostRawId , ghostRawId ) ;
182192 const hostType = state . getIn ( [ "TransientFields" , ghostHostRawId ] ) ;
183193 const hostId = hostType
184194 ? state . getIn ( [ hostType , ghostHostRawId ] ) . get ( "id" )
185- : Object . create ( new ValaaReference ( ) . initResolverComponent ( { inactive : true } ) )
195+ : Object . create ( knownId
196+ ? Object . getPrototypeOf ( knownId )
197+ : new ValaaReference ( ) . initResolverComponent ( { inactive : true } ) )
186198 . initNSS ( ghostHostRawId ) ;
187199 const id = Object . create ( Object . getPrototypeOf ( hostId ) ) . initNSS ( ghostRawId ) ;
188200 id . connectGhostPath ( ghostPath ) ;
@@ -196,6 +208,7 @@ function _createMaterializeGhostAction (resolver: Resolver, state: State,
196208 throw wrapError ( error , `During createMaterializeGhostAction(${ dumpify ( ghostObjectPath ) } :${
197209 typeName } /${ actualType } }), with:`,
198210 "\n\ttransientType:" , actualType ,
211+ "\n\tknownId:" , knownId ,
199212 "\n\tghost host prototype:" , ghostHostPrototypeRawId ,
200213 "\n\tghost host:" , ghostHostRawId ,
201214 "\n\tghost id:" , ghostRawId ) ;
0 commit comments