cleanup code

This commit is contained in:
pypy
2020-01-11 22:13:44 +09:00
parent e1796634a9
commit 7c4bf0c50a
+94 -64
View File
@@ -8,9 +8,9 @@ if (window.CefSharp) {
CefSharp.BindObjectAsync('VRCX'), CefSharp.BindObjectAsync('VRCX'),
CefSharp.BindObjectAsync('VRCXStorage'), CefSharp.BindObjectAsync('VRCXStorage'),
CefSharp.BindObjectAsync('SQLite') CefSharp.BindObjectAsync('SQLite')
]).catch(() => { ]).catch(function () {
location = 'https://github.com/pypy-vrc/vrcx'; location = 'https://github.com/pypy-vrc/vrcx';
}).then(() => { }).then(function () {
VRCXStorage.GetBool = function (key) { VRCXStorage.GetBool = function (key) {
return this.Get(key) === 'true'; return this.Get(key) === 'true';
@@ -83,6 +83,8 @@ if (window.CefSharp) {
timeout: 6000 timeout: 6000
}); });
var isObject = (any) => any === Object(any);
var escapeTag = (s) => String(s).replace(/["&'<>]/gu, (c) => `&#${c.charCodeAt(0)};`); var escapeTag = (s) => String(s).replace(/["&'<>]/gu, (c) => `&#${c.charCodeAt(0)};`);
Vue.filter('escapeTag', escapeTag); Vue.filter('escapeTag', escapeTag);
@@ -90,21 +92,21 @@ if (window.CefSharp) {
Vue.filter('commaNumber', commaNumber); Vue.filter('commaNumber', commaNumber);
var formatDate = (s, format) => { var formatDate = (s, format) => {
var ctx = new Date(s); var dt = new Date(s);
if (isNaN(ctx)) { if (isNaN(dt)) {
return escapeTag(s); return escapeTag(s);
} }
var hours = ctx.getHours(); var hours = dt.getHours();
var map = { var map = {
'YYYY': String(10000 + ctx.getFullYear()).substr(-4), 'YYYY': String(10000 + dt.getFullYear()).substr(-4),
'MM': String(101 + ctx.getMonth()).substr(-2), 'MM': String(101 + dt.getMonth()).substr(-2),
'DD': String(100 + ctx.getDate()).substr(-2), 'DD': String(100 + dt.getDate()).substr(-2),
'HH24': String(100 + hours).substr(-2), 'HH24': String(100 + hours).substr(-2),
'HH': String(100 + (hours > 12 'HH': String(100 + (hours > 12
? hours - 12 ? hours - 12
: hours)).substr(-2), : hours)).substr(-2),
'MI': String(100 + ctx.getMinutes()).substr(-2), 'MI': String(100 + dt.getMinutes()).substr(-2),
'SS': String(100 + ctx.getSeconds()).substr(-2), 'SS': String(100 + dt.getSeconds()).substr(-2),
'AMPM': hours >= 12 'AMPM': hours >= 12
? 'PM' ? 'PM'
: 'AM' : 'AM'
@@ -154,47 +156,52 @@ if (window.CefSharp) {
var API = {}; var API = {};
API.$handler = {}; API.eventHandlers = new Map();
API.$emit = function (event, ...args) { API.$emit = function (name, ...args) {
// console.log(name, ...args);
var handlers = this.eventHandlers.get(name);
if (handlers === undefined) {
return;
}
try { try {
// console.log(event, ...args); var { length } = handlers;
var h = this.$handler[event]; for (var i = 0; i < length; ++i) {
if (h) { handlers[i].apply(this, args);
h.forEach((f) => f(...args));
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
}; };
API.$on = function (event, callback) { API.$on = function (name, fx) {
var h = this.$handler[event]; var handlers = this.eventHandlers.get(name);
if (h) { if (handlers === undefined) {
h.push(callback); handlers = [];
this.eventHandlers.set(name, handlers);
}
handlers.push(fx);
};
API.$off = function (name, fx) {
var handlers = this.eventHandlers.get(name);
if (handlers === undefined) {
return;
}
var { length } = handlers;
for (var i = 0; i < length; ++i) {
if (handlers[i] === fx) {
if (length > 1) {
handlers.splice(i, 1);
} else { } else {
this.$handler[event] = [callback]; this.eventHandlers.delete(name);
}
break;
}
} }
}; };
API.$off = function (event, callback) { API.pendingGetRequests = new Map();
var h = this.$handler[event];
if (h) {
h.find((val, idx, arr) => {
if (val !== callback) {
return false;
}
if (arr.length > 1) {
arr.splice(idx, 1);
} else {
delete this.$handler[event];
}
return true;
});
}
};
API.$fetch = {};
API.call = function (endpoint, options) { API.call = function (endpoint, options) {
var input = `https://api.vrchat.cloud/api/1/${endpoint}`; var input = `https://api.vrchat.cloud/api/1/${endpoint}`;
@@ -206,28 +213,33 @@ if (window.CefSharp) {
referrerPolicy: 'no-referrer', referrerPolicy: 'no-referrer',
...options ...options
}; };
if (init.method === 'GET') { var isGetRequest = init.method === 'GET';
if (init.body) {
if (isGetRequest) {
// transform body to url
if (isObject(init.body)) {
var url = new URL(input); var url = new URL(input);
for (var key in init.body) { for (var key in init.body) {
url.searchParams.set(key, init.body[key]); url.searchParams.set(key, init.body[key]);
} }
input = url.toString(); input = url.toString();
init.body = null;
} }
delete init.body;
// merge requests // merge requests
if (this.$fetch[input]) { var request = this.pendingGetRequests.get(input);
return this.$fetch[input]; if (request) {
return request;
} }
} else { } else {
init.headers = { init.headers = {
'Content-Type': 'application/json;charset=utf-8', 'Content-Type': 'application/json;charset=utf-8',
...init.headers ...init.headers
}; };
init.body = init.body init.body = isObject(init.body)
? JSON.stringify(init.body) ? JSON.stringify(init.body)
: '{}'; : '{}';
} }
var req = fetch(input, init).catch((err) => { var req = fetch(input, init).catch((err) => {
this.$throw(0, err); this.$throw(0, err);
}).then((res) => res.json().catch(() => { }).then((res) => res.json().catch(() => {
@@ -236,8 +248,14 @@ if (window.CefSharp) {
} }
this.$throw(0, 'Invalid JSON'); this.$throw(0, 'Invalid JSON');
}).then((json) => { }).then((json) => {
if (!res.ok) { if (res.ok) {
if (typeof json.error === 'object') { if (json.success) {
new Noty({
type: 'success',
text: escapeTag(json.success.message)
}).show();
}
} else if (typeof json.error === 'object') {
this.$throw( this.$throw(
json.error.status_code || res.status, json.error.status_code || res.status,
json.error.message, json.error.message,
@@ -251,18 +269,20 @@ if (window.CefSharp) {
} else { } else {
this.$throw(res.status, json); this.$throw(res.status, json);
} }
}
return json; return json;
})); }));
if (init.method === 'GET') {
this.$fetch[input] = req.finally(() => { if (isGetRequest) {
delete this.$fetch[input]; req.finally(() => {
this.pendingGetRequests.delete(input);
}); });
this.pendingGetRequests.set(input, req);
} }
return req; return req;
}; };
API.$status = { API.statusCodes = {
100: 'Continue', 100: 'Continue',
101: 'Switching Protocols', 101: 'Switching Protocols',
102: 'Processing', 102: 'Processing',
@@ -338,18 +358,28 @@ if (window.CefSharp) {
}; };
API.$throw = function (code, error) { API.$throw = function (code, error) {
throw { var text = [];
'status_code': code, if (code) {
error var status = this.statusCodes[code];
}; if (status) {
text.push(`${code} ${status}`);
} else {
text.push(`${code}`);
}
}
if (error !== undefined) {
text.push(error);
}
text = text.map((s) => escapeTag(s)).join('<br>');
throw new Error(text);
}; };
// API: Config // API: Config
API.config = {}; API.config = {};
API.$on('CONFIG', (args) => { API.$on('CONFIG', function (args) {
args.ref = API.updateConfig(args.json); args.ref = this.updateConfig(args.json);
}); });
API.getConfig = function () { API.getConfig = function () {
@@ -468,7 +498,7 @@ if (window.CefSharp) {
} else if (L.isPrivate) { } else if (L.isPrivate) {
this.text = 'Private'; this.text = 'Private';
} else if (L.worldId) { } else if (L.worldId) {
var ref = API.world[L.worldId]; var ref = API.cachedWorlds.get(L.worldId);
if (ref) { if (ref) {
if (L.instanceId) { if (L.instanceId) {
this.text = `${ref.name} #${L.instanceName} ${L.accessType}`; this.text = `${ref.name} #${L.instanceName} ${L.accessType}`;
@@ -504,10 +534,10 @@ if (window.CefSharp) {
// API: World // API: World
API.world = {}; API.cachedWorlds = new Map();
API.$on('WORLD', (args) => { API.$on('WORLD', function (args) {
args.ref = API.updateWorld(args.json); args.ref = this.updateWorld(args.json);
}); });
/* /*
@@ -529,7 +559,7 @@ if (window.CefSharp) {
}; };
API.updateWorld = function (ref) { API.updateWorld = function (ref) {
var ctx = this.world[ref.id]; var ctx = this.cachedWorlds.get(ref.id);
if (ctx) { if (ctx) {
Object.assign(ctx, ref); Object.assign(ctx, ref);
} else { } else {
@@ -570,7 +600,7 @@ if (window.CefSharp) {
// //
...ref ...ref
}; };
this.world[ctx.id] = ctx; this.cachedWorlds.set(ctx.id, ctx);
} }
if (ctx.tags) { if (ctx.tags) {
ctx.labs_ = ctx.tags.includes('system_labs'); ctx.labs_ = ctx.tags.includes('system_labs');