Skip to content
Closed
7 changes: 6 additions & 1 deletion packages/analytics/src/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Amplify,
ConsoleLogger as Logger,
Hub,
browserOrNode,
Parser,
} from '@aws-amplify/core';
import { AWSPinpointProvider } from './Providers/AWSPinpointProvider';
Expand Down Expand Up @@ -75,6 +76,11 @@ export class AnalyticsClass {
Hub.listen('auth', listener);
Hub.listen('storage', listener);
Hub.listen('analytics', listener);

// Register module each time on the client, but not on the server to prevent memory leaks
if (!browserOrNode().isNode) {
Amplify.register(this);
}
}

public getModuleName() {
Expand Down Expand Up @@ -401,4 +407,3 @@ const sendEvents = () => {
};

export const Analytics = new AnalyticsClass();
Amplify.register(Analytics);
8 changes: 7 additions & 1 deletion packages/api-graphql/src/GraphQLAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { parse } from 'graphql/language/parser';
import Observable from 'zen-observable-ts';
import {
Amplify,
browserOrNode,
ConsoleLogger as Logger,
Constants,
Credentials,
Expand Down Expand Up @@ -57,6 +58,12 @@ export class GraphQLAPIClass {
*/
constructor(options) {
this._options = options;

// Register module each time on the client, but not on the server to prevent memory leaks
if (!browserOrNode().isNode) {
Amplify.register(this);
}

logger.debug('API Options', this._options);
}

Expand Down Expand Up @@ -378,4 +385,3 @@ export class GraphQLAPIClass {
}

export const GraphQLAPI = new GraphQLAPIClass(null);
Amplify.register(GraphQLAPI);
8 changes: 7 additions & 1 deletion packages/api-rest/src/RestAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { RestClient } from './RestClient';
import {
Amplify,
browserOrNode,
ConsoleLogger as Logger,
Credentials,
} from '@aws-amplify/core';
Expand All @@ -38,6 +39,12 @@ export class RestAPIClass {
*/
constructor(options) {
this._options = options;

// Register module each time on the client, but not on the server to prevent memory leaks
if (!browserOrNode().isNode) {
Amplify.register(this);
}

logger.debug('API Options', this._options);
}

Expand Down Expand Up @@ -336,4 +343,3 @@ export class RestAPIClass {
}

export const RestAPI = new RestAPIClass(null);
Amplify.register(RestAPI);
8 changes: 7 additions & 1 deletion packages/api/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from '@aws-amplify/api-graphql';
import {
Amplify,
browserOrNode,
ConsoleLogger as Logger,
Credentials,
} from '@aws-amplify/core';
Expand Down Expand Up @@ -52,6 +53,12 @@ export class APIClass {
this._options = options;
this._restApi = new RestAPIClass(options);
this._graphqlApi = new GraphQLAPIClass(options);

// Register module each time on the client, but not on the server to prevent memory leaks
if (!browserOrNode().isNode) {
Amplify.register(this);
}

logger.debug('API Options', this._options);
}

Expand Down Expand Up @@ -196,4 +203,3 @@ export class APIClass {
}

export const API = new APIClass(null);
Amplify.register(API);
7 changes: 5 additions & 2 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export class AuthClass {
break;
}
});

// Register module each time on the client, but not on the server to prevent memory leaks
if (JS.browserOrNode().isBrowser) {
Amplify.register(this);
}
}

public getModuleName() {
Expand Down Expand Up @@ -2085,5 +2090,3 @@ export class AuthClass {
}

export const Auth = new AuthClass(null);

Amplify.register(Auth);
9 changes: 6 additions & 3 deletions packages/core/src/Credentials.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConsoleLogger as Logger } from './Logger';
import { StorageHelper } from './StorageHelper';
import { makeQuerablePromise } from './JS';
import { makeQuerablePromise, browserOrNode } from './JS';
import { FacebookOAuth, GoogleOAuth } from './OAuthHelper';
import { jitteredExponentialRetry } from './Util';
import { ICredentials } from './types';
Expand Down Expand Up @@ -40,6 +40,11 @@ export class CredentialsClass {
this.configure(config);
this._refreshHandlers['google'] = GoogleOAuth.refreshGoogleToken;
this._refreshHandlers['facebook'] = FacebookOAuth.refreshFacebookToken;

// Register module each time on the client, but not on the server to prevent memory leaks
if (!browserOrNode().isNode) {
Amplify.register(this);
}
}

public getModuleName() {
Expand Down Expand Up @@ -546,8 +551,6 @@ export class CredentialsClass {

export const Credentials = new CredentialsClass(null);

Amplify.register(Credentials);

/**
* @deprecated use named import
*/
Expand Down
16 changes: 14 additions & 2 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Amplify, ConsoleLogger as Logger, Hub, JS } from '@aws-amplify/core';
import {
Amplify,
browserOrNode,
ConsoleLogger as Logger,
Hub,
JS,
} from '@aws-amplify/core';
import { Draft, immerable, produce, setAutoFreeze } from 'immer';
import { v4 as uuid4 } from 'uuid';
import Observable, { ZenObservable } from 'zen-observable-ts';
Expand Down Expand Up @@ -524,6 +530,13 @@ class DataStore {
private sync: SyncEngine;
private syncPageSize: number;

constructor() {
// Register module each time on the client, but not on the server to prevent memory leaks
if (!browserOrNode().isNode) {
Amplify.register(this);
}
}

getModuleName() {
return 'DataStore';
}
Expand Down Expand Up @@ -1047,6 +1060,5 @@ class DataStore {
}

const instance = new DataStore();
Amplify.register(instance);

export { DataStore as DataStoreClass, initSchema, instance as DataStore };
12 changes: 10 additions & 2 deletions packages/interactions/src/Interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
InteractionsMessage,
InteractionsResponse,
} from './types';
import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
import {
Amplify,
browserOrNode,
ConsoleLogger as Logger,
} from '@aws-amplify/core';
import { AWSLexProvider } from './Providers';
const logger = new Logger('Interactions');

Expand All @@ -35,6 +39,11 @@ export class InteractionsClass {
this._options = options;
logger.debug('Interactions Options', this._options);
this._pluggables = {};

// Register module each time on the client, but not on the server to prevent memory leaks
if (!browserOrNode().isNode) {
Amplify.register(this);
}
}

public getModuleName() {
Expand Down Expand Up @@ -147,4 +156,3 @@ export class InteractionsClass {
}

export const Interactions = new InteractionsClass(null);
Amplify.register(Interactions);
12 changes: 10 additions & 2 deletions packages/predictions/src/Predictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import {
AbstractInterpretPredictionsProvider,
AbstractPredictionsProvider,
} from './types/Providers';
import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
import {
Amplify,
browserOrNode,
ConsoleLogger as Logger,
} from '@aws-amplify/core';

const logger = new Logger('Predictions');

Expand All @@ -42,6 +46,11 @@ export class PredictionsClass {
this._convertPluggables = [];
this._identifyPluggables = [];
this._interpretPluggables = [];

// Register module each time on the client, but not on the server to prevent memory leaks
if (!browserOrNode().isNode) {
Amplify.register(this);
}
}

public getModuleName() {
Expand Down Expand Up @@ -246,4 +255,3 @@ export class PredictionsClass {
}

export const Predictions = new PredictionsClass({});
Amplify.register(Predictions);
6 changes: 5 additions & 1 deletion packages/pubsub/src/PubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export class PubSubClass {
logger.debug('PubSub Options', this._options);
this._pluggables = [];
this.subscribe = this.subscribe.bind(this);

// Register module each time on the client, but not on the server to prevent memory leaks
if (!browserOrNode().isNode) {
Amplify.register(this);
}
}

public getModuleName() {
Expand Down Expand Up @@ -183,4 +188,3 @@ export class PubSubClass {
}

export const PubSub = new PubSubClass(null);
Amplify.register(PubSub);
19 changes: 15 additions & 4 deletions packages/pushnotification/__tests__/PushNotification-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
jest.mock('@aws-amplify/core', () => ({
__esModule: true,
...jest.requireActual('@aws-amplify/core'),
browserOrNode() {
return {
isBrowser: true,
isNode: false,
};
},
}));

import { DeviceEventEmitter, Platform, NativeModules } from 'react-native';
import Amplify from 'aws-amplify';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
Expand Down Expand Up @@ -73,12 +84,12 @@ describe('PushNotification:', () => {
// Spy should be at 0 (it was already called on import)
expect(registerSpy).toHaveBeenCalledTimes(0);

// Global Amplify should keep original instance, not new instances
// Global Amplify will always reference last registered instance
const NewPushNotification = new PushNotification(null);
expect(Amplify.Pushnotification).not.toEqual(NewPushNotification);
expect(Amplify.Pushnotification).toEqual(NewPushNotification);

// Amplify.register should not have been called for the new instance
expect(registerSpy).toHaveBeenCalledTimes(0);
// Amplify.register should be called for the new instance
expect(registerSpy).toHaveBeenCalledTimes(1);
registerSpy.mockClear();
});
});
Expand Down
14 changes: 12 additions & 2 deletions packages/pushnotification/src/PushNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import {
AppState,
} from 'react-native';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import { Amplify, ConsoleLogger as Logger, JS } from '@aws-amplify/core';
import {
Amplify,
browserOrNode,
ConsoleLogger as Logger,
isEmpty,
} from '@aws-amplify/core';

const logger = new Logger('Notification');

Expand Down Expand Up @@ -56,14 +61,19 @@ export default class PushNotification {
this._iosInitialized = false;

this._notificationOpenedHandlers = [];

// Register module each time on the client, but not on the server to prevent memory leaks
if (!browserOrNode().isNode) {
Amplify.register(this);
}
}

getModuleName() {
return 'Pushnotification';
}

configure(config) {
if (JS.isEmpty(config)) return this._config;
if (isEmpty(config)) return this._config;
Comment on lines -66 to +76
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS is @deprecated, so I fixed it while I was here.

let conf = config ? config.PushNotification || config : {};

if (config['aws_mobile_analytics_app_id']) {
Expand Down
2 changes: 0 additions & 2 deletions packages/pushnotification/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import { Amplify } from '@aws-amplify/core';
import NotificationClass from './PushNotification';

const _instance = new NotificationClass(null);
const PushNotification = _instance;
Amplify.register(PushNotification);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still needed though, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, but this was hoisted into Pushnotification.ts' constructor.


export default PushNotification;
12 changes: 11 additions & 1 deletion packages/storage/src/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
* and limitations under the License.
*/

import { ConsoleLogger as Logger, Parser } from '@aws-amplify/core';
import {
Amplify,
browserOrNode,
ConsoleLogger as Logger,
Parser,
} from '@aws-amplify/core';
import { AWSS3Provider } from './providers';
import { StorageProvider } from './types';

Expand Down Expand Up @@ -46,6 +51,11 @@ export class Storage {
this.put = this.put.bind(this);
this.remove = this.remove.bind(this);
this.list = this.list.bind(this);

// Register module each time on the client, but not on the server to prevent memory leaks
if (!browserOrNode().isNode) {
Amplify.register(this);
}
}

public getModuleName() {
Expand Down
3 changes: 1 addition & 2 deletions packages/storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import { Storage as StorageClass } from './Storage';

import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
import { ConsoleLogger as Logger } from '@aws-amplify/core';

const logger = new Logger('Storage');

Expand Down Expand Up @@ -48,7 +48,6 @@ const getInstance = () => {
};

export const Storage: StorageClass = getInstance();
Amplify.register(Storage);

/**
* @deprecated use named import
Expand Down
Loading