Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
01aa7752
Commit
01aa7752
authored
Jul 16, 2018
by
Fatih Acet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Debounce Autosave callback.
parent
ef2b2546
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
10 deletions
+28
-10
app/assets/javascripts/autosave.js
app/assets/javascripts/autosave.js
+10
-1
spec/javascripts/autosave_spec.js
spec/javascripts/autosave_spec.js
+18
-9
No files found.
app/assets/javascripts/autosave.js
View file @
01aa7752
/* eslint-disable no-param-reassign, prefer-template, no-void, consistent-return */
/* eslint-disable no-param-reassign, prefer-template, no-void, consistent-return */
import
_
from
'
underscore
'
;
import
AccessorUtilities
from
'
./lib/utils/accessor
'
;
import
AccessorUtilities
from
'
./lib/utils/accessor
'
;
export
default
class
Autosave
{
export
default
class
Autosave
{
...
@@ -13,7 +14,13 @@ export default class Autosave {
...
@@ -13,7 +14,13 @@ export default class Autosave {
this
.
key
=
'
autosave/
'
+
key
;
this
.
key
=
'
autosave/
'
+
key
;
this
.
field
.
data
(
'
autosave
'
,
this
);
this
.
field
.
data
(
'
autosave
'
,
this
);
this
.
restore
();
this
.
restore
();
this
.
field
.
on
(
'
input
'
,
()
=>
this
.
save
());
this
.
field
.
on
(
'
input
'
,
this
.
debounceInputHandler
());
}
debounceInputHandler
()
{
return
_
.
debounce
(()
=>
{
this
.
save
();
},
Autosave
.
DEBOUNCE_TIMER
);
}
}
restore
()
{
restore
()
{
...
@@ -57,4 +64,6 @@ export default class Autosave {
...
@@ -57,4 +64,6 @@ export default class Autosave {
dispose
()
{
dispose
()
{
this
.
field
.
off
(
'
input
'
);
this
.
field
.
off
(
'
input
'
);
}
}
static
DEBOUNCE_TIMER
=
300
;
}
}
spec/javascripts/autosave_spec.js
View file @
01aa7752
import
$
from
'
jquery
'
;
import
$
from
'
jquery
'
;
import
_
from
'
underscore
'
;
import
Autosave
from
'
~/autosave
'
;
import
Autosave
from
'
~/autosave
'
;
import
AccessorUtilities
from
'
~/lib/utils/accessor
'
;
import
AccessorUtilities
from
'
~/lib/utils/accessor
'
;
...
@@ -10,12 +11,24 @@ describe('Autosave', () => {
...
@@ -10,12 +11,24 @@ describe('Autosave', () => {
describe
(
'
class constructor
'
,
()
=>
{
describe
(
'
class constructor
'
,
()
=>
{
beforeEach
(()
=>
{
beforeEach
(()
=>
{
spyOn
(
AccessorUtilities
,
'
isLocalStorageAccessSafe
'
).
and
.
returnValue
(
true
);
spyOn
(
AccessorUtilities
,
'
isLocalStorageAccessSafe
'
).
and
.
returnValue
(
true
);
spyOn
(
_
,
'
debounce
'
);
spyOn
(
Autosave
.
prototype
,
'
save
'
);
spyOn
(
Autosave
.
prototype
,
'
restore
'
);
spyOn
(
Autosave
.
prototype
,
'
restore
'
);
autosave
=
new
Autosave
(
field
,
key
);
});
});
it
(
'
should set .isLocalStorageAvailable
'
,
()
=>
{
it
(
'
should debounce the input handler
'
,
()
=>
{
autosave
=
new
Autosave
(
field
,
key
);
expect
(
_
.
debounce
).
toHaveBeenCalled
();
expect
(
autosave
.
save
).
not
.
toHaveBeenCalled
();
const
[
cb
,
timer
]
=
_
.
debounce
.
calls
.
argsFor
(
0
);
cb
();
// execute debounced callback
expect
(
timer
).
toEqual
(
Autosave
.
DEBOUNCE_TIMER
);
expect
(
autosave
.
save
).
toHaveBeenCalled
();
});
it
(
'
should set .isLocalStorageAvailable
'
,
()
=>
{
expect
(
AccessorUtilities
.
isLocalStorageAccessSafe
).
toHaveBeenCalled
();
expect
(
AccessorUtilities
.
isLocalStorageAccessSafe
).
toHaveBeenCalled
();
expect
(
autosave
.
isLocalStorageAvailable
).
toBe
(
true
);
expect
(
autosave
.
isLocalStorageAvailable
).
toBe
(
true
);
});
});
...
@@ -59,12 +72,10 @@ describe('Autosave', () => {
...
@@ -59,12 +72,10 @@ describe('Autosave', () => {
Autosave
.
prototype
.
restore
.
call
(
autosave
);
Autosave
.
prototype
.
restore
.
call
(
autosave
);
expect
(
expect
(
field
.
trigger
).
toHaveBeenCalled
();
field
.
trigger
,
).
toHaveBeenCalled
();
});
});
it
(
'
triggers native event
'
,
(
done
)
=>
{
it
(
'
triggers native event
'
,
done
=>
{
autosave
.
field
.
get
(
0
).
addEventListener
(
'
change
'
,
()
=>
{
autosave
.
field
.
get
(
0
).
addEventListener
(
'
change
'
,
()
=>
{
done
();
done
();
});
});
...
@@ -81,9 +92,7 @@ describe('Autosave', () => {
...
@@ -81,9 +92,7 @@ describe('Autosave', () => {
it
(
'
does not trigger event
'
,
()
=>
{
it
(
'
does not trigger event
'
,
()
=>
{
spyOn
(
field
,
'
trigger
'
).
and
.
callThrough
();
spyOn
(
field
,
'
trigger
'
).
and
.
callThrough
();
expect
(
expect
(
field
.
trigger
).
not
.
toHaveBeenCalled
();
field
.
trigger
,
).
not
.
toHaveBeenCalled
();
});
});
});
});
});
});
...
...
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