11module openflow {
22 "use strict" ;
3+
4+
5+ class messagequeue {
6+ constructor (
7+ public msg : QueueMessage ,
8+ public callback : any ) { }
9+ }
10+ interface IHashTable < T > {
11+ [ key : string ] : T ;
12+ }
13+ export class api {
14+ static $inject = [ "$rootScope" , "$location" , "WebSocketClient" ] ;
15+ public messageQueue : IHashTable < messagequeue > = { } ;
16+ constructor ( public $rootScope :ng . IRootScopeService , public $location , public WebSocketClient :WebSocketClient ) {
17+
18+ var cleanup = $rootScope . $on ( 'queuemessage' , ( event , data :QueueMessage ) => {
19+ if ( event && data ) { }
20+ if ( this . messageQueue [ data . correlationId ] !== undefined ) {
21+ this . messageQueue [ data . correlationId ] . callback ( data ) ;
22+ delete this . messageQueue [ data . correlationId ] ;
23+ }
24+ } ) ;
25+ }
26+ async Insert ( collection :string , model : any ) : Promise < any > {
27+ var q : InsertOneMessage = new InsertOneMessage ( ) ;
28+ q . collectionname = collection ; q . item = model ;
29+ var msg : Message = new Message ( ) ; msg . command = "insertone" ; msg . data = JSON . stringify ( q ) ;
30+ q = await this . WebSocketClient . Send < InsertOneMessage > ( msg ) ;
31+ return q . result ;
32+ }
33+ async Update ( collection :string , model : any ) : Promise < any > {
34+ var q : UpdateOneMessage = new UpdateOneMessage ( ) ;
35+ q . collectionname = collection ; q . item = model ;
36+ var msg : Message = new Message ( ) ; msg . command = "updateone" ; msg . data = JSON . stringify ( q ) ;
37+ q = await this . WebSocketClient . Send < UpdateOneMessage > ( msg ) ;
38+ return q . result ;
39+ }
40+ async Delete ( collection :string , model : any ) : Promise < void > {
41+ var q : DeleteOneMessage = new DeleteOneMessage ( ) ;
42+ q . collectionname = collection ; q . _id = model . _id ;
43+ var msg : Message = new Message ( ) ; msg . command = "deleteone" ; msg . data = JSON . stringify ( q ) ;
44+ q = await this . WebSocketClient . Send < DeleteOneMessage > ( msg ) ;
45+ }
46+
47+ async _QueueMessage ( queuename : string , data : any ) :Promise < QueueMessage > {
48+ return new Promise < QueueMessage > ( async ( resolve , reject ) => {
49+ var q : QueueMessage = new QueueMessage ( ) ;
50+ q . correlationId = Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) ;
51+ q . queuename = queuename ; q . data = JSON . stringify ( data ) ;
52+ var msg :Message = new Message ( ) ; msg . command = "queuemessage" ; msg . data = JSON . stringify ( q ) ;
53+ this . messageQueue [ q . correlationId ] = new messagequeue ( q , ( msgresult :QueueMessage ) => {
54+ resolve ( msgresult ) ;
55+ } ) ;
56+ await this . WebSocketClient . Send ( msg ) ;
57+ } ) ;
58+ }
59+ async QueueMessage ( queuename : string , data : any ) :Promise < any > {
60+ var result :any = await this . _QueueMessage ( queuename , data ) ;
61+ var msg = JSON . parse ( result . data ) ;
62+ return msg ;
63+ }
64+
65+ }
66+
367 function _timeSince ( timeStamp ) {
468 var now : Date = new Date ( ) ,
569 secondsPast : number = ( now . getTime ( ) - timeStamp . getTime ( ) ) / 1000 ;
@@ -146,13 +210,15 @@ module openflow {
146210 "$scope" ,
147211 "$location" ,
148212 "$routeParams" ,
149- "WebSocketClient"
213+ "WebSocketClient" ,
214+ "api"
150215 ] ;
151216 constructor (
152217 public $scope : ng . IScope ,
153218 public $location : ng . ILocationService ,
154219 public $routeParams : ng . route . IRouteParamsService ,
155- public WebSocketClient : WebSocketClient
220+ public WebSocketClient : WebSocketClient ,
221+ public api : api
156222 ) {
157223 }
158224 async loadData ( ) : Promise < void > {
@@ -164,30 +230,7 @@ module openflow {
164230 this . models = q . result ;
165231 if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
166232 }
167- async Insert ( model : any ) : Promise < any > {
168- var q : InsertOneMessage = new InsertOneMessage ( ) ;
169- // model.name = "Find me " + Math.random().toString(36).substr(2, 9);
170- q . collectionname = this . collection ; q . item = model ;
171- var msg : Message = new Message ( ) ; msg . command = "insertone" ; msg . data = JSON . stringify ( q ) ;
172- q = await this . WebSocketClient . Send < InsertOneMessage > ( msg ) ;
173- return q . result ;
174- }
175- async Update ( model : any ) : Promise < any > {
176- var q : UpdateOneMessage = new UpdateOneMessage ( ) ;
177- // model.name = "Find me " + Math.random().toString(36).substr(2, 9);
178- q . collectionname = this . collection ; q . item = model ;
179- var msg : Message = new Message ( ) ; msg . command = "updateone" ; msg . data = JSON . stringify ( q ) ;
180- q = await this . WebSocketClient . Send < UpdateOneMessage > ( msg ) ;
181- return q . result ;
182- }
183- async Delete ( model : any ) : Promise < void > {
184- var q : DeleteOneMessage = new DeleteOneMessage ( ) ;
185- q . collectionname = this . collection ; q . _id = model . _id ;
186- var msg : Message = new Message ( ) ; msg . command = "deleteone" ; msg . data = JSON . stringify ( q ) ;
187- q = await this . WebSocketClient . Send < DeleteOneMessage > ( msg ) ;
188- // this.models = this.models.filter(function (m: any):boolean { return m._id!==model._id;});
189- // if (!this.$scope.$$phase) { this.$scope.$apply(); }
190- }
233+
191234
192235 ToggleOrder ( field : string ) {
193236 if ( this . orderby [ field ] == undefined ) {
@@ -218,13 +261,15 @@ module openflow {
218261 "$scope" ,
219262 "$location" ,
220263 "$routeParams" ,
221- "WebSocketClient"
264+ "WebSocketClient" ,
265+ "api"
222266 ] ;
223267 constructor (
224268 public $scope : ng . IScope ,
225269 public $location : ng . ILocationService ,
226270 public $routeParams : ng . route . IRouteParamsService ,
227- public WebSocketClient : WebSocketClient
271+ public WebSocketClient : WebSocketClient ,
272+ public api : api
228273 ) {
229274 this . id = $routeParams . id ;
230275 this . basequery = { _id : this . id } ;
@@ -239,27 +284,5 @@ module openflow {
239284 this . keys = Object . keys ( this . model ) ;
240285 if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
241286 }
242- async Insert ( model : any ) : Promise < any > {
243- var q : InsertOneMessage = new InsertOneMessage ( ) ;
244- // model.name = "Find me " + Math.random().toString(36).substr(2, 9);
245- q . collectionname = this . collection ; q . item = model ;
246- var msg : Message = new Message ( ) ; msg . command = "insertone" ; msg . data = JSON . stringify ( q ) ;
247- q = await this . WebSocketClient . Send < InsertOneMessage > ( msg ) ;
248- return q . result ;
249- }
250- async Update ( model : any ) : Promise < any > {
251- var q : UpdateOneMessage = new UpdateOneMessage ( ) ;
252- // model.name = "Find me " + Math.random().toString(36).substr(2, 9);
253- q . collectionname = this . collection ; q . item = model ;
254- var msg : Message = new Message ( ) ; msg . command = "updateone" ; msg . data = JSON . stringify ( q ) ;
255- q = await this . WebSocketClient . Send < UpdateOneMessage > ( msg ) ;
256- return q . result ;
257- }
258- async Delete ( model : any ) : Promise < void > {
259- var q : DeleteOneMessage = new DeleteOneMessage ( ) ;
260- q . collectionname = this . collection ; q . _id = model . _id ;
261- var msg : Message = new Message ( ) ; msg . command = "deleteone" ; msg . data = JSON . stringify ( q ) ;
262- q = await this . WebSocketClient . Send < DeleteOneMessage > ( msg ) ;
263- }
264287 }
265288}
0 commit comments