Skip to content

Commit

Permalink
fix(pal): clear only store defined marks and measures
Browse files Browse the repository at this point in the history
Before: on store.dispatch all marks and measures are cleared, including user defined.
After: on store.dispatch only store defined marks and measures are cleared.

No breaking changes
  • Loading branch information
Viktor Jevdokimov committed Mar 23, 2021
1 parent f93accc commit d395528
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export class Store<T> {
private middlewares: Map<Middleware<T>, { placement: MiddlewarePlacement, settings?: any }> = new Map();
private _state: BehaviorSubject<T>;
private options: Partial<StoreOptions>;
private _markNames: Set<string> = new Set<string>();
private _measureNames: Set<string> = new Set<string>();

private dispatchQueue: DispatchQueueItem<T>[] = [];

Expand Down Expand Up @@ -191,7 +193,7 @@ export class Store<T> {
throw new UnregisteredActionError(unregisteredAction.reducer);
}

PLATFORM.performance.mark("dispatch-start");
this.mark("dispatch-start");

const pipedActions = actions.map((a) => ({
type: this.actions.get(a.reducer)!.type,
Expand Down Expand Up @@ -219,8 +221,8 @@ export class Store<T> {
);

if (beforeMiddleswaresResult === false) {
PLATFORM.performance.clearMarks();
PLATFORM.performance.clearMeasures();
this.clearMarks();
this.clearMeasures();

return;
}
Expand All @@ -229,13 +231,13 @@ export class Store<T> {
for (const action of pipedActions) {
result = await action.reducer(result, ...action.params);
if (result === false) {
PLATFORM.performance.clearMarks();
PLATFORM.performance.clearMeasures();
this.clearMarks();
this.clearMeasures();

return;
}

PLATFORM.performance.mark("dispatch-after-reducer-" + action.type);
this.mark("dispatch-after-reducer-" + action.type);

if (!result && typeof result !== "object") {
throw new Error("The reducer has to return a new state");
Expand All @@ -249,8 +251,8 @@ export class Store<T> {
);

if (resultingState === false) {
PLATFORM.performance.clearMarks();
PLATFORM.performance.clearMeasures();
this.clearMarks();
this.clearMeasures();

return;
}
Expand All @@ -262,16 +264,16 @@ export class Store<T> {
}

this._state.next(resultingState);
PLATFORM.performance.mark("dispatch-end");
this.mark("dispatch-end");

if (this.options.measurePerformance === PerformanceMeasurement.StartEnd) {
PLATFORM.performance.measure(
this.measure(
"startEndDispatchDuration",
"dispatch-start",
"dispatch-end"
);

const measures = PLATFORM.performance.getEntriesByName("startEndDispatchDuration");
const measures = PLATFORM.performance.getEntriesByName("startEndDispatchDuration", "measure");
this.logger[getLogType(this.options, "performanceLog", LogLevel.info)](
`Total duration ${measures[0].duration} of dispatched action ${callingAction.name}:`,
measures
Expand All @@ -285,8 +287,8 @@ export class Store<T> {
);
}

PLATFORM.performance.clearMarks();
PLATFORM.performance.clearMeasures();
this.clearMarks();
this.clearMeasures();

this.updateDevToolsState({ type: callingAction.name, params: callingAction.params }, resultingState);
}
Expand All @@ -313,7 +315,7 @@ export class Store<T> {

return await prev;
} finally {
PLATFORM.performance.mark(`dispatch-${placement}-${curr[0].name}`);
this.mark(`dispatch-${placement}-${curr[0].name}`);
}
}, state);
}
Expand Down Expand Up @@ -380,6 +382,28 @@ export class Store<T> {
private registerHistoryMethods() {
this.registerAction("jump", jump as Reducer<T>);
}

private mark(markName: string) {
this._markNames.add(markName);
PLATFORM.performance.mark(markName);
}

private clearMarks() {
this._markNames.forEach((markName: string) =>
PLATFORM.performance.clearMarks(markName));
this._markNames.clear();
}

private measure(measureName: string, startMarkName: string, endMarkName: string) {
this._measureNames.add(measureName);
PLATFORM.performance.measure(measureName, startMarkName, endMarkName)
}

private clearMeasures() {
this._measureNames.forEach((measureName: string) =>
PLATFORM.performance.clearMeasures(measureName));
this._measureNames.clear();
}
}

export function dispatchify<T, P extends any[]>(action: Reducer<T, P> | string) {
Expand Down

0 comments on commit d395528

Please sign in to comment.