Skip to content

Commit 6a5d1c1

Browse files
fix(lint): fix singleton pattern ESLint errors
Co-Authored-By: [email protected] <[email protected]>
1 parent 0c313ba commit 6a5d1c1

File tree

2 files changed

+73
-52
lines changed

2 files changed

+73
-52
lines changed

src/api/box-edit/BoxEdit.js

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,48 @@
1-
import { BrowserInstance } from './BrowserUtils';
1+
// @flow
2+
23
import { ComServerClient } from './ComServerClient';
34
import { CONSTANTS } from './constants';
5+
import { createRequestData, createExecuteData, isBlacklistedExtension } from './boxEditUtils';
46

57
const TIMEOUT_MS = 5000;
68
const EXTENSION_CHECK_DEBOUNCE_TIME = 100;
79

810
let extensionRequestTimeout = null;
9-
10-
function createRequestData(extensions) {
11-
return JSON.stringify({
12-
request_type: 'get_default_application',
13-
extension: extensions,
14-
});
15-
}
16-
17-
function createExecuteData(fileId, token, authCode, tokenScope) {
18-
const execData = JSON.stringify({
19-
auth_code: authCode,
20-
auth_token: token,
21-
browser_type: BrowserInstance.getName(),
22-
command_type: 'launch_application',
23-
file_id: fileId.toString(),
24-
token_scope: tokenScope,
25-
});
26-
return execData;
27-
}
28-
29-
function isBlacklistedExtension(extension) {
30-
const { EXTENSION_BLACKLIST } = CONSTANTS;
31-
let uppercaseExt = extension.toUpperCase();
32-
33-
// if ext has a leading ., strip it
34-
if (uppercaseExt.charAt(0) === '.') {
35-
uppercaseExt = uppercaseExt.substr(1);
36-
}
37-
38-
return uppercaseExt in EXTENSION_BLACKLIST;
39-
}
40-
11+
let instance = null;
12+
13+
type ExtensionRequestQueueItem = {|
14+
promise: Promise<string>,
15+
reject: Function,
16+
resolve: Function,
17+
|};
18+
19+
type TokenData = {|
20+
data: {|
21+
token: string,
22+
auth_code: string,
23+
token_scope: string,
24+
|},
25+
|};
4126
class BoxEdit {
42-
static instance = null;
27+
client: ComServerClient;
28+
29+
extensionRequestQueue: Map<string, ExtensionRequestQueueItem>;
4330

4431
constructor() {
45-
if (!BoxEdit.instance) {
46-
this.extensionRequestQueue = new Map();
47-
BoxEdit.instance = this;
32+
this.extensionRequestQueue = new Map();
33+
if (!instance) {
34+
instance = this;
4835
}
4936
}
5037

51-
static getInstance() {
52-
return BoxEdit.instance || new BoxEdit();
38+
static getInstance(): any {
39+
if (!instance) {
40+
instance = new BoxEdit();
41+
}
42+
return instance;
5343
}
5444

55-
queueGetNativeAppNameFromLocal = extension => {
45+
queueGetNativeAppNameFromLocal(extension: string): Promise<string> {
5646
// There's already a pending or fulfilled request for the appname
5747
if (this.extensionRequestQueue.has(extension)) {
5848
const queueItem = this.extensionRequestQueue.get(extension);
@@ -71,18 +61,18 @@ class BoxEdit {
7161
this.extensionRequestQueue.set(extension, extensionRequest);
7262

7363
return appNameRequestPromise;
74-
};
64+
}
7565

76-
checkBoxEditAvailability = () => {
66+
checkBoxEditAvailability = (): Promise<any> => {
7767
return this.getBoxEditAvailability();
7868
};
7969

80-
getBoxEditAvailability = () => {
70+
getBoxEditAvailability = (): Promise<any> => {
8171
this.client = new ComServerClient(CONSTANTS.BOX_EDIT_APP_NAME);
8272
return this.client.getComServerStatus();
8373
};
8474

85-
canOpenWithBoxEdit = async extensions => {
75+
canOpenWithBoxEdit = async (extensions: Array<string>): Promise<Map<string, string>> => {
8676
const extensionToAppTuples = await Promise.all(
8777
extensions.map(async ext => {
8878
try {
@@ -97,14 +87,10 @@ class BoxEdit {
9787
const resultMap = new Map();
9888
extensionToAppTuples.forEach(tuple => resultMap.set(...tuple));
9989

100-
return Promise.resolve(resultMap);
90+
return resultMap;
10191
};
10292

103-
openFile = (fileID, token) => {
104-
// @NOTE. canOpenWithBoxEdit, create token taken care of higher levels
105-
// therefore not ported into React library
106-
107-
// TODO is token the right name?
93+
openFile = (fileID: string, token: TokenData): Promise<any> => {
10894
const executeDataAsString = createExecuteData(
10995
fileID,
11096
token.data.token,
@@ -115,7 +101,7 @@ class BoxEdit {
115101
return this.client.sendCommand(executeDataAsString, TIMEOUT_MS);
116102
};
117103

118-
getAppForExtension = extension => {
104+
getAppForExtension = (extension: string): Promise<string> => {
119105
try {
120106
if (isBlacklistedExtension(extension)) {
121107
throw new Error('blacklisted');
@@ -135,7 +121,7 @@ class BoxEdit {
135121
}
136122
};
137123

138-
processExtensionRequestQueue = () => {
124+
processExtensionRequestQueue = (): Promise<any> => {
139125
const copyQueue = new Map();
140126

141127
const extensions = [];
@@ -184,4 +170,5 @@ class BoxEdit {
184170
};
185171
}
186172

173+
// Export singleton instance
187174
export default BoxEdit.getInstance();

src/api/box-edit/boxEditUtils.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @flow
2+
import { BrowserInstance } from './BrowserUtils';
3+
import { CONSTANTS } from './constants';
4+
5+
export function createRequestData(extensions: Array<string>): string {
6+
return JSON.stringify({
7+
request_type: 'get_default_application',
8+
extension: extensions,
9+
});
10+
}
11+
12+
export function createExecuteData(fileId: string, token: string, authCode: string, tokenScope: string): string {
13+
const execData = JSON.stringify({
14+
auth_code: authCode,
15+
auth_token: token,
16+
browser_type: BrowserInstance.getName(),
17+
command_type: 'launch_application',
18+
file_id: fileId.toString(),
19+
token_scope: tokenScope,
20+
});
21+
return execData;
22+
}
23+
24+
export function isBlacklistedExtension(extension: string): boolean {
25+
const { EXTENSION_BLACKLIST } = CONSTANTS;
26+
let uppercaseExt = extension.toUpperCase();
27+
28+
// if ext has a leading ., strip it
29+
if (uppercaseExt.charAt(0) === '.') {
30+
uppercaseExt = uppercaseExt.substr(1);
31+
}
32+
33+
return uppercaseExt in EXTENSION_BLACKLIST;
34+
}

0 commit comments

Comments
 (0)