refactor: invokeMerge

This commit is contained in:
cha0s 2022-03-08 14:50:16 -06:00
parent 46a52b5d28
commit 218145bcc9
12 changed files with 28 additions and 25 deletions

View File

@ -21,5 +21,5 @@
- [ ] static documentation site generator
- [ ] autogenerated config dox page
- [x] remove `invokeParallel()`
- [ ] Specialize `invokeReduce()` with `invokeMerge()`.
- [x] Specialize `invokeReduce()` with `invokeMerge()`.
- [ ] Rename all hooks to dot-first notation; rewrite `lookupFlecks()`.

View File

@ -87,7 +87,7 @@ else {
const flecks = Flecks.bootstrap();
debug('bootstrapped');
// Register commands.
const commands = flecks.invokeReduce('@flecks/core/commands', undefined, undefined, program);
const commands = flecks.invokeMerge('@flecks/core/commands', program);
const keys = Object.keys(commands).sort();
for (let i = 0; i < keys.length; ++i) {
const {

View File

@ -172,7 +172,7 @@ export default class Flecks {
if (!hook || 'string' !== typeof hook) {
throw new TypeError('Flecks.gather(): Expects parameter 1 (hook) to be string');
}
const raw = this.invokeReduce(hook);
const raw = this.invokeMerge(hook);
check(raw, hook);
const decorated = this.invokeComposed(`${hook}.decorate`, raw);
check(decorated, `${hook}.decorate`);
@ -261,7 +261,15 @@ export default class Flecks {
return candidate.fn(...(args.concat(this)));
}
invokeReduce(hook, initial = {}, reducer = (r, o) => ({...r, ...o}), ...args) {
invokeMerge(hook, ...args) {
return this.invokeReduce(hook, (r, o) => ({...r, ...o}), {}, ...args);
}
async invokeMergeAsync(hook, ...args) {
return this.invokeReduceAsync(hook, (r, o) => ({...r, ...o}), {}, ...args);
}
invokeReduce(hook, reducer, initial, ...args) {
if (!this.hooks[hook]) {
return initial;
}
@ -269,7 +277,7 @@ export default class Flecks {
.reduce((r, {fleck}) => reducer(r, this.invokeFleck(hook, fleck, ...args)), initial);
}
async invokeReduceAsync(hook, initial = {}, reducer = (r, o) => ({...r, ...o}), ...args) {
async invokeReduceAsync(hook, reducer, initial, ...args) {
if (!this.hooks[hook]) {
return initial;
}

View File

@ -25,12 +25,12 @@ it('can invoke', () => {
});
});
it('can invoke reduced', () => {
expect(flecks.invokeReduce('@flecks/core/test/invoke-reduce'))
it('can invoke merge', () => {
expect(flecks.invokeMerge('@flecks/core/test/invoke-merge'))
.to.deep.equal({foo: 69, bar: 420});
});
it('can invoke reduced async', async () => {
expect(await flecks.invokeReduce('@flecks/core/test/invoke-reduce'))
it('can invoke merge async', async () => {
expect(await flecks.invokeMergeAsync('@flecks/core/test/invoke-merge-async'))
.to.deep.equal({foo: 69, bar: 420});
});

View File

@ -25,7 +25,7 @@ export default {
// eslint-disable-next-line no-param-reassign
O.foo *= 2;
},
'@flecks/core/test/invoke-reduce': () => ({foo: 69}),
'@flecks/core/test/invoke-reduce-async': () => new Promise((resolve) => resolve({foo: 69})),
'@flecks/core/test/invoke-merge': () => ({foo: 69}),
'@flecks/core/test/invoke-merge-async': () => new Promise((resolve) => resolve({foo: 69})),
},
};

View File

@ -14,7 +14,7 @@ export default {
resolve();
}, 0);
}),
'@flecks/core/test/invoke-reduce': () => ({bar: 420}),
'@flecks/core/test/invoke-reduce-async': () => new Promise((resolve) => resolve({bar: 420})),
'@flecks/core/test/invoke-merge': () => ({bar: 420}),
'@flecks/core/test/invoke-merge-async': () => new Promise((resolve) => resolve({bar: 420})),
},
};

View File

@ -13,7 +13,7 @@ export default {
if (!flecks.get('@flecks/docker/server.enabled')) {
return;
}
const containers = await flecks.invokeReduceAsync('@flecks/docker/containers');
const containers = await flecks.invokeMergeAsync('@flecks/docker/containers');
await Promise.all(
Object.entries(containers)
.map(([key, config]) => startContainer(flecks, key, config)),

View File

@ -95,7 +95,7 @@ const FlecksInvocations = (state, filename) => ({
if (isStringLiteral(path.node.arguments[0])) {
state.addInvocation(
path.node.arguments[0].value,
'invokeReduce',
'invokeMerge',
filename,
path.node.loc,
);

View File

@ -1,12 +1,7 @@
import {Transform} from 'stream';
const config = async (flecks, req) => {
const httpConfig = await flecks.invokeReduceAsync(
'@flecks/http/config',
undefined,
undefined,
req,
);
const httpConfig = await flecks.invokeMergeAsync('@flecks/http/config', req);
const config = {};
const {resolver} = flecks.get('$flecks/http.flecks');
const keys = Object.keys(resolver);

View File

@ -30,7 +30,7 @@ export default class SocketClient extends decorate(Socket) {
{
reconnectionDelay: 'production' === process.env.NODE_ENV ? 1000 : 100,
reconnectionDelayMax: 'production' === process.env.NODE_ENV ? 5000 : 500,
...this.flecks.invokeReduce('@flecks/socket/client'),
...this.flecks.invokeMerge('@flecks/socket/client'),
},
);
this.socket.emitPromise = promisify(this.socket.emit.bind(this.socket));

View File

@ -14,7 +14,7 @@ export default class SocketServer {
this.onConnect = this.onConnect.bind(this);
this.flecks = flecks;
this.httpServer = httpServer;
const hooks = flecks.invokeReduce('@flecks/socket/intercom');
const hooks = flecks.invokeMerge('@flecks/socket/intercom');
debug('intercom hooks(%O)', hooks);
this.localIntercom = async ({payload, type}, fn) => {
debug('customHook: %s(%o)', type, payload);
@ -31,7 +31,7 @@ export default class SocketServer {
async connect() {
this.io = SocketIoServer(this.httpServer, {
...await this.flecks.invokeReduceAsync('@flecks/socket/server'),
...await this.flecks.invokeMergeAsync('@flecks/socket/server'),
serveClient: false,
});
this.flecks.set('$flecks/socket.io', this.io);

View File

@ -38,7 +38,7 @@ export default {
sameSite: true,
saveUninitialized: false,
secret: flecks.get('@flecks/user/session/server.cookieSecret'),
...await flecks.invokeReduceAsync('@flecks/user/session'),
...await flecks.invokeMergeAsync('@flecks/user/session'),
}));
},
'@flecks/socket/server/request.socket': (flecks) => (socket, next) => {