From b60fc1b056e2623109034ef14ad51017a1c78921 Mon Sep 17 00:00:00 2001 From: Faris Nabiev Date: Thu, 7 Dec 2023 11:32:49 +0300 Subject: [PATCH] fix: replace functional approaches with oop --- .../src/diagrams/sequence/sequenceDb.js | 4 +- .../mermaid/src/utils/imperativeState.spec.ts | 38 +++------ packages/mermaid/src/utils/imperativeState.ts | 80 +++++++++++-------- 3 files changed, 58 insertions(+), 64 deletions(-) diff --git a/packages/mermaid/src/diagrams/sequence/sequenceDb.js b/packages/mermaid/src/diagrams/sequence/sequenceDb.js index 5bc3008a24..ea2c742846 100644 --- a/packages/mermaid/src/diagrams/sequence/sequenceDb.js +++ b/packages/mermaid/src/diagrams/sequence/sequenceDb.js @@ -10,9 +10,9 @@ import { setAccTitle, setDiagramTitle, } from '../common/commonDb.js'; -import { createImperativeState } from '../../utils/imperativeState.js'; +import { ImperativeState } from '../../utils/imperativeState.js'; -const state = createImperativeState(() => ({ +const state = new ImperativeState(() => ({ prevActor: undefined, actors: {}, createdActors: {}, diff --git a/packages/mermaid/src/utils/imperativeState.spec.ts b/packages/mermaid/src/utils/imperativeState.spec.ts index e78a7d4955..e7f9ea9ad1 100644 --- a/packages/mermaid/src/utils/imperativeState.spec.ts +++ b/packages/mermaid/src/utils/imperativeState.spec.ts @@ -1,22 +1,4 @@ -import { createImperativeState, domain } from './imperativeState.js'; - -describe('domain.optional', () => { - it('should set undefined without args', () => { - expect(domain.optional()).toBeUndefined(); - }); - - it('should set identity with args', () => { - const value = {}; - expect(domain.optional(value)).toEqual(value); - }); -}); - -describe('domain.identity', () => { - it('should set identity', () => { - const value = {}; - expect(domain.identity(value)).toEqual(value); - }); -}); +import { ImperativeState } from './imperativeState.js'; describe('createImperativeState', () => { it('should create state with values from initializer', () => { @@ -24,9 +6,9 @@ describe('createImperativeState', () => { flag: false, }; - const state = createImperativeState(() => ({ - foo: domain.optional(), - bar: domain.identity([]), + const state = new ImperativeState(() => ({ + foo: undefined as number | undefined, + bar: [] as string[], baz, })); @@ -36,9 +18,9 @@ describe('createImperativeState', () => { }); it('should update records', () => { - const state = createImperativeState(() => ({ - foo: domain.optional(), - bar: domain.identity([]), + const state = new ImperativeState(() => ({ + foo: undefined as number | undefined, + bar: [] as string[], baz: { flag: false, }, @@ -56,9 +38,9 @@ describe('createImperativeState', () => { }); it('should reset records', () => { - const state = createImperativeState(() => ({ - foo: domain.optional(), - bar: domain.identity([]), + const state = new ImperativeState(() => ({ + foo: undefined as number | undefined, + bar: [] as string[], baz: { flag: false, }, diff --git a/packages/mermaid/src/utils/imperativeState.ts b/packages/mermaid/src/utils/imperativeState.ts index bc63844b1a..28741afa4c 100644 --- a/packages/mermaid/src/utils/imperativeState.ts +++ b/packages/mermaid/src/utils/imperativeState.ts @@ -1,37 +1,49 @@ -export const createImperativeState = >(init: () => S) => { - const state = init(); +/** + * Resettable state storage. + * @example + * ``` + * const state = new ImperativeState(() => { + * foo: undefined as string | undefined, + * bar: [] as number[], + * baz: 1 as number | undefined, + * }); + * + * state.records.foo = "hi"; + * console.log(state.records.foo); // prints "hi"; + * state.reset(); + * console.log(state.records.foo); // prints "default"; + * + * // typeof state.records: + * // { + * // foo: string | undefined, // actual: undefined + * // bar: number[], // actual: [] + * // baz: number | undefined, // actual: 1 + * // } + * ``` + */ +export class ImperativeState> { + init: () => S; + records: S; - return { - get records() { - return state; - }, - reset: () => { - Object.keys(state).forEach((key) => { - delete state[key]; - }); - Object.entries(init()).forEach(([key, value]: [keyof S, any]) => { - state[key] = value; - }); - }, - }; -}; + /** + * @param init - Function that creates the default state. + */ + constructor(init: () => S) { + this.init = init; + this.records = init(); + } -export const domain = { - optional: (value?: V) => value, - identity: (value: V) => value, -} as const; - -/* -const state = createImperativeState(() => ({ - foo: domain.optional(), - bar: domain.identity([]), - baz: domain.optional(1), -})); - -typeof state.records: -{ - foo: string | undefined, // actual: undefined - bar: number[], // actual: [] - baz: number | undefined, // actual: 1 + reset() { + Object.keys(this.records).forEach((key) => { + delete this.records[key]; + }); + Object.entries(this.init()).forEach( + ([key, value]: [ + keyof S, + any // eslint-disable-line @typescript-eslint/no-explicit-any + ]) => { + this.records[key] = value; + } + ); + } } -*/