@@ -11,6 +11,7 @@ import { warn } from '../warning'
1111import {
1212 view as reactiveView ,
1313 type View ,
14+ type ViewOptions ,
1415 effectScope ,
1516 type EffectScope ,
1617 draft ,
@@ -207,7 +208,6 @@ export class ModelInternal<M extends Model = Model> {
207208 models : Record < string , ModelInstance < any > >
208209 modelProxies : Record < string , ModelInstance < any > >
209210 viewInstances : ViewExt [ ] = [ ]
210- private _modelViews : ViewExt [ ] = [ ]
211211 accessContext : AccessContext
212212
213213 stateRef : {
@@ -250,6 +250,7 @@ export class ModelInternal<M extends Model = Model> {
250250 this . isolate = this . isolate . bind ( this )
251251 this . getApi = this . getApi . bind ( this )
252252 this . _update = this . _update . bind ( this )
253+ this . _invalidateApiSnapshot = this . _invalidateApiSnapshot . bind ( this )
253254 this . _flushQueryListeners = this . _flushQueryListeners . bind ( this )
254255
255256 this . options = model
@@ -367,17 +368,6 @@ export class ModelInternal<M extends Model = Model> {
367368 }
368369
369370 getApi ( ) {
370- // Invalidate cache if any model-defined view is dirty (e.g. from
371- // cross-model dependency changes that bypass _setState).
372- if ( this . _api !== null ) {
373- for ( let i = 0 ; i < this . _modelViews . length ; i ++ ) {
374- if ( this . _modelViews [ i ] . dirty ) {
375- this . _api = null
376- break
377- }
378- }
379- }
380-
381371 if ( this . _api === null ) {
382372 const data = Object . create ( this . _apiProto )
383373 this . _api = Object . assign ( data , this . _currentState , this . views )
@@ -431,7 +421,7 @@ export class ModelInternal<M extends Model = Model> {
431421 } )
432422 }
433423
434- createView ( viewFn : ( s : any ) => any ) : ViewExt {
424+ createView ( viewFn : ( s : any ) => any , options : ViewOptions = { } ) : ViewExt {
435425 let view ! : ViewExt
436426 this . effectScope . run ( ( ) => {
437427 view = reactiveView ( ( ) => {
@@ -458,7 +448,7 @@ export class ModelInternal<M extends Model = Model> {
458448 } finally {
459449 this . accessContext = oldCtx
460450 }
461- } ) as ViewExt
451+ } , options ) as ViewExt
462452 } )
463453
464454 view . getSnapshot = ( ) => {
@@ -535,7 +525,7 @@ export class ModelInternal<M extends Model = Model> {
535525 destroy ( ) {
536526 // reset props
537527 this . _destroyed = true
538- this . _api = null
528+ this . _invalidateApiSnapshot ( )
539529 this . _lastDraftToSnapshot = new WeakMap ( )
540530
541531 this . _currentState = null
@@ -572,8 +562,12 @@ export class ModelInternal<M extends Model = Model> {
572562 return this . _queryHashScope
573563 }
574564
575- private _setState ( newState : ModelStateFromModel < M > ) {
565+ private _invalidateApiSnapshot ( ) {
576566 this . _api = null
567+ }
568+
569+ private _setState ( newState : ModelStateFromModel < M > ) {
570+ this . _invalidateApiSnapshot ( )
577571 this . _currentState = newState
578572 this . stateValue = this . stateRef . value
579573 }
@@ -683,8 +677,9 @@ export class ModelInternal<M extends Model = Model> {
683677 for ( const viewName of Object . keys ( views ) ) {
684678 this . _cacheAccess ( viewName , AccessTypes . VIEW )
685679 const viewFn = views [ viewName ]
686- const view = this . createView ( viewFn )
687- this . _modelViews . push ( view )
680+ const view = this . createView ( viewFn , {
681+ onDirty : this . _invalidateApiSnapshot ,
682+ } )
688683 Object . defineProperty ( this . views , viewName , {
689684 configurable : true ,
690685 enumerable : true ,
0 commit comments