Skip to content

Commit

Permalink
refactor backing store utils
Browse files Browse the repository at this point in the history
  • Loading branch information
koros committed Oct 13, 2023
1 parent 07cc001 commit 809a1a5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
5 changes: 2 additions & 3 deletions packages/abstractions/src/store/backingStoreUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export const BackingStoreKey = "backingStore";
* @param obj
* @returns
*/
export function isBackingStoreEnabled<T extends Parsable>(obj: T, parsableFactory: ParsableFactory<T>): boolean {
// Check if the object has a backingStore property
const fields = parsableFactory({} as ParseNode)(obj);
export function isBackingStoreEnabled(fields: Record<string, (node: ParseNode) => void> ): boolean {
// Check if the fields contain the backing store key
return Object.keys(fields).includes(BackingStoreKey);
};
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { assert } from "chai";
import { isBackingStoreEnabled } from "../../../src/store/backingStoreUtils";
import { TestBackedModel, createTestBackedModelFromDiscriminatorValue, createTestParserFromDiscriminatorValue } from "./testEntity";
import { ParseNode } from "../../../src";

it("Test backing store should be enabled if the parsableFactory has backingStore property", async () => {
const testBackedModel = {} as TestBackedModel;
const backingStoreEnabled = isBackingStoreEnabled(testBackedModel, createTestBackedModelFromDiscriminatorValue);
const fields = createTestBackedModelFromDiscriminatorValue({} as ParseNode)(testBackedModel);
const backingStoreEnabled = isBackingStoreEnabled(fields);
assert.isTrue(backingStoreEnabled);
});

it("Test backing store should not be enabled if the parsableFactory lacks backingStore property", async () => {
const testBackedModel = {} as TestBackedModel;
const backingStoreEnabled = isBackingStoreEnabled(testBackedModel, createTestParserFromDiscriminatorValue);
const testModel = {} as TestBackedModel;
const fields = createTestParserFromDiscriminatorValue({} as ParseNode)(testModel);
const backingStoreEnabled = isBackingStoreEnabled(fields);
assert.isFalse(backingStoreEnabled);
});
3 changes: 2 additions & 1 deletion packages/serialization/form/src/formParseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export class FormParseNode implements ParseNode {
parsableFactory: ParsableFactory<T>,
): T => {
const temp: T = {} as T;
const value: T = isBackingStoreEnabled(temp, parsableFactory) ? new Proxy({}, createBackedModelProxyHandler<T>()) as T : temp;
const enableBackingStore = isBackingStoreEnabled(parsableFactory(this)(temp));
const value: T = enableBackingStore ? new Proxy({}, createBackedModelProxyHandler<T>()) as T : temp;
if (this.onBeforeAssignFieldValues) {
this.onBeforeAssignFieldValues(value);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/serialization/json/src/jsonParseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export class JsonParseNode implements ParseNode {
parsableFactory: ParsableFactory<T>,
): T => {
const temp: T = {} as T;
const value: T = isBackingStoreEnabled(temp, parsableFactory) ? new Proxy({}, createBackedModelProxyHandler<T>()) as T : temp;
const enableBackingStore = isBackingStoreEnabled(parsableFactory(this)(temp));
const value: T = enableBackingStore ? new Proxy({}, createBackedModelProxyHandler<T>()) as T : temp;
if (this.onBeforeAssignFieldValues) {
this.onBeforeAssignFieldValues(value);
}
Expand Down

0 comments on commit 809a1a5

Please sign in to comment.