@@ -29,12 +29,14 @@ export interface IVueDatasourceRepository {
2929 ) => Promise < void >
3030 update : ( oldVal : string , newVal : string ) => void
3131 getDataWithOptions : ( options ?: any ) => Promise < void >
32+ getDatasourceInstance : ( ) => any
3233}
3334
3435export function useDatasourceRepository (
3536 dataSourceId : Ref < string > ,
3637 type : string ,
3738 data : Ref < any > ,
39+ subscriptions : Array < ( ) => any > = [ ] ,
3840) : IVueDatasourceRepository {
3941 const instance = getCurrentInstance ( )
4042 const container = instance ?. appContext . config . globalProperties
@@ -139,24 +141,44 @@ export function useDatasourceRepository(
139141 try {
140142 const oldDataSource = datasourceRepository . getDatasource ( oldVal )
141143 oldDataSource . unsubscribe ( getData )
144+ subscriptions . forEach ( fn => {
145+ oldDataSource . unsubscribe ( fn ) ;
146+ } ) ;
142147 } catch ( e ) {
143148 console . warn ( e )
144149 }
145150 try {
146151 const dataSource = datasourceRepository . getDatasource ( newVal )
147152 // TODO: fix duplicate subscription
148153 dataSource . subscribe ( ( ) => getData ( ) )
154+ subscriptions . forEach ( fn => {
155+ dataSource . subscribe ( fn ) ;
156+ } )
149157 } catch ( e ) {
150158 console . warn ( e )
151159 }
152160 }
153161
162+ const getDatasourceInstance = ( ) => {
163+ try {
164+ const dataSource = datasourceRepository . getDatasource ( dataSourceId . value )
165+ return dataSource
166+ }
167+ catch ( e ) {
168+ console . warn ( e )
169+ return null
170+ }
171+ }
172+
154173 onMounted ( ( ) => {
155174 getData ( )
156175
157176 try {
158177 const dataSource = datasourceRepository . getDatasource ( dataSourceId . value )
159178 dataSource . subscribe ( getData )
179+ subscriptions . forEach ( fn => {
180+ dataSource . subscribe ( fn ) ;
181+ } )
160182 } catch ( e ) {
161183 console . warn ( e )
162184 }
@@ -166,6 +188,9 @@ export function useDatasourceRepository(
166188 try {
167189 const dataSource = datasourceRepository . getDatasource ( dataSourceId . value )
168190 dataSource . unsubscribe ( getData )
191+ subscriptions . forEach ( fn => {
192+ dataSource . unsubscribe ( fn ) ;
193+ } )
169194 } catch ( e ) {
170195 console . warn ( e )
171196 }
@@ -176,5 +201,6 @@ export function useDatasourceRepository(
176201 callEvent,
177202 update,
178203 getDataWithOptions,
204+ getDatasourceInstance,
179205 }
180206}
0 commit comments