Skip to content

Commit

Permalink
Merge branch 'master' into feature/chelonia-in-service-worker
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Sep 29, 2024
2 parents bce3c5c + 08bc5e0 commit a9068b1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions frontend/controller/app/chatroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ sbp('okTurtles.events/on', JOINED_CHATROOM, ({ identityContractID, groupContract
let attemptCount = 0
// Sometimes, the state may not be ready (it needs to be copied from the SW
// to Vuex). In this case, we try again after a short delay.
// The specific issue is that the browsing-side state is updated in response
// to the EVENT_HANDLED event. Although that event is correctly emitted
// prior to JOINED_CHATROOM, processing might take slightly longer, causing
// rootState[chatRoomID]?.members?.[identityContractID] to be briefly
// undefined.
// TODO: Figure out a better way of doing this that doesn't require a timeout
const setCurrentChatRoomId = () => {
if (!rootState[chatRoomID]?.members?.[identityContractID]) {
Expand Down
15 changes: 11 additions & 4 deletions frontend/controller/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ sbp('sbp/selectors/register', {
try {
const swRegistration = await navigator.serviceWorker.register('/assets/js/sw-primary.js', { scope: '/' })

console.error('@@@SW swRegistration')

// This ensures that the 'store-client-id' message is always sent to the
// service worker, even if it's not immediately available. Calling `removeEventListener` is fine because presumably the page performing an
// update is sending this message. However, the real question is whether
// we _need_ to store a client ID (broadcasting should be preferred) and
// for instances where a single client ID is needed, this should be
// done periodically (e.g., to identify an active window).
// TODO: Clarify the use and need for 'store-client-id'. This will become
// clearer once implementing notifications.
if (swRegistration.active) {
console.error('@@@SW swRegistration active')
swRegistration.active?.postMessage({ type: 'store-client-id' })
} else {
console.error('@@@SW swRegistration handling')
const handler = () => {
navigator.serviceWorker.removeEventListener('controllerchange', handler, false)
navigator.serviceWorker.controller.postMessage({ type: 'store-client-id' })
Expand All @@ -68,8 +72,11 @@ sbp('sbp/selectors/register', {
// with contracts than if the service worker had to be restarted.
setInterval(() => navigator.serviceWorker.controller?.postMessage({ type: 'ping' }), 5000)

<<<<<<< HEAD
console.error('@@@SW swRegistration 70')

=======
>>>>>>> master
navigator.serviceWorker.addEventListener('message', event => {
const data = event.data

Expand Down
6 changes: 6 additions & 0 deletions test/cypress/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ afterEach(function () {

// Prevent errors when English is not the current OS locale language.
Cypress.on('window:before:load', window => {
// We use defineProperty because the property may be read-only, and thus
// setting it directly may not work
// Also, `configurable` is set to true so that running this code multiple
// times doesn't raise an error. Otherwise, the property is marked as
// non-configurable and if this code runs more than once, an error with be
// thrown.
Object.defineProperty(window.navigator, 'language', { value: 'en-US-POSIX', configurable: true })
Object.defineProperty(window.navigator, 'languages', { value: ['en-US-POSIX', 'en-US', 'en'], configurable: true })
})
Expand Down

0 comments on commit a9068b1

Please sign in to comment.