'use strict';

const { reduceAsync, identity } = require('../utilities');
const { rpcHandlers } = require('../rpc');

// Returns a reducer function that takes the schema as input and output,
// and iterate over rpc-specific reduce functions
const getRpcReducer = function (name, postProcess) {
  const processors = getProcessors({ name });
  return rpcReducer.bind(null, { processors, postProcess });
};

const getProcessors = function ({ name }) {
  return Object.values(rpcHandlers)
    .map(rpcHandler => rpcHandler[name])
    .filter(handler => handler);
};

const rpcReducer = function (
  { processors, postProcess = identity },
  { schema },
) {
  return reduceAsync(
    processors,
    (schemaA, func) => func(schemaA),
    schema,
    (schemaA, schemaB) => postProcess({ ...schemaA, ...schemaB }),
  );
};

// Apply rpc-specific compile-time logic
const rpcSchema = getRpcReducer('compileSchema');

// Apply rpc-specific startup logic
const rpcStartServer = getRpcReducer('startServer', schema => ({ schema }));

module.exports = {
  rpcSchema,
  rpcStartServer,
};

@@ -120,18 +120,8 @@
 / Re
-turns a re
 duce
@@ -234,357 +234,40 @@
 nst 
-getRpcReducer = function (name, postProcess) %7B%0A  const processors = getProcessors(%7B name %7D);%0A  return rpcReducer.bind(null, %7B processors, postProcess %7D);%0A%7D;%0A%0Aconst getProcessors = function (%7B name %7D) %7B%0A  return Object.values(rpcHandlers)%0A    .map(rpcHandler =%3E rpcHandler%5Bname%5D)%0A    .filter(handler =%3E handler);%0A%7D;%0A%0Aconst rpcReducer = function (%0A  %7B
+rpcReducer = function (%7B schema,
  pro
@@ -303,24 +303,8 @@
 ty %7D
-,%0A  %7B schema %7D,%0A
 ) %7B%0A
@@ -526,30 +526,83 @@
 chema = 
-getRpcReducer(
+function (%7B schema %7D) %7B%0A  const processors = getProcessors(%7B name: 
 'compile
@@ -608,18 +608,68 @@
 eSchema'
+ %7D
 );
+%0A  return rpcReducer(%7B schema, processors %7D);%0A%7D;
 %0A%0A// App
@@ -725,24 +725,346 @@
 r = 
-getRpcReducer('s
+function (%7B schema %7D) %7B%0A  const processors = getProcessors(%7B name: 'startServer' %7D);%0A  return rpcReducer(%7B schema, processors, postProcess: rpcStartServerProcess %7D);%0A%7D;%0A%0Aconst getProcessors = function (%7B name %7D) %7B%0A  return Object.values(rpcHandlers)%0A    .map(rpcHandler =%3E rpcHandler%5Bname%5D)%0A    .filter(handler =%3E handler);%0A%7D;%0A%0Aconst rpcS
 tart
@@ -1073,10 +1073,17 @@
 rver
-',
+Process =
  sch
@@ -1101,17 +1101,16 @@
 chema %7D)
-)
 ;%0A%0Amodul

Fix function names being lost in binding