Skip to content

Commit

Permalink
Merge branch 'vitar-fix-pal-mark-measure-clear-scope' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
zewa666 committed Mar 24, 2021
2 parents f93accc + 97539fa commit 5955aab
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 18 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
57 changes: 57 additions & 0 deletions test/unit/middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,38 @@ describe("middlewares", () => {
done();
});
});

it("should log all dispatch durations", done => {
spyOn(PLATFORM.performance, "mark").and.callThrough();
const store = createStoreWithState(initialState);

const decreaseBefore = (currentState: TestState, originalState?: TestState) => {
const newState = Object.assign({}, currentState);
newState.counter = originalState!.counter;

return newState;
}
store.registerMiddleware(decreaseBefore, MiddlewarePlacement.Before);

const resetBefore = (currentState: TestState, originalState?: TestState) => {
expect(currentState.counter).toBe(0);
return originalState;
}
store.registerMiddleware(resetBefore, MiddlewarePlacement.Before);

store.registerAction("IncrementAction", incrementAction);
store.dispatch(incrementAction);

store.state.pipe(
skip(1),
take(1)
).subscribe(() => {
expect(PLATFORM.performance.mark).toHaveBeenNthCalledWith(2, "dispatch-before-decreaseBefore");
expect(PLATFORM.performance.mark).toHaveBeenNthCalledWith(3, "dispatch-before-resetBefore");
expect(PLATFORM.performance.mark).toHaveBeenNthCalledWith(4, "dispatch-after-reducer-IncrementAction");
done();
});
});
});

describe("which are applied after the action dispatches", () => {
Expand Down Expand Up @@ -317,6 +349,31 @@ describe("middlewares", () => {
done();
});
});

it("should log all dispatch durations", done => {
spyOn(PLATFORM.performance, "mark").and.callThrough();
const store = createStoreWithState(initialState);

const decreaseAfter = (currentState: TestState, originalState: TestState | undefined) => {
const newState = Object.assign({}, currentState);
newState.counter = originalState!.counter;

return newState;
}
store.registerMiddleware(decreaseAfter, MiddlewarePlacement.After);

store.registerAction("IncrementAction", incrementAction);
store.dispatch(incrementAction);

store.state.pipe(
skip(1),
take(1)
).subscribe(() => {
expect(PLATFORM.performance.mark).toHaveBeenNthCalledWith(2, "dispatch-after-reducer-IncrementAction");
expect(PLATFORM.performance.mark).toHaveBeenNthCalledWith(3, "dispatch-after-decreaseAfter");
done();
});
});
});

it("should handle throwing middlewares and maintain queue", done => {
Expand Down
Loading

0 comments on commit 5955aab

Please sign in to comment.