-
Notifications
You must be signed in to change notification settings - Fork 58
chore: add check for navigator locks #356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Mutex } from 'async-mutex'; | ||
|
||
export const getNavigationLocks = (): LockManager => { | ||
if ('locks' in navigator && navigator.locks) { | ||
return navigator.locks; | ||
} | ||
console.warn('Navigator locks are not available in this context.' + | ||
'This may be due to running in an unsecure context. ' + | ||
'Consider using HTTPS or a secure context for full functionality.' + | ||
'Using fallback implementation.'); | ||
|
||
const mutexes = new Map<string, Mutex>(); | ||
|
||
const getMutex = (name: string): Mutex => { | ||
if (!mutexes.has(name)) { | ||
mutexes.set(name, new Mutex()); | ||
} | ||
return mutexes.get(name)!; | ||
}; | ||
|
||
const fallbackLockManager: LockManager = { | ||
request: async ( | ||
name: string, | ||
optionsOrCallback: LockOptions | LockGrantedCallback, | ||
maybeCallback?: LockGrantedCallback | ||
): Promise<LockManagerSnapshot> => { | ||
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : maybeCallback!; | ||
const options: LockOptions = typeof optionsOrCallback === 'object' ? optionsOrCallback : {}; | ||
|
||
const mutex = getMutex(name); | ||
const release = await mutex.acquire(); | ||
try { | ||
const lock: Lock = { name, mode: options.mode || 'exclusive' }; | ||
return await callback(lock); | ||
} finally { | ||
release(); | ||
mutexes.delete(name); | ||
} | ||
}, | ||
|
||
query: async (): Promise<LockManagerSnapshot> => { | ||
return { | ||
held: Array.from(mutexes.keys()).map(name => ({ name, mode: 'exclusive' as const })), | ||
pending: [] // We can't accurately track pending locks in this implementation as this requires a queue | ||
}; | ||
} | ||
}; | ||
|
||
return fallbackLockManager; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
import { getNavigationLocks } from '../../src/shared/navigator'; | ||
|
||
describe('getNavigationLocks', () => { | ||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it('should return native navigator.locks if available', () => { | ||
const mockLocks = { | ||
request: vi.fn(), | ||
query: vi.fn(), | ||
}; | ||
|
||
vi.spyOn(navigator, 'locks', 'get').mockReturnValue(mockLocks); | ||
|
||
const result = getNavigationLocks(); | ||
expect(result).toBe(mockLocks); | ||
}); | ||
|
||
it('should return fallback implementation if navigator.locks is not available', () => { | ||
// @ts-ignore | ||
vi.spyOn(navigator, 'locks', 'get').mockReturnValue(undefined); | ||
|
||
const result = getNavigationLocks(); | ||
expect(result).toHaveProperty('request'); | ||
expect(result).toHaveProperty('query'); | ||
expect(result).not.toBe(navigator.locks); | ||
}); | ||
|
||
it('fallback request should acquire and release a lock', async () => { | ||
// @ts-ignore | ||
vi.spyOn(navigator, 'locks', 'get').mockReturnValue(undefined); | ||
const locks = getNavigationLocks(); | ||
|
||
const mockCallback = vi.fn().mockResolvedValue('result'); | ||
const result = await locks.request('test-lock', mockCallback); | ||
|
||
expect(mockCallback).toHaveBeenCalledWith(expect.objectContaining({ | ||
name: 'test-lock', | ||
mode: 'exclusive' | ||
})); | ||
expect(result).toBe('result'); | ||
}); | ||
|
||
it('fallback query should return held locks', async () => { | ||
// @ts-ignore | ||
vi.spyOn(navigator, 'locks', 'get').mockReturnValue(undefined); | ||
const locks = getNavigationLocks(); | ||
|
||
// Acquire a lock first | ||
await locks.request('test-lock', async () => { | ||
const queryResult = await locks.query(); | ||
expect(queryResult.held).toHaveLength(1); | ||
expect(queryResult.held![0]).toEqual(expect.objectContaining({ | ||
name: 'test-lock', | ||
mode: 'exclusive' | ||
})); | ||
expect(queryResult.pending).toHaveLength(0); | ||
}); | ||
|
||
const finalQueryResult = await locks.query(); | ||
expect(finalQueryResult.held).toHaveLength(0); | ||
}); | ||
|
||
it('fallback implementation should handle concurrent requests', async () => { | ||
// @ts-ignore | ||
vi.spyOn(navigator, 'locks', 'get').mockReturnValue(undefined); | ||
const locks = getNavigationLocks(); | ||
|
||
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); | ||
|
||
const request1 = locks.request('test-lock', async () => { | ||
await delay(200); | ||
return 'first'; | ||
}); | ||
|
||
const request2 = locks.request('test-lock', async () => { | ||
return 'second'; | ||
}); | ||
|
||
const [result1, result2] = await Promise.all([request1, request2]); | ||
|
||
expect(result1).toBe('first'); | ||
expect(result2).toBe('second'); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.