Skip to content

Commit

Permalink
Backward incompatible changes on JS API
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Aug 20, 2021
1 parent 8aef50f commit 42ce422
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion adia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .renderer import Renderer


__version__ = '3.0.1'
__version__ = '4.0.0'
__all__ = [
'Diagram',
'SequenceDiagram',
Expand Down
48 changes: 22 additions & 26 deletions webclinic/adia.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,37 @@ class ADia {
delay = 0; // ms
loadingProbeInterval = 300; // ms
input;
source;

/* Hooks */
onresult = null
onsuccess = null
onerror = null
onstatus = null
oninit = null

// inititializing, processing, idle
#_status = 'initializing';

/* Private Fields */
#_delayTimer;
#_source;

constructor() {
this.hooks = {
result: [],
success: [],
error: [],
status: [],
init: [],
}
this.ensureADiaAPI();
}

addHook(name, func) {
let handlers = this.hooks[name]
if (!handlers.includes(func)) {
handlers.push(func)
}
}

hook(name, data) {
let handlers = this.hooks[name]
if (handlers == undefined) {
let handler = this[name]
if (handler == undefined) {
throw `Invalid hook name: ${name}`;
}

for (var i = 0; i < handlers.length; i++) {
handlers[i](this, data);
if (handler == null) {
return
}

handler(this, data);
}

get status() {
Expand All @@ -45,7 +41,7 @@ class ADia {

set status(newValue) {
this.#_status = newValue;
this.hook('status', newValue);
this.hook('onstatus', newValue);
}

ensureADiaAPI() {
Expand All @@ -63,14 +59,14 @@ class ADia {
return;
}
let newSource = this.input();
if (this.#_source == newSource) {
if (this.source == newSource) {
/* Do Nothing */
return;
}

this.status = 'processing';
this.#_source = newSource;
window.__adia__.send(this.#_source);
this.source = newSource;
window.__adia__.send(this.source);
}

go() {
Expand All @@ -93,15 +89,15 @@ class ADia {
onResult(result) {
if (result.version != undefined) {
this.__version__ = result.version
this.hook('init')
this.hook('oninit')
}
else {
this.hook('result', result)
this.hook('onresult', result)
if (result.error) {
this.hook('error', result.error)
this.hook('onerror', result.error)
}
else {
this.hook('success', result.diagram)
this.hook('onsuccess', result.diagram)
}
}
this.status = 'idle';
Expand Down
16 changes: 8 additions & 8 deletions webclinic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@

/* ADia configuration */
window.aDia.delay = 10
window.aDia.addHook('init', (aDia) => {
window.aDia.oninit = (aDia) => {
versionArea.innerText = `ADia version: ${aDia.__version__}`
});
};

window.aDia.input = () => sourceArea.value
window.aDia.addHook('result', () => {
window.aDia.input = () => sourceArea.value
window.aDia.onresult = () => {
errorArea.value = '';
targetArea.value = '';
});
};

window.aDia.addHook('success', (aDia, dia) => targetArea.value = dia)
window.aDia.addHook('error', (aDia, err) => errorArea.value = msg)
window.aDia.addHook('status', (aDia, state) => statusArea.innerText = state)
window.aDia.onsuccess = (aDia, dia) => targetArea.value = dia
window.aDia.onerror = (aDia, err) => errorArea.value = msg
window.aDia.onstatus = (aDia, state) => statusArea.innerText = state


const go = aDia.go.bind(aDia);
Expand Down

0 comments on commit 42ce422

Please sign in to comment.