Skip to content

Commit

Permalink
feat(vuejs): convert DmAutomation component script to typescript
Browse files Browse the repository at this point in the history
* refact Astor to be a class
* disabled Null checks in tsconfig
  • Loading branch information
hbollon committed Jul 18, 2021
1 parent e0d65ce commit 50bd2ba
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 146 deletions.
232 changes: 122 additions & 110 deletions resources/static/vue-igopher/src/plugins/astilectron.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,132 @@
declare const astilectron: any; // eslint-disable-line
declare const astilectron: any;
import { Emitter } from 'mitt';
export class Astor {
skipWait: boolean;
debug: boolean;
isReady: boolean;
emitter: Emitter;

/* eslint-disable */
export default {
install (Vue: any, options: any) {
const { debug, skipWait, emitter } = options;
constructor(debug: boolean, skipWait: boolean, emitter: Emitter) {
this.debug = debug !== undefined ? debug : false;
this.skipWait = skipWait !== undefined ? skipWait : false;
this.emitter = emitter;
this.isReady = false;
}

const astor = {
skipWait: false,
debug: false,
isReady: false,
init: function() {
this.log('init');
this.isReady = false;

if (skipWait) {
this.onAstilectronReady();
return;
}
init() {
this.log('init');
this.isReady = false;

document.addEventListener('astilectron-ready', this.onAstilectronReady.bind(this));
},
onAstilectronReady: function() {
this.log('astilectron is ready');
astilectron.onMessage(this.onAstilectronMessage.bind(this));
this.log('removing ready listener');
document.removeEventListener('astilectron-ready', this.onAstilectronReady.bind(this));
this.isReady = true;
},
onIsReady: function(callback: any) {
let self = this;
let delay = 100;
if (!this.isReady) {
setTimeout( () => {
if (this.isReady) {
self.log('astor is ready');
callback();
} else {
self.onIsReady(callback);
}
}, delay);
} else {
this.log('astor is ready');
if (this.skipWait) {
this.onAstilectronReady();
return;
}

document.addEventListener('astilectron-ready', this.onAstilectronReady.bind(this));
}

onAstilectronReady() {
this.log('astilectron is ready');
astilectron.onMessage(this.onAstilectronMessage.bind(this));
this.log('removing ready listener');
document.removeEventListener('astilectron-ready', this.onAstilectronReady.bind(this));
this.isReady = true;
}

onIsReady(callback: any) {
let self = this;
let delay = 100;
if (!this.isReady) {
setTimeout( () => {
if (this.isReady) {
self.log('astor is ready');
callback();
}
},
onAstilectronMessage: function(message: any) {
if (Array.prototype.slice.call(arguments).length == 1) {
if (message) {
this.log('GO -> Vue', message);
this.emit(message.name, message);
}
} else {
let identifier = message;
message = Array.prototype.slice.call(arguments)[1];
if (message) {
this.log('GO -> Vue', message);
this.emit(identifier, message);
}
}
},
trigger: function(name: string, payload = {}, callback = null) {
let logMessage = 'Vue -> GO';
let identifier = name;

if (callback !== null) {
logMessage = logMessage + ' (scoped)';
identifier = identifier + this.getScope();
}

this.log(logMessage, {name: name, payload: payload});
if (callback !== null) {
this.listen(identifier, callback, true);
}
astilectron.sendMessage({msg: name, payload: payload}, this.onAstilectronMessage.bind(this, identifier));
},
listen: function(name: any, callback: any, once = false) {
if (once) {
this.log('listen once', {name: name, callback: callback});
const wrappedHandler = (evt: any) => {
callback(evt);
emitter.off(name, wrappedHandler);
}
emitter.on(name, wrappedHandler);
} else {
this.log('listen', {name: name, callback: callback});
emitter.on(name, callback);
}
},
emit: function(name: any, payload = {}) {
this.log('EMIT', {name: name, payload: payload});
emitter.emit(name, payload);
},
remove: function(name: any, callback: any) {
emitter.off(name, callback);
},
log: function (message: any, data?: any) {
if (!this.debug) {
return;
self.onIsReady(callback);
}
}, delay);
} else {
this.log('astor is ready');
callback();
}
}

if (data) {
console.log('ASTOR | ' + message, data);
} else {
console.log('ASTOR | ' + message);
}
},
getScope: function() {
return '#' + Math.random().toString(36).substr(2, 7);
onAstilectronMessage(message: any) {
if (Array.prototype.slice.call(arguments).length == 1) {
if (message) {
this.log('GO -> Vue', message);
this.emit(message.name, message);
}
} else {
let identifier = message;
message = Array.prototype.slice.call(arguments)[1];
if (message) {
this.log('GO -> Vue', message);
this.emit(identifier, message);
}
}

Vue.config.globalProperties.$astor = astor;
Vue.config.globalProperties.$astor.debug = debug;
Vue.config.globalProperties.$astor.skipWait = skipWait;
Vue.config.globalProperties.$astor.init();

Vue.provide('astor', astor);
}
}

trigger(name: string, payload = {}, callback: any = null) {
let logMessage = 'Vue -> GO';
let identifier = name;

if (callback !== null) {
logMessage = logMessage + ' (scoped)';
identifier = identifier + this.getScope();
}

this.log(logMessage, {name: name, payload: payload});
if (callback !== null) {
this.listen(identifier, callback, true);
}
astilectron.sendMessage({msg: name, payload: payload}, this.onAstilectronMessage.bind(this, identifier));
}

listen(name: any, callback: any, once = false) {
if (once) {
this.log('listen once', {name: name, callback: callback});
const wrappedHandler = (evt: any) => {
callback(evt);
this.emitter.off(name, wrappedHandler);
}
this.emitter.on(name, wrappedHandler);
} else {
this.log('listen', {name: name, callback: callback});
this.emitter.on(name, callback);
}
}

emit(name: any, payload = {}) {
this.log('EMIT', {name: name, payload: payload});
this.emitter.emit(name, payload);
}

remove(name: any, callback: any) {
this.emitter.off(name, callback);
}

log(message: any, data?: any) {
if (!this.debug) {
return;
}

if (data) {
console.log('ASTOR | ' + message, data);
} else {
console.log('ASTOR | ' + message);
}
}

getScope() {
return '#' + Math.random().toString(36).substr(2, 7);
}
}

export default {
install (Vue: any, options: any) {
const { debug, skipWait, emitter } = options;

const astor: Astor = new Astor(debug, skipWait, emitter)
}
}

0 comments on commit 50bd2ba

Please sign in to comment.