Skip to content

Commit

Permalink
fix: properly reload the transformer script (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe authored Feb 6, 2023
1 parent c18a0af commit 78f9ee8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 38 deletions.
4 changes: 2 additions & 2 deletions js/import/import.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ const attachListeners = () => {
processNext();
}));

IMPORTFILEURL_FIELD.addEventListener('change', (event) => {
IMPORTFILEURL_FIELD.addEventListener('change', async (event) => {
if (config.importer) {
config.importer.setImportFileURL(event.target.value);
await config.importer.setImportFileURL(event.target.value);
}
});

Expand Down
86 changes: 50 additions & 36 deletions js/shared/pollimporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,73 @@ export default class PollImporter {
this.listeners = [];
this.errorListeners = [];
this.transformation = {};
this.projectTransform = null;
this.projectTransformFileURL = '';
this.running = false;

this.#init();
}

async #init() {
async #loadProjectTransform() {
const $this = this;
const loadModule = async (projectTransformFileURL) => {
try {
const mod = await import(projectTransformFileURL);
if (mod.default) {
this.projectTransform = mod.default;
}
} catch (err) {
// eslint-disable-next-line no-console
console.warn('failed to load project transform module', err);
const mod = await import(projectTransformFileURL);
if (mod.default) {
$this.projectTransform = mod.default;
}
};
const poll = async () => {
const projectTransformFileURL = `${this.config.importFileURL}?cf=${new Date().getTime()}`;
try {
const res = await fetch(projectTransformFileURL);
const body = await res.text();

if (body !== $this.lastProjectTransformFileBody) {
$this.lastProjectTransformFileBody = body;
await loadModule(projectTransformFileURL);
if ($this.transformation.url && $this.transformation.document) {
$this.transform();
}
}
} catch (err) {
if ($this.lastProjectTransformFileBody !== 'nofilefound') {
// eslint-disable-next-line no-console
console.warn('failed to poll project transform module', err);
$this.lastProjectTransformFileBody = 'nofilefound';
if ($this.transformation.url && $this.transformation.document) {
$this.transform();
}
}
const projectTransformFileURL = `${this.config.importFileURL}?cf=${new Date().getTime()}`;
let body = '';
try {
const res = await fetch(projectTransformFileURL);
body = await res.text();

if (res.ok && body !== this.lastProjectTransformFileBody) {
this.lastProjectTransformFileBody = body;
await loadModule(projectTransformFileURL);
this.projectTransformFileURL = projectTransformFileURL;
// eslint-disable-next-line no-console
console.log(`Loaded importer file: ${projectTransformFileURL}`);
return true;
}
};
} catch (err) {
// ignore here, we know the file does not exist
}
if (body !== this.lastProjectTransformFileBody) {
// eslint-disable-next-line no-console
console.warn(`Importer file does not exist: ${projectTransformFileURL}`);
this.lastProjectTransformFileBody = body;
this.projectTransformFileURL = '';
return true;
}
return false;
}

if (!this.projectTransformInterval) {
await poll();
if (this.poll) {
this.projectTransformInterval = setInterval(poll, 5000);
async #init() {
const $this = this;
const poll = async () => {
if ($this.running) return;
const hasChanged = await $this.#loadProjectTransform();
if (hasChanged && $this.transformation.url && $this.transformation.document) {
$this.transform();
}
};

await poll();
if (!this.projectTransformInterval && this.poll) {
this.projectTransformInterval = setInterval(poll, 5000);
}
}

async transform() {
this.running = true;
const {
includeDocx, url, document, params,
} = this.transformation;

// eslint-disable-next-line no-console
console.log(`Starting transformation of ${url} with import file: ${this.projectTransformFileURL || 'none (default)'}`);
try {
let results;
if (includeDocx) {
Expand Down Expand Up @@ -118,6 +130,7 @@ export default class PollImporter {
});
});
}
this.running = false;
}

setTransformationInput({
Expand All @@ -134,8 +147,9 @@ export default class PollImporter {
};
}

setImportFileURL(importFileURL) {
async setImportFileURL(importFileURL) {
this.config.importFileURL = importFileURL;
await this.#loadProjectTransform();
}

addListener(listener) {
Expand Down

0 comments on commit 78f9ee8

Please sign in to comment.