Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7fed8fe

Browse files
Akos Kittakittaakos
Akos Kitta
authored andcommitted
Let DI framework create MonitorService instances
Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
1 parent f4a68e7 commit 7fed8fe

File tree

5 files changed

+53
-61
lines changed

5 files changed

+53
-61
lines changed

‎arduino-ide-extension/src/node/arduino-ide-backend-module.ts‎

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -236,26 +236,14 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
236236
bind(MonitorServiceFactory).toFactory(
237237
({ container }) =>
238238
(options: MonitorServiceFactoryOptions) => {
239-
const logger = container.get<ILogger>(ILogger);
240-
241-
const monitorSettingsProvider = container.get<MonitorSettingsProvider>(
242-
MonitorSettingsProvider
243-
);
244-
245-
const webSocketProvider =
246-
container.get<WebSocketProvider>(WebSocketProvider);
247-
248-
const { board, port, coreClientProvider, monitorID } = options;
249-
250-
return new MonitorService(
251-
logger,
252-
monitorSettingsProvider,
253-
webSocketProvider,
254-
board,
255-
port,
256-
coreClientProvider,
257-
monitorID
258-
);
239+
const child = container.createChild();
240+
child
241+
.bind<MonitorServiceFactoryOptions>(MonitorServiceFactoryOptions)
242+
.toConstantValue({
243+
...options,
244+
});
245+
child.bind(MonitorService).toSelf();
246+
return child.get<MonitorService>(MonitorService);
259247
}
260248
);
261249

‎arduino-ide-extension/src/node/core-client-provider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ export namespace CoreClientProvider {
397397
@injectable()
398398
export abstract class CoreClientAware {
399399
@inject(CoreClientProvider)
400-
protected readonly coreClientProvider: CoreClientProvider;// TODO: should be `private`, fix injection in subclasses. (https://github.com/arduino/arduino-ide/issues/1161)
400+
private readonly coreClientProvider: CoreClientProvider;
401401
/**
402402
* Returns with a promise that resolves when the core client is initialized and ready.
403403
*/

‎arduino-ide-extension/src/node/monitor-manager.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ export class MonitorManager extends CoreClientAware {
317317
board,
318318
port,
319319
monitorID,
320-
coreClientProvider: this.coreClientProvider,
321320
});
322321
this.monitorServices.set(monitorID, monitor);
323322
monitor.onDispose(
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
import { Board, Port } from '../common/protocol';
2-
import { CoreClientProvider } from './core-client-provider';
3-
import { MonitorService } from './monitor-service';
1+
import type { Board, Port } from '../common/protocol';
2+
import type { MonitorService } from './monitor-service';
43

54
export const MonitorServiceFactory = Symbol('MonitorServiceFactory');
65
export interface MonitorServiceFactory {
7-
(options: {
8-
board: Board;
9-
port: Port;
10-
monitorID: string;
11-
coreClientProvider: CoreClientProvider;
12-
}): MonitorService;
6+
(options: MonitorServiceFactoryOptions): MonitorService;
137
}
148

9+
export const MonitorServiceFactoryOptions = Symbol(
10+
'MonitorServiceFactoryOptions'
11+
);
1512
export interface MonitorServiceFactoryOptions {
1613
board: Board;
1714
port: Port;
1815
monitorID: string;
19-
coreClientProvider: CoreClientProvider;
2016
}

‎arduino-ide-extension/src/node/monitor-service.ts‎

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ClientDuplexStream } from '@grpc/grpc-js';
22
import { Disposable, Emitter, ILogger } from '@theia/core';
3-
import { inject, named } from '@theia/core/shared/inversify';
3+
import { inject, named,postConstruct } from '@theia/core/shared/inversify';
44
import { Board, Port, Status, Monitor } from '../common/protocol';
55
import {
66
EnumerateMonitorPortSettingsRequest,
@@ -10,7 +10,7 @@ import {
1010
MonitorRequest,
1111
MonitorResponse,
1212
} from './cli-protocol/cc/arduino/cli/commands/v1/monitor_pb';
13-
import { CoreClientAware,CoreClientProvider } from './core-client-provider';
13+
import { CoreClientAware } from './core-client-provider';
1414
import { WebSocketProvider } from './web-socket/web-socket-provider';
1515
import { Port as gRPCPort } from 'arduino-ide-extension/src/node/cli-protocol/cc/arduino/cli/commands/v1/port_pb';
1616
import {
@@ -19,6 +19,7 @@ import {
1919
MonitorSettingsProvider,
2020
} from './monitor-settings/monitor-settings-provider';
2121
import { Deferred } from '@theia/core/lib/common/promise-util';
22+
import { MonitorServiceFactoryOptions } from './monitor-service-factory';
2223

2324
export const MonitorServiceName = 'monitor-service';
2425
type DuplexHandlerKeys =
@@ -33,55 +34,63 @@ interface DuplexHandler {
3334
callback: (...args: any) => void;
3435
}
3536

37+
const MAX_WRITE_TO_STREAM_TRIES = 10;
38+
const WRITE_TO_STREAM_TIMEOUT_MS = 30000;
39+
3640
export class MonitorService extends CoreClientAware implements Disposable {
41+
@inject(ILogger)
42+
@named(MonitorServiceName)
43+
private readonly logger: ILogger;
44+
45+
@inject(MonitorSettingsProvider)
46+
private readonly monitorSettingsProvider: MonitorSettingsProvider;
47+
48+
@inject(WebSocketProvider)
49+
private readonly webSocketProvider: WebSocketProvider;
50+
3751
// Bidirectional gRPC stream used to receive and send data from the running
3852
// pluggable monitor managed by the Arduino CLI.
39-
protected duplex: ClientDuplexStream<MonitorRequest, MonitorResponse> | null;
53+
private duplex: ClientDuplexStream<MonitorRequest, MonitorResponse> | null;
4054

4155
// Settings used by the currently running pluggable monitor.
4256
// They can be freely modified while running.
43-
protected settings: MonitorSettings = {};
57+
private settings: MonitorSettings = {};
4458

4559
// List of messages received from the running pluggable monitor.
4660
// These are flushed from time to time to the frontend.
47-
protected messages: string[] = [];
61+
private messages: string[] = [];
4862

4963
// Handles messages received from the frontend via websocket.
50-
protected onMessageReceived?: Disposable;
64+
private onMessageReceived?: Disposable;
5165

5266
// Sends messages to the frontend from time to time.
53-
protected flushMessagesInterval?: NodeJS.Timeout;
67+
private flushMessagesInterval?: NodeJS.Timeout;
5468

5569
// Triggered each time the number of clients connected
5670
// to the this service WebSocket changes.
57-
protected onWSClientsNumberChanged?: Disposable;
71+
private onWSClientsNumberChanged?: Disposable;
5872

5973
// Used to notify that the monitor is being disposed
60-
protected readonly onDisposeEmitter = new Emitter<void>();
74+
private readonly onDisposeEmitter = new Emitter<void>();
6175
readonly onDispose = this.onDisposeEmitter.event;
6276

63-
protected _initialized = new Deferred<void>();
64-
protected creating: Deferred<Status>;
65-
66-
MAX_WRITE_TO_STREAM_TRIES=10;
67-
WRITE_TO_STREAM_TIMEOUT_MS=30000;
77+
private _initialized = new Deferred<void>();
78+
private creating: Deferred<Status>;
79+
privatereadonlyboard: Board;
80+
privatereadonlyport: Port;
81+
privatereadonlymonitorID: string;
6882

6983
constructor(
70-
@inject(ILogger)
71-
@named(MonitorServiceName)
72-
protected readonly logger: ILogger,
73-
@inject(MonitorSettingsProvider)
74-
protected readonly monitorSettingsProvider: MonitorSettingsProvider,
75-
@inject(WebSocketProvider)
76-
protected readonly webSocketProvider: WebSocketProvider,
77-
78-
private readonly board: Board,
79-
private readonly port: Port,
80-
protected override readonly coreClientProvider: CoreClientProvider,
81-
private readonly monitorID: string
84+
@inject(MonitorServiceFactoryOptions) options: MonitorServiceFactoryOptions
8285
) {
8386
super();
87+
this.board = options.board;
88+
this.port = options.port;
89+
this.monitorID = options.monitorID;
90+
}
8491

92+
@postConstruct()
93+
protected init(): void {
8594
this.onWSClientsNumberChanged =
8695
this.webSocketProvider.onClientsNumberChanged(async (clients: number) => {
8796
if (clients === 0) {
@@ -94,7 +103,7 @@ export class MonitorService extends CoreClientAware implements Disposable {
94103
this.updateClientsSettings(this.settings);
95104
});
96105

97-
this.portMonitorSettings(port.protocol, board.fqbn!).then(
106+
this.portMonitorSettings(this.port.protocol, this.board.fqbn!).then(
98107
async (settings) => {
99108
this.settings = {
100109
...this.settings,
@@ -258,8 +267,8 @@ export class MonitorService extends CoreClientAware implements Disposable {
258267
}
259268

260269
pollWriteToStream(request: MonitorRequest): Promise<boolean> {
261-
let attemptsRemaining = this.MAX_WRITE_TO_STREAM_TRIES;
262-
const writeTimeoutMs = this.WRITE_TO_STREAM_TIMEOUT_MS;
270+
let attemptsRemaining = MAX_WRITE_TO_STREAM_TRIES;
271+
const writeTimeoutMs = WRITE_TO_STREAM_TIMEOUT_MS;
263272

264273
const createWriteToStreamExecutor =
265274
(duplex: ClientDuplexStream<MonitorRequest, MonitorResponse>) =>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /