Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/__tests__/Container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ describe("Container", () => {
const container: Container<{ TestService: string }> = new Container({} as any);
expect(() => container.get("TestService")).toThrowError('Could not find Service for Token "TestService"');
});

test("a factory returning undefined is only invoked once", () => {
const factory = jest.fn().mockReturnValue(undefined);
const containerWithService = Container.provides("TestService", factory);

expect(containerWithService.get("TestService")).toBeUndefined();
expect(containerWithService.get("TestService")).toBeUndefined();
expect(factory).toHaveBeenCalledTimes(1);
});
});

describe("when getting the Container Token", () => {
Expand Down
44 changes: 44 additions & 0 deletions src/__tests__/memoize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { isMemoized, memoize } from "../memoize";

describe("memoize", () => {
test("invokes the delegate only once and returns the cached result", () => {
const delegate = jest.fn().mockReturnValue("value");
const memoized = memoize(delegate);

expect(memoized()).toBe("value");
expect(memoized()).toBe("value");
expect(delegate).toHaveBeenCalledTimes(1);
});

test("invokes the delegate only once even when it returns undefined", () => {
const delegate = jest.fn().mockReturnValue(undefined);
const memoized = memoize(delegate);

expect(memoized()).toBeUndefined();
expect(memoized()).toBeUndefined();
expect(delegate).toHaveBeenCalledTimes(1);
});

test("does not cache when the delegate throws, allowing a retry", () => {
const delegate = jest
.fn()
.mockImplementationOnce(() => {
throw new Error("first call fails");
})
.mockReturnValue("recovered");
const memoized = memoize(delegate);

expect(() => memoized()).toThrowError("first call fails");
expect(memoized()).toBe("recovered");
expect(delegate).toHaveBeenCalledTimes(2);
});

test("exposes the original function via delegate and is detected by isMemoized", () => {
const delegate = () => 42;
const memoized = memoize(delegate);

expect(memoized.delegate).toBe(delegate);
expect(isMemoized(memoized)).toBe(true);
expect(isMemoized(delegate)).toBe(false);
});
});
7 changes: 6 additions & 1 deletion src/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ export function isMemoized(fn: unknown): fn is Memoized<AnyFunction> {
}

export function memoize<Fn extends AnyFunction>(delegate: Fn): Memoized<Fn> {
// Track invocation with a flag rather than checking `memo` against `undefined`, so that
// factories which legitimately return `undefined` are still only invoked once. The flag is
// set only after `delegate` returns, preserving the existing behavior of retrying on throw.
let invoked = false;
let memo: any;
const memoized = function (this: any, ...args: any[]) {
if (typeof memo !== "undefined") return memo;
if (invoked) return memo;
memo = delegate.apply(this, args);
invoked = true;
return memo;
};
memoized.delegate = delegate;
Expand Down
Loading