Skip to content

Commit aacfa53

Browse files
authored
Merge pull request #6 from youngmonkeys/dev
feat: update code style
2 parents 7bc3fef + 1a72fd6 commit aacfa53

23 files changed

+650
-593
lines changed

babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
presets: ['module:metro-react-native-babel-preset'],
2+
presets: ['module:metro-react-native-babel-preset'],
33
};

example/App.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
import React from 'react';
22
import {StyleSheet, View} from 'react-native';
3-
import Mvc from 'mvc-es6'
4-
import LoginView from './views/LoginView'
5-
import MessageView from './views/MessageView'
6-
import {SocketProxy} from './socket'
3+
import Mvc from 'mvc-es6';
4+
import LoginView from './views/LoginView';
5+
import MessageView from './views/MessageView';
6+
import {SocketProxy} from './socket';
77

88
export default class App extends React.Component {
99
constructor(args) {
1010
super(args);
1111
this.mvc = Mvc.getInstance();
12-
this.mvc.newController("router");
13-
this.mvc.newController("login");
14-
this.mvc.newController("message");
15-
this.state = {currentView : "login"}
12+
this.mvc.newController('router');
13+
this.mvc.newController('login');
14+
this.mvc.newController('message');
15+
this.state = {currentView: 'login'};
1616
SocketProxy.getInstance().setup();
1717
}
1818

1919
componentDidMount() {
20-
let routerController = this.mvc.getController("router");
21-
routerController.addDefaultView("change", viewName => {
22-
this.setState({currentView : viewName});
20+
let routerController = this.mvc.getController('router');
21+
routerController.addDefaultView('change', viewName => {
22+
this.setState({currentView: viewName});
2323
});
2424
}
25-
25+
2626
render() {
2727
let {currentView} = this.state;
2828
var displayView = <LoginView />;
29-
if(currentView == 'message') {
30-
displayView = <MessageView />
29+
if (currentView === 'message') {
30+
displayView = <MessageView />;
3131
}
32-
return (
33-
<View style={styles.container}>
34-
{displayView}
35-
</View>
36-
);
32+
return <View style={styles.container}>{displayView}</View>;
3733
}
3834
}
3935
var styles = StyleSheet.create({
@@ -46,4 +42,4 @@ var styles = StyleSheet.create({
4642
textAlign: 'center',
4743
margin: 10,
4844
},
49-
});
45+
});

example/socket/SocketConstants.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export const Command = {
2-
GREET: "greet",
3-
SECURE_CHAT: "secureChat",
4-
ERROR: 'err'
5-
}
2+
GREET: 'greet',
3+
SECURE_CHAT: 'secureChat',
4+
ERROR: 'err',
5+
};
66

7-
export const ZONE_NAME = "example"
8-
export const APP_NAME = "hello-world"
7+
export const ZONE_NAME = 'example';
8+
export const APP_NAME = 'hello-world';

example/socket/SocketProxy.js

Lines changed: 112 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,121 @@
11
import Ezy from 'ezyfox-server-react-native-client';
22
import Mvc from 'mvc-es6';
3-
import {Command, ZONE_NAME, APP_NAME} from "./SocketConstants";
3+
import {Command, ZONE_NAME, APP_NAME} from './SocketConstants';
44

55
class SocketProxy {
6+
static instance = null;
67

7-
static instance = null;
8-
9-
static getInstance() {
10-
if (!SocketProxy.instance)
11-
SocketProxy.instance = new SocketProxy();
12-
return SocketProxy.instance;
13-
}
14-
15-
setup() {
16-
let config = new Ezy.ClientConfig();
17-
config.clientName = ZONE_NAME
18-
config.enableSSL = true;
19-
config.enableDebug = true;
20-
let clients = Ezy.Clients.getInstance();
21-
clients.newDefaultClient(config, client => {
22-
this.doSetup(client);
23-
});
24-
}
25-
26-
doSetup(client) {
27-
let mvc = Mvc.getInstance();
28-
let models = mvc.models;
29-
let handshakeHandler = new Ezy.HandshakeHandler();
30-
handshakeHandler.getLoginRequest = function () {
31-
let connection = models.connection;
32-
let username = connection.username;
33-
let password = connection.password;
34-
return [ZONE_NAME, username, password, []];
35-
};
36-
37-
let loginSuccessHandler = new Ezy.LoginSuccessHandler();
38-
loginSuccessHandler.handleLoginSuccess = function () {
39-
let accessAppRequest = [APP_NAME, []];
40-
this.client.send(Ezy.Command.APP_ACCESS, accessAppRequest);
41-
};
42-
43-
let loginErrorHandler = new Ezy.LoginErrorHandler();
44-
loginErrorHandler.handleLoginError = function (event) {
45-
let loginController = mvc.getController("login");
46-
loginController.updateViews("loginError", event[1]);
47-
};
48-
49-
let accessAppHandler = new Ezy.AppAccessHandler();
50-
accessAppHandler.postHandle = function (app, data) {
51-
let routerController = mvc.getController("router");
52-
routerController.updateViews('change', 'message');
53-
};
54-
55-
let exitAppHandler = new Ezy.AppExitHandler();
56-
exitAppHandler.postHandle = function (app, data) {
57-
this.client.close();
58-
};
59-
60-
let connectionSuccessHandler = new Ezy.ConnectionSuccessHandler();
61-
connectionSuccessHandler.postHandle = function (event) {
62-
console.log("connected to server");
63-
};
64-
65-
let disconnectionHandler = new Ezy.DisconnectionHandler();
66-
disconnectionHandler.preHandle = function (event) {
67-
let routerController = mvc.getController("router");
68-
routerController.updateViews('change', 'login');
69-
};
70-
71-
let setup = client.setup;
72-
setup.addEventHandler(Ezy.EventType.CONNECTION_SUCCESS, connectionSuccessHandler);
73-
setup.addEventHandler(Ezy.EventType.DISCONNECTION, disconnectionHandler);
74-
setup.addDataHandler(Ezy.Command.HANDSHAKE, handshakeHandler);
75-
setup.addDataHandler(Ezy.Command.LOGIN, loginSuccessHandler);
76-
setup.addDataHandler(Ezy.Command.LOGIN_ERROR, loginErrorHandler);
77-
setup.addDataHandler(Ezy.Command.APP_ACCESS, accessAppHandler);
78-
setup.addDataHandler(Ezy.Command.APP_EXIT, exitAppHandler);
79-
let setupApp = setup.setupApp(APP_NAME);
80-
81-
let messageController = mvc.getController("message");
82-
83-
setupApp.addDataHandler(Command.GREET, function (app, data) {
84-
console.log("received message: " + JSON.stringify(data))
85-
messageController.updateViews(Command.GREET, data.message);
86-
app.send(Command.SECURE_CHAT, {who: "Young Monkey"});
87-
});
88-
89-
setupApp.addDataHandler(Command.SECURE_CHAT, function (app, data) {
90-
console.log("received message: " + JSON.stringify(data))
91-
messageController.updateViews(Command.SECURE_CHAT, data["secure-message"]);
92-
});
93-
94-
return client;
95-
}
96-
97-
connect() {
98-
let client = this.getClient();
99-
client.connect("ws.tvd12.com", 3005);
100-
}
101-
102-
isConnected() {
103-
let client = this.getClient();
104-
let connected = client && client.isConnected();
105-
return connected;
106-
}
107-
108-
getClient() {
109-
let clients = Ezy.Clients.getInstance();
110-
let client = clients.getDefaultClient();
111-
return client;
8+
static getInstance() {
9+
if (!SocketProxy.instance) {
10+
SocketProxy.instance = new SocketProxy();
11211
}
12+
return SocketProxy.instance;
13+
}
14+
15+
setup() {
16+
let config = new Ezy.ClientConfig();
17+
config.clientName = ZONE_NAME;
18+
config.enableSSL = true;
19+
config.enableDebug = true;
20+
let clients = Ezy.Clients.getInstance();
21+
clients.newDefaultClient(config, client => {
22+
this.doSetup(client);
23+
});
24+
}
25+
26+
doSetup(client) {
27+
let mvc = Mvc.getInstance();
28+
let models = mvc.models;
29+
let handshakeHandler = new Ezy.HandshakeHandler();
30+
handshakeHandler.getLoginRequest = function () {
31+
let connection = models.connection;
32+
let username = connection.username;
33+
let password = connection.password;
34+
return [ZONE_NAME, username, password, []];
35+
};
36+
37+
let loginSuccessHandler = new Ezy.LoginSuccessHandler();
38+
loginSuccessHandler.handleLoginSuccess = function () {
39+
let accessAppRequest = [APP_NAME, []];
40+
this.client.send(Ezy.Command.APP_ACCESS, accessAppRequest);
41+
};
42+
43+
let loginErrorHandler = new Ezy.LoginErrorHandler();
44+
loginErrorHandler.handleLoginError = function (event) {
45+
let loginController = mvc.getController('login');
46+
loginController.updateViews('loginError', event[1]);
47+
};
48+
49+
let accessAppHandler = new Ezy.AppAccessHandler();
50+
accessAppHandler.postHandle = function (app, data) {
51+
let routerController = mvc.getController('router');
52+
routerController.updateViews('change', 'message');
53+
};
54+
55+
let exitAppHandler = new Ezy.AppExitHandler();
56+
exitAppHandler.postHandle = function (app, data) {
57+
this.client.close();
58+
};
59+
60+
let connectionSuccessHandler = new Ezy.ConnectionSuccessHandler();
61+
connectionSuccessHandler.postHandle = function (event) {
62+
console.log('connected to server');
63+
};
64+
65+
let disconnectionHandler = new Ezy.DisconnectionHandler();
66+
disconnectionHandler.preHandle = function (event) {
67+
let routerController = mvc.getController('router');
68+
routerController.updateViews('change', 'login');
69+
};
70+
71+
let setup = client.setup;
72+
setup.addEventHandler(
73+
Ezy.EventType.CONNECTION_SUCCESS,
74+
connectionSuccessHandler,
75+
);
76+
setup.addEventHandler(Ezy.EventType.DISCONNECTION, disconnectionHandler);
77+
setup.addDataHandler(Ezy.Command.HANDSHAKE, handshakeHandler);
78+
setup.addDataHandler(Ezy.Command.LOGIN, loginSuccessHandler);
79+
setup.addDataHandler(Ezy.Command.LOGIN_ERROR, loginErrorHandler);
80+
setup.addDataHandler(Ezy.Command.APP_ACCESS, accessAppHandler);
81+
setup.addDataHandler(Ezy.Command.APP_EXIT, exitAppHandler);
82+
let setupApp = setup.setupApp(APP_NAME);
83+
84+
let messageController = mvc.getController('message');
85+
86+
setupApp.addDataHandler(Command.GREET, function (app, data) {
87+
console.log('received message: ' + JSON.stringify(data));
88+
messageController.updateViews(Command.GREET, data.message);
89+
app.send(Command.SECURE_CHAT, {who: 'Young Monkey'});
90+
});
91+
92+
setupApp.addDataHandler(Command.SECURE_CHAT, function (app, data) {
93+
console.log('received message: ' + JSON.stringify(data));
94+
messageController.updateViews(
95+
Command.SECURE_CHAT,
96+
data['secure-message'],
97+
);
98+
});
99+
100+
return client;
101+
}
102+
103+
connect() {
104+
let client = this.getClient();
105+
client.connect('ws.tvd12.com', 3005);
106+
}
107+
108+
isConnected() {
109+
let client = this.getClient();
110+
let connected = client && client.isConnected();
111+
return connected;
112+
}
113+
114+
getClient() {
115+
let clients = Ezy.Clients.getInstance();
116+
let client = clients.getDefaultClient();
117+
return client;
118+
}
113119
}
114120

115121
export default SocketProxy;

example/socket/SocketRequests.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
import Ezy from 'ezyfox-server-react-native-client'
2-
import { Command } from './SocketConstants'
1+
import Ezy from 'ezyfox-server-react-native-client';
2+
import {Command} from './SocketConstants';
33

44
export default class SocketRequests {
5-
6-
static sendGreet() {
7-
let app = this.getApp();
8-
if(app) {
9-
app.send(Command.GREET, {who: 'React Developer'});
10-
}
5+
static sendGreet() {
6+
let app = this.getApp();
7+
if (app) {
8+
app.send(Command.GREET, {who: 'React Developer'});
119
}
10+
}
1211

13-
static exitApp() {
14-
let app = this.getApp();
15-
if(app) {
16-
let client = Ezy.Clients.getInstance().getDefaultClient();
17-
client.send(Ezy.Command.APP_EXIT, [app.id]);
18-
}
12+
static exitApp() {
13+
let app = this.getApp();
14+
if (app) {
15+
let client = Ezy.Clients.getInstance().getDefaultClient();
16+
client.send(Ezy.Command.APP_EXIT, [app.id]);
1917
}
18+
}
2019

21-
static getApp() {
22-
let client = Ezy.Clients.getInstance().getDefaultClient();
23-
return client ? client.getApp() : null
24-
}
25-
}
20+
static getApp() {
21+
let client = Ezy.Clients.getInstance().getDefaultClient();
22+
return client ? client.getApp() : null;
23+
}
24+
}

example/socket/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import SocketProxy from "./SocketProxy";
2-
import SocketRequests from "./SocketRequests";
3-
import { Command } from './SocketConstants'
1+
import SocketProxy from './SocketProxy';
2+
import SocketRequests from './SocketRequests';
3+
import {Command} from './SocketConstants';
44

5-
export { SocketProxy, SocketRequests, Command }
5+
export {SocketProxy, SocketRequests, Command};

0 commit comments

Comments
 (0)