11import { Subject } from 'rxjs/Subject';
22import { Observable } from 'rxjs/Observable';
3- import { LoopBackConfig } from '../../ lb.config';
4- import { AccessToken, LoopBackFilter } from '../../models ';
5- import { SocketConnections } from '../../ sockets/socket.connections';
3+ import { LoopBackConfig } from '../lb.config';
4+ import { AccessToken, LoopBackFilter } from './index ';
5+ import { SocketConnections } from '../sockets/socket.connections';
66
7- class Reference {
7+ export class FireLoopRef< T > {
88
99 private instance: any;
1010 private socket: any;
1111 private name: string;
12- private parent: Reference ;
12+ private parent: FireLoopRef< T > ;
1313 private childs: any = {};
1414 private observables: any = {};
1515
16- constructor(name: string, socket: any, parent: Reference = null) {
16+ constructor(name: string, socket: any, parent: FireLoopRef< any > = null) {
1717 this.name = name;
1818 this.parent = parent;
1919 this.socket = socket;
2020 return this;
2121 }
2222
23- public upsert(data: any): Observable<any > {
23+ public upsert(data: any): Observable<T > {
2424 let id: number = Math.floor(Math.random() * 100800) *
2525 Math.floor(Math.random() * 100700) *
2626 Math.floor(Math.random() * 198500);
@@ -32,18 +32,18 @@ class Reference {
3232 parent : this.parent && this.parent.instance ? this.parent.instance : null
3333 }
3434 };
35- let subject: Subject<any > = new Subject<any >();
35+ let subject: Subject<T > = new Subject<T >();
3636 this.socket.emit('ME:RT:1://event', request);
3737 this.socket.on(`${this.name}.value.result.${id}`, (res: any) =>
3838 subject.next(res.error ? Observable.throw(res.error) : res)
3939 );
4040 return subject.asObservable();
4141 }
4242
43- public on(event: string, filter: LoopBackFilter = { limit: 100 }): Observable<any > {
43+ public on(event: string, filter: LoopBackFilter = { limit: 100 }): Observable<T | T[] > {
4444 event = `${this.name}.${event}`;
4545 if (this.observables[event]) { return this.observables[event]; }
46- let subject: Subject<any > = new Subject<any >();
46+ let subject: Subject<T > = new Subject<T >();
4747 if (event.match(/(value)/))
4848 this.pull(event, filter, subject);
4949 // Listen for broadcast announces
@@ -59,41 +59,25 @@ class Reference {
5959 return this.observables[event];
6060 }
6161
62- private pull(event: string, filter: any, subject: Subject<any >): void {
62+ private pull(event: string, filter: any, subject: Subject<T >): void {
6363 this.socket.emit(`${event}.pull.request`, filter);
6464 this.socket.on(`${event}.pull.requested`, (res: any) => subject.next(res));
6565 }
6666
67- public make(instance: any): Reference {
67+ public make(instance: any): FireLoopRef< T > {
6868 this.instance = instance;
6969 return this;
7070 }
7171
72- public child(name: string): Reference {
72+ public child< T > (name: string): FireLoopRef< T > {
7373 if (!this.parent) {
7474 let childName = `${this.name}.${name}`;
7575 if (this.childs[childName]) { return this.childs[childName]; }
76- this.childs[childName] = new Reference (childName, this.socket, this);
76+ this.childs[childName] = new FireLoopRef< T > (childName, this.socket, this);
7777 return this.childs[childName];
7878 } else {
7979 console.warn('Only 1 child level is supported');
8080 // TODO ADD UNLIMITED LEVELS
8181 }
8282 }
83- }
84-
85- export class FireLoop {
86-
87- private socket: any;
88- private references: any = {};
89-
90- constructor(token: AccessToken) {
91- this.socket = SocketConnections.getHandler(LoopBackConfig.getPath(), token);
92- }
93-
94- public ref(name: string): Reference {
95- if (this.references[name]) { return this.references[name]; }
96- this.references[name] = new Reference(name, this.socket);
97- return this.references[name];
98- }
99- }
83+ }
0 commit comments