Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

feat: Add callback on method Channel.sendCommand #29

Merged
merged 1 commit into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions src/channel/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import {OCast} from "../ocast";
import {Transport} from "../protocol/transport";
import {TransportMessage} from "../protocol/transport.message";
import {EnumError} from "../type/enum.error";
Expand All @@ -32,6 +31,7 @@ const Log: Logger = Logger.getInstance();
*/
export class Channel {
protected static sequenceMessage: number = 1;
private waitingReplies: {[key: string]: (transport: Transport) => void} = {};

private ws: WebSocket = null;

Expand All @@ -52,6 +52,14 @@ export class Channel {
* @param {Transport} transport - message received
*/
public onMessage(transport: Transport) {
if (transport.type === EnumTransport.REPLY) {
const id = transport.id;
if (this.waitingReplies[id]) {
this.waitingReplies[id](transport.message.data);
delete this.waitingReplies[id];
}
return;
}
Log.warn(TAG + "onMessage need to be implemented for namespace " + this.name);
this.sendReply(transport.id, transport.src, {params: {code: EnumError.NO_IMPLEMENTATION}});
}
Expand Down Expand Up @@ -89,7 +97,10 @@ export class Channel {
const message = new Transport(UUID, dst, EnumTransport.COMMAND, Channel.sequenceMessage++,
new TransportMessage(this.name, data));
Log.debug(TAG + "sendCommand : " + JSON.stringify(message));
this.sendMessage(message);
return new Promise((resolve, reject) => {
this.waitingReplies[message.id] = resolve;
this.sendMessage(message);
});
}

/**
Expand Down
35 changes: 18 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
*/
import "./channel/media.notifier";

export {EnumError} from "./type/enum.error";
export {EnumMediaStatus} from "./type/enum.media.status";
export {EnumMedia} from "./type/enum.media";
export {EnumTrack} from "./type/enum.track";
export {EnumTransferMode} from "./type/enum.transfermode";
export {Channel} from "./channel/channel";
export {MediaChannel} from "./channel/media.channel";
export {WebappChannel} from "./channel/webapp.channel";
export {Media} from "./media/media";
export {IMediaNotifier} from "./channel/media.notifier";
export {VideoMedia} from "./media/video.media";
export {ImageMedia} from "./media/image.media";
export {PlaybackStatus} from "./protocol/playback.status";
export {Metadata} from "./protocol/metadata";
export {VideoPlaybackStatus} from "./media/video.playback.status";
export {Logger} from "./util/logger";
export {OCast} from "./ocast";
export { EnumError } from "./type/enum.error";
export { EnumMediaStatus } from "./type/enum.media.status";
export { EnumMedia } from "./type/enum.media";
export { EnumTrack } from "./type/enum.track";
export { EnumTransferMode } from "./type/enum.transfermode";
export { EnumTransport } from "./type/enum.transport";
export { Channel } from "./channel/channel";
export { MediaChannel } from "./channel/media.channel";
export { WebappChannel } from "./channel/webapp.channel";
export { Media } from "./media/media";
export { IMediaNotifier } from "./channel/media.notifier";
export { VideoMedia } from "./media/video.media";
export { ImageMedia } from "./media/image.media";
export { PlaybackStatus } from "./protocol/playback.status";
export { Metadata } from "./protocol/metadata";
export { VideoPlaybackStatus } from "./media/video.playback.status";
export { Logger } from "./util/logger";
export { OCast } from "./ocast";
export { Transport } from "./protocol/transport";
7 changes: 7 additions & 0 deletions src/ocast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ export class OCast {
public getChannel(service: string): Channel {
return this.channels[service] as Channel;
}
/**
* Initialize MediaChannel
* @private
*/
public setupChannel(channel: Channel): void {
this.channels[channel.name] = channel;
}
/**
* Initialize MediaChannel
* @private
Expand Down