fix(messaging): unregisterRedundantToken
When redundant token is found don't clone context object
MODIFY
packages/core/src/context/context.helper.ts
packages/core/src/context/context.helper.ts
import * as T from 'fp-ts/lib/Task';
import * as O from 'fp-ts/lib/Option';
+import * as IO from 'fp-ts/lib/IO';
import { pipe } from 'fp-ts/lib/pipeable';
import { constant } from 'fp-ts/lib/function';
import { logger, LoggerToken, mockLogger } from '../logger';
import { isTestEnv } from '../+internal/utils';
-import { registerAll, BoundDependency, createContext, bindTo, resolve, Context, lookup, DerivedContextToken, unregister } from './context';
+import { registerAll, BoundDependency, createContext, bindTo, resolve, Context, lookup, DerivedContextToken } from './context';
import { ContextToken } from './context.token.factory';
/**
* `INTERNAL` - unregisters redundant token if available in DerivedContext
* @since v3.4.0
*/
-const unregisterRedundantToken = (token: ContextToken) => (context: Context): Context =>
- pipe(
+const unregisterRedundantToken = (token: ContextToken) => (context: Context): IO.IO<Context> => {
+ const deleteToken = pipe(
+ () => context.delete(token),
+ IO.map(constant(context)),
+ );
+
+ return pipe(
lookup(context)(DerivedContextToken),
O.chain(derivedContext => lookup(derivedContext)(token)),
- O.fold(
- constant(context),
- () => unregister(token)(context)),
+ O.fold(() => IO.of(context), () => deleteToken),
);
+}
/**
* Constructs and resolves a new or derived context based on provided dependencies
@@ -32,7 +37,7 @@ export const constructContext = (context?: Context) => (...dependencies: BoundDe
...dependencies,
]),
context => () => resolve(context),
- T.map(unregisterRedundantToken(LoggerToken)),
+ T.chain(context => T.fromIO(unregisterRedundantToken(LoggerToken)(context))),
)();
/**

MODIFY
packages/core/src/context/context.ts
packages/core/src/context/context.ts
@@ -41,11 +41,11 @@ export interface BoundDependency<T, U extends ContextDependency = ContextDepende
export const DerivedContextToken = createContextToken<Context>('DerivedContext');
const isEagerDependency = (x: any): x is ContextEagerReader => {
- return x.eval && x.tag === ContextReaderTag.EAGER_READER;
+ return Boolean(x.eval && x.tag === ContextReaderTag.EAGER_READER);
};
const isLazyDependency = (x: any): x is ContextLazyReader => {
- return x.eval && x.tag === ContextReaderTag.LAZY_READER;
+ return Boolean(x.eval && x.tag === ContextReaderTag.LAZY_READER);
};
export const createContext = () => M.empty;

fix(messaging): unregisterRedundantToken

When redundant token is found don't clone context object