Skip to content

Commit

Permalink
refactor(Lime): refactor modules to use default exports for main classes
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurxavierx committed Dec 27, 2016
1 parent a11fdb0 commit 8bc1433
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 55 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "lime-js",
"version": "2.3.1",
"description": "JavaScript LIME implementation",
"main": "dist/lime.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion src/Lime/Guid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function Guid() {
export default function Guid() {
let d = new Date().getTime();
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
var r = (d + Math.random() * 16) % 16 | 0;
Expand Down
22 changes: 11 additions & 11 deletions src/Lime/Lime.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
// Lime
export { Guid } from "./Guid";
export { default as Guid } from "./Guid";

// Lime.Protocol
export
{ Envelope
{ default as Envelope
, EnvelopeListener
} from "./Protocol/Envelope";
export
{ Message
{ default as Message
, MessageListener
} from "./Protocol/Message";
export
{ Notification
{ default as Notification
, NotificationEvent
, NotificationListener
} from "./Protocol/Notification";
export
{ Command
{ default as Command
, CommandMethod
, CommandStatus
, CommandListener
} from "./Protocol/Command";
export
{ Session
{ default as Session
, SessionState
, SessionEncryption
, SessionCompression
} from "./Protocol/Session";
export
{ Reason
{ default as Reason
, ReasonCodes
} from "./Protocol/Reason";

// Lime.Protocol.Security
export
{ Authentication
{ default as Authentication
, GuestAuthentication
, PlainAuthentication
, TransportAuthentication
Expand All @@ -43,13 +43,13 @@ export

// Lime.Protocol.Channel
export
{ Channel
{ default as Channel
, MessageChannel
, CommandChannel
, NotificationChannel
, SessionChannel
} from "./Protocol/Client/Channel";
export { ClientChannel } from "./Protocol/Client/ClientChannel";
export { default as ClientChannel } from "./Protocol/Client/ClientChannel";

// Lime.Protocol.Network
export { Transport } from "./Protocol/Network/Transport";
export { default as Transport } from "./Protocol/Network/Transport";
28 changes: 15 additions & 13 deletions src/Lime/Protocol/Client/Channel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Envelope} from "../Envelope";
import {Message, MessageListener} from "../Message";
import {Command, CommandListener, CommandMethod, CommandStatus} from "../Command";
import {Notification, NotificationListener, NotificationEvent} from "../Notification";
import {Session, SessionListener, SessionState} from "../Session";
import {Transport} from "../Network/Transport";
import Envelope from "../Envelope";
import Message, { MessageListener } from "../Message";
import Command, { CommandListener, CommandMethod, CommandStatus } from "../Command";
import Notification, { NotificationListener, NotificationEvent } from "../Notification";
import Session, { SessionListener, SessionState } from "../Session";
import Transport from "../Network/Transport";

export interface MessageChannel extends MessageListener {
sendMessage(message: Message): void;
Expand All @@ -21,7 +21,7 @@ export interface SessionChannel extends SessionListener {
sendSession(session: Session): void;
}

export abstract class Channel implements MessageChannel, CommandChannel, NotificationChannel, SessionChannel {
abstract class Channel implements MessageChannel, CommandChannel, NotificationChannel, SessionChannel {

private autoReplyPings: boolean;
private autoNotifyReceipt: boolean;
Expand Down Expand Up @@ -54,7 +54,7 @@ export abstract class Channel implements MessageChannel, CommandChannel, Notific
else if (Envelope.isCommand(envelope)) {
const command = <Command>envelope;
if (this.autoReplyPings && command.id &&
command.uri === "/ping" &&
command.uri === "/ping" &&
command.method === CommandMethod.GET &&
this.isForMe(command))
{
Expand Down Expand Up @@ -115,9 +115,9 @@ export abstract class Channel implements MessageChannel, CommandChannel, Notific
}

private notifyMessage(message: Message) {
if (this.autoNotifyReceipt &&
message.id &&
message.from &&
if (this.autoNotifyReceipt &&
message.id &&
message.from &&
this.isForMe(message)) {
const notification: Notification = {
id: message.id,
Expand All @@ -129,8 +129,10 @@ export abstract class Channel implements MessageChannel, CommandChannel, Notific
}

private isForMe(envelope: Envelope): boolean {
return !envelope.to ||
envelope.to === this.localNode ||
return !envelope.to ||
envelope.to === this.localNode ||
this.localNode.substring(0, envelope.to.length) === envelope.to;
}
}

export default Channel;
16 changes: 8 additions & 8 deletions src/Lime/Protocol/Client/ClientChannel.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {Message} from "../Message";
import {Notification} from "../Notification";
import {Command} from "../Command";
import {Session, SessionCompression, SessionEncryption, SessionState} from "../Session";
import {Channel} from "./Channel";
import {Transport} from "../Network/Transport";
import {Authentication} from "../Security/Authentication";
import Message from "../Message";
import Notification from "../Notification";
import Command from "../Command";
import Session, { SessionCompression, SessionEncryption, SessionState } from "../Session";
import Channel from "./Channel";
import Transport from "../Network/Transport";
import Authentication from "../Security/Authentication";
import * as Promise from "bluebird";

export class ClientChannel extends Channel {
export default class ClientChannel extends Channel {

constructor(transport: Transport, autoReplyPings: boolean = true, autoNotifyReceipt: boolean = false) {
super(transport, autoReplyPings, autoNotifyReceipt);
Expand Down
7 changes: 4 additions & 3 deletions src/Lime/Protocol/Command.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {Envelope} from "./Envelope";
import {Reason} from "./Reason";
import Envelope from "./Envelope";
import Reason from "./Reason";

export interface Command extends Envelope {
interface Command extends Envelope {
uri?: string;
type?: string;
resource?: any;
method: CommandMethod;
status?: CommandStatus;
reason?: Reason;
}
export default Command;

export interface CommandListener {
onCommand(command: Command): void;
Expand Down
14 changes: 8 additions & 6 deletions src/Lime/Protocol/Envelope.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {Message} from './Message';
import {Notification} from './Notification';
import {Command} from './Command';
import {Session} from './Session';
import Message from './Message';
import Notification from './Notification';
import Command from './Command';
import Session from './Session';

export interface Envelope {
interface Envelope {
id?: string;
from?: string;
to?: string;
pp?: string;
metadata?: any;
}

export const Envelope = {
const Envelope = {
isMessage: (envelope: Envelope) => envelope.hasOwnProperty('content'),
isNotification: (envelope: Envelope) => envelope.hasOwnProperty('event'),
isCommand: (envelope: Envelope) => envelope.hasOwnProperty('method'),
Expand All @@ -21,3 +21,5 @@ export const Envelope = {
export interface EnvelopeListener {
onEnvelope(envelope: Envelope): void
}

export default Envelope;
5 changes: 3 additions & 2 deletions src/Lime/Protocol/Message.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Envelope} from "./Envelope";
import Envelope from "./Envelope";

export interface Message extends Envelope {
interface Message extends Envelope {
type: string;
content: any;
}
export default Message;

export interface MessageListener {
onMessage(command: Message): void;
Expand Down
8 changes: 5 additions & 3 deletions src/Lime/Protocol/Network/Transport.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Envelope, EnvelopeListener} from "../Envelope";
import {SessionCompression, SessionEncryption} from "../Session";
import Envelope, { EnvelopeListener } from "../Envelope";
import { SessionCompression, SessionEncryption } from "../Session";
import * as Promise from "bluebird";

export interface Transport extends EnvelopeListener {
interface Transport extends EnvelopeListener {
open(uri: string): Promise<void>;
close(): Promise<void>;

Expand All @@ -16,3 +16,5 @@ export interface Transport extends EnvelopeListener {
setEncryption(encryption: SessionEncryption): void;
encryption: SessionEncryption;
}

export default Transport;
7 changes: 4 additions & 3 deletions src/Lime/Protocol/Notification.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {Envelope} from "./Envelope";
import {Reason} from "./Reason";
import Envelope from "./Envelope";
import Reason from "./Reason";

export interface Notification extends Envelope {
interface Notification extends Envelope {
event: NotificationEvent;
reason?: Reason;
}
export default Notification;

export interface NotificationListener {
onNotification(command: Notification): void;
Expand Down
2 changes: 1 addition & 1 deletion src/Lime/Protocol/Reason.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Represents a known reason for events occurred during the client-server
* interactions.
*/
export class Reason {
export default class Reason {
code: number;
description: string;

Expand Down
2 changes: 1 addition & 1 deletion src/Lime/Protocol/Security/Authentication.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class Authentication {
export default class Authentication {
scheme: AuthenticationScheme;
}
export class GuestAuthentication extends Authentication {
Expand Down
7 changes: 4 additions & 3 deletions src/Lime/Protocol/Session.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Envelope} from "./Envelope";
import {Reason} from "./Reason";
import Envelope from "./Envelope";
import Reason from "./Reason";

export interface Session extends Envelope {
interface Session extends Envelope {
state: SessionState;

encryptionOptions?: SessionEncryption[];
Expand All @@ -15,6 +15,7 @@ export interface Session extends Envelope {

reason?: Reason;
}
export default Session;

export interface SessionListener {
onSession(command: Session): void;
Expand Down

0 comments on commit 8bc1433

Please sign in to comment.