Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fns-wire): support rewiring #30

Merged
merged 1 commit into from
Dec 11, 2020
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
78 changes: 78 additions & 0 deletions src/fn-wire/use-fn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,82 @@ describe('useFn', () => {
});
expect(fn).toBeCalledWith(3);
});
describe('when wire changed', () => {
it('should subscribe to the new wire', () => {
const fn = jest.fn();
const { result, rerender } = renderHook(
({ firstWire }: { firstWire: boolean }) => {
const wire1 = useFnsWire<{ test(n: number): void }>(null);
const wire2 = useFnsWire<{ test(n: number): void }>(null);
useFn(firstWire ? wire1 : wire2, 'test', fn);
return { wire1, wire2 };
},
{ initialProps: { firstWire: true } },
);
rerender({ firstWire: false });

act(() => {
result.current.wire2.fns.test(3);
});
expect(fn).toBeCalledWith(3);
});
it('should unsubscribe from first wire ', () => {
const fn = jest.fn();
const { result, rerender } = renderHook(
({ firstWire }: { firstWire: boolean }) => {
const wire1 = useFnsWire<{ test(n: number): void }>(null);
const wire2 = useFnsWire<{ test(n: number): void }>(null);
useFn(firstWire ? wire1 : wire2, 'test', fn);
return { wire1, wire2 };
},
{ initialProps: { firstWire: true } },
);
rerender({ firstWire: false });

act(() => {
result.current.wire1.fns.test(3);
});
expect(fn).not.toBeCalled();
});
});
describe('when up-link wire changed', () => {
it('should subscribe to the new wire', () => {
const fn = jest.fn();
const { result, rerender } = renderHook(
({ firstWire }: { firstWire: boolean }) => {
const wire1 = useFnsWire<{ test(n: number): void }>(null);
const wire2 = useFnsWire<{ test(n: number): void }>(null);
const wire = useFnsWire(firstWire ? wire1 : wire2);
useFn(wire, 'test', fn);
return { wire1, wire2 };
},
{ initialProps: { firstWire: true } },
);
rerender({ firstWire: false });

act(() => {
result.current.wire2.fns.test(3);
});
expect(fn).toBeCalledWith(3);
});
it('should unsubscribe from first wire ', () => {
const fn = jest.fn();
const { result, rerender } = renderHook(
({ firstWire }: { firstWire: boolean }) => {
const wire1 = useFnsWire<{ test(n: number): void }>(null);
const wire2 = useFnsWire<{ test(n: number): void }>(null);
const wire = useFnsWire(firstWire ? wire1 : wire2);
useFn(wire, 'test', fn);
return { wire1, wire2 };
},
{ initialProps: { firstWire: true } },
);
rerender({ firstWire: false });

act(() => {
result.current.wire1.fns.test(3);
});
expect(fn).not.toBeCalled();
});
});
});
2 changes: 0 additions & 2 deletions src/fn-wire/use-fn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect } from 'react';
import { KeyOfMethods } from '../utils/type-utils';
import { useStabilityGuard } from '../utils/use-stability-guard';
import { FnsWire } from './fns-wire';

/**
Expand Down Expand Up @@ -31,7 +30,6 @@ export function useFn<Fns, K extends KeyOfMethods<Fns>>(
name: K,
fn: Fns[K],
) {
useStabilityGuard(wire);
useEffect(() => {
return wire?.fn(name, fn);
}, [wire, name, fn]);
Expand Down
13 changes: 11 additions & 2 deletions src/fn-wire/use-fns-wire.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { createFnsWire } from './create-fns-wire';
import { FnsWire } from './fns-wire';

const create = <Fns>(upLink: FnsWire<Fns> | null | undefined) => {
return createFnsWire(upLink || {});
};

export function useFnsWire<Fns = {}>(
upLink: FnsWire<Fns> | null | undefined,
): FnsWire<Fns> {
const [[wire, connect]] = useState(() => createFnsWire(upLink || {}));
const [[wire, connect], set] = useState(() => create(upLink));
const lastUpLinkRef = useRef(upLink);
if (lastUpLinkRef.current !== upLink) {
lastUpLinkRef.current = upLink;
set(create(upLink));
}
useEffect(() => {
return connect();
}, [connect]);
Expand Down