Skip to content

Commit cf7b036

Browse files
committed
chore(examples/fdc3-trade-simulator): Add null check for window.fdc3 in js-trader-app
1 parent 6565e05 commit cf7b036

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

examples/fdc3-trade-simulator/js-trader-app/src/app/components/trade-idea-generator/trade-idea-generator.component.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -167,32 +167,33 @@ export class TradeIdeaGeneratorComponent implements OnDestroy {
167167
};
168168

169169
try {
170-
if (!this.channel) {
171-
this.channel = await window.fdc3.getOrCreateChannel('tradeIdeasChannel');
170+
if(window.fdc3) {
171+
if (!this.channel) {
172+
this.channel = await window.fdc3.getOrCreateChannel('tradeIdeasChannel');
173+
}
174+
175+
const topic: string = "fdc3." + this.symbols.value as string + "." + this.trader!;
176+
177+
if (!this.listeners.get(topic)) {
178+
const listener = await this.channel.addContextListener(topic, (context, metadata) => {
179+
if (context['result'].success === false){
180+
this.feedbackSubject.next(context['result'].error as string);
181+
return;
182+
}
183+
184+
const result = context['result'];
185+
if (result.action as string == "BUY") {
186+
const price: number = result.tradePrice as number;
187+
this.feedbackSubject.next(this.trader + " has bought " + this.currentValue + " symbol of: " + this.symbols.value as string + " for $" + price + ".");
188+
} else {
189+
this.feedbackSubject.next(this.trader + " has indicated that " + this.currentValue + " symbol of: " + this.symbols.value as string + " is/are available for buying.");
190+
}
191+
});
192+
this.listeners.set(topic, listener);
193+
}
194+
195+
await this.channel.broadcast(context);
172196
}
173-
174-
const topic: string = "fdc3." + this.symbols.value as string + "." + this.trader!;
175-
176-
if (!this.listeners.get(topic)) {
177-
const listener = await this.channel.addContextListener(topic, (context, metadata) => {
178-
if (context['result'].success === false){
179-
this.feedbackSubject.next(context['result'].error as string);
180-
return;
181-
}
182-
183-
const result = context['result'];
184-
if (result.action as string == "BUY") {
185-
const price: number = result.tradePrice as number;
186-
this.feedbackSubject.next(this.trader + " has bought " + this.currentValue + " symbol of: " + this.symbols.value as string + " for $" + price + ".");
187-
} else {
188-
this.feedbackSubject.next(this.trader + " has indicated that " + this.currentValue + " symbol of: " + this.symbols.value as string + " is/are available for buying.");
189-
}
190-
});
191-
this.listeners.set(topic, listener);
192-
}
193-
194-
await this.channel.broadcast(context);
195-
196197
} catch (error) {
197198
this.feedbackSubject.next('Failed to broadcast trade idea.');
198199
console.error('Error is thrown while broadcasting trade idea:', error);

0 commit comments

Comments
 (0)