Files
pybricks-code/src/sagas/mpy.ts
T
David Lechner 69e52be259 remove wasm file extension rename workaround
This modifies the webpack config instead of renaming the .wasm file
to workaround default webpack behavior regarding import .wasm files.
2021-01-11 16:47:49 -06:00

39 lines
1.2 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2020 The Pybricks Authors
import { CompileResult, compile as mpyCrossCompile } from '@pybricks/mpy-cross-v5';
import wasm from '@pybricks/mpy-cross-v5/build/mpy-cross.wasm';
import { call, put, takeEvery } from 'redux-saga/effects';
import {
MpyActionType,
MpyCompileAction,
didCompile,
didFailToCompile,
} from '../actions/mpy';
/**
* Compiles a script to .mpy and dispatches either didCompile on success or
* didFailToCompile on error.
* @param action A mpy compile action.
*/
function* compile(action: MpyCompileAction): Generator {
const result = (yield call(() =>
mpyCrossCompile(
'main.py',
action.script,
action.options,
// HACK: testing user agent for jsdom is needed only for getting unit tests to work
navigator.userAgent.includes('jsdom') ? undefined : wasm,
),
)) as CompileResult;
if (result.status === 0 && result.mpy) {
yield put(didCompile(result.mpy));
} else {
yield put(didFailToCompile(result.err));
}
}
export default function* (): Generator {
yield takeEvery(MpyActionType.Compile, compile);
}