mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-28 07:03:17 +08:00
fix: replace functional approaches with oop
This commit is contained in:
parent
61fb9e50d3
commit
b60fc1b056
@ -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: {},
|
||||
|
@ -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<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,
|
||||
},
|
||||
|
@ -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;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user