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
672324e4
Commit
672324e4
authored
Apr 20, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Apr 20, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate navigation-observer module to TS
parent
f69af757
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
19 deletions
+19
-19
navigation-observer.ts
src/annotator/util/navigation-observer.ts
+19
-19
No files found.
src/annotator/util/navigation-observer.
j
s
→
src/annotator/util/navigation-observer.
t
s
View file @
672324e4
...
...
@@ -6,14 +6,14 @@ import { ListenerCollection } from '../../shared/listener-collection';
*
* The `handler` is not invoked if the observed method throws.
*
* @template {any} T
* @param {T} object
* @param {keyof T} method
* @param {(...args: unknown[]) => void} handler - Handler that is invoked
* after the monitored method has been called.
* @return {() => void} Callback that removes the observer and restores `object[method]`.
* @param handler - Handler that is invoked after the monitored method has been called.
* @return Callback that removes the observer and restores `object[method]`.
*/
function
observeCalls
(
object
,
method
,
handler
)
{
function
observeCalls
<
T
>
(
object
:
T
,
method
:
keyof
T
,
handler
:
(...
args
:
unknown
[])
=>
void
):
()
=>
void
{
const
origHandler
=
object
[
method
];
/* istanbul ignore next */
...
...
@@ -21,21 +21,20 @@ function observeCalls(object, method, handler) {
throw
new
Error
(
'Can only intercept functions'
);
}
/** @param {unknown[]} args */
const
wrapper
=
(...
args
)
=>
{
const
wrapper
=
(...
args
:
unknown
[])
=>
{
const
result
=
origHandler
.
call
(
object
,
...
args
);
handler
(...
args
);
return
result
;
};
object
[
method
]
=
/** @type {any} */
(
wrapper
);
// @ts-expect-error Already checked type is function some lines above
object
[
method
]
=
wrapper
;
return
()
=>
{
object
[
method
]
=
origHandler
;
};
}
/** @param {string} url */
function
stripFragment
(
url
)
{
function
stripFragment
(
url
:
string
):
string
{
return
url
.
replace
(
/#.*/
,
''
);
}
...
...
@@ -45,11 +44,9 @@ function stripFragment(url) {
* This is a wrapper around `window.navigation` which checks both that the
* object exists and has the expected type. See also
* https://github.com/hypothesis/client/issues/5324.
*
* @return {EventTarget|null}
*/
export
function
getNavigation
()
{
const
navigation
=
/** @type {any} */
(
window
).
navigation
;
export
function
getNavigation
()
:
EventTarget
|
null
{
const
navigation
=
(
window
as
any
).
navigation
;
if
(
// @ts-expect-error - Navigation API is missing from TS
typeof
Navigation
===
'function'
&&
...
...
@@ -76,16 +73,19 @@ export function getNavigation() {
* [2] https://developer.mozilla.org/en-US/docs/Web/API/History
*/
export
class
NavigationObserver
{
private
_listeners
:
ListenerCollection
;
private
_unpatchHistory
:
(()
=>
void
)
|
undefined
;
/**
* Begin observing navigation changes.
*
* @param
{(url: string) => void}
onNavigate - Callback invoked when a navigation
* @param onNavigate - Callback invoked when a navigation
* occurs. The callback is fired after the navigation has completed and the
* new URL is reflected in `location.href`.
* @param
{() => string}
getLocation - Test seam that returns the current URL
* @param getLocation - Test seam that returns the current URL
*/
constructor
(
onNavigate
,
onNavigate
:
(
url
:
string
)
=>
void
,
/* istanbul ignore next - default overridden in tests */
getLocation
=
()
=>
location
.
href
)
{
...
...
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