Skip to content

Instantly share code, notes, and snippets.

@tannerlinsley
Created October 14, 2020 19:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tannerlinsley/a610226f837a455f209496f39edcd34c to your computer and use it in GitHub Desktop.
Save tannerlinsley/a610226f837a455f209496f39edcd34c to your computer and use it in GitHub Desktop.

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 ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment