-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-hooks): add interceptService hook
- Loading branch information
Showing
7 changed files
with
1,821 additions
and
3,441 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,70 @@ | ||
import { useState } from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
import Pandino from '@pandino/pandino'; | ||
import { OBJECTCLASS } from '@pandino/pandino-api'; | ||
import loaderConfiguration from '@pandino/loader-configuration-dom'; | ||
import { PandinoProvider } from '@pandino/react-hooks'; | ||
import { PandinoProvider, useServiceInterceptor } from '@pandino/react-hooks'; | ||
import { CUSTOM_COMPONENT_INTERFACE_KEY, CustomComponent } from '@react-systemjs/component-api'; | ||
import { App } from './App'; | ||
import { SOME_SERVICE_INTERFACE_KEY, SomeServiceImpl } from './service'; | ||
|
||
const root = createRoot(document.querySelector('#root')!); | ||
|
||
const pandino = new Pandino({ | ||
...loaderConfiguration, | ||
}); | ||
(async () => { | ||
const pandino = new Pandino({ | ||
...loaderConfiguration, | ||
}); | ||
|
||
await pandino.init(); | ||
await pandino.start(); | ||
await pandino.init(); | ||
await pandino.start(); | ||
|
||
const bundleContext = pandino.getBundleContext(); | ||
const bundleContext = pandino.getBundleContext(); | ||
|
||
export const ComponentOne: CustomComponent = (props) => { | ||
const [data, setData] = useState<{ firstName: string; lastName?: string }>({ ...props }); | ||
type Formatter = (input: string) => string; | ||
|
||
return ( | ||
<div className="component-one" style={{ border: '1px solid black', padding: '1rem' }}> | ||
<h3>Component One</h3> | ||
<p>FirstName: {data.firstName}</p> | ||
<p>LastName: {data.lastName}</p> | ||
</div> | ||
); | ||
}; | ||
const ComponentOne: CustomComponent = (props) => { | ||
const [data, setData] = useState<{ firstName: string; lastName?: string }>({ ...props }); | ||
const intercept = useServiceInterceptor(); | ||
const formatter = intercept<Formatter>(`(${OBJECTCLASS}=x-formatter)`, (input: string) => input.split('').reverse().join('')); | ||
|
||
return ( | ||
<div className="component-one" style={{ border: '1px solid black', padding: '1rem' }}> | ||
<h3>Component One</h3> | ||
<p>FirstName: {data.firstName}</p> | ||
<p>LastName: {data.lastName}</p> | ||
<p>Decorator test: {formatter('abcd')}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
const reg = bundleContext.registerService<CustomComponent>(CUSTOM_COMPONENT_INTERFACE_KEY, ComponentOne); | ||
window.setTimeout(() => { | ||
reg.unregister(); | ||
const reg = bundleContext.registerService<CustomComponent>(CUSTOM_COMPONENT_INTERFACE_KEY, ComponentOne); | ||
window.setTimeout(() => { | ||
bundleContext.registerService<CustomComponent>(CUSTOM_COMPONENT_INTERFACE_KEY, ComponentOne); | ||
reg.unregister(); | ||
bundleContext.registerService<Formatter>('x-formatter', (input) => input.toUpperCase()); | ||
window.setTimeout(() => { | ||
bundleContext.registerService<CustomComponent>(CUSTOM_COMPONENT_INTERFACE_KEY, ComponentOne); | ||
}, 2000); | ||
}, 2000); | ||
}, 2000); | ||
|
||
// (async () => { | ||
// const componentOneBundle = await bundleContext.installBundle('./component-one.system-manifest.json'); | ||
// window.setTimeout(() => { | ||
// pandino.uninstallBundle(componentOneBundle as any); | ||
// window.setTimeout(() => { | ||
// bundleContext.installBundle('./component-one.system-manifest.json'); | ||
// }, 2000); | ||
// }, 2000); | ||
// })(); | ||
// const componentOneBundle = await bundleContext.installBundle('./component-one.system-manifest.json'); | ||
// window.setTimeout(() => { | ||
// pandino.uninstallBundle(componentOneBundle as any); | ||
// window.setTimeout(() => { | ||
// bundleContext.installBundle('./component-one.system-manifest.json'); | ||
// }, 2000); | ||
// }, 2000); | ||
|
||
// (async () => { | ||
// let someReg = await bundleContext.registerService(SOME_SERVICE_INTERFACE_KEY, new SomeServiceImpl()); | ||
// window.setTimeout(async () => { | ||
// someReg.unregister(); | ||
// window.setTimeout(async () => { | ||
// someReg = await bundleContext.registerService(SOME_SERVICE_INTERFACE_KEY, new SomeServiceImpl()); | ||
// }, 2000); | ||
// }, 2000); | ||
// })(); | ||
// let someReg = bundleContext.registerService(SOME_SERVICE_INTERFACE_KEY, new SomeServiceImpl()); | ||
// window.setTimeout(() => { | ||
// someReg.unregister(); | ||
// window.setTimeout(() => { | ||
// bundleContext.registerService(SOME_SERVICE_INTERFACE_KEY, new SomeServiceImpl()); | ||
// }, 2000); | ||
// }, 2000); | ||
|
||
root.render( | ||
<PandinoProvider ctx={bundleContext}> | ||
<App /> | ||
</PandinoProvider>, | ||
); | ||
root.render( | ||
<PandinoProvider ctx={bundleContext}> | ||
<App /> | ||
</PandinoProvider>, | ||
); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './ComponentProxy'; | ||
export * from './PandinoContext'; | ||
export * from './useServiceInterceptor'; | ||
export * from './useTrackComponent'; | ||
export * from './useTrackService'; |
41 changes: 41 additions & 0 deletions
41
packages/@pandino/react-hooks/src/useServiceInterceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { FRAMEWORK_SERVICE_UTILS, ServiceReference, ServiceUtils } from '@pandino/pandino-api'; | ||
import { useBundleContext } from './PandinoContext'; | ||
|
||
export type InterceptorHook = () => <T>(filter: string, target: T) => T; | ||
|
||
export const useServiceInterceptor: InterceptorHook = <T>() => { | ||
const { bundleContext } = useBundleContext(); | ||
const serviceUtilsRef = bundleContext.getServiceReference<ServiceUtils>(FRAMEWORK_SERVICE_UTILS); | ||
const serviceUtils = bundleContext.getService(serviceUtilsRef); | ||
const refsUsed = useRef<Set<ServiceReference<any>>>(new Set<ServiceReference<any>>()); | ||
|
||
useEffect(() => { | ||
// cleanup refs, regs | ||
return () => { | ||
try { | ||
if (serviceUtilsRef) { | ||
bundleContext.ungetService(serviceUtilsRef); | ||
} | ||
for (const ref of refsUsed.current) { | ||
bundleContext.ungetService(ref); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
}, []); | ||
|
||
return (filter: string, target: T) => { | ||
const refs = bundleContext.getServiceReferences(undefined, filter); | ||
const ref = serviceUtils.getBestServiceReference(refs); | ||
for (const refI of refs) { | ||
refsUsed.current.add(refI); | ||
} | ||
if (ref) { | ||
refsUsed.current.add(ref); | ||
return bundleContext.getService(ref); | ||
} | ||
return target; | ||
}; | ||
}; |