@@ -18,6 +18,8 @@ export class FireLoopRef<T> {
1818 private instance : any ;
1919 // Model Childs
2020 private childs : any = { } ;
21+ // Disposable Events
22+ private disposable : { [ key : string ] : any } = { } ;
2123 /**
2224 * @method constructor
2325 * @param {any } model The model we want to create a reference
@@ -37,8 +39,8 @@ export class FireLoopRef<T> {
3739 private relationship : string = null
3840 ) {
3941 this . socket . emit (
40- `Subscribe.${ ! parent ? model . getModelName ( ) : parent . model . getModelName ( ) } ` ,
41- { id : this . id , scope : model . getModelName ( ) , relationship : relationship }
42+ `Subscribe.${ ! parent ? model . getModelName ( ) : parent . model . getModelName ( ) } ` ,
43+ { id : this . id , scope : model . getModelName ( ) , relationship : relationship }
4244 ) ;
4345 return this ;
4446 }
@@ -54,9 +56,13 @@ export class FireLoopRef<T> {
5456 * }
5557 **/
5658 public dispose ( ) : void {
57- this . operation ( 'dispose' , { } )
58- . subscribe ( )
59- . unsubscribe ( ) ;
59+ const subscription = this . operation ( 'dispose' , { } ) . subscribe ( ( ) => {
60+ Object . keys ( this . disposable ) . forEach ( ( channel : string ) => {
61+ this . socket . removeListener ( channel , this . disposable [ channel ] ) ;
62+ this . socket . removeAllListeners ( channel ) ;
63+ } ) ;
64+ subscription . unsubscribe ( ) ;
65+ } ) ;
6066 }
6167 /**
6268 * @method upsert
@@ -116,9 +122,9 @@ export class FireLoopRef<T> {
116122 public onRemote ( method : string ) : Observable < any > {
117123 let event : string = 'remote' ;
118124 if ( ! this . relationship ) {
119- event = `${ this . model . getModelName ( ) } .${ event } ` ;
125+ event = `${ this . model . getModelName ( ) } .${ event } ` ;
120126 } else {
121- event = `${ this . parent . model . getModelName ( ) } .${ this . relationship } .${ event } ` ;
127+ event = `${ this . parent . model . getModelName ( ) } .${ this . relationship } .${ event } ` ;
122128 }
123129 return this . broadcasts ( event , { } ) ;
124130 }
@@ -141,10 +147,10 @@ export class FireLoopRef<T> {
141147 }
142148 let request : any ;
143149 if ( ! this . relationship ) {
144- event = `${ this . model . getModelName ( ) } .${ event } ` ;
150+ event = `${ this . model . getModelName ( ) } .${ event } ` ;
145151 request = { filter } ;
146152 } else {
147- event = `${ this . parent . model . getModelName ( ) } .${ this . relationship } .${ event } ` ;
153+ event = `${ this . parent . model . getModelName ( ) } .${ this . relationship } .${ event } ` ;
148154 request = { filter, parent : this . parent . instance } ;
149155 }
150156 if ( event . match ( / ( v a l u e | c h a n g e | s t a t s ) / ) ) {
@@ -182,7 +188,7 @@ export class FireLoopRef<T> {
182188 **/
183189 public make ( instance : any ) : FireLoopRef < T > {
184190 let reference : FireLoopRef < T > = new FireLoopRef < T > ( this . model , this . socket ) ;
185- reference . instance = instance ;
191+ reference . instance = instance ;
186192 return reference ;
187193 }
188194 /**
@@ -200,15 +206,15 @@ export class FireLoopRef<T> {
200206 let settings : any = this . model . getModelDefinition ( ) . relations [ relationship ] ;
201207 // Verify the relationship actually exists
202208 if ( ! settings ) {
203- throw new Error ( `Invalid model relationship ${ this . model . getModelName ( ) } <-> ${ relationship } , verify your model settings.` ) ;
209+ throw new Error ( `Invalid model relationship ${ this . model . getModelName ( ) } <-> ${ relationship } , verify your model settings.` ) ;
204210 }
205211 // Verify if the relationship model is public
206212 if ( settings . model === '' ) {
207- throw new Error ( `Relationship model is private, cam't use ${ relationship } unless you set your model as public.` ) ;
213+ throw new Error ( `Relationship model is private, cam't use ${ relationship } unless you set your model as public.` ) ;
208214 }
209215 // Lets get a model reference and add a reference for all of the models
210- let model : any = this . model . models . get ( settings . model ) ;
211- model . models = this . model . models ;
216+ let model : any = this . model . models . get ( settings . model ) ;
217+ model . models = this . model . models ;
212218 // If everything goes well, we will store a child reference and return it.
213219 this . childs [ relationship ] = new FireLoopRef < T > ( model , this . socket , this , relationship ) ;
214220 return this . childs [ relationship ] ;
@@ -224,8 +230,8 @@ export class FireLoopRef<T> {
224230 private pull ( event : string , request : any ) : Observable < T > {
225231 let sbj : Subject < T > = new Subject < T > ( ) ;
226232 let that : FireLoopRef < T > = this ;
227- let nowEvent : any = `${ event } .pull.requested.${ this . id } ` ;
228- this . socket . emit ( `${ event } .pull.request.${ this . id } ` , request ) ;
233+ let nowEvent : any = `${ event } .pull.requested.${ this . id } ` ;
234+ this . socket . emit ( `${ event } .pull.request.${ this . id } ` , request ) ;
229235 function pullNow ( data : any ) {
230236 if ( that . socket . removeListener ) {
231237 that . socket . removeListener ( nowEvent , pullNow ) ;
@@ -246,12 +252,21 @@ export class FireLoopRef<T> {
246252 **/
247253 private broadcasts ( event : string , request : any ) : Observable < T > {
248254 let sbj : Subject < T > = new Subject < T > ( ) ;
249- this . socket . on (
250- `${ event } .broadcast.announce.${ this . id } ` ,
251- ( res : T ) =>
252- this . socket . emit ( `${ event } .broadcast.request.${ this . id } ` , request )
253- ) ;
254- this . socket . on ( `${ event } .broadcast.${ this . id } ` , ( data : any ) => sbj . next ( data ) ) ;
255+ let channels : { announce : string , broadcast : string } = {
256+ announce : `${ event } .broadcast.announce.${ this . id } ` ,
257+ broadcast : `${ event } .broadcast.${ this . id } `
258+ } ;
259+ let that = this ;
260+ // Announces Handler
261+ this . disposable [ channels . announce ] = function ( res : T ) {
262+ that . socket . emit ( `${ event } .broadcast.request.${ that . id } ` , request )
263+ } ;
264+ // Broadcasts Handler
265+ this . disposable [ channels . broadcast ] = function ( data : any ) {
266+ sbj . next ( data ) ;
267+ } ;
268+ this . socket . on ( channels . announce , this . disposable [ channels . announce ] ) ;
269+ this . socket . on ( channels . broadcast , this . disposable [ channels . broadcast ] ) ;
255270 return sbj . asObservable ( ) ;
256271 }
257272 /**
@@ -264,9 +279,9 @@ export class FireLoopRef<T> {
264279 **/
265280 private operation ( event : string , data : any ) : Observable < T > {
266281 if ( ! this . relationship ) {
267- event = `${ this . model . getModelName ( ) } .${ event } .${ this . id } ` ;
282+ event = `${ this . model . getModelName ( ) } .${ event } .${ this . id } ` ;
268283 } else {
269- event = `${ this . parent . model . getModelName ( ) } .${ this . relationship } .${ event } .${ this . id } ` ;
284+ event = `${ this . parent . model . getModelName ( ) } .${ this . relationship } .${ event } .${ this . id } ` ;
270285 }
271286 let subject : Subject < T > = new Subject < T > ( ) ;
272287 let config : { data : any , parent : any } = {
@@ -276,9 +291,9 @@ export class FireLoopRef<T> {
276291 this . socket . emit ( event , config ) ;
277292 let resultEvent : string = '' ;
278293 if ( ! this . relationship ) {
279- resultEvent = `${ this . model . getModelName ( ) } .value.result.${ this . id } ` ;
294+ resultEvent = `${ this . model . getModelName ( ) } .value.result.${ this . id } ` ;
280295 } else {
281- resultEvent = `${ this . parent . model . getModelName ( ) } .${ this . relationship } .value.result.${ this . id } ` ;
296+ resultEvent = `${ this . parent . model . getModelName ( ) } .${ this . relationship } .value.result.${ this . id } ` ;
282297 }
283298 this . socket . on ( resultEvent , ( res : any ) => {
284299 if ( res . error ) {
@@ -287,6 +302,9 @@ export class FireLoopRef<T> {
287302 subject . next ( res ) ;
288303 }
289304 } ) ;
305+ if ( event . match ( 'dispose' ) ) {
306+ setTimeout ( ( ) => subject . next ( ) ) ;
307+ }
290308 // This event listener will be wiped within socket.connections
291309 this . socket . sharedObservables . sharedOnDisconnect . subscribe ( ( ) => subject . complete ( ) ) ;
292310 return subject . asObservable ( ) . catch ( ( error : any ) => Observable . throw ( error ) ) ;
0 commit comments