Skip to content

Commit 1b5e079

Browse files
committed
#155 処理負荷の高い箇所をリファクタリング
1 parent f238078 commit 1b5e079

23 files changed

+231
-144
lines changed

src/application/Application.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ export class Application
105105
* @description configで設定したリクエストのレスポンスマップを返却します
106106
* Returns the response map of the request set in config
107107
*
108-
* @return {Map<string, any>}
108+
* @return {Map<string, unknown>}
109109
* @method
110110
* @public
111111
*/
112-
getResponse (): Map<string, any>
112+
getResponse (): Map<string, unknown>
113113
{
114114
return response;
115115
}
@@ -118,11 +118,11 @@ export class Application
118118
* @description キャッシュのMapオブジェクトを返却します
119119
* Returns the Map object of the cache
120120
*
121-
* @return {Map<string, any>}
121+
* @return {Map<string, unknown>}
122122
* @method
123123
* @public
124124
*/
125-
getCache (): Map<string, any>
125+
getCache (): Map<string, unknown>
126126
{
127127
return cache;
128128
}

src/application/service/QueryStringParserService.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ export const execute = (name: string = ""): IQueryObject =>
6161
* 任意で設定したQueryStringを分解
6262
* Decompose an arbitrarily set QueryString
6363
*/
64-
if (name.indexOf("?") > -1) {
65-
const idx = name.indexOf("?");
66-
queryString = name.slice(idx);
67-
const parsed = parseQueryString(name.slice(idx + 1));
64+
const questionIdx = name.indexOf("?");
65+
if (questionIdx > -1) {
66+
queryString = name.slice(questionIdx);
67+
const parsed = parseQueryString(name.slice(questionIdx + 1));
6868
for (const [key, value] of parsed) {
6969
query.set(key, value);
7070
}
71-
name = name.slice(0, idx);
71+
name = name.slice(0, questionIdx);
7272
}
7373

7474
if (name.charAt(0) === ".") {

src/application/usecase/ApplicationGotoViewUseCase.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ vi.mock("../service/QueryStringParserService", () => ({
3030
}));
3131

3232
vi.mock("../../infrastructure/usecase/RequestUseCase", () => ({
33-
execute: vi.fn().mockResolvedValue([])
33+
execute: vi.fn().mockResolvedValue([]),
34+
getRequests: vi.fn().mockReturnValue([])
3435
}));
3536

3637
vi.mock("./ExecuteCallbackUseCase", () => ({

src/application/usecase/ApplicationGotoViewUseCase.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { $getConfig } from "../variable/Config";
33
import { $getContext } from "../variable/Context";
44
import { response } from "../../infrastructure/variable/Response";
55
import { execute as queryStringParserService } from "../service/QueryStringParserService";
6-
import { execute as requestUseCase } from "../../infrastructure/usecase/RequestUseCase";
6+
import {
7+
execute as requestUseCase,
8+
getRequests
9+
} from "../../infrastructure/usecase/RequestUseCase";
710
import { execute as executeCallbackUseCase } from "./ExecuteCallbackUseCase";
811
import { execute as responseRemoveVariableUseCase } from "../../infrastructure/usecase/ResponseRemoveVariableUseCase";
912
import { ViewBinderService } from "../../domain/service/ViewBinderService";
@@ -53,7 +56,8 @@ export const execute = async (
5356
* 前の画面で取得したレスポンスデータを初期化
5457
* Initialize the response data obtained on the previous screen
5558
*/
56-
responseRemoveVariableUseCase(application.currentName);
59+
const previousRequests = getRequests(application.currentName);
60+
responseRemoveVariableUseCase(previousRequests);
5761

5862
/**
5963
* 指定されたパス、もしくはURLからアクセス先を算出
@@ -87,15 +91,23 @@ export const execute = async (
8791
const responses = await requestUseCase(application.currentName);
8892

8993
/**
90-
* レスポンス情報をマップに登録
91-
* Response information is registered on the map
94+
* レスポンス情報をマップに登録し、コールバックを実行
95+
* Register response information on the map and execute callbacks
9296
*/
9397
for (let idx = 0; idx < responses.length; ++idx) {
9498

9599
const object = responses[idx];
96100
if (object.name) {
97101
response.set(object.name, object.response);
98102
}
103+
104+
/**
105+
* リクエストごとのコールバック処理を実行
106+
* Execute callback for each request
107+
*/
108+
if (object.callback) {
109+
await executeCallbackUseCase(object.callback, object.response);
110+
}
99111
}
100112

101113
if (hasLoading) {

src/application/variable/Cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @type {Map<string, any>}
2+
* @type {Map<string, unknown>}
33
* @protected
44
*/
5-
export const cache: Map<string, any> = new Map();
5+
export const cache: Map<string, unknown> = new Map();

src/application/variable/Context.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import type { Context } from "../Context";
22

33
/**
4-
* @type {Context}
5-
* @public
4+
* @type {Context | null}
5+
* @private
66
*/
7-
let $context: Context;
7+
let $context: Context | null = null;
88

99
/**
1010
* @description コンテキストを取得します
1111
* Get the context
1212
*
1313
* @return {Context}
14+
* @throws {Error} コンテキストが初期化されていない場合
1415
* @method
1516
* @protected
1617
*/
1718
export const $getContext = (): Context =>
1819
{
19-
return $context as NonNullable<Context>;
20+
if (!$context) {
21+
throw new Error("Context is not initialized. Call run() first.");
22+
}
23+
return $context;
2024
};
2125

2226
/**

src/application/variable/Query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @type {Map<string, any>}
2+
* @type {Map<string, string>}
33
* @protected
44
*/
5-
export const query: Map<string, any> = new Map();
5+
export const query: Map<string, string> = new Map();

src/domain/service/ScreenCaptureService.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ export const ScreenCaptureService =
131131
}
132132

133133
/**
134-
* rootの子要素を全て削除
135-
* Remove all child elements of root
134+
* rootの子要素を全て削除(末尾から削除することでO(n)に最適化)
135+
* Remove all child elements of root (optimized to O(n) by removing from end)
136136
*/
137-
while (root.numChildren > 0) {
138-
root.removeChildAt(0);
137+
for (let idx = root.numChildren - 1; idx >= 0; --idx) {
138+
root.removeChildAt(idx);
139139
}
140140

141141
/**

src/domain/service/ViewBinderService.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ export const ViewBinderService =
4444
await context.view.initialize();
4545

4646
/**
47-
* rootの子要素を全て削除
48-
* Remove all child elements of root
47+
* rootの子要素を全て削除(末尾から削除することでO(n)に最適化)
48+
* Remove all child elements of root (optimized to O(n) by removing from end)
4949
*/
5050
const root = context.root;
51-
while (root.numChildren) {
52-
root.removeChildAt(0);
51+
for (let idx = root.numChildren - 1; idx >= 0; --idx) {
52+
root.removeChildAt(idx);
5353
}
5454

5555
/**

src/domain/variable/Loading.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ILoading } from "../../interface/ILoading";
22

33
/**
4-
* @type {object}
4+
* @type {ILoading | null}
55
* @default null
66
* @private
77
*/
@@ -11,25 +11,25 @@ let $instance: ILoading | null = null;
1111
* @description ローダーのインスタンスを取得
1212
* Get loader instance
1313
*
14-
* @return {ILoading}
14+
* @return {ILoading | null}
1515
* @method
16-
* @public
16+
* @protected
1717
*/
18-
export const $getInstance = (): ILoading =>
18+
export const $getInstance = (): ILoading | null =>
1919
{
20-
return $instance as ILoading;
20+
return $instance;
2121
};
2222

2323
/**
2424
* @description ローダーのインスタンスを設定
2525
* Set loader instance
2626
*
27-
* @param {ILoading} instance
27+
* @param {ILoading | null} instance
2828
* @return {void}
2929
* @method
30-
* @public
30+
* @protected
3131
*/
32-
export const $setInstance = (instance: ILoading): void =>
32+
export const $setInstance = (instance: ILoading | null): void =>
3333
{
3434
$instance = instance;
3535
};

0 commit comments

Comments
 (0)