11import { Subject } from 'rxjs/Subject';
22import { Observable } from 'rxjs/Rx';
33import { LoopBackFilter, StatFilter } from './index';
4+ import { SocketConnection } from '../sockets/socket.connections';
45/**
56 * @class FireLoopRef<T >
67 * @author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official >
@@ -31,7 +32,7 @@ export class FireLoopRef<T> {
3132 **/
3233 constructor(
3334 private model: any,
34- private socket: any ,
35+ private socket: SocketConnection ,
3536 private parent: any = null,
3637 private relationship: any = null
3738 ) {
@@ -42,6 +43,21 @@ export class FireLoopRef<T> {
4243 return this;
4344 }
4445 /**
46+ * @method dispose
47+ * @description
48+ * This method is super important to avoid memory leaks in the server.
49+ * This method requires to be called on components destroy
50+ *
51+ * ngOnDestroy() {
52+ * this.someRef.dispose()
53+ * }
54+ **/
55+ public dispose() {
56+ this.operation('dispose', {})
57+ .subscribe()
58+ .unsubscribe();
59+ }
60+ /**
4561 * @method upsert
4662 * @param data
4763 * @description
@@ -51,7 +67,7 @@ export class FireLoopRef<T> {
5167 return this.operation('upsert', data);
5268 }
5369 /**
54- * @method upsert
70+ * @method create
5571 * @param data
5672 * @description
5773 * Operation wrapper for create function.
@@ -60,7 +76,7 @@ export class FireLoopRef<T> {
6076 return this.operation('create', data);
6177 }
6278 /**
63- * @method upsert
79+ * @method remove
6480 * @param data
6581 * @description
6682 * Operation wrapper for remove function.
@@ -69,6 +85,37 @@ export class FireLoopRef<T> {
6985 return this.operation('remove', data);
7086 }
7187 /**
88+ * @method remote
89+ * @param method
90+ * @param params
91+ * @description
92+ * This method calls for any remote method. It is flexible enough to
93+ * allow you call either built-in or custom remote methods.
94+ *
95+ * FireLoop provides this interface to enable calling remote methods
96+ * but also to optionally send any defined accept params that will be
97+ * applied within the server.
98+ **/
99+ public remote(method: string, params?: any[], broadcast: Boolean = false): Observable<T > {
100+ return this.operation('remote', { method, params, broadcast });
101+ }
102+ /**
103+ * @method onRemote
104+ * @param method
105+ * @param params
106+ * @description
107+ * This method listen for public .
108+ **/
109+ public onRemote(method: string): Observable<T > {
110+ let event: string = 'remote';
111+ if (!this.relationship) {
112+ event = `${ this.model.getModelName() }.${event}`;
113+ } else {
114+ event = `${ this.parent.model.getModelName() }.${ this.relationship }.${event}`;
115+ }
116+ return this.broadcasts(event, {});
117+ }
118+ /**
72119 * @method on
73120 * @param event
74121 * @param filter
@@ -81,6 +128,9 @@ export class FireLoopRef<T> {
81128 * - child_removed (Triggers when a child is removed)
82129 **/
83130 public on(event: string, filter: LoopBackFilter = { limit: 100, order: 'id DESC' }): Observable<T | T[] > {
131+ if (event === 'remote') {
132+ throw new Error('The "remote" event is not allowed using "on()" method, use "onRemote()" instead');
133+ }
84134 let request: any;
85135 if (!this.relationship) {
86136 event = `${ this.model.getModelName() }.${event}`;
@@ -113,8 +163,12 @@ export class FireLoopRef<T> {
113163 * @method make
114164 * @param instance
115165 * @description
116- * This method will set a model instance into this current FireLoop Reference.
166+ * This method will set a model instance into this a new FireLoop Reference.
117167 * This allows to persiste parentship when creating related instances.
168+ *
169+ * It also allows to have multiple different persisted instance references to same model.
170+ * otherwise if using singleton will replace a previous instance for a new instance, when
171+ * we actually want to have more than 1 instance of same model.
118172 **/
119173 public make(instance: any): FireLoopRef<T > {
120174 let reference: FireLoopRef<T > = new FireLoopRef<T >(this.model, this.socket);
@@ -165,7 +219,7 @@ export class FireLoopRef<T> {
165219 }
166220 sbj.next(data);
167221 };
168- this.socket.onZone (nowEvent, pullNow);
222+ this.socket.on (nowEvent, pullNow);
169223 return sbj.asObservable();
170224 }
171225 /**
@@ -176,12 +230,12 @@ export class FireLoopRef<T> {
176230 **/
177231 private broadcasts(event: string, request: any): Observable<T > {
178232 let sbj: Subject<T > = new Subject<T >();
179- this.socket.onZone (
233+ this.socket.on (
180234 `${event}.broadcast.announce.${ this.id }`,
181235 (res: T) =>
182236 this.socket.emit(`${event}.broadcast.request.${ this.id }`, request)
183237 );
184- this.socket.onZone (`${ event }.broadcast.${ this.id }`, (data: any) => sbj.next(data));
238+ this.socket.on (`${ event }.broadcast.${ this.id }`, (data: any) => sbj.next(data));
185239 return sbj.asObservable();
186240 }
187241 /**
@@ -203,10 +257,16 @@ export class FireLoopRef<T> {
203257 parent: this.parent && this.parent.instance ? this.parent.instance : null
204258 };
205259 this.socket.emit(event, config);
206- this.socket.onZone(`${ this.model.getModelName() }.value.result.${ this.id }`, (res: any) =>
207- subject.next(res.error ? Observable.throw(res.error) : res)
208- );
209- return subject.asObservable();
260+ this.socket.on(`${ this.model.getModelName() }.value.result.${ this.id }`, (res: any) => {
261+ if (res.error) {
262+ subject.error(res);
263+ } else {
264+ subject.next(res);
265+ }
266+ });
267+ // This event listener will be wiped within socket.connections
268+ this.socket.sharedObservables.sharedOnDisconnect.subscribe(() => subject.complete());
269+ return subject.asObservable().catch((error: any) => Observable.throw(error));
210270 }
211271 /**
212272 * @method buildId
0 commit comments