Skip to content

Commit

Permalink
feat: replace local storeage with callback
Browse files Browse the repository at this point in the history
  • Loading branch information
everright committed Apr 19, 2022
1 parent 76632b6 commit 9951c14
Show file tree
Hide file tree
Showing 15 changed files with 443 additions and 314 deletions.
6 changes: 2 additions & 4 deletions src/app/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class StorageService {
private fs: typeof fs;
private path: typeof path;
constructor() {
console.log('test');
this.ipcRenderer = window.require('electron').ipcRenderer;
this.app= window.require('electron').app;
this.fs = window.require('fs');
Expand All @@ -23,14 +22,13 @@ class StorageService {
* @param args
*/
private storageListenHandle(args: StorageHandleArgs): void {
console.log(args);
const action: string = args.action || undefined;
const handleResult: StorageHandleResult = {
status: StorageHandleStatus.invalid,
data: undefined
data: undefined,
callback: args.callback || null
};
if (storage && storage[action] && typeof storage[action] === 'function') {
console.log('storage run');
storage[action](...args.params).subscribe((result: any) => {
handleResult.data = result;
if (isNotEmpty(result)) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/electron-main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ try {
++count;
}
deleteFile(storageTemp);
returnValue = data;
returnValue = data;
} else if (args.type === 'result') {
subView.appView.view.webContents.send('storageCallback', args.result);
}
Expand Down
18 changes: 10 additions & 8 deletions src/core/api-manager/browser/src/app/pages/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ export class ApiService {
apiData.name.length > 50 ? apiData.name.slice(0, 50) + '...' : apiData.name
}</strong> 吗?删除后不可恢复!`,
nzOnOk: () => {
const result: StorageHandleResult = this.storage.run('apiDataRemove', [apiData.uuid]);
if (result.status === StorageHandleStatus.success) {
this.messageService.send({ type: 'deleteApi', data: { uuid: apiData.uuid } });
}
this.storage.run('apiDataRemove', [apiData.uuid], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
this.messageService.send({ type: 'deleteApi', data: { uuid: apiData.uuid } });
}
});
},
});
}
bulkDelete(apis) {
const result: StorageHandleResult = this.storage.run('apiDataBulkRemove', [apis]);
if (result.status === StorageHandleStatus.success) {
this.messageService.send({ type: 'bulkDeleteApi', data: { uuids: apis } });
}
this.storage.run('apiDataBulkRemove', [apis], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
this.messageService.send({ type: 'bulkDeleteApi', data: { uuids: apis } });
}
});
}
export(apiData: ApiData) {
const modal: NzModalRef = this.modalService.create({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ export class ApiDetailComponent implements OnInit {
});
}
getApiByUuid(id: number) {
const result: StorageHandleResult = this.storage.run('apiDataLoad', [id]);
if (result.status === StorageHandleStatus.success) {
['requestBody', 'responseBody'].forEach((tableName) => {
if (['xml', 'json'].includes(result.data[`${tableName}Type`])) {
result.data[tableName] = treeToListHasLevel(result.data[tableName]);
}
});
this.apiData = result.data;
}
this.storage.run('apiDataLoad', [id], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
['requestBody', 'responseBody'].forEach((tableName) => {
if (['xml', 'json'].includes(result.data[`${tableName}Type`])) {
result.data[tableName] = treeToListHasLevel(result.data[tableName]);
}
});
this.apiData = result.data;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,37 @@ export class ApiEditComponent implements OnInit, OnDestroy {
isLeaf: false,
},
];
const result: StorageHandleResult = this.storage.run('groupLoadAllByProjectID', [1]);
if (result.status === StorageHandleStatus.success) {
result.data.forEach((item: Group) => {
delete item.updatedAt;
treeItems.push({
title: item.name,
key: item.uuid.toString(),
weight: item.weight || 0,
parentID: (item.parentID || 0).toString(),
isLeaf: false,
this.storage.run('groupLoadAllByProjectID', [1], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
result.data.forEach((item: Group) => {
delete item.updatedAt;
treeItems.push({
title: item.name,
key: item.uuid.toString(),
weight: item.weight || 0,
parentID: (item.parentID || 0).toString(),
isLeaf: false,
});
});
});
treeItems.sort((a, b) => a.weight - b.weight);
}
listToTree(treeItems, this.groups, '-1');
this.afterInitGroup();
treeItems.sort((a, b) => a.weight - b.weight);
}
listToTree(treeItems, this.groups, '-1');
this.afterInitGroup();
});
}
getApi(id) {
const result: StorageHandleResult = this.storage.run('apiDataLoad', [id]);
if (result.status === StorageHandleStatus.success) {
['requestBody', 'responseBody'].forEach((tableName) => {
if (['xml', 'json'].includes(result.data[`${tableName}Type`])) {
result.data[tableName] = treeToListHasLevel(result.data[tableName]);
}
});
this.apiData = result.data;
this.changeGroupID$.next(this.apiData.groupID);
this.validateForm.patchValue(this.apiData);
}
this.storage.run('apiDataLoad', [id], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
['requestBody', 'responseBody'].forEach((tableName) => {
if (['xml', 'json'].includes(result.data[`${tableName}Type`])) {
result.data[tableName] = treeToListHasLevel(result.data[tableName]);
}
});
this.apiData = result.data;
this.changeGroupID$.next(this.apiData.groupID);
this.validateForm.patchValue(this.apiData);
}
});
}
saveApi() {
//manual set dirty in case user submit directly without edit
Expand Down Expand Up @@ -256,21 +258,23 @@ export class ApiEditComponent implements OnInit, OnDestroy {

private editApi(formData) {
if (formData.uuid) {
const result: StorageHandleResult = this.storage.run('apiDataUpdate', [formData, this.apiData.uuid]);
if (result.status === StorageHandleStatus.success) {
this.message.success('编辑成功');
this.messageService.send({ type: 'editApi', data: result.data });
} else {
this.message.success('编辑失败');
}
this.storage.run('apiDataUpdate', [formData, this.apiData.uuid], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
this.message.success('编辑成功');
this.messageService.send({ type: 'editApi', data: result.data });
} else {
this.message.success('编辑失败');
}
});
} else {
const result: StorageHandleResult = this.storage.run('apiDataCreate', [formData]);
if (result.status === StorageHandleStatus.success) {
this.message.success('新增成功');
this.messageService.send({ type: 'addApi', data: result.data });
} else {
this.message.success('新增失败');
}
this.storage.run('apiDataCreate', [formData], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
this.message.success('新增成功');
this.messageService.send({ type: 'addApi', data: result.data });
} else {
this.message.success('新增失败');
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,25 @@ export class ApiGroupEditComponent implements OnInit {
}

create(): void {
const result: StorageHandleResult = this.storage.run('groupCreate', [this.group]);
if (result.status === StorageHandleStatus.success) {
this.modalRef.destroy();
this.messageService.send({ type: 'updateGroupSuccess', data: { group: result.data } });
} else {
console.log(result.data);
}
this.storage.run('groupCreate', [this.group], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
this.modalRef.destroy();
this.messageService.send({ type: 'updateGroupSuccess', data: { group: result.data } });
} else {
console.log(result.data);
}
});
}

update(): void {
const result: StorageHandleResult = this.storage.run('groupUpdate', [this.group, this.group.uuid]);
if (result.status === StorageHandleStatus.success) {
this.modalRef.destroy();
this.messageService.send({ type: 'updateGroupSuccess', data: { group: result.data } });
} else {
console.log(result.data);
}
this.storage.run('groupUpdate', [this.group, this.group.uuid], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
this.modalRef.destroy();
this.messageService.send({ type: 'updateGroupSuccess', data: { group: result.data } });
} else {
console.log(result.data);
}
});
}

/**
Expand Down Expand Up @@ -107,14 +109,14 @@ export class ApiGroupEditComponent implements OnInit {
const data: GroupApiDataModel = { group: [this.group.uuid], api: [] };
this.getChildrenFromTree(this.treeItems, data, `group-${this.group.uuid}`);
this.modalRef.destroy();
const result: StorageHandleResult = this.storage.run('groupBulkRemove', [data.group]);
if (result.status !== StorageHandleStatus.success) {
return;
}
this.messageService.send({ type: 'updateGroupSuccess', data: {} });
//delete group api
if (data.api.length > 0) {
this.messageService.send({ type: 'gotoBulkDeleteApi', data: { uuids: data.api } });
}
this.storage.run('groupBulkRemove', [data.group], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
this.messageService.send({ type: 'updateGroupSuccess', data: {} });
//delete group api
if (data.api.length > 0) {
this.messageService.send({ type: 'gotoBulkDeleteApi', data: { uuids: data.api } });
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,43 +85,45 @@ export class ApiGroupTreeComponent implements OnInit, OnDestroy {
this.getGroups();
}
getGroups() {
let result: StorageHandleResult = this.storage.run('groupLoadAllByProjectID', [this.projectID]);
if (result.status === StorageHandleStatus.success) {
result.data.forEach((item) => {
delete item.__proto__.updatedAt;
this.groupByID[item.uuid] = item;
this.treeItems.push({
title: item.name,
key: `group-${item.uuid}`,
weight: item.weight || 0,
parentID: item.parentID ? `group-${item.parentID}` : '0',
isLeaf: false,
this.storage.run('groupLoadAllByProjectID', [this.projectID], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
result.data.forEach((item) => {
delete item.updatedAt;
this.groupByID[item.uuid] = item;
this.treeItems.push({
title: item.name,
key: `group-${item.uuid}`,
weight: item.weight || 0,
parentID: item.parentID ? `group-${item.parentID}` : '0',
isLeaf: false,
});
});
});
}
this.getApis();
}
this.getApis();
});
}
getApis() {
let result: StorageHandleResult = this.storage.run('apiDataLoadAllByProjectID', [this.projectID]);
if (result.status === StorageHandleStatus.success) {
let apiItems = {};
result.data.forEach((item) => {
delete item.__proto__.updatedAt;
apiItems[item.uuid] = item;
this.treeItems.push({
title: item.name,
key: item.uuid.toString(),
weight: item.weight || 0,
parentID: item.groupID ? `group-${item.groupID}` : '0',
method: item.method,
isLeaf: true,
this.storage.run('apiDataLoadAllByProjectID', [this.projectID], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
let apiItems = {};
result.data.forEach((item) => {
delete item.updatedAt;
apiItems[item.uuid] = item;
this.treeItems.push({
title: item.name,
key: item.uuid.toString(),
weight: item.weight || 0,
parentID: item.groupID ? `group-${item.groupID}` : '0',
method: item.method,
isLeaf: true,
});
});
});
this.apiDataItems = apiItems;
this.messageService.send({ type: 'loadApi', data: this.apiDataItems });
this.generateGroupTreeData();
this.restoreExpandStatus();
}
this.apiDataItems = apiItems;
this.messageService.send({ type: 'loadApi', data: this.apiDataItems });
this.generateGroupTreeData();
this.restoreExpandStatus();
}
});
}
restoreExpandStatus() {
let key = this.expandKeys.slice(0);
Expand Down Expand Up @@ -284,10 +286,14 @@ export class ApiGroupTreeComponent implements OnInit, OnDestroy {
*/
updateoperateApiEvent(data: GroupApiDataModel) {
if (data.group.length > 0) {
this.storage.run('groupBulkUpdate', [data.group]);
this.storage.run('groupBulkUpdate', [data.group], (result: StorageHandleResult) => {

});
}
if (data.api.length > 0) {
this.storage.run('apiDataBulkUpdate', [data.api]);
this.storage.run('apiDataBulkUpdate', [data.api], (result: StorageHandleResult) => {

});
}
}
private watchRouterChange() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ export class ApiTestComponent implements OnInit, OnDestroy {
this.testResult = result.response;
}
getApi(id) {
const result: StorageHandleResult = this.storage.run('apiDataLoad', [id]);
if (result.status === StorageHandleStatus.success) {
this.apiData = this.apiTest.getTestDataFromApi(result.data);
this.validateForm.patchValue(this.apiData);
}
this.storage.run('apiDataLoad', [id], (result: StorageHandleResult) => {
if (result.status === StorageHandleStatus.success) {
this.apiData = this.apiTest.getTestDataFromApi(result.data);
this.validateForm.patchValue(this.apiData);
}
});
}
saveTestDataToApi() {
let apiData = this.apiTest.getApiFromTestData({
Expand Down
Loading

0 comments on commit 9951c14

Please sign in to comment.