Skip to content

Commit

Permalink
fix(react-hooks): add missing service listener cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
noherczeg committed Mar 6, 2023
1 parent 6174065 commit 4783a4c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
20 changes: 4 additions & 16 deletions examples/react-systemjs/packages/app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import type { FC } from 'react';
import { useEffect, useMemo, useState } from 'react';
import { useState } from 'react';
import { OBJECTCLASS } from '@pandino/pandino-api';
import { ComponentProxy, useTrackService } from '@pandino/react-hooks';
import { ComponentProxy } from '@pandino/react-hooks';
import { CUSTOM_COMPONENT_INTERFACE_KEY } from '@react-systemjs/component-api';
import { SOME_SERVICE_INTERFACE_KEY, SomeService } from './service';
import { FallbackComponent } from './FallbackComponent';

export const App: FC = () => {
const [firstName] = useState<string>('John');
const { service: someService } = useTrackService<SomeService>(`(${OBJECTCLASS}=${SOME_SERVICE_INTERFACE_KEY})`);
const text = useMemo<string | undefined>(() => someService?.someMethod(), [someService]);

console.count('Rendering App.');

useEffect(() => {
console.info(`SomeService implementation: ${someService}`);
}, [someService]);

useEffect(() => {
console.info(`text: ${text}`);
}, [text]);

return (
<ComponentProxy filter={`(${OBJECTCLASS}=${CUSTOM_COMPONENT_INTERFACE_KEY})`} firstName={firstName}>
<div className={'fallback'}>fallback for: {firstName}</div>
<div className={'service'}>SomeService test: {someService?.someMethod()}</div>
<div className={'text'}>Text: {text}</div>
<FallbackComponent firstName={firstName} />
</ComponentProxy>
);
};
21 changes: 21 additions & 0 deletions examples/react-systemjs/packages/app/src/FallbackComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useMemo } from 'react';
import { OBJECTCLASS } from '@pandino/pandino-api';
import { useTrackService } from '@pandino/react-hooks';
import type { CustomComponent } from '@react-systemjs/component-api';
import { SOME_SERVICE_INTERFACE_KEY, SomeService } from './service';

export const FallbackComponent: CustomComponent = (props) => {
const { service: someService } = useTrackService<SomeService>(`(${OBJECTCLASS}=${SOME_SERVICE_INTERFACE_KEY})`);
const text = useMemo<string | undefined>(() => someService?.someMethod(), [someService]);

console.count('Rendering FallbackComponent.');
console.info(`SomeService implementation: ${someService}`);

return (
<div>
<div className={'fallback'}>fallback for: {props.firstName}</div>
<div className={'service'}>SomeService test: {someService?.someMethod()}</div>
<div className={'text'}>Text: {text}</div>
</div>
);
};
4 changes: 4 additions & 0 deletions packages/@pandino/react-hooks/src/useTrackService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export const useTrackService: ServiceTrackerHook = <T>(filter: string) => {
service: getService(filter),
});
}

return () => {
bundleContext.removeServiceListener(listener);
};
}, [filter]);

return tracker;
Expand Down

0 comments on commit 4783a4c

Please sign in to comment.