Skip to content

Commit 9f25a43

Browse files
committed
feat(api): rejecting promise depending on the success to standarized the api
1 parent 8fdbf79 commit 9f25a43

4 files changed

Lines changed: 87 additions & 50 deletions

File tree

example/src/App.tsx

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ export default function App() {
5252
const executeGetLz4VersionNumber = async () => {
5353
const _versionNumber = await getLz4VersionNumber();
5454

55-
console.log({ _versionNumber });
56-
5755
setVersionNumber(_versionNumber);
5856
};
5957

@@ -95,7 +93,12 @@ export default function App() {
9593
operation: 'compress',
9694
});
9795
} catch (error) {
98-
console.log({ error });
96+
setFileOperationResult({
97+
...(error as FileOperationResult),
98+
sourcePath,
99+
destinationPath,
100+
operation: 'compress',
101+
});
99102
}
100103
}
101104
} catch (error) {
@@ -106,26 +109,35 @@ export default function App() {
106109
const executeCompressFileUsingDocumentPicker = async () => {
107110
onReset();
108111

109-
const [file] = await pick({ copyTo: 'cachesDirectory' });
110-
111-
if (!file || !file.fileCopyUri) return;
112-
113-
const sourcePath = file.fileCopyUri;
114-
const destinationPath = file.fileCopyUri.replace(/(.*)(\..*)/, '$1.lz4');
115-
116112
try {
117-
const decompressFileResult = await compressFile(
118-
sourcePath,
119-
destinationPath,
120-
onProgress
121-
);
122-
123-
setFileOperationResult({
124-
...decompressFileResult,
125-
sourcePath,
126-
destinationPath,
127-
operation: 'compress',
128-
});
113+
const [file] = await pick({ copyTo: 'cachesDirectory' });
114+
115+
if (!file || !file.fileCopyUri) return;
116+
117+
const sourcePath = file.fileCopyUri;
118+
const destinationPath = file.fileCopyUri.replace(/(.*)(\..*)/, '$1.lz4');
119+
120+
try {
121+
const compressFileResult = await compressFile(
122+
sourcePath,
123+
destinationPath,
124+
onProgress
125+
);
126+
127+
setFileOperationResult({
128+
...compressFileResult,
129+
sourcePath,
130+
destinationPath,
131+
operation: 'compress',
132+
});
133+
} catch (error) {
134+
setFileOperationResult({
135+
...(error as FileOperationResult),
136+
sourcePath,
137+
destinationPath,
138+
operation: 'compress',
139+
});
140+
}
129141
} catch (error) {
130142
console.log({ error });
131143
}
@@ -134,26 +146,35 @@ export default function App() {
134146
const executeDecompressFile = async () => {
135147
onReset();
136148

137-
const [file] = await pick({ copyTo: 'cachesDirectory' });
138-
139-
if (!file || !file.fileCopyUri) return;
140-
141-
const sourcePath = file.fileCopyUri;
142-
const destinationPath = file.fileCopyUri.replace(/(.*)(\..*)/, '$1.lz4');
143-
144149
try {
145-
const decompressFileResult = await decompressFile(
146-
sourcePath,
147-
destinationPath,
148-
onProgress
149-
);
150-
151-
setFileOperationResult({
152-
...decompressFileResult,
153-
sourcePath,
154-
destinationPath,
155-
operation: 'decompress',
156-
});
150+
const [file] = await pick({ copyTo: 'cachesDirectory' });
151+
152+
if (!file || !file.fileCopyUri) return;
153+
154+
const sourcePath = file.fileCopyUri;
155+
const destinationPath = file.fileCopyUri.replace(/(.*)(\..*)/, '$1.lz4');
156+
157+
try {
158+
const decompressFileResult = await decompressFile(
159+
sourcePath,
160+
destinationPath,
161+
onProgress
162+
);
163+
164+
setFileOperationResult({
165+
...decompressFileResult,
166+
sourcePath,
167+
destinationPath,
168+
operation: 'decompress',
169+
});
170+
} catch (error) {
171+
setFileOperationResult({
172+
...(error as FileOperationResult),
173+
sourcePath,
174+
destinationPath,
175+
operation: 'decompress',
176+
});
177+
}
157178
} catch (error) {
158179
console.log({ error });
159180
}

src/__tests__/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('LZ4 Module', () => {
3232
(global as any).lz4 = {
3333
getLz4VersionNumber: jest.fn(() => Promise.resolve(1001)),
3434
getLz4VersionString: jest.fn(() => Promise.resolve('1.0.0')),
35-
performFileOperation: jest.fn(() => Promise.resolve(fileOperationResult)),
35+
performFileOperation: jest.fn(() => fileOperationResult),
3636
};
3737
});
3838

src/index.tsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,20 @@ export function compressFile(
5050
const strippedSourcePath = formatFilePath(sourcePath);
5151
const strippedDestinationPath = formatFilePath(destinationPath);
5252

53-
return _global.lz4.performFileOperation(
53+
const result = _global.lz4.performFileOperation(
5454
'compress',
5555
strippedSourcePath,
5656
strippedDestinationPath,
5757
onProgress
5858
);
59+
60+
return new Promise((resolve, reject) => {
61+
if (result.success) {
62+
resolve(result);
63+
} else {
64+
reject(result);
65+
}
66+
});
5967
}
6068

6169
/**
@@ -73,12 +81,20 @@ export function decompressFile(
7381
const strippedSourcePath = formatFilePath(sourcePath);
7482
const strippedDestinationPath = formatFilePath(destinationPath);
7583

76-
return _global.lz4.performFileOperation(
77-
'decompress',
78-
strippedSourcePath,
79-
strippedDestinationPath,
80-
onProgress
81-
);
84+
return new Promise((resolve, reject) => {
85+
const result = _global.lz4.performFileOperation(
86+
'decompress',
87+
strippedSourcePath,
88+
strippedDestinationPath,
89+
onProgress
90+
);
91+
92+
if (result.success) {
93+
resolve(result);
94+
} else {
95+
reject(result);
96+
}
97+
});
8298
}
8399

84100
export type { FileOperationResult, FileOperationMode } from './types';

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ export interface Lz4Type {
3838
sourcePath: string,
3939
destinationPath: string,
4040
onProgress?: (processedSize: number, totalSize: number) => void
41-
) => Promise<FileOperationResult>;
41+
) => FileOperationResult;
4242
};
4343
}

0 commit comments

Comments
 (0)