Page Menu
Home
GnuPG
Search
Configure Global Search
Log In
Files
F35156018
ShapedRequest.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Size
2 KB
Subscribers
None
ShapedRequest.js
View Options
/**
* @requires javelin-install
* javelin-util
* javelin-request
* @provides phabricator-shaped-request
* @javelin
*/
/**
* Send requests with rate limiting and retries, in response to some application
* trigger. This is used to implement comment previews in Differential and
* Maniphest.
*/
JX
.
install
(
'PhabricatorShapedRequest'
,
{
construct
:
function
(
uri
,
callback
,
data_callback
)
{
this
.
_uri
=
uri
;
this
.
_callback
=
callback
;
this
.
_dataCallback
=
data_callback
;
},
members
:
{
_callback
:
null
,
_dataCallback
:
null
,
_request
:
null
,
_min
:
null
,
_defer
:
null
,
_last
:
null
,
start
:
function
()
{
this
.
trigger
();
},
trigger
:
function
()
{
if
(
this
.
_request
)
{
// Waiting on a request, rate-limit.
return
;
}
if
(
this
.
_min
&&
(
new
Date
().
getTime
()
<
this
.
_min
))
{
// Just got a request back, rate-limit.
return
;
}
this
.
_defer
&&
this
.
_defer
.
stop
();
var
data
=
this
.
_dataCallback
();
if
(
this
.
shouldSendRequest
(
this
.
_last
,
data
))
{
this
.
_last
=
data
;
var
request
=
new
JX
.
Request
(
this
.
_uri
,
JX
.
bind
(
this
,
function
(
r
)
{
this
.
_callback
(
r
);
this
.
_min
=
new
Date
().
getTime
()
+
this
.
getRateLimit
();
this
.
_defer
&&
this
.
_defer
.
stop
();
this
.
_defer
=
JX
.
defer
(
JX
.
bind
(
this
,
this
.
trigger
),
this
.
getRateLimit
());
}));
request
.
listen
(
'finally'
,
JX
.
bind
(
this
,
function
()
{
this
.
_request
=
null
;
}));
request
.
setData
(
data
);
request
.
setTimeout
(
this
.
getFrequency
());
request
.
send
();
}
else
{
this
.
_defer
=
JX
.
defer
(
JX
.
bind
(
this
,
this
.
trigger
),
this
.
getFrequency
());
}
},
shouldSendRequest
:
function
(
last
,
data
)
{
if
(
last
===
null
)
{
return
true
;
}
for
(
var
k
in
last
)
{
if
(
data
[
k
]
!==
last
[
k
])
{
return
true
;
}
}
return
false
;
}
},
properties
:
{
rateLimit
:
500
,
frequency
:
1000
}
});
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Feb 2, 8:11 AM (20 h, 40 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
9c/0c/925bdc1c738bbc199e176bd90163
Attached To
rPHAB Phabricator
Event Timeline
Log In to Comment