I have a very large class of 59 methods and about 3000 lines of code. I know that's far larger than most people would want, but the class represents a virtual machine, and most of the methods are run time functions, so I think this is one of the times when a large class is warranted.
The question I have is how can I best organise the source code of this class, as I would like to split it up into multiple files.
I originally used John Resig's class pattern. I would export an object literal from each file, combine them together, and then create the class. This works very adequately, but I want to move to ES classes.
I've experimented with the Mixin pattern from MDN. Each file could export a function, and then I can combine them together with reduce
.
export default Base => class extends Base { ... }
const inherit = ( ...fns ) => fns.reduce( ( v, f ) => f( v ) ) const Glk = inherit( GlkAPI, DateTime, Fref, Misc, Stream, Style, Window )
This is functional, but it's convoluted, and it adds a lot of layers to the inheritance tree, which is not good for performance.
So I'm wondering if there are any better options.
Things I've considered:
Doing a build time concatenation. The resulting file could be very nice, as if it were a single file to start with, but I'd lose the ability to lint the source code, as the inside of a class is not valid outside of that context. Or I could add a dummy class wrapper around each file, but then I'd need to trim it out when combining, and that's not a simple concatenation.
Use Typescript declaration merging. This sounds good in principle, but I don't think you can merge classes together, and merging a class with a namespace wouldn't work either as the namespace properties would become static class properties.
Create one simple class and then manually augment the
prototype
with functions exported from the other files. I guess this is my leading option, but I'm wondering what other projects with big classes have done.