Skip to content

Commit

Permalink
Unwind async/await chains (#957)
Browse files Browse the repository at this point in the history
*Cleanup several spots where runtime.run() no longer needs to be awaited, now that #928 is merged.
  • Loading branch information
JeffersGlass committed Nov 16, 2022
1 parent 0b23310 commit 7e24289
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 24 deletions.
6 changes: 3 additions & 3 deletions pyscriptjs/src/components/pybutton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function make_PyButton(runtime: Runtime) {
}
}

async connectedCallback() {
connectedCallback() {
const deprecationMessage = (
'The element <py-button> is deprecated, create a function with your ' +
'inline code and use <button py-click="function()" class="py-button"> instead.'
Expand Down Expand Up @@ -76,8 +76,8 @@ export function make_PyButton(runtime: Runtime) {

// now that we appended and the element is attached, lets connect with the event handlers
// defined for this widget
await runtime.runButDontRaise(this.code);
await runtime.runButDontRaise(registrationCode);
runtime.runButDontRaise(this.code);
runtime.runButDontRaise(registrationCode);
logger.debug('py-button connected');
}
}
Expand Down
6 changes: 3 additions & 3 deletions pyscriptjs/src/components/pyinputbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function make_PyInputBox(runtime: Runtime) {
}
}

async connectedCallback() {
connectedCallback() {
const deprecationMessage = (
'The element <py-input> is deprecated, ' +
'use <input class="py-input"> instead.'
Expand Down Expand Up @@ -50,8 +50,8 @@ export function make_PyInputBox(runtime: Runtime) {
registrationCode += `\n${this.mount_name}.element.addEventListener('keypress', create_proxy(on_keypress_${this.mount_name}))`;
}

await runtime.runButDontRaise(this.code);
await runtime.runButDontRaise(registrationCode);
runtime.runButDontRaise(this.code);
runtime.runButDontRaise(registrationCode);
logger.debug('py-inputbox connected');
}
}
Expand Down
16 changes: 7 additions & 9 deletions pyscriptjs/src/components/pyscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ const pyAttributeToEvent: Map<string, string> = new Map<string, string>([
]);

/** Initialize all elements with py-* handlers attributes */
export async function initHandlers(runtime: Runtime) {
export function initHandlers(runtime: Runtime) {
logger.debug('Initializing py-* event handlers...');
for (const pyAttribute of pyAttributeToEvent.keys()) {
await createElementsWithEventListeners(runtime, pyAttribute);
createElementsWithEventListeners(runtime, pyAttribute);
}
}

/** Initializes an element with the given py-on* attribute and its handler */
async function createElementsWithEventListeners(runtime: Runtime, pyAttribute: string): Promise<void> {
function createElementsWithEventListeners(runtime: Runtime, pyAttribute: string) {
const matches: NodeListOf<HTMLElement> = document.querySelectorAll(`[${pyAttribute}]`);
for (const el of matches) {
if (el.id.length === 0) {
Expand All @@ -161,12 +161,10 @@ async function createElementsWithEventListeners(runtime: Runtime, pyAttribute: s
from pyodide.ffi import create_proxy
Element("${el.id}").element.addEventListener("${event}", create_proxy(${handlerCode}))
`;
await runtime.run(source);
runtime.run(source);
} else {
el.addEventListener(event, () => {
(async () => {
await runtime.run(handlerCode);
})();
runtime.run(handlerCode);
});
}
// TODO: Should we actually map handlers in JS instead of Python?
Expand All @@ -186,7 +184,7 @@ async function createElementsWithEventListeners(runtime: Runtime, pyAttribute: s
}

/** Mount all elements with attribute py-mount into the Python namespace */
export async function mountElements(runtime: Runtime) {
export function mountElements(runtime: Runtime) {
const matches: NodeListOf<HTMLElement> = document.querySelectorAll('[py-mount]');
logger.info(`py-mount: found ${matches.length} elements`);

Expand All @@ -195,5 +193,5 @@ export async function mountElements(runtime: Runtime) {
const mountName = el.getAttribute('py-mount') || el.id.split('-').join('_');
source += `\n${mountName} = Element("${el.id}")`;
}
await runtime.run(source);
runtime.run(source);
}
4 changes: 2 additions & 2 deletions pyscriptjs/src/components/pywidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ function createWidget(runtime: Runtime, name: string, code: string, klass: strin
this.shadow.appendChild(this.wrapper);
}

async connectedCallback() {
await runtime.runButDontRaise(this.code);
connectedCallback() {
runtime.runButDontRaise(this.code);
this.proxyClass = runtime.globals.get(this.klass);
this.proxy = this.proxyClass(this);
this.proxy.connect();
Expand Down
4 changes: 2 additions & 2 deletions pyscriptjs/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class PyScriptApp {

this.logStatus('Setting up virtual environment...');
await this.setupVirtualEnv(runtime);
await mountElements(runtime);
mountElements(runtime);

// lifecycle (6.5)
this.plugins.afterSetup(runtime);
Expand All @@ -188,7 +188,7 @@ export class PyScriptApp {
// lifecycle (8)
createCustomElements(runtime);

await initHandlers(runtime);
initHandlers(runtime);

// NOTE: runtime message is used by integration tests to know that
// pyscript initialization has complete. If you change it, you need to
Expand Down
2 changes: 1 addition & 1 deletion pyscriptjs/src/pyodide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class PyodideRuntime extends Runtime {
await this.loadPackage('micropip');

logger.info('importing pyscript.py');
await this.run(pyscript as string);
this.run(pyscript as string);

logger.info('pyodide loaded and initialized');
}
Expand Down
8 changes: 4 additions & 4 deletions pyscriptjs/tests/unit/pyodide.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ describe('PyodideRuntime', () => {
});

it('should check if runtime can run python code asynchronously', async () => {
expect(await runtime.run("2+3")).toBe(5);
expect(runtime.run("2+3")).toBe(5);
});

it('should capture stdout', async () => {
stdio.reset();
await runtime.run("print('hello')");
runtime.run("print('hello')");
expect(stdio.captured_stdout).toBe("hello\n");
});

it('should check if runtime is able to load a package', async () => {
await runtime.loadPackage("numpy");
await runtime.run("import numpy as np");
await runtime.run("x = np.ones((10,))");
runtime.run("import numpy as np");
runtime.run("x = np.ones((10,))");
expect(runtime.globals.get('x').toJs()).toBeInstanceOf(Float64Array);
});

Expand Down

0 comments on commit 7e24289

Please sign in to comment.