import { connect , IViewInstance , ChangeOptions } from '@goldfishjs/reactive' ; import PageStore from './PageStore' ; import attachLogic from './attachLogic' ; import AppStore from './AppStore' ; import getMiniDataSetter from './getMiniDataSetter' ; export type PageInstance < D , S > = tinyapp . IPageInstance < D > & Omit < IViewInstance , 'store' > & { store : S ; } ; export type PageOptions < D , S > = ThisType < tinyapp . IPageInstance < D > & { store : S ; } > & tinyapp . PageOptions < D > ; export default function createMiniPage < AS extends AppStore , PS extends PageStore < AS > , D = any > ( storeClass , pageOptions = { } , options ? ) { attachLogic < 'onUnload' , Required < PageOptions < D , PS > > [ 'onUnload' ] > ( pageOptions , 'onUnload' , 'before' , function ( this ) { this . store && ( this . store . isSyncDataSafe = false ) ; } ) ; const beforeCreateStore = options && options . beforeCreateStore ; const afterCreateStore = options && options . afterCreateStore ; connect ( pageOptions , 'onLoad' , 'onUnload' , function ( this , obj , keyPathList , newV , oldV , options ? ) { if ( ! this . store . isSyncDataSafe ) { return ; } const miniDataSetter = getMiniDataSetter ( ) ; miniDataSetter . set ( this , obj , keyPathList , newV , oldV , options ) ; } , instance => { beforeCreateStore && beforeCreateStore ( instance as PageInstance < D , PS > ) ; const store = new storeClass ( ) ; afterCreateStore && afterCreateStore ( instance as PageInstance < D , PS > , store ) ; store . appStore = ( getApp ( ) as any ) . store ; return store ; } , { onError : pageOptions . onError , } ) ; return pageOptions ; }