Clean code.
This commit is contained in:
parent
324fceb5c3
commit
fe9cdbc46b
3 changed files with 20 additions and 39 deletions
|
@ -1,33 +1,27 @@
|
||||||
const {lo} = require('../globals');
|
const isStr = x => typeof x === 'string';
|
||||||
|
const isFn = x => typeof x === 'function';
|
||||||
|
|
||||||
const subscriptions = {};
|
const subscriptions = {};
|
||||||
|
|
||||||
function sub(topic, listener) {
|
const sub = (topic, listener) => {
|
||||||
if (lo.isString(topic) && lo.isFunction(listener)) {
|
if (isStr(topic) && isFn(listener)) {
|
||||||
if (!subscriptions[topic]) {
|
if (!subscriptions[topic]) {
|
||||||
subscriptions[topic] = [];
|
subscriptions[topic] = [];
|
||||||
}
|
}
|
||||||
subscriptions[topic].push(listener);
|
subscriptions[topic].push(listener);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function unsub(topic, listener) {
|
const pub = (topic, ...args) => {
|
||||||
if (lo.isString(topic) && lo.isFunction(listener) && subscriptions[topic]) {
|
|
||||||
subscriptions[topic] = lo.without(subscriptions[topic], listener);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function pub(topic, ...args) {
|
|
||||||
// console.log(topic, args);
|
// console.log(topic, args);
|
||||||
if (lo.isString(topic) && subscriptions[topic]) {
|
if (isStr(topic) && subscriptions[topic]) {
|
||||||
lo.each(subscriptions[topic], listener => {
|
subscriptions[topic].forEach(listener => {
|
||||||
listener.apply(topic, args);
|
listener.apply(topic, args);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
sub,
|
sub,
|
||||||
unsub,
|
|
||||||
pub
|
pub
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const {jq, lo} = require('./globals');
|
const {jq, lo} = require('./globals');
|
||||||
|
|
||||||
function request(data) {
|
const request = data => {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
jq.ajax({
|
jq.ajax({
|
||||||
url: '?',
|
url: '?',
|
||||||
|
@ -11,9 +11,9 @@ function request(data) {
|
||||||
.done(json => resolve(json))
|
.done(json => resolve(json))
|
||||||
.fail(() => resolve());
|
.fail(() => resolve());
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
function formRequest(data) {
|
const formRequest = data => {
|
||||||
const $form = jq('<form method="post" action="?" style="display:none;"/>');
|
const $form = jq('<form method="post" action="?" style="display:none;"/>');
|
||||||
|
|
||||||
lo.each(data, (val, key) => {
|
lo.each(data, (val, key) => {
|
||||||
|
@ -24,7 +24,7 @@ function formRequest(data) {
|
||||||
});
|
});
|
||||||
|
|
||||||
$form.appendTo('body').submit().remove();
|
$form.appendTo('body').submit().remove();
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
request,
|
request,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/* eslint-disable func-names,no-var */
|
/* eslint-disable func-names,no-var */
|
||||||
(function (win) {
|
(function (win) {
|
||||||
if (typeof win !== 'object' || win.window !== win || !win.document) {
|
if (!win || win.window !== win || !win.document) {
|
||||||
throw new Error('no-window');
|
throw new Error('no-window');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,26 +15,13 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert('console', win.console && typeof win.console.log === 'function');
|
function isFn(x) {
|
||||||
assert('assign', win.Object && typeof win.Object.assign === 'function');
|
return typeof x === 'function';
|
||||||
assert('promise', win.Promise && typeof win.Promise === 'function');
|
}
|
||||||
assert('history', win.history && typeof win.history.pushState === 'function');
|
|
||||||
|
|
||||||
assert('canvas', (function () {
|
assert('console', win.console && isFn(win.console.log));
|
||||||
var elem = win.document.createElement('canvas');
|
assert('assign', win.Object && isFn(win.Object.assign));
|
||||||
return elem.getContext && elem.getContext('2d');
|
assert('promise', isFn(win.Promise));
|
||||||
}()));
|
|
||||||
|
|
||||||
assert('storage', (function () {
|
|
||||||
var key = '#test#';
|
|
||||||
try {
|
|
||||||
win.localStorage.setItem(key, key);
|
|
||||||
win.localStorage.removeItem(key);
|
|
||||||
return true;
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}()));
|
|
||||||
}(this));
|
}(this));
|
||||||
/* eslint-enable */
|
/* eslint-enable */
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue