Skip to content

Commit

Permalink
feat: Integrated ServiceWorker with v6 (#12261)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblanc authored Oct 16, 2023
1 parent af08620 commit 8f3ba99
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 140 deletions.
1 change: 1 addition & 0 deletions packages/aws-amplify/__tests__/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('aws-amplify Exports', () => {
Array [
"Hub",
"I18n",
"ServiceWorker",
]
`);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-amplify/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/*
This file maps exports from `aws-amplify/utils`.
*/
export { Hub, I18n } from '@aws-amplify/core';
export { Hub, I18n, ServiceWorker } from '@aws-amplify/core';
3 changes: 2 additions & 1 deletion packages/core/__tests__/ServiceWorker.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AmplifyError, ServiceWorker } from '../src/libraryUtils';
import { AmplifyError } from '../src/libraryUtils';
import { ServiceWorker } from '../src';
import { ServiceWorkerErrorCode } from '../src/ServiceWorker/errorHelpers';

describe('ServiceWorker test', () => {
Expand Down
106 changes: 0 additions & 106 deletions packages/core/src/Amplify.ts

This file was deleted.

37 changes: 20 additions & 17 deletions packages/core/src/ServiceWorker/ServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

import { ConsoleLogger as Logger } from '../Logger';
import { isBrowser } from '../utils';
import { Amplify } from '../Amplify';
import { AmplifyError } from '../errors';
import { assert, ServiceWorkerErrorCode } from './errorHelpers';
import { record } from '../providers/pinpoint';
import { Amplify, fetchAuthSession } from '../singleton';

/**
* Provides a means to registering a service worker in the browser
* and communicating with it via postMessage events.
Expand Down Expand Up @@ -205,15 +207,25 @@ export class ServiceWorkerClass {
* https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/state
**/
_setupListeners() {
this.serviceWorker.addEventListener('statechange', event => {
this.serviceWorker.addEventListener('statechange', async event => {
const currentState = this.serviceWorker.state;
this._logger.debug(`ServiceWorker statechange: ${currentState}`);
Amplify.Analytics;
if (isAmplifyWithAnalytics(Amplify)) {
(Amplify as AmplifyWithAnalytics).Analytics.record({
name: 'ServiceWorker',
attributes: {
state: currentState,

const { appId, region } = Amplify.getConfig().Analytics?.Pinpoint ?? {};
const { credentials } = await fetchAuthSession();

if (appId && region && credentials) {
// Pinpoint is configured, record an event
record({
appId,
region,
category: 'Core',
credentials,
event: {
name: 'ServiceWorker',
attributes: {
state: currentState,
},
},
});
}
Expand All @@ -223,12 +235,3 @@ export class ServiceWorkerClass {
});
}
}

type AmplifyWithAnalytics = {
Analytics: {
record: Function;
};
};
function isAmplifyWithAnalytics(amplify: any): amplify is AmplifyWithAnalytics {
return amplify.Analytics && typeof amplify.Analytics.record === 'function';
}
12 changes: 0 additions & 12 deletions packages/core/src/ServiceWorker/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

export { ServiceWorkerClass as ServiceWorker } from './ServiceWorker';
3 changes: 3 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@ export { Cache } from './Cache';

// Internationalization utilities
export { I18n } from './I18n';

// Service worker
export { ServiceWorker } from './ServiceWorker';
3 changes: 0 additions & 3 deletions packages/core/src/libraryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ export {
} from './Platform/types';
export { setCustomUserAgent } from './Platform/customUserAgent';

// Service worker
export { ServiceWorker } from './ServiceWorker';

// Other utilities & constants
export { BackgroundProcessManager } from './BackgroundProcessManager';
export { Mutex } from './Mutex';
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/providers/pinpoint/types/pinpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UserProfile } from '../../../types';

export type SupportedCategory =
| 'Analytics'
| 'Core'
| 'InAppMessaging'
| 'PushNotification';

Expand Down

0 comments on commit 8f3ba99

Please sign in to comment.