Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
refactored little
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Apr 21, 2018
1 parent b2fcbcf commit 6798d60
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 54 deletions.
8 changes: 4 additions & 4 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ const fetchJson = require('./helpers/fetch-json');
if (mixin.getLoginStatus()) {
const secure = location.protocol == 'https:';

let webSocket;
try {
const accessToken = localStorage.getItem('accessToken');
webSocket = await WebSocketEvents.connect(`${secure ? 'wss' : 'ws'}://${mixin.config.apiHost}?access_token=${accessToken}`);
const webSocket = await WebSocketEvents.connect(`${secure ? 'wss' : 'ws'}://${mixin.config.apiHost}?access_token=${accessToken}`);
webSocket.addEventListener('close', (ev) => { console.log('close:', ev); });
webSocket.addEventListener('error', (ev) => { console.log('error:', ev); });
WebSocketEvents.init(webSocket);
Expand All @@ -50,9 +49,10 @@ const fetchJson = require('./helpers/fetch-json');
return;
}

const streamingRest = new StreamingRest(webSocket);
mixin.streamingRest = new StreamingRest(mixin.webSocket);

try {
const rest = await streamingRest.request('get', `/users/${mixin.userId}`);
const rest = await mixin.streamingRest.request('get', `/users/${mixin.userId}`);
mixin.user = rest.response.user;
}
catch (err) {
Expand Down
4 changes: 1 addition & 3 deletions src/client/tags/frost-applications.tag
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
</style>

<script>
const StreamingRest = require('../helpers/streaming-rest');
this.applications = [];
this.loading = true;
this.error = false;
Expand All @@ -50,8 +49,7 @@
this.central.on('add-application', centralAddApplicationHandler);

(async () => {
const streamingRest = new StreamingRest(this.webSocket);
const rest = await streamingRest.request('get', '/applications');
const rest = await this.streamingRest.request('get', '/applications');
if (rest.response.applications == null) {
if (rest.statusCode != 204) {
alert(`api error: failed to fetch list of appliations. ${rest.response.message}`);
Expand Down
25 changes: 10 additions & 15 deletions src/client/tags/frost-follow-button.tag
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
<button if={ showing } onclick={ follow }>{ following ? 'フォローしています' : 'フォロー' }</button>

<script>
const StreamingRest = require('../helpers/streaming-rest');

this.on('mount', () => {
(async () => {
const streamingRest = new StreamingRest(this.webSocket);

const rest = await streamingRest.request('get', `/users/${this.user.id}/followings/${this.opts.dataTargetId}`);
this.on('mount', async () => {
try {
const rest = await this.streamingRest.request('get', `/users/${this.user.id}/followings/${this.opts.dataTargetId}`);
if (rest.statusCode == 200) {
this.following = rest.response.following;
this.showing = true;
Expand All @@ -21,17 +17,16 @@
alert(`api error: ${rest.response.message}`);
}

this.follow = () => {
(async () => {
await streamingRest.request(this.following ? 'delete' : 'put', `/users/${this.user.id}/followings/${this.opts.dataTargetId}`);
this.following = !this.following;
this.update();
})();
this.follow = async () => {
await this.streamingRest.request(this.following ? 'delete' : 'put', `/users/${this.user.id}/followings/${this.opts.dataTargetId}`);
this.following = !this.following;
this.update();
};
this.update();
})().catch((err) => {
}
catch(err) {
console.error(err);
});
}
});

</script>
Expand Down
4 changes: 1 addition & 3 deletions src/client/tags/frost-form-create-application.tag
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
</form>

<script>
const StreamingRest = require('../helpers/streaming-rest');
this.isShowModal = false;
let widgetId;

Expand All @@ -54,8 +53,7 @@
}

(async () => {
const streamingRest = new StreamingRest(this.webSocket);
const rest = await streamingRest.request('post', '/applications', {
const rest = await this.streamingRest.request('post', '/applications', {
body: {
name: this.refs.name.value,
description: this.refs.description.value,
Expand Down
4 changes: 1 addition & 3 deletions src/client/tags/frost-form-create-status.tag
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
</style>

<script>
const StreamingRest = require('../helpers/streaming-rest');
this.textMax = 256;
this.text = '';
this.lock = false;
Expand Down Expand Up @@ -82,8 +81,7 @@

this.createStatus = () => {
(async () => {
const streamingRest = new StreamingRest(this.webSocket);
const rest = await streamingRest.request('post', '/posts/post_status', { body: { text: this.text } });
const rest = await this.streamingRest.request('post', '/posts/post_status', { body: { text: this.text } });
this.clear();
return 'success';
})().catch((err) => {
Expand Down
22 changes: 11 additions & 11 deletions src/client/tags/frost-form-login.tag
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
<script>
const fetchJson = require('../helpers/fetch-json');

submit(e) {
async submit(e) {
e.preventDefault();

fetchJson('PUT', '/session', {
screenName: this.refs.screenName.value,
password: this.refs.password.value,
_csrf: this.csrf
})
.then(async (res) => {
try {
const res = await fetchJson('PUT', '/session', {
screenName: this.refs.screenName.value,
password: this.refs.password.value,
_csrf: this.csrf
})
const json = await res.json();

if (res.ok) {
Expand All @@ -44,10 +44,10 @@
const json = await res.json();
alert('ログインに失敗しました: ' + json.error.message);
}
})
.catch((reason) => {
alert('ログインに失敗しました: ' + reason);
});
}
catch (err) {
alert('ログインに失敗しました: ' + err);
}
}
</script>
</frost-form-login>
4 changes: 1 addition & 3 deletions src/client/tags/frost-page-user.tag
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
</style>

<script>
const StreamingRest = require('../helpers/streaming-rest');
this.user = null;
this.loading = true;

Expand All @@ -68,8 +67,7 @@

try {
// ユーザー情報をフェッチ
const streamingRest = new StreamingRest(this.webSocket);
const rest = await streamingRest.request('get', '/users', { query: { 'screen_names': params.screenName } });
const rest = await this.streamingRest.request('get', '/users', { query: { 'screen_names': params.screenName } });

this.user = rest.response.users[0];
this.loading = false;
Expand Down
4 changes: 1 addition & 3 deletions src/client/tags/frost-page-userlist.tag
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
</style>

<script>
const StreamingRest = require('../helpers/streaming-rest');
this.users = [];
this.loading = true;

Expand All @@ -30,8 +29,7 @@

try {
// ユーザー情報をフェッチ
const streamingRest = new StreamingRest(this.webSocket);
const rest = await streamingRest.request('get', '/users');
const rest = await this.streamingRest.request('get', '/users');

this.users = rest.response.users;
this.loading = false;
Expand Down
8 changes: 2 additions & 6 deletions src/client/tags/frost-tabs-user-page.tag
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@
</style>

<script>
const StreamingRest = require('../helpers/streaming-rest');

if (this.opts.dataUser == null) {
throw new Error('data-user property is required');
}
Expand All @@ -103,12 +101,10 @@
}

this.on('mount', async () => {
const streamingRest = new StreamingRest(this.webSocket);

let restFollowings, restFollowers;
[restFollowings, restFollowers] = await Promise.all([
streamingRest.request('get', `/users/${this.user.id}/followings`, { query: { limit: 100 } }),
streamingRest.request('get', `/users/${this.user.id}/followers`, { query: { limit: 100 } })
this.streamingRest.request('get', `/users/${this.user.id}/followings`, { query: { limit: 100 } }),
this.streamingRest.request('get', `/users/${this.user.id}/followers`, { query: { limit: 100 } })
]);
this.followings = restFollowings.response.users;
this.followers = restFollowers.response.users;
Expand Down
4 changes: 1 addition & 3 deletions src/client/tags/frost-timeline.tag
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
</style>

<script>
const StreamingRest = require('../helpers/streaming-rest');
this.timelinePosts = [];
this.loading = false;
this.error = false;
Expand Down Expand Up @@ -116,8 +115,7 @@
this.update();

(async () => {
const streamingRest = new StreamingRest(this.webSocket);
const rest = await streamingRest.request('get', endpoint, { query: { limit: 100 } });
const rest = await this.streamingRest.request('get', endpoint, { query: { limit: 100 } });
if (rest.response.posts == null) {
if (rest.statusCode != 204) {
alert(`api error: failed to fetch ${this.opts.dataName} timeline posts. ${rest.response.message}`);
Expand Down

0 comments on commit 6798d60

Please sign in to comment.