Skip to content

Commit

Permalink
fix: gc change event
Browse files Browse the repository at this point in the history
  • Loading branch information
dineug committed Oct 30, 2024
1 parent 1a40cc6 commit e9bb389
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
1 change: 0 additions & 1 deletion packages/erd-editor/src/components/erd-editor/ErdEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ const ErdEditor: FC<ErdEditorProps, ErdEditorElement> = (props, ctx) => {
if (isChange) {
procGC(store.state, gcIds);
store.dispatchSync(validationIdsAction());
ctx.dispatchEvent(new CustomEvent('change'));
}
});
};
Expand Down
19 changes: 9 additions & 10 deletions packages/erd-editor/src/engine/replication-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { procGC } from '@/services/schema-gc/procGC';
import { SchemaGCService } from '@/services/schema-gc/schemaGCService';
import { toSafeString } from '@/utils/validation';

type ReducerRecord = {
type ListenerRecord = {
[P in keyof InternalActionMap]: (payload: InternalActionMap[P]) => void;
};

Expand All @@ -30,7 +30,7 @@ type InternalActionMap = {

export type ReplicationStore = {
readonly value: string;
on: (reducers: Partial<ReducerRecord>) => Unsubscribe;
on: (listeners: Partial<ListenerRecord>) => Unsubscribe;
setInitialValue: (value: string) => void;
dispatch: (actions: Array<AnyAction> | AnyAction) => void;
dispatchSync: (actions: Array<AnyAction> | AnyAction) => void;
Expand All @@ -47,24 +47,24 @@ export function createReplicationStore(
const change$ = new Observable<Array<AnyAction>>(subscriber =>
store.subscribe(actions => subscriber.next(actions))
).pipe(actionsFilter(ChangeActionTypes), debounceTime(200));
const observers = new Set<Partial<ReducerRecord>>();
const observers = new Set<Partial<ListenerRecord>>();
const schemaGCService = new SchemaGCService();

const on = (reducers: Partial<ReducerRecord>): Unsubscribe => {
observers.has(reducers) || observers.add(reducers);
const on = (listeners: Partial<ListenerRecord>): Unsubscribe => {
observers.has(listeners) || observers.add(listeners);

return () => {
observers.delete(reducers);
observers.delete(listeners);
};
};

const emit = <T extends InternalActionType>(
type: T,
payload: InternalActionMap[T]
) => {
observers.forEach(reducers => {
const reducer = Reflect.get(reducers, type);
safeCallback(reducer, payload);
observers.forEach(listeners => {
const listener = Reflect.get(listeners, type);
safeCallback(listener, payload);
});
};

Expand All @@ -85,7 +85,6 @@ export function createReplicationStore(
if (isChange) {
procGC(store.state, gcIds);
store.dispatchSync(validationIdsAction());
emit(InternalActionType.change, undefined);
}
});
};
Expand Down
23 changes: 12 additions & 11 deletions packages/erd-editor/src/utils/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,27 @@ type InternalActionMap = {
};
};

type Reducer<K extends keyof M, M> = (action: Action<K, M>) => void;
type ReducerRecord<K extends keyof M, M> = {
[P in K]: Reducer<P, M>;
type ListenerRecord = {
[P in keyof InternalActionMap]: (
action: Action<P, InternalActionMap>
) => void;
};

export class Emitter<M extends InternalActionMap = InternalActionMap> {
#observers = new Set<Partial<ReducerRecord<keyof M, M>>>();
export class Emitter {
#observers = new Set<Partial<ListenerRecord>>();

on(reducers: Partial<ReducerRecord<keyof M, M>>) {
this.#observers.has(reducers) || this.#observers.add(reducers);
on(listeners: Partial<ListenerRecord>) {
this.#observers.has(listeners) || this.#observers.add(listeners);

return () => {
this.#observers.delete(reducers);
this.#observers.delete(listeners);
};
}

emit(action: AnyAction) {
this.#observers.forEach(reducers => {
const reducer = Reflect.get(reducers, action.type);
safeCallback(reducer, action);
this.#observers.forEach(listeners => {
const listener = Reflect.get(listeners, action.type);
safeCallback(listener, action);
});
}

Expand Down

0 comments on commit e9bb389

Please sign in to comment.