5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 19:50:15 +08:00

basics working

This commit is contained in:
Lea Anthony 2019-02-19 21:06:24 +11:00
parent a4b1f469e9
commit d971495ad3
5 changed files with 56 additions and 43 deletions

2
app.go
View File

@ -46,6 +46,8 @@ func CreateApp(optionalConfig ...*AppConfig) *App {
log: newCustomLogger("App"), log: newCustomLogger("App"),
} }
result.log.Info("AIOSDOIIOASIDAOSIOASID")
appconfig, err := newAppConfig(userConfig) appconfig, err := newAppConfig(userConfig)
if err != nil { if err != nil {
result.log.Fatalf("Cannot use custom HTML: %s", err.Error()) result.log.Fatalf("Cannot use custom HTML: %s", err.Error())

File diff suppressed because one or more lines are too long

View File

@ -1 +1,5 @@
<div id="app"></div> <!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"></head>
<body><div id="app"></div><script type="text/javascript"></script></body>
</html>

View File

@ -1,5 +1,4 @@
// Wails runtime JS // Wails runtime JS
(function () { (function () {
window.wails = window.wails || {}; window.wails = window.wails || {};
window.backend = {}; window.backend = {};
@ -11,7 +10,8 @@
function cryptoRandom() { function cryptoRandom() {
var array = new Uint32Array(1); var array = new Uint32Array(1);
return window.crypto.getRandomValues(array)[0]; return window.crypto.getRandomValues(array)[0];
} }
// LOLRandom // LOLRandom
function basicRandom() { function basicRandom() {
@ -32,9 +32,9 @@
// Don't xss yourself :-) // Don't xss yourself :-)
try { try {
new Function("var " + name); new Function("var " + name);
return true return true;
} catch (e) { } catch (e) {
return false return false;
} }
} }
@ -57,7 +57,7 @@
elem.appendChild(document.createTextNode(css)); elem.appendChild(document.createTextNode(css));
} }
var head = document.head || document.getElementsByTagName('head')[0]; var head = document.head || document.getElementsByTagName('head')[0];
head.appendChild(elem) head.appendChild(elem);
} }
/************************* Bindings *************************/ /************************* Bindings *************************/
@ -67,25 +67,27 @@
// Creates the path given in the bindings path // Creates the path given in the bindings path
function addBindingPath(pathSections) { function addBindingPath(pathSections) {
// Start at the base path // Start at the base path
var currentPath = bindingsBasePath var currentPath = bindingsBasePath;
// for each section of the given path // for each section of the given path
for (var section of pathSections) { for (var sectionIndex in pathSections) {
var section = pathSections[sectionIndex];
// Is section a valid javascript identifier? // Is section a valid javascript identifier?
if (!isValidIdentifier(section)) { if (!isValidIdentifier(section)) {
var errMessage = section + " is not a valid javascript identifier." var errMessage = section + " is not a valid javascript identifier.";
var err = new Error(errMessage) var err = new Error(errMessage);
return [null, err] return [null, err];
} }
// Add if doesn't exist // Add if doesn't exist
if (!currentPath[section]) { if (!currentPath[section]) {
currentPath[section] = {} currentPath[section] = {};
} }
// update current path to new path // update current path to new path
currentPath = currentPath[section] currentPath = currentPath[section];
} }
return [currentPath, null] return [currentPath, null];
} }
function newBinding(bindingName) { function newBinding(bindingName) {
@ -96,15 +98,17 @@
// Get the actual function/method call name // Get the actual function/method call name
var callName = bindingSections.pop(); var callName = bindingSections.pop();
let pathToBinding; var pathToBinding;
let err; var err;
// Add path to binding // Add path to binding
[pathToBinding, err] = addBindingPath(bindingSections) var bs = addBindingPath(bindingSections);
var pathToBinding = bs[0];
var err = bs[1];
if (err != null) { if (err != null) {
// We need to return an error // We need to return an error
return err return err;
} }
// Add binding call // Add binding call
@ -115,7 +119,7 @@
// Actual function // Actual function
function dynamic() { function dynamic() {
var args = [].slice.call(arguments) var args = [].slice.call(arguments);
return call(bindingName, args, timeout); return call(bindingName, args, timeout);
} }
@ -160,12 +164,12 @@
var callbackID; var callbackID;
do { do {
callbackID = bindingName + "-" + randomFunc(); callbackID = bindingName + "-" + randomFunc();
} while (callbacks[callbackID]) } while (callbacks[callbackID]);
// Set timeout // Set timeout
if (timeout > 0) { if (timeout > 0) {
var timeoutHandle = setTimeout(function () { var timeoutHandle = setTimeout(function () {
reject(Error("Call to " + bindingName + " timed out. Request ID: " + callbackID)) reject(Error("Call to " + bindingName + " timed out. Request ID: " + callbackID));
}, timeout); }, timeout);
} }
@ -176,7 +180,7 @@
resolve: resolve resolve: resolve
} }
try { try {
var payloaddata = JSON.stringify(data) var payloaddata = JSON.stringify(data);
// Create the message // Create the message
message = { message = {
type: "call", type: "call",
@ -188,39 +192,38 @@
} }
// Make the call // Make the call
var payload = JSON.stringify(message) var payload = JSON.stringify(message);
external.invoke(payload); external.invoke(payload);
} catch (e) { } catch (e) {
console.error(e) console.error(e);
} }
}) })
} }
// Called by the backend to return data to a previously called // Called by the backend to return data to a previously called
// binding invocation // binding invocation
function callback(incomingMessage) { function callback(incomingMessage) {
// Parse the message // Parse the message
var message var message;
try { try {
message = JSON.parse(incomingMessage) message = JSON.parse(incomingMessage);
} catch (e) { } catch (e) {
wails.log.debug("Invalid JSON passed to callback: " + e.message) wails.log.debug("Invalid JSON passed to callback: " + e.message);
wails.log.debug("Message: " + incomingMessage) wails.log.debug("Message: " + incomingMessage);
return return;
} }
callbackID = message.callbackid callbackID = message.callbackid;
callbackData = callbacks[callbackID] callbackData = callbacks[callbackID];
if (!callbackData) { if (!callbackData) {
console.error("Callback '" + callbackID + "' not registed!!!") console.error("Callback '" + callbackID + "' not registed!!!");
return return
} }
clearTimeout(callbackData.timeoutHandle) clearTimeout(callbackData.timeoutHandle);
delete callbacks[callbackID] delete callbacks[callbackID];
if (message.error) { if (message.error) {
return callbackData.reject(message.error) return callbackData.reject(message.error);
} }
return callbackData.resolve(message.data) return callbackData.resolve(message.data);
} }
/************************************************************/ /************************************************************/
@ -236,11 +239,12 @@
eventListeners[eventName].push(callback); eventListeners[eventName].push(callback);
} }
// notify informs frontend listeners that an event was emitted with the given data // notify informs frontend listeners that an event was emitted with the given data
function notify(eventName, data) { function notify(eventName, data) {
if (eventListeners[eventName]) { if (eventListeners[eventName]) {
eventListeners[eventName].forEach(element => { eventListeners[eventName].forEach(function(element) {
var parsedData = [] var parsedData = [];
// Parse data if we have it // Parse data if we have it
if (data) { if (data) {
try { try {
@ -274,6 +278,7 @@
// Events calls // Events calls
window.wails.events = { emit: emit, on: on }; window.wails.events = { emit: emit, on: on };
/************************************************************/ /************************************************************/
/************************* Logging **************************/ /************************* Logging **************************/
@ -327,7 +332,8 @@
callbacks: callbacks, callbacks: callbacks,
injectCSS: injectCSS, injectCSS: injectCSS,
addScript: addScript, addScript: addScript,
} }
/************************************************************/ /************************************************************/

View File

@ -0,0 +1 @@
(function(){window.wails=window.wails||{};window.backend={};function cryptoRandom(){var array=new Uint32Array(1);return window.crypto.getRandomValues(array)[0]}function basicRandom(){return Math.random()*9007199254740991}var randomFunc;if(window.crypto){randomFunc=cryptoRandom}else{randomFunc=basicRandom}function isValidIdentifier(name){try{new Function("var "+name);return true}catch(e){return false}}function addScript(js,callbackID){var script=document.createElement("script");script.text=js;document.body.appendChild(script);window.wails.events.emit(callbackID)}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 bindingsBasePath=window.backend;function addBindingPath(pathSections){var currentPath=bindingsBasePath;for(var sectionIndex in pathSections){var section=pathSections[sectionIndex];if(!isValidIdentifier(section)){var errMessage=section+" is not a valid javascript identifier.";var err=new Error(errMessage);return[null,err]}if(!currentPath[section]){currentPath[section]={}}currentPath=currentPath[section]}return[currentPath,null]}function newBinding(bindingName){var bindingSections=bindingName.split('.').splice(1);var callName=bindingSections.pop();var pathToBinding;var err;var bs=addBindingPath(bindingSections);var pathToBinding=bs[0];var err=bs[1];if(err!=null){return err}pathToBinding[callName]=function(){var timeout=0;function dynamic(){var args=[].slice.call(arguments);return call(bindingName,args,timeout)}dynamic.setTimeout=function(newTimeout){timeout=newTimeout};dynamic.getTimeout=function(){return timeout};return dynamic}()}var callbacks={};function call(bindingName,data,timeout){if(timeout==null||timeout==undefined){timeout=0}return new Promise(function(resolve,reject){var callbackID;do{callbackID=bindingName+"-"+randomFunc()}while(callbacks[callbackID]);if(timeout>0){var timeoutHandle=setTimeout(function(){reject(Error("Call to "+bindingName+" timed out. Request ID: "+callbackID))},timeout)}callbacks[callbackID]={timeoutHandle:timeoutHandle,reject:reject,resolve:resolve};try{var payloaddata=JSON.stringify(data);message={type:"call",callbackid:callbackID,payload:{bindingName:bindingName,data:payloaddata}};var payload=JSON.stringify(message);external.invoke(payload)}catch(e){console.error(e)}})}function callback(incomingMessage){var message;try{message=JSON.parse(incomingMessage)}catch(e){wails.log.debug("Invalid JSON passed to callback: "+e.message);wails.log.debug("Message: "+incomingMessage);return}callbackID=message.callbackid;callbackData=callbacks[callbackID];if(!callbackData){console.error("Callback '"+callbackID+"' not registed!!!");return}clearTimeout(callbackData.timeoutHandle);delete callbacks[callbackID];if(message.error){return callbackData.reject(message.error)}return callbackData.resolve(message.data)}var eventListeners={};function on(eventName,callback){eventListeners[eventName]=eventListeners[eventName]||[];eventListeners[eventName].push(callback)}function notify(eventName,data){if(eventListeners[eventName]){eventListeners[eventName].forEach(function(element){var parsedData=[];if(data){try{parsedData=JSON.parse(data)}catch(e){wails.log.error("Invalid JSON data sent to notify. Event name = "+eventName)}}element.apply(null,parsedData)})}}function emit(eventName){var data=JSON.stringify([].slice.apply(arguments).slice(1));message={type:"event",payload:{name:eventName,data:data}};external.invoke(JSON.stringify(message))}window.wails.events={emit:emit,on:on};function sendLogMessage(level,message){message={type:"log",payload:{level:level,message:message}};external.invoke(JSON.stringify(message))}function logDebug(message){sendLogMessage("debug",message)}function logInfo(message){sendLogMessage("info",message)}function logWarning(message){sendLogMessage("warning",message)}function logError(message){sendLogMessage("error",message)}function logFatal(message){sendLogMessage("fatal",message)}window.wails.log={debug:logDebug,info:logInfo,warning:logWarning,error:logError,fatal:logFatal};window.wails._={newBinding:newBinding,callback:callback,notify:notify,sendLogMessage:sendLogMessage,callbacks:callbacks,injectCSS:injectCSS,addScript:addScript};window.wails.events.emit("wails:loaded");alert("end");})();