Skip to content

Commit

Permalink
fix: replace functional approaches with oop
Browse files Browse the repository at this point in the history
  • Loading branch information
rflban committed Dec 7, 2023
1 parent 61fb9e5 commit b60fc1b
Showing 3 changed files with 58 additions and 64 deletions.
4 changes: 2 additions & 2 deletions packages/mermaid/src/diagrams/sequence/sequenceDb.js
Original file line number Diff line number Diff line change
@@ -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: {},
38 changes: 10 additions & 28 deletions packages/mermaid/src/utils/imperativeState.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
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', () => {
const baz = {
flag: false,
};

const state = createImperativeState(() => ({
foo: domain.optional<number>(),
bar: domain.identity<string[]>([]),
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<number>(),
bar: domain.identity<string[]>([]),
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<number>(),
bar: domain.identity<string[]>([]),
const state = new ImperativeState(() => ({
foo: undefined as number | undefined,
bar: [] as string[],
baz: {
flag: false,
},
80 changes: 46 additions & 34 deletions packages/mermaid/src/utils/imperativeState.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
export const createImperativeState = <S extends Record<string, unknown>>(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<S extends Record<string, unknown>> {
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: <V>(value?: V) => value,
identity: <V>(value: V) => value,
} as const;

/*
const state = createImperativeState(() => ({
foo: domain.optional<string>(),
bar: domain.identity<number[]>([]),
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;
}
);
}
}
*/

0 comments on commit b60fc1b

Please sign in to comment.