Skip to content

Commit b711b2b

Browse files
committed
cleanup
1 parent 504616d commit b711b2b

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

examples/channel-name/index.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,26 @@ broker.createService({
150150

151151
await Promise.delay(100);
152152

153-
await ctx.call("test.demo.level.5", null, { parentCtx: ctx });
153+
const headers = this.broker.channelAdapter.parseMessageHeaders(raw);
154+
155+
await broker.sendToChannel("my.topic.level.5", ctx.params, {
156+
ctx,
157+
headers
158+
});
159+
}
160+
},
161+
162+
"my.topic.level.5": {
163+
async handler(ctx, raw) {
164+
const parentChannelName = ctx.parentChannelName;
165+
const level = ctx.level;
166+
const caller = ctx.caller;
167+
const msg = `Flow level: ${level}, Type: Channel, Name: ${ctx.currentChannelName}, Caller: ${caller}, Parent channel: ${parentChannelName}`;
168+
this.logger.info(msg);
169+
170+
await Promise.delay(100);
171+
172+
await ctx.call("test.demo.level.6", null, { parentCtx: ctx });
154173
}
155174
}
156175
}
@@ -159,9 +178,9 @@ broker.createService({
159178
broker.createService({
160179
name: "test",
161180
actions: {
162-
"demo.level.5": {
181+
"demo.level.6": {
163182
async handler(ctx) {
164-
const channelName = ctx?.options?.parentCtx?.currentChannelName;
183+
const channelName = ctx?.options?.parentCtx?.channelName;
165184
const level = ctx.level;
166185
const caller = ctx.caller;
167186
const msg = `Flow level: ${level}, Type: Action, Name: ${ctx.action.name}, Caller: ${caller}, Channel name: ${channelName}`;

src/index.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,8 @@ module.exports = function ChannelsMiddleware(mwOpts) {
193193
opts.headers.$caller = opts.ctx.service.fullName;
194194
}
195195

196-
if (opts.ctx.currentChannelName) {
197-
opts.headers.$parentChannelName = opts.ctx.currentChannelName;
198-
}
199-
if (opts.ctx.currentServiceName) {
200-
opts.headers.$parentServiceName = opts.ctx.currentServiceName;
196+
if (opts.ctx.channelName) {
197+
opts.headers.$parentChannelName = opts.ctx.channelName;
201198
}
202199

203200
// Serialize meta and headers
@@ -295,12 +292,7 @@ module.exports = function ChannelsMiddleware(mwOpts) {
295292
// Wrap the handler with context creating
296293
if (chan.context) {
297294
wrappedHandler = (msg, raw) => {
298-
let parentCtx,
299-
caller,
300-
meta,
301-
ctxHeaders,
302-
parentChannelName,
303-
parentServiceName;
295+
let parentCtx, caller, meta, ctxHeaders, parentChannelName;
304296
const headers = adapter.parseMessageHeaders(raw);
305297
if (headers) {
306298
if (headers.$requestID) {
@@ -312,7 +304,6 @@ module.exports = function ChannelsMiddleware(mwOpts) {
312304
};
313305
caller = headers.$caller;
314306
parentChannelName = headers.$parentChannelName;
315-
parentServiceName = headers.$parentServiceName;
316307
}
317308

318309
if (headers.$meta) {
@@ -330,20 +321,15 @@ module.exports = function ChannelsMiddleware(mwOpts) {
330321

331322
const ctx = Context.create(broker, null, msg, {
332323
parentCtx,
333-
caller: headers?.$parentServiceName
334-
? headers.$parentServiceName
335-
: caller,
324+
caller,
336325
meta,
337326
headers: ctxHeaders
338327
});
339328

340-
// attach channelName to context so in the handler we can use it
341-
ctx.currentChannelName = chan.name;
342-
ctx.currentServiceName = svc.fullName;
343-
// It's the parent channel name that triggered this event
329+
ctx.channelName = chan.name;
344330
ctx.parentChannelName = parentChannelName;
345-
ctx.parentServiceName = parentServiceName;
346331

332+
// Attach current service that has the channel handler to the context
347333
ctx.service = svc;
348334

349335
return handler2(ctx, raw);

0 commit comments

Comments
 (0)