-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add host-aware PWA support #150
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 all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { useWakeLock, type UseWakeLockReturn } from '@vueuse/core' | ||
| import { onScopeDispose, watch, type Ref } from 'vue' | ||
|
|
||
| export type ActiveTurnWakeLock = Pick< | ||
| UseWakeLockReturn, | ||
| 'isSupported' | 'isActive' | 'request' | 'release' | ||
| > | ||
|
|
||
| export const useActiveTurnWakeLock = ( | ||
| hasActiveTurn: Readonly<Ref<boolean>>, | ||
| createWakeLock: () => ActiveTurnWakeLock = useWakeLock | ||
| ) => { | ||
| const wakeLock = createWakeLock() | ||
| let shouldHoldWakeLock = false | ||
| let wakeLockRequested = false | ||
| let syncOperation = Promise.resolve() | ||
|
|
||
| const releaseWakeLock = async () => { | ||
| try { | ||
| await wakeLock.release() | ||
| } catch { | ||
| // A normal turn must continue even when the browser denies wake-lock cleanup. | ||
| } | ||
| } | ||
|
|
||
| const syncWakeLock = async () => { | ||
| if (!shouldHoldWakeLock) { | ||
| if (!wakeLockRequested && !wakeLock.isActive.value) { | ||
| return | ||
| } | ||
| wakeLockRequested = false | ||
| await releaseWakeLock() | ||
| return | ||
| } | ||
|
|
||
| if (!wakeLock.isSupported.value) { | ||
| return | ||
| } | ||
|
|
||
| if (wakeLockRequested || wakeLock.isActive.value) { | ||
| return | ||
| } | ||
|
|
||
| wakeLockRequested = true | ||
| try { | ||
| await wakeLock.request('screen') | ||
| } catch { | ||
| wakeLockRequested = false | ||
| return | ||
| } | ||
|
|
||
| if (!shouldHoldWakeLock) { | ||
| wakeLockRequested = false | ||
| await releaseWakeLock() | ||
| } | ||
| } | ||
|
|
||
| const queueWakeLockSync = () => { | ||
| syncOperation = syncOperation.then(syncWakeLock, syncWakeLock) | ||
| } | ||
|
|
||
| watch(hasActiveTurn, shouldHold => { | ||
| shouldHoldWakeLock = shouldHold | ||
| queueWakeLockSync() | ||
| }, { immediate: true }) | ||
|
comfuture marked this conversation as resolved.
|
||
|
|
||
| onScopeDispose(() => { | ||
| shouldHoldWakeLock = false | ||
| queueWakeLockSync() | ||
| }) | ||
|
|
||
| return { | ||
| isSupported: wakeLock.isSupported, | ||
| isActive: wakeLock.isActive | ||
| } | ||
| } | ||
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,199 @@ | ||
| import { computed, effectScope, nextTick, ref } from 'vue' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { | ||
| useActiveTurnWakeLock, | ||
| type ActiveTurnWakeLock | ||
| } from '../app/composables/useActiveTurnWakeLock' | ||
| import { useChatSession, useHasActiveChatTurn } from '../app/composables/useChatSession' | ||
|
|
||
| const deferred = () => { | ||
| let resolve!: () => void | ||
| const promise = new Promise<void>((resolvePromise) => { | ||
| resolve = resolvePromise | ||
| }) | ||
| return { promise, resolve } | ||
| } | ||
|
|
||
| const createFixture = (input?: { | ||
| active?: boolean | ||
| supported?: boolean | ||
| request?: () => Promise<void> | ||
| }) => { | ||
| const active = ref(input?.active ?? false) | ||
| const request = vi.fn(input?.request ?? (async () => { | ||
| active.value = true | ||
| })) | ||
| const release = vi.fn(async () => { | ||
| active.value = false | ||
| }) | ||
| const wakeLock: ActiveTurnWakeLock = { | ||
| isSupported: computed(() => input?.supported ?? true), | ||
| isActive: computed(() => active.value), | ||
| request, | ||
| release | ||
| } | ||
|
|
||
| return { | ||
| active, | ||
| request, | ||
| release, | ||
| createWakeLock: () => wakeLock | ||
| } | ||
| } | ||
|
|
||
| let sessionCounter = 0 | ||
| const createSession = () => useChatSession(`wake-lock-test:${sessionCounter += 1}`) | ||
|
|
||
| describe('active turn wake lock', () => { | ||
| it('aggregates submitted and streaming turns across normal sessions', async () => { | ||
| const first = createSession() | ||
| const second = createSession() | ||
| const hasActiveTurn = useHasActiveChatTurn() | ||
|
|
||
| expect(hasActiveTurn.value).toBe(false) | ||
| first.status.value = 'submitted' | ||
| await nextTick() | ||
| expect(hasActiveTurn.value).toBe(true) | ||
|
|
||
| first.status.value = 'streaming' | ||
| second.status.value = 'submitted' | ||
| await nextTick() | ||
| expect(hasActiveTurn.value).toBe(true) | ||
|
|
||
| first.status.value = 'ready' | ||
| await nextTick() | ||
| expect(hasActiveTurn.value).toBe(true) | ||
|
|
||
| second.status.value = 'error' | ||
| await nextTick() | ||
| expect(hasActiveTurn.value).toBe(false) | ||
| }) | ||
|
|
||
| it('holds one lock until the final active turn ends', async () => { | ||
| const hasActiveTurn = ref(false) | ||
| const fixture = createFixture() | ||
| const scope = effectScope() | ||
| scope.run(() => useActiveTurnWakeLock(hasActiveTurn, fixture.createWakeLock)) | ||
|
|
||
| hasActiveTurn.value = true | ||
| await nextTick() | ||
| await vi.waitFor(() => { | ||
| expect(fixture.request).toHaveBeenCalledWith('screen') | ||
| }) | ||
|
|
||
| hasActiveTurn.value = true | ||
| await nextTick() | ||
| expect(fixture.request).toHaveBeenCalledTimes(1) | ||
|
|
||
| hasActiveTurn.value = false | ||
| await nextTick() | ||
| await vi.waitFor(() => { | ||
| expect(fixture.release).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| scope.stop() | ||
| }) | ||
|
|
||
| it('does not request unsupported locks or surface browser failures', async () => { | ||
| const hasActiveTurn = ref(true) | ||
| const unsupported = createFixture({ supported: false }) | ||
| const unsupportedScope = effectScope() | ||
| unsupportedScope.run(() => | ||
| useActiveTurnWakeLock(hasActiveTurn, unsupported.createWakeLock) | ||
| ) | ||
| await nextTick() | ||
| expect(unsupported.request).not.toHaveBeenCalled() | ||
| unsupportedScope.stop() | ||
|
|
||
| const failing = createFixture({ | ||
| request: async () => { | ||
| throw new Error('Wake lock denied') | ||
| } | ||
| }) | ||
| const failingScope = effectScope() | ||
| failingScope.run(() => useActiveTurnWakeLock(hasActiveTurn, failing.createWakeLock)) | ||
| await nextTick() | ||
| await vi.waitFor(() => { | ||
| expect(failing.request).toHaveBeenCalledWith('screen') | ||
| }) | ||
| failingScope.stop() | ||
| }) | ||
|
|
||
| it('releases a late request after the final turn already ended', async () => { | ||
| const hasActiveTurn = ref(false) | ||
| const pendingRequest = deferred() | ||
| const fixture = createFixture({ request: () => pendingRequest.promise }) | ||
| const scope = effectScope() | ||
| scope.run(() => useActiveTurnWakeLock(hasActiveTurn, fixture.createWakeLock)) | ||
|
|
||
| hasActiveTurn.value = true | ||
| await nextTick() | ||
| await vi.waitFor(() => { | ||
| expect(fixture.request).toHaveBeenCalledTimes(1) | ||
| }) | ||
| hasActiveTurn.value = false | ||
| await nextTick() | ||
| expect(fixture.release).not.toHaveBeenCalled() | ||
|
|
||
| pendingRequest.resolve() | ||
| await pendingRequest.promise | ||
| await vi.waitFor(() => { | ||
| expect(fixture.release).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| scope.stop() | ||
| }) | ||
|
|
||
| it('coalesces a stop and restart behind an older pending request', async () => { | ||
| const hasActiveTurn = ref(false) | ||
| const firstRequest = deferred() | ||
| const fixture = createFixture({ request: () => firstRequest.promise }) | ||
| const scope = effectScope() | ||
| scope.run(() => useActiveTurnWakeLock(hasActiveTurn, fixture.createWakeLock)) | ||
|
|
||
| hasActiveTurn.value = true | ||
| await nextTick() | ||
| await vi.waitFor(() => { | ||
| expect(fixture.request).toHaveBeenCalledTimes(1) | ||
| }) | ||
| hasActiveTurn.value = false | ||
| await nextTick() | ||
| hasActiveTurn.value = true | ||
| await nextTick() | ||
|
|
||
| expect(fixture.request).toHaveBeenCalledTimes(1) | ||
| expect(fixture.release).not.toHaveBeenCalled() | ||
|
|
||
| fixture.active.value = true | ||
| firstRequest.resolve() | ||
| await firstRequest.promise | ||
| await vi.waitFor(() => { | ||
| expect(fixture.active.value).toBe(true) | ||
| }) | ||
| expect(fixture.request).toHaveBeenCalledTimes(1) | ||
| expect(fixture.release).not.toHaveBeenCalled() | ||
|
|
||
| hasActiveTurn.value = false | ||
| await nextTick() | ||
| await vi.waitFor(() => { | ||
| expect(fixture.release).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| scope.stop() | ||
| }) | ||
|
|
||
| it('releases an active lock when the app-level scope is disposed', async () => { | ||
| const hasActiveTurn = ref(true) | ||
| const fixture = createFixture() | ||
| const scope = effectScope() | ||
| scope.run(() => useActiveTurnWakeLock(hasActiveTurn, fixture.createWakeLock)) | ||
| await vi.waitFor(() => { | ||
| expect(fixture.request).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| scope.stop() | ||
| await vi.waitFor(() => { | ||
| expect(fixture.release).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) | ||
| }) |
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.