refactor: mixin

This commit is contained in:
cha0s 2024-01-19 03:59:20 -06:00
parent 757cd89e99
commit d4e8b35031
12 changed files with 104 additions and 115 deletions

View File

@ -362,10 +362,10 @@ exports.Flecks = class Flecks {
*/
static from(runtime) {
const {flecks} = runtime;
const mixins = Object.entries(flecks)
.map(([, M]) => M.hooks?.['@flecks/core.mixin'])
.filter((e) => e);
const Flecks = compose(...mixins)(this);
const mixinDescription = Object.entries(flecks)
.map(([path, {mixin}]) => [path, mixin]).filter(([, mixin]) => mixin);
debugSilly('mixins: %O', mixinDescription.map(([path]) => path));
const Flecks = compose(...mixinDescription.map(([, mixin]) => mixin))(this);
return new Flecks(runtime);
}

View File

@ -49,24 +49,6 @@ export const hooks = {
register(gathered, flecks.db.sequelize);
}
},
'@flecks/core.mixin': (Flecks) => (
class FlecksWithDb extends Flecks {
db = {
Models: {},
$$sequelize: undefined,
get sequelize() {
return this.$$sequelize;
},
set sequelize(sequelize) {
this.$$sequelize = sequelize;
this.transaction = sequelize.transaction.bind(sequelize);
},
transaction: () => {},
}
}
),
'@flecks/core.starting': (flecks) => {
flecks.db.Models = flecks.gather('@flecks/db/server.models', {typeProperty: 'name'});
},
@ -78,3 +60,20 @@ export const hooks = {
{after: '@flecks/docker/server'},
),
};
export const mixin = (Flecks) => class FlecksWithDb extends Flecks {
db = {
Models: {},
$$sequelize: undefined,
get sequelize() {
return this.$$sequelize;
},
set sequelize(sequelize) {
this.$$sequelize = sequelize;
this.transaction = sequelize.transaction.bind(sequelize);
},
transaction: () => {},
}
};

View File

@ -17,13 +17,6 @@ async function createWindow(flecks) {
}
export const hooks = {
'@flecks/core.mixin': (Flecks) => (
class FlecksWithElectron extends Flecks {
electron = electron.app ? electron : undefined;
}
),
'@flecks/electron/server.initialize': async (electron, flecks) => {
electron.app.on('window-all-closed', () => {
const {quitOnClosed} = flecks.get('@flecks/electron');
@ -84,3 +77,9 @@ export const hooks = {
{after: '@flecks/web/server', before: '@flecks/repl/server'},
),
};
export const mixin = (Flecks) => class FlecksWithElectron extends Flecks {
electron = electron.app ? electron : undefined;
};

View File

@ -24,13 +24,6 @@ export const hooks = {
ttl: 30,
},
}),
'@flecks/core.mixin': (Flecks) => (
class FlecksWithGovernor extends Flecks {
governor = {}
}
),
'@flecks/db/server.models': Flecks.provide(require.context('./models', false, /\.js$/)),
'@flecks/web/server.request.route': (flecks) => {
const {web} = flecks.get('@flecks/governor/server');
@ -135,3 +128,9 @@ export const hooks = {
)
),
};
export const mixin = (Flecks) => class FlecksWithGovernor extends Flecks {
governor = {}
};

View File

@ -19,13 +19,6 @@ export const hooks = {
*/
successRedirect: '/',
}),
'@flecks/core.mixin': (Flecks) => (
class FlecksWithSession extends Flecks {
passport;
}
),
'@flecks/db/server.models': Flecks.provide(require.context('./models')),
'@flecks/socket.packets.decorate': Flecks.decorate(require.context('./packets/decorators')),
'@flecks/web/server.request.route': Flecks.priority(
@ -134,3 +127,9 @@ export const hooks = {
return routes;
},
};
export const mixin = (Flecks) => class FlecksWithSession extends Flecks {
passport;
};

View File

@ -5,13 +5,6 @@ import configureStore, {createReducer} from '../store';
import localStorageEnhancer from './local-storage';
export const hooks = {
'@flecks/core.mixin': (Flecks) => (
class FlecksWithRedux extends Flecks {
redux;
}
),
'@flecks/react.providers': Flecks.priority(
async (req, flecks) => {
const slices = await flecks.invokeMergeUnique('@flecks/redux.slices');
@ -32,3 +25,9 @@ export const hooks = {
Flecks.decorate(require.context('./packets/decorators', false, /\.js$/))
),
};
export const mixin = (Flecks) => class FlecksWithRedux extends Flecks {
redux;
};

View File

@ -1,12 +1,11 @@
import {createReplServer} from './repl';
export const hooks = {
'@flecks/core.mixin': (Flecks) => (
class FlecksWithRepl extends Flecks {
repl;
}
),
'@flecks/server.up': (flecks) => createReplServer(flecks),
};
export const mixin = (Flecks) => class FlecksWithRepl extends Flecks {
repl;
};

View File

@ -16,13 +16,6 @@ export const hooks = {
'Set the FLECKS_ENV__flecks_session_server__cookieSecret environment variable!'
),
}),
'@flecks/core.mixin': (Flecks) => (
class FlecksWithSession extends Flecks {
session;
}
),
'@flecks/web/server.request.route': (flecks) => {
const urle = express.urlencoded({extended: true});
return (req, res, next) => {
@ -60,3 +53,9 @@ export const hooks = {
});
},
};
export const mixin = (Flecks) => class FlecksWithSession extends Flecks {
session;
};

View File

@ -1,19 +1,6 @@
import SocketClient from './socket';
export const hooks = {
'@flecks/core.mixin': (Flecks) => (
class FlecksWithSocket extends Flecks {
constructor(...args) {
super(...args);
if (!this.socket) {
this.socket = {};
}
this.socket.client = undefined;
}
}
),
'@flecks/web/client.up': (flecks) => {
const socket = new SocketClient(flecks);
flecks.socket.client = socket;
@ -27,3 +14,15 @@ export const hooks = {
path: `/${id}/socket.io`,
}),
};
export const mixin = (Flecks) => class FlecksWithSocket extends Flecks {
constructor(...args) {
super(...args);
if (!this.socket) {
this.socket = {};
}
this.socket.client = undefined;
}
};

View File

@ -8,19 +8,6 @@ export * from './hooks';
export {Packet, Packer, ValidationError} from './packet';
export const hooks = {
'@flecks/core.mixin': (Flecks) => (
class FlecksWithSocket extends Flecks {
constructor(...args) {
super(...args);
if (!this.socket) {
this.socket = {};
}
this.socket.Packets = {};
}
}
),
'@flecks/core.starting': (flecks) => {
flecks.socket.Packets = flecks.gather('@flecks/socket.packets', {check: badPacketsCheck});
},
@ -40,3 +27,15 @@ export const hooks = {
Refresh,
}),
};
export const mixin = (Flecks) => class FlecksWithSocket extends Flecks {
constructor(...args) {
super(...args);
if (!this.socket) {
this.socket = {};
}
this.socket.Packets = {};
}
};

View File

@ -14,17 +14,16 @@ export const hooks = {
'@flecks/socket.server': ({config: {'@flecks/core': {id}}}) => ({
path: `/${id}/socket.io`,
}),
'@flecks/core.mixin': (Flecks) => (
class FlecksWithSocketServer extends Flecks {
constructor(...args) {
super(...args);
if (!this.socket) {
this.socket = {};
}
this.socket.server = undefined;
}
}
),
};
export const mixin = (Flecks) => class FlecksWithSocketServer extends Flecks {
constructor(...args) {
super(...args);
if (!this.socket) {
this.socket = {};
}
this.socket.server = undefined;
}
};

View File

@ -9,18 +9,6 @@ const debug = D('@flecks/web/server');
export {configSource};
export const hooks = {
'@flecks/core.mixin': (Flecks) => (
class FlecksWithWeb extends Flecks {
constructor(...args) {
super(...args);
if (!this.web) {
this.web = {flecks: undefined, server: undefined};
}
}
}
),
'@flecks/web.config': async ({onlyAllow}, flecks) => ({
'@flecks/web': onlyAllow(flecks.get('@flecks/web'), ['appMountId', 'title']),
}),
@ -45,3 +33,14 @@ export const hooks = {
},
'@flecks/server.up': (flecks) => createHttpServer(flecks),
};
export const mixin = (Flecks) => class FlecksWithWeb extends Flecks {
constructor(...args) {
super(...args);
if (!this.web) {
this.web = {flecks: undefined, server: undefined};
}
}
};