Skip to content

Commit

Permalink
Merge pull request #4183 from easyops-cn/jojiang/debug-data-value
Browse files Browse the repository at this point in the history
fix(): 搜集调试契约兼容 v2 框架
  • Loading branch information
weareoutman authored May 17, 2024
2 parents b84b6ce + fef72fc commit ad2599e
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 22 deletions.
2 changes: 2 additions & 0 deletions dll/editor-bricks-helper/manifest.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@
"batchSetAppsLocalTheme",
"checkIf",
"checkIfByTransform",
"clearDebugContract",
"collectDebugContract",
"constructEventListener",
"createHistory",
"createRuntime",
Expand Down
7 changes: 7 additions & 0 deletions etc/brick-kit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { BrickPackage } from '@next-core/brick-types';
import { BrickTemplateFactory } from '@next-core/brick-types';
import { ContextConf } from '@next-core/brick-types';
import { ContextResolveTriggerBrickLifeCycle } from '@next-core/brick-types';
import { Contract } from '@next-core/brick-types';
import { ContractRequest } from '@next-core/brick-types';
import { ContractResponse } from '@next-core/brick-types';
import { CustomApiInfo } from '@next-core/brick-utils';
Expand Down Expand Up @@ -151,6 +152,12 @@ export function checkIf(rawIf: string | boolean, context: PluginRuntimeContext):
// @internal @deprecated (undocumented)
export function checkIfByTransform(rawIf: string | boolean, data: unknown): boolean;

// @public (undocumented)
export function clearDebugContract(): void;

// @public (undocumented)
export function collectDebugContract(contracts: Contract[] | undefined): void;

// @public (undocumented)
export function constructEventListener(handler: BrickEventHandler): EventListener;

Expand Down
2 changes: 2 additions & 0 deletions packages/brick-dll/manifest.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -18647,6 +18647,8 @@
"batchSetAppsLocalTheme",
"checkIf",
"checkIfByTransform",
"clearDebugContract",
"collectDebugContract",
"constructEventListener",
"createHistory",
"createRuntime",
Expand Down
28 changes: 28 additions & 0 deletions packages/brick-kit/src/core/CollectContracts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
getContract,
collectWidgetContract,
clearCollectWidgetContract,
collectDebugContract,
clearDebugContract,
} from "./CollectContracts";

describe("CollectContract", () => {
Expand Down Expand Up @@ -54,4 +56,30 @@ describe("CollectContract", () => {

expect(getContract("easyops.api.cmdb.model.postSearch")).toEqual(undefined);
});

it("should work with debug mode", () => {
const contracts = [
{
namespaceId: "easyops.api.cmdb.model",
name: "getDetail",
version: "1.0.0",
},
{
namespaceId: "easyops.api.cmdb.model",
name: "postSearch",
version: "1.2.0",
},
];
collectDebugContract(contracts);

expect(getContract("easyops.api.cmdb.model.postSearch")).toEqual({
namespaceId: "easyops.api.cmdb.model",
name: "postSearch",
version: "1.2.0",
});

clearDebugContract();

expect(getContract("easyops.api.cmdb.model.postSearch")).toEqual(undefined);
});
});
15 changes: 14 additions & 1 deletion packages/brick-kit/src/core/CollectContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Contract } from "@next-core/brick-types";

const contractsMap: Map<string, Contract> = new Map();
const widgetContractMap: Map<string, Contract> = new Map();
const debugContractMap: Map<string, Contract> = new Map();

const addContract = (
contracts: Contract[],
Expand All @@ -24,6 +25,18 @@ export function clearCollectWidgetContract(): void {
widgetContractMap.clear();
}

export function collectDebugContract(contracts: Contract[] | undefined): void {
addContract(contracts, debugContractMap);
}

export function clearDebugContract(): void {
debugContractMap.clear();
}

export function getContract(name: string): Contract {
return contractsMap.get(name) || widgetContractMap.get(name);
return (
contractsMap.get(name) ||
widgetContractMap.get(name) ||
debugContractMap.get(name)
);
}
42 changes: 21 additions & 21 deletions packages/brick-kit/src/core/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,28 +212,28 @@ export async function _dev_only_debugDataValue(
tplContextId,
};

if (debugData.value) {
return computeRealValue(debugData.value, runtimeContext, true);
}

const hasTransform = debugData.resolve.transform;
const result: Record<string, unknown> = {};
await _internalApiGetResolver().resolveOne(
"reference",
hasTransform
? debugData.resolve
: {
...debugData.resolve,
transform: "value",
},
result,
null,
runtimeContext,
{ cache: "reload" }
);
if (debugData.resolve) {
const hasTransform = debugData.resolve.transform;
const result: Record<string, unknown> = {};
await _internalApiGetResolver().resolveOne(
"reference",
hasTransform
? debugData.resolve
: {
...debugData.resolve,
transform: "value",
},
result,
null,
runtimeContext,
{ cache: "reload" }
);

// 跟 v3 的数据结构保持一致,有 transform 时返回完整定义,无 transform 时直接返回数据
return hasTransform ? result : result.value;
}

// 跟 v3 的数据结构保持一致,有 transform 时返回完整定义,无 transform 时直接返回数据
return hasTransform ? result : result.value;
return computeRealValue(debugData.value, runtimeContext, true);
}

/* istanbul ignore next */
Expand Down
4 changes: 4 additions & 0 deletions packages/brick-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export * from "./useApplyPageTitle";
export * from "./useCurrentApp";
export * from "./useLocation";
export * from "./useRecentApps";
export {
collectDebugContract,
clearDebugContract,
} from "./core/CollectContracts";
export {
BrickAsComponent,
SingleBrickAsComponent,
Expand Down

0 comments on commit ad2599e

Please sign in to comment.