11namespace Endpoints {
22 export class AngularEndpointsService {
3- static $inject = [ '$http' ] ;
3+ static $inject = [ '$http' , '$q' ] ;
44 static $http : ng . IHttpService ;
5+ static $q : ng . IQService ;
6+ static endpointCache = { } ;
57
6- constructor ( $http : ng . IHttpService ) {
8+ constructor ( $http : ng . IHttpService , $q : ng . IQService ) {
79 AngularEndpointsService . $http = $http ;
10+ AngularEndpointsService . $q = $q ;
811 }
912
1013 static call < TView > ( endpoint : IEndpoint , data ) {
@@ -17,12 +20,31 @@ namespace Endpoints {
1720 return call . then ( response => response . data ) ;
1821 }
1922
23+ static callCached < TView > ( endpoint : IEndpoint , data ) {
24+ var cacheKey = endpoint . toString ( ) ;
25+
26+ if ( this . endpointCache [ cacheKey ] == null ) {
27+ return this . call < TView > ( endpoint , data ) . then ( result => {
28+ this . endpointCache [ cacheKey ] = result ;
29+ return this . endpointCache [ cacheKey ] ;
30+ } ) ;
31+ }
32+
33+ const deferred = this . $q . defer ( ) ;
34+ deferred . resolve ( this . endpointCache [ cacheKey ] ) ;
35+ return deferred . promise ;
36+ }
37+
2038 public Test : Endpoints . Test . ITestService = {
2139 Get : ( args : Endpoints . Test . IGet ) : Endpoints . Test . IGetWithCall => {
2240 var endpoint = new Endpoints . Test . Get ( args ) ;
2341 return _ . extendOwn ( endpoint , {
2442 call < TView > ( ) {
2543 return AngularEndpointsService . call < TView > ( this , null ) ;
44+ } ,
45+
46+ callCached < TView > ( ) {
47+ return AngularEndpointsService . callCached < TView > ( this , null ) ;
2648 }
2749 } ) ;
2850 } ,
@@ -32,6 +54,10 @@ namespace Endpoints {
3254 return _ . extendOwn ( endpoint , {
3355 call < TView > ( ) {
3456 return AngularEndpointsService . call < TView > ( this , null ) ;
57+ } ,
58+
59+ callCached < TView > ( ) {
60+ return AngularEndpointsService . callCached < TView > ( this , null ) ;
3561 }
3662 } ) ;
3763 } ,
@@ -41,6 +67,10 @@ namespace Endpoints {
4167 return _ . extendOwn ( endpoint , {
4268 call < TView > ( ) {
4369 return AngularEndpointsService . call < TView > ( this , null ) ;
70+ } ,
71+
72+ callCached < TView > ( ) {
73+ return AngularEndpointsService . callCached < TView > ( this , null ) ;
4474 }
4575 } ) ;
4676 } ,
@@ -50,6 +80,10 @@ namespace Endpoints {
5080 return _ . extendOwn ( endpoint , {
5181 call < TView > ( ) {
5282 return AngularEndpointsService . call < TView > ( this , null ) ;
83+ } ,
84+
85+ callCached < TView > ( ) {
86+ return AngularEndpointsService . callCached < TView > ( this , null ) ;
5387 }
5488 } ) ;
5589 } ,
@@ -59,7 +93,7 @@ namespace Endpoints {
5993 return _ . extendOwn ( endpoint , {
6094 call < TView > ( value : Interfaces . IDummyClass ) {
6195 return AngularEndpointsService . call < TView > ( this , value != null ? value : null ) ;
62- }
96+ } ,
6397 } ) ;
6498 } ,
6599
@@ -68,7 +102,7 @@ namespace Endpoints {
68102 return _ . extendOwn ( endpoint , {
69103 call < TView > ( value : Interfaces . IDerivedClassWithShadowedProperty ) {
70104 return AngularEndpointsService . call < TView > ( this , value != null ? value : null ) ;
71- }
105+ } ,
72106 } ) ;
73107 } ,
74108
@@ -77,7 +111,7 @@ namespace Endpoints {
77111 return _ . extendOwn ( endpoint , {
78112 call < TView > ( value : Interfaces . IDerivedClassWithAnotherShadowedProperty ) {
79113 return AngularEndpointsService . call < TView > ( this , value != null ? value : null ) ;
80- }
114+ } ,
81115 } ) ;
82116 } ,
83117
@@ -86,7 +120,7 @@ namespace Endpoints {
86120 return _ . extendOwn ( endpoint , {
87121 call < TView > ( value : string ) {
88122 return AngularEndpointsService . call < TView > ( this , value != null ? `"${ value } "` : null ) ;
89- }
123+ } ,
90124 } ) ;
91125 } ,
92126
@@ -95,7 +129,7 @@ namespace Endpoints {
95129 return _ . extendOwn ( endpoint , {
96130 call < TView > ( ) {
97131 return AngularEndpointsService . call < TView > ( this , null ) ;
98- }
132+ } ,
99133 } ) ;
100134 }
101135 }
@@ -106,6 +140,10 @@ namespace Endpoints {
106140 return _ . extendOwn ( endpoint , {
107141 call < TView > ( ) {
108142 return AngularEndpointsService . call < TView > ( this , null ) ;
143+ } ,
144+
145+ callCached < TView > ( ) {
146+ return AngularEndpointsService . callCached < TView > ( this , null ) ;
109147 }
110148 } ) ;
111149 } ,
@@ -115,6 +153,10 @@ namespace Endpoints {
115153 return _ . extendOwn ( endpoint , {
116154 call < TView > ( ) {
117155 return AngularEndpointsService . call < TView > ( this , null ) ;
156+ } ,
157+
158+ callCached < TView > ( ) {
159+ return AngularEndpointsService . callCached < TView > ( this , null ) ;
118160 }
119161 } ) ;
120162 } ,
@@ -124,6 +166,10 @@ namespace Endpoints {
124166 return _ . extendOwn ( endpoint , {
125167 call < TView > ( ) {
126168 return AngularEndpointsService . call < TView > ( this , null ) ;
169+ } ,
170+
171+ callCached < TView > ( ) {
172+ return AngularEndpointsService . callCached < TView > ( this , null ) ;
127173 }
128174 } ) ;
129175 } ,
@@ -133,7 +179,7 @@ namespace Endpoints {
133179 return _ . extendOwn ( endpoint , {
134180 call < TView > ( value : Interfaces . IMegaClass ) {
135181 return AngularEndpointsService . call < TView > ( this , value != null ? value : null ) ;
136- }
182+ } ,
137183 } ) ;
138184 }
139185 }
0 commit comments