mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-16 17:09:28 +08:00
fix: linting for hound
This commit is contained in:
parent
b0a075cdf2
commit
f6ff7d7b16
@ -15,66 +15,66 @@ window.backend = {};
|
||||
|
||||
// Determines if the given identifier is valid Javascript
|
||||
function isValidIdentifier(name) {
|
||||
// Don't xss yourself :-)
|
||||
try {
|
||||
new Function('var ' + name);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
// Don't xss yourself :-)
|
||||
try {
|
||||
new Function('var ' + name);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-lines-per-function
|
||||
export function NewBinding(bindingName) {
|
||||
|
||||
// Get all the sections of the binding
|
||||
var bindingSections = [].concat(bindingName.split('.').splice(1));
|
||||
var pathToBinding = window.backend;
|
||||
// Get all the sections of the binding
|
||||
var bindingSections = [].concat(bindingName.split('.').splice(1));
|
||||
var pathToBinding = window.backend;
|
||||
|
||||
// Check if we have a path (IE Struct)
|
||||
if (bindingSections.length > 1) {
|
||||
// Iterate over binding sections, adding them to the window.backend object
|
||||
for (let index = 0; index < bindingSections.length - 1; index += 1) {
|
||||
const name = bindingSections[index];
|
||||
// Is name a valid javascript identifier?
|
||||
if (!isValidIdentifier(name)) {
|
||||
return new Error(`${name} is not a valid javascript identifier.`);
|
||||
}
|
||||
pathToBinding[name] = {};
|
||||
pathToBinding = pathToBinding[name];
|
||||
}
|
||||
}
|
||||
// Check if we have a path (IE Struct)
|
||||
if (bindingSections.length > 1) {
|
||||
// Iterate over binding sections, adding them to the window.backend object
|
||||
for (let index = 0; index < bindingSections.length - 1; index += 1) {
|
||||
const name = bindingSections[index];
|
||||
// Is name a valid javascript identifier?
|
||||
if (!isValidIdentifier(name)) {
|
||||
return new Error(`${name} is not a valid javascript identifier.`);
|
||||
}
|
||||
pathToBinding[name] = {};
|
||||
pathToBinding = pathToBinding[name];
|
||||
}
|
||||
}
|
||||
|
||||
// Get the actual function/method call name
|
||||
const name = bindingSections.pop();
|
||||
// Get the actual function/method call name
|
||||
const name = bindingSections.pop();
|
||||
|
||||
// Is name a valid javascript identifier?
|
||||
if (!isValidIdentifier(name)) {
|
||||
return new Error(`${name} is not a valid javascript identifier.`);
|
||||
}
|
||||
// Is name a valid javascript identifier?
|
||||
if (!isValidIdentifier(name)) {
|
||||
return new Error(`${name} is not a valid javascript identifier.`);
|
||||
}
|
||||
|
||||
// Add binding call
|
||||
pathToBinding[name] = function () {
|
||||
// Add binding call
|
||||
pathToBinding[name] = function () {
|
||||
|
||||
// No timeout by default
|
||||
var timeout = 0;
|
||||
// No timeout by default
|
||||
var timeout = 0;
|
||||
|
||||
// Actual function
|
||||
function dynamic() {
|
||||
var args = [].slice.call(arguments);
|
||||
return Call(bindingName, args, timeout);
|
||||
}
|
||||
// Actual function
|
||||
function dynamic() {
|
||||
var args = [].slice.call(arguments);
|
||||
return Call(bindingName, args, timeout);
|
||||
}
|
||||
|
||||
// Allow setting timeout to function
|
||||
dynamic.setTimeout = function (newTimeout) {
|
||||
timeout = newTimeout;
|
||||
};
|
||||
// Allow setting timeout to function
|
||||
dynamic.setTimeout = function (newTimeout) {
|
||||
timeout = newTimeout;
|
||||
};
|
||||
|
||||
// Allow getting timeout to function
|
||||
dynamic.getTimeout = function () {
|
||||
return timeout;
|
||||
};
|
||||
// Allow getting timeout to function
|
||||
dynamic.getTimeout = function () {
|
||||
return timeout;
|
||||
};
|
||||
|
||||
return dynamic;
|
||||
}();
|
||||
return dynamic;
|
||||
}();
|
||||
}
|
||||
|
@ -12,9 +12,9 @@ The lightweight framework for web-like apps
|
||||
import { SystemCall } from './calls';
|
||||
|
||||
export function OpenURL(url) {
|
||||
return SystemCall('Browser.OpenURL', url);
|
||||
return SystemCall('Browser.OpenURL', url);
|
||||
}
|
||||
|
||||
export function OpenFile(filename) {
|
||||
return SystemCall('Browser.OpenFile', filename);
|
||||
return SystemCall('Browser.OpenFile', filename);
|
||||
}
|
||||
|
@ -16,21 +16,21 @@ var callbacks = {};
|
||||
|
||||
// AwesomeRandom
|
||||
function cryptoRandom() {
|
||||
var array = new Uint32Array(1);
|
||||
return window.crypto.getRandomValues(array)[0];
|
||||
var array = new Uint32Array(1);
|
||||
return window.crypto.getRandomValues(array)[0];
|
||||
}
|
||||
|
||||
// LOLRandom
|
||||
function basicRandom() {
|
||||
return Math.random() * 9007199254740991;
|
||||
return Math.random() * 9007199254740991;
|
||||
}
|
||||
|
||||
// Pick one based on browser capability
|
||||
var randomFunc;
|
||||
if (window.crypto) {
|
||||
randomFunc = cryptoRandom;
|
||||
randomFunc = cryptoRandom;
|
||||
} else {
|
||||
randomFunc = basicRandom;
|
||||
randomFunc = basicRandom;
|
||||
}
|
||||
|
||||
|
||||
@ -43,47 +43,47 @@ if (window.crypto) {
|
||||
|
||||
export function Call(bindingName, data, timeout) {
|
||||
|
||||
// Timeout infinite by default
|
||||
if (timeout == null || timeout == undefined) {
|
||||
timeout = 0;
|
||||
}
|
||||
// Timeout infinite by default
|
||||
if (timeout == null || timeout == undefined) {
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
// Create a promise
|
||||
return new Promise(function (resolve, reject) {
|
||||
// Create a promise
|
||||
return new Promise(function (resolve, reject) {
|
||||
|
||||
// Create a unique callbackID
|
||||
var callbackID;
|
||||
do {
|
||||
callbackID = bindingName + '-' + randomFunc();
|
||||
} while (callbacks[callbackID]);
|
||||
// Create a unique callbackID
|
||||
var callbackID;
|
||||
do {
|
||||
callbackID = bindingName + '-' + randomFunc();
|
||||
} while (callbacks[callbackID]);
|
||||
|
||||
// Set timeout
|
||||
if (timeout > 0) {
|
||||
var timeoutHandle = setTimeout(function () {
|
||||
reject(Error('Call to ' + bindingName + ' timed out. Request ID: ' + callbackID));
|
||||
}, timeout);
|
||||
}
|
||||
// Set timeout
|
||||
if (timeout > 0) {
|
||||
var timeoutHandle = setTimeout(function () {
|
||||
reject(Error('Call to ' + bindingName + ' timed out. Request ID: ' + callbackID));
|
||||
}, timeout);
|
||||
}
|
||||
|
||||
// Store callback
|
||||
callbacks[callbackID] = {
|
||||
timeoutHandle: timeoutHandle,
|
||||
reject: reject,
|
||||
resolve: resolve
|
||||
};
|
||||
// Store callback
|
||||
callbacks[callbackID] = {
|
||||
timeoutHandle: timeoutHandle,
|
||||
reject: reject,
|
||||
resolve: resolve
|
||||
};
|
||||
|
||||
try {
|
||||
const payload = {
|
||||
bindingName: bindingName,
|
||||
data: JSON.stringify(data),
|
||||
}
|
||||
try {
|
||||
const payload = {
|
||||
bindingName: bindingName,
|
||||
data: JSON.stringify(data),
|
||||
}
|
||||
|
||||
// Make the call
|
||||
SendMessage('call', payload, callbackID)
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
// Make the call
|
||||
SendMessage('call', payload, callbackID)
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -91,37 +91,37 @@ export function Call(bindingName, data, timeout) {
|
||||
// binding invocation
|
||||
export function Callback(incomingMessage) {
|
||||
|
||||
// Decode the message - Credit: https://stackoverflow.com/a/13865680
|
||||
incomingMessage = decodeURIComponent(incomingMessage.replace(/\s+/g, '').replace(/[0-9a-f]{2}/g, '%$&'));
|
||||
// Decode the message - Credit: https://stackoverflow.com/a/13865680
|
||||
incomingMessage = decodeURIComponent(incomingMessage.replace(/\s+/g, '').replace(/[0-9a-f]{2}/g, '%$&'));
|
||||
|
||||
// Parse the message
|
||||
var message;
|
||||
try {
|
||||
message = JSON.parse(incomingMessage);
|
||||
} catch (e) {
|
||||
const error = `Invalid JSON passed to callback: ${e.message}. Message: ${incomingMessage}`;
|
||||
Debug(error);
|
||||
throw new Error(error);
|
||||
}
|
||||
var callbackID = message.callbackid;
|
||||
var callbackData = callbacks[callbackID];
|
||||
if (!callbackData) {
|
||||
// eslint-disable-next-line
|
||||
const error = `Callback '${callbackID}' not registed!!!`;
|
||||
console.error(error);
|
||||
throw new Error(error);
|
||||
}
|
||||
clearTimeout(callbackData.timeoutHandle);
|
||||
// Parse the message
|
||||
var message;
|
||||
try {
|
||||
message = JSON.parse(incomingMessage);
|
||||
} catch (e) {
|
||||
const error = `Invalid JSON passed to callback: ${e.message}. Message: ${incomingMessage}`;
|
||||
Debug(error);
|
||||
throw new Error(error);
|
||||
}
|
||||
var callbackID = message.callbackid;
|
||||
var callbackData = callbacks[callbackID];
|
||||
if (!callbackData) {
|
||||
// eslint-disable-next-line
|
||||
const error = `Callback '${callbackID}' not registed!!!`;
|
||||
console.error(error);
|
||||
throw new Error(error);
|
||||
}
|
||||
clearTimeout(callbackData.timeoutHandle);
|
||||
|
||||
delete callbacks[callbackID];
|
||||
delete callbacks[callbackID];
|
||||
|
||||
if (message.error) {
|
||||
return callbackData.reject(message.error);
|
||||
}
|
||||
return callbackData.resolve(message.data);
|
||||
if (message.error) {
|
||||
return callbackData.reject(message.error);
|
||||
}
|
||||
return callbackData.resolve(message.data);
|
||||
}
|
||||
|
||||
// systemCall is used to call wails methods from the frontend
|
||||
export function SystemCall(method, data) {
|
||||
return Call('.wails.' + method, data);
|
||||
return Call('.wails.' + method, data);
|
||||
}
|
@ -14,92 +14,92 @@ import { SendMessage } from './ipc';
|
||||
|
||||
// Defines a single listener with a maximum number of times to callback
|
||||
class Listener {
|
||||
constructor(callback, maxCallbacks) {
|
||||
// Default of -1 means infinite
|
||||
maxCallbacks = maxCallbacks || -1;
|
||||
// Callback invokes the callback with the given data
|
||||
// Returns true if this listener should be destroyed
|
||||
this.Callback = (data) => {
|
||||
callback.apply(null, data);
|
||||
// If maxCallbacks is infinite, return false (do not destroy)
|
||||
if (maxCallbacks === -1) {
|
||||
return false;
|
||||
}
|
||||
// Decrement maxCallbacks. Return true if now 0, otherwise false
|
||||
maxCallbacks -= 1;
|
||||
return maxCallbacks === 0;
|
||||
};
|
||||
}
|
||||
constructor(callback, maxCallbacks) {
|
||||
// Default of -1 means infinite
|
||||
maxCallbacks = maxCallbacks || -1;
|
||||
// Callback invokes the callback with the given data
|
||||
// Returns true if this listener should be destroyed
|
||||
this.Callback = (data) => {
|
||||
callback.apply(null, data);
|
||||
// If maxCallbacks is infinite, return false (do not destroy)
|
||||
if (maxCallbacks === -1) {
|
||||
return false;
|
||||
}
|
||||
// Decrement maxCallbacks. Return true if now 0, otherwise false
|
||||
maxCallbacks -= 1;
|
||||
return maxCallbacks === 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
var eventListeners = {};
|
||||
|
||||
// Registers an event listener that will be invoked `maxCallbacks` times before being destroyed
|
||||
export function OnMultiple(eventName, callback, maxCallbacks) {
|
||||
eventListeners[eventName] = eventListeners[eventName] || [];
|
||||
const thisListener = new Listener(callback, maxCallbacks);
|
||||
eventListeners[eventName].push(thisListener);
|
||||
eventListeners[eventName] = eventListeners[eventName] || [];
|
||||
const thisListener = new Listener(callback, maxCallbacks);
|
||||
eventListeners[eventName].push(thisListener);
|
||||
}
|
||||
|
||||
// Registers an event listener that will be invoked every time the event is emitted
|
||||
export function On(eventName, callback) {
|
||||
OnMultiple(eventName, callback);
|
||||
OnMultiple(eventName, callback);
|
||||
}
|
||||
|
||||
// Registers an event listener that will be invoked once then destroyed
|
||||
export function Once(eventName, callback) {
|
||||
OnMultiple(eventName, callback, 1);
|
||||
OnMultiple(eventName, callback, 1);
|
||||
}
|
||||
|
||||
// Notify informs frontend listeners that an event was emitted with the given data
|
||||
export function Notify(eventName, data) {
|
||||
|
||||
// Check if we have any listeners for this event
|
||||
if (eventListeners[eventName]) {
|
||||
// Check if we have any listeners for this event
|
||||
if (eventListeners[eventName]) {
|
||||
|
||||
// Keep a list of listener indexes to destroy
|
||||
const newEventListenerList = eventListeners[eventName].slice();
|
||||
// Keep a list of listener indexes to destroy
|
||||
const newEventListenerList = eventListeners[eventName].slice();
|
||||
|
||||
// Iterate listeners
|
||||
for (let count = 0; count < eventListeners[eventName].length; count += 1) {
|
||||
// Iterate listeners
|
||||
for (let count = 0; count < eventListeners[eventName].length; count += 1) {
|
||||
|
||||
// Get next listener
|
||||
const listener = eventListeners[eventName][count];
|
||||
// Get next listener
|
||||
const listener = eventListeners[eventName][count];
|
||||
|
||||
// Parse data if we have it
|
||||
var parsedData = [];
|
||||
if (data) {
|
||||
try {
|
||||
parsedData = JSON.parse(data);
|
||||
} catch (e) {
|
||||
Error('Invalid JSON data sent to notify. Event name = ' + eventName);
|
||||
}
|
||||
}
|
||||
// Do the callback
|
||||
const destroy = listener.Callback(parsedData);
|
||||
if (destroy) {
|
||||
// if the listener indicated to destroy itself, add it to the destroy list
|
||||
newEventListenerList.splice(count, 1);
|
||||
}
|
||||
}
|
||||
// Parse data if we have it
|
||||
var parsedData = [];
|
||||
if (data) {
|
||||
try {
|
||||
parsedData = JSON.parse(data);
|
||||
} catch (e) {
|
||||
Error('Invalid JSON data sent to notify. Event name = ' + eventName);
|
||||
}
|
||||
}
|
||||
// Do the callback
|
||||
const destroy = listener.Callback(parsedData);
|
||||
if (destroy) {
|
||||
// if the listener indicated to destroy itself, add it to the destroy list
|
||||
newEventListenerList.splice(count, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Update callbacks with new list of listners
|
||||
eventListeners[eventName] = newEventListenerList;
|
||||
}
|
||||
// Update callbacks with new list of listners
|
||||
eventListeners[eventName] = newEventListenerList;
|
||||
}
|
||||
}
|
||||
|
||||
// Emit an event with the given name and data
|
||||
export function Emit(eventName) {
|
||||
|
||||
// Calculate the data
|
||||
var data = JSON.stringify([].slice.apply(arguments).slice(1));
|
||||
// Calculate the data
|
||||
var data = JSON.stringify([].slice.apply(arguments).slice(1));
|
||||
|
||||
// Notify backend
|
||||
const payload = {
|
||||
name: eventName,
|
||||
data: data,
|
||||
}
|
||||
SendMessage('event', payload)
|
||||
// Notify backend
|
||||
const payload = {
|
||||
name: eventName,
|
||||
data: data,
|
||||
}
|
||||
SendMessage('event', payload)
|
||||
}
|
||||
|
||||
const heartbeatCallbacks = {};
|
||||
@ -108,32 +108,32 @@ const heartbeatCallbacks = {};
|
||||
// the event is acknowledged via `Event.Acknowledge`. Once this happens, `callback` is invoked ONCE
|
||||
export function Heartbeat(eventName, timeInMilliseconds, callback) {
|
||||
|
||||
// Declare interval variable
|
||||
let interval = null;
|
||||
// Declare interval variable
|
||||
let interval = null;
|
||||
|
||||
// Setup callback
|
||||
function dynamicCallback() {
|
||||
// Kill interval
|
||||
clearInterval(interval);
|
||||
// Callback
|
||||
callback();
|
||||
}
|
||||
// Setup callback
|
||||
function dynamicCallback() {
|
||||
// Kill interval
|
||||
clearInterval(interval);
|
||||
// Callback
|
||||
callback();
|
||||
}
|
||||
|
||||
// Register callback
|
||||
heartbeatCallbacks[eventName] = dynamicCallback;
|
||||
// Register callback
|
||||
heartbeatCallbacks[eventName] = dynamicCallback;
|
||||
|
||||
// Start emitting the event
|
||||
interval = setInterval(function () {
|
||||
Emit(eventName);
|
||||
}, timeInMilliseconds);
|
||||
// Start emitting the event
|
||||
interval = setInterval(function () {
|
||||
Emit(eventName);
|
||||
}, timeInMilliseconds);
|
||||
}
|
||||
|
||||
export function Acknowledge(eventName) {
|
||||
// If we are waiting for acknowledgement for this event type
|
||||
if (heartbeatCallbacks[eventName]) {
|
||||
// Acknowledge!
|
||||
heartbeatCallbacks[eventName]();
|
||||
} else {
|
||||
throw new Error(`Cannot acknowledge unknown heartbeat '${eventName}'`);
|
||||
}
|
||||
// If we are waiting for acknowledgement for this event type
|
||||
if (heartbeatCallbacks[eventName]) {
|
||||
// Acknowledge!
|
||||
heartbeatCallbacks[eventName]();
|
||||
} else {
|
||||
throw new Error(`Cannot acknowledge unknown heartbeat '${eventName}'`);
|
||||
}
|
||||
}
|
@ -10,20 +10,20 @@ The lightweight framework for web-like apps
|
||||
/* jshint esversion: 6 */
|
||||
|
||||
function Invoke(message) {
|
||||
if (window && window.external && window.external.invoke) {
|
||||
window.external.invoke(message);
|
||||
} else {
|
||||
//eslint-disable-line
|
||||
console.log(`[No external.invoke] ${message}`);
|
||||
}
|
||||
if (window && window.external && window.external.invoke) {
|
||||
window.external.invoke(message);
|
||||
} else {
|
||||
//eslint-disable-line
|
||||
console.log(`[No external.invoke] ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function SendMessage(type, payload, callbackID) {
|
||||
const message = {
|
||||
type,
|
||||
callbackID,
|
||||
payload
|
||||
};
|
||||
const message = {
|
||||
type,
|
||||
callbackID,
|
||||
payload
|
||||
};
|
||||
|
||||
Invoke(JSON.stringify(message));
|
||||
Invoke(JSON.stringify(message));
|
||||
}
|
||||
|
@ -15,31 +15,31 @@ import { SendMessage } from './ipc';
|
||||
// level + message
|
||||
function sendLogMessage(level, message) {
|
||||
|
||||
// Log Message
|
||||
const payload = {
|
||||
level: level,
|
||||
message: message,
|
||||
}
|
||||
SendMessage('log', payload)
|
||||
// Log Message
|
||||
const payload = {
|
||||
level: level,
|
||||
message: message,
|
||||
}
|
||||
SendMessage('log', payload)
|
||||
}
|
||||
|
||||
export function Debug(message) {
|
||||
sendLogMessage('debug', message);
|
||||
sendLogMessage('debug', message);
|
||||
}
|
||||
|
||||
export function Info(message) {
|
||||
sendLogMessage('info', message);
|
||||
sendLogMessage('info', message);
|
||||
}
|
||||
|
||||
export function Warning(message) {
|
||||
sendLogMessage('warning', message);
|
||||
sendLogMessage('warning', message);
|
||||
}
|
||||
|
||||
export function Error(message) {
|
||||
sendLogMessage('error', message);
|
||||
sendLogMessage('error', message);
|
||||
}
|
||||
|
||||
export function Fatal(message) {
|
||||
sendLogMessage('fatal', message);
|
||||
sendLogMessage('fatal', message);
|
||||
}
|
||||
|
||||
|
@ -12,23 +12,23 @@ The lightweight framework for web-like apps
|
||||
import { Emit } from './events';
|
||||
|
||||
export function AddScript(js, callbackID) {
|
||||
var script = document.createElement('script');
|
||||
script.text = js;
|
||||
document.body.appendChild(script);
|
||||
if (callbackID) {
|
||||
Emit(callbackID);
|
||||
}
|
||||
var script = document.createElement('script');
|
||||
script.text = js;
|
||||
document.body.appendChild(script);
|
||||
if (callbackID) {
|
||||
Emit(callbackID);
|
||||
}
|
||||
}
|
||||
|
||||
// Adapted from webview - thanks zserge!
|
||||
export function InjectCSS(css) {
|
||||
var elem = document.createElement('style');
|
||||
elem.setAttribute('type', 'text/css');
|
||||
if (elem.styleSheet) {
|
||||
elem.styleSheet.cssText = css;
|
||||
} else {
|
||||
elem.appendChild(document.createTextNode(css));
|
||||
}
|
||||
var head = document.head || document.getElementsByTagName('head')[0];
|
||||
head.appendChild(elem);
|
||||
var elem = document.createElement('style');
|
||||
elem.setAttribute('type', 'text/css');
|
||||
if (elem.styleSheet) {
|
||||
elem.styleSheet.cssText = css;
|
||||
} else {
|
||||
elem.appendChild(document.createTextNode(css));
|
||||
}
|
||||
var head = document.head || document.getElementsByTagName('head')[0];
|
||||
head.appendChild(elem);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user