So this is kind of a multi stage plugin system. Let me explain:
Plugins get sent to the makeUseTable
constructor, which then type the available options usable on useTable
.
const useTable = makeUseTable({
plugins: [myPlugin],
})
function App() {
const instance = useTable({
thesePropsAreGood: true,
})
}
So up to this point, the only thing that the types have been applied to are the options, which has been solved, but there's more :)
Internally, useTable creates an instance. Here's some pseudo code
const myPlugin = {
name: 'my-plugin',
plugs: {
doStuff: (instance) => {},
},
}
function makeUseTable({ plugins }) {
// Options Type should be created here
const plugs = plugins.composeIntoPlugs()
return function useTable(options) {
const instanceRef = React.useRef()
if (!instanceRef.current) {
instanceRef.current = createInstance(options) // Instance Type should be created here
}
const instance = instanceRef.current
instance.stuff = plugs.doStuff(instance)
return instance // Return the instance
}
}
I realize this is all pseudo code, and if it still doesn't make sense, I can do a live code with some smart people to show them how I'm actually using this and what I'm building if that will help. But it's very deep and not contrived at all ;)