Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
f0312ef8
Commit
f0312ef8
authored
Oct 07, 2015
by
Robert Knight
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2594 from hypothesis/fix-groups-race
Fix race for session data on startup
parents
d026bccf
804def7d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
2 deletions
+79
-2
app.coffee
h/static/scripts/app.coffee
+3
-0
session.js
h/static/scripts/session.js
+38
-2
session-test.js
h/static/scripts/test/session-test.js
+38
-0
No files found.
h/static/scripts/app.coffee
View file @
f0312ef8
...
...
@@ -7,6 +7,9 @@ require('angular-jwt')
streamer
=
require
(
'./streamer'
)
resolve
=
# Ensure that we have available a) the current authenticated userid, and b)
# the list of user groups.
sessionState
:
[
'session'
,
(
session
)
->
session
.
load
().
$promise
]
store
:
[
'store'
,
(
store
)
->
store
.
$promise
]
streamer
:
streamer
.
connect
threading
:
[
...
...
h/static/scripts/session.js
View file @
f0312ef8
'use strict'
;
var
Promise
=
require
(
'core-js/library/es6/promise'
);
var
angular
=
require
(
'angular'
);
var
CACHE_TTL
=
5
*
60
*
1000
;
// 5 minutes
var
ACCOUNT_ACTIONS
=
[
[
'login'
,
'POST'
],
[
'logout'
,
'POST'
],
...
...
@@ -29,7 +32,7 @@ function sessionActions(options) {
}
// Finally, add a simple method for getting the current session state
actions
.
load
=
{
method
:
'GET'
};
actions
.
_
load
=
{
method
:
'GET'
};
if
(
typeof
options
!==
'undefined'
)
{
for
(
var
act
in
actions
)
{
...
...
@@ -68,9 +71,38 @@ function session($document, $http, $resource, flash) {
var
endpoint
=
new
URL
(
'/app'
,
base
).
href
;
var
resource
=
$resource
(
endpoint
,
{},
actions
);
// Blank inital model state
// Blank init
i
al model state
resource
.
state
=
{};
// Cache the result of _load()
var
lastLoad
;
var
lastLoadTime
;
/**
* @name session.load()
* @description
* Fetches the session data from the server. This function returns an object
* with a $promise property which resolves to the session data.
*
* N.B. The data is cached for CACHE_TTL across all actions of the session
* service: that is, a call to login() will update the session data and a call
* within CACHE_TTL milliseconds to load() will return that data rather than
* triggering a new request.
*/
resource
.
load
=
function
()
{
if
(
!
lastLoadTime
||
(
Date
.
now
()
-
lastLoadTime
)
>
CACHE_TTL
)
{
lastLoad
=
resource
.
_load
();
lastLoadTime
=
Date
.
now
();
// If the load fails, we need to clear out lastLoadTime so another load
// attempt will succeed.
lastLoad
.
$promise
.
catch
(
function
()
{
lastLoadTime
=
null
;
});
}
return
lastLoad
;
};
function
prepare
(
data
,
headersGetter
)
{
var
csrfTok
=
resource
.
state
.
csrf
;
if
(
typeof
csrfTok
!==
'undefined'
)
{
...
...
@@ -105,6 +137,10 @@ function session($document, $http, $resource, flash) {
// Copy the model data (including the CSRF token) into `resource.state`.
angular
.
copy
(
model
,
resource
.
state
);
// Replace lastLoad with the latest data, and update lastLoadTime.
lastLoad
=
{
$promise
:
Promise
.
resolve
(
model
),
$resolved
:
true
};
lastLoadTime
=
Date
.
now
();
// Return the model
return
model
;
}
...
...
h/static/scripts/test/session-test.js
View file @
f0312ef8
...
...
@@ -112,5 +112,43 @@ describe('h:session', function () {
$httpBackend
.
flush
();
assert
.
deepEqual
(
session
.
state
,
response
.
model
);
});
it
(
'an immediately-following call to #load() should not trigger a new request'
,
function
()
{
$httpBackend
.
expectPOST
(
url
).
respond
({});
session
.
login
();
$httpBackend
.
flush
();
session
.
load
();
});
});
describe
(
'#load()'
,
function
()
{
var
url
=
'http://foo.com/app'
;
it
(
'should fetch the session data'
,
function
()
{
$httpBackend
.
expectGET
(
url
).
respond
({});
session
.
load
();
$httpBackend
.
flush
();
});
it
(
'should cache the session data'
,
function
()
{
$httpBackend
.
expectGET
(
url
).
respond
({});
session
.
load
();
session
.
load
();
$httpBackend
.
flush
();
});
it
(
'should eventually expire the cache'
,
function
()
{
var
clock
=
sandbox
.
useFakeTimers
();
$httpBackend
.
expectGET
(
url
).
respond
({});
session
.
load
();
$httpBackend
.
flush
();
clock
.
tick
(
301
*
1000
);
$httpBackend
.
expectGET
(
url
).
respond
({});
session
.
load
();
$httpBackend
.
flush
();
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment