mirror of
https://github.com/wassname/talk.git
synced 2026-07-11 22:01:26 +08:00
Merge branch 'rte-rework' of ssh://github.com/coralproject/talk into rte-rework
This commit is contained in:
+32
-4
@@ -42,6 +42,32 @@ const decorateContextPlugins = (context, contextPlugins) => {
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Some pieces of the Context are quite complex to setup, using multiple merges
|
||||
* and other lodash functions. This proxies that access such that it is only
|
||||
* loaded if it is used. Helpful for a query that only uses a loader, and not a
|
||||
* mutator.
|
||||
*
|
||||
* @param {Object} ctx the graph proxy
|
||||
* @param {Function} loader the loadable component that should be proxied
|
||||
*/
|
||||
const createLazyContextLoader = (ctx, loader) =>
|
||||
new Proxy(
|
||||
{ loaded: false, data: null },
|
||||
{
|
||||
get: (obj, prop) => {
|
||||
if (obj.loaded) {
|
||||
return obj.data[prop];
|
||||
}
|
||||
|
||||
obj.data = loader(ctx);
|
||||
obj.loaded = true;
|
||||
|
||||
return obj.data[prop];
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Stores the request context.
|
||||
*/
|
||||
@@ -61,16 +87,18 @@ class Context {
|
||||
this.connectors = connectors;
|
||||
|
||||
// Create the loaders.
|
||||
this.loaders = loaders(this);
|
||||
this.loaders = createLazyContextLoader(this, loaders);
|
||||
|
||||
// Create the mutators.
|
||||
this.mutators = mutators(this);
|
||||
this.mutators = createLazyContextLoader(this, mutators);
|
||||
|
||||
// Decorate the plugin context.
|
||||
this.plugins = decorateContextPlugins(this, contextPlugins);
|
||||
this.plugins = createLazyContextLoader(this, () =>
|
||||
decorateContextPlugins(this, contextPlugins)
|
||||
);
|
||||
|
||||
// Bind the publish/subscribe to the context.
|
||||
this.pubsub = getBroker();
|
||||
this.pubsub = createLazyContextLoader(this, () => getBroker());
|
||||
|
||||
// Bind the parent context.
|
||||
this.parent = ctx;
|
||||
|
||||
+10
-10
@@ -1,18 +1,18 @@
|
||||
const { version } = require('../package.json');
|
||||
const Logger = require('bunyan');
|
||||
const { LOGGING_LEVEL, REVISION_HASH } = require('../config');
|
||||
const logger = new Logger({
|
||||
src: true,
|
||||
name: 'talk',
|
||||
version,
|
||||
revision: REVISION_HASH,
|
||||
level: LOGGING_LEVEL,
|
||||
serializers: Logger.stdSerializers,
|
||||
});
|
||||
|
||||
// Create the logging instance that all logger's are branched from.
|
||||
function createLogger(name, traceID) {
|
||||
return new Logger({
|
||||
src: true,
|
||||
name,
|
||||
traceID,
|
||||
version,
|
||||
revision: REVISION_HASH,
|
||||
level: LOGGING_LEVEL,
|
||||
serializers: Logger.stdSerializers,
|
||||
});
|
||||
return logger.child({ origin: name, traceID });
|
||||
}
|
||||
|
||||
module.exports = { createLogger };
|
||||
module.exports = { logger, createLogger };
|
||||
|
||||
Reference in New Issue
Block a user