Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
get testing set up (#9)
Browse files Browse the repository at this point in the history
also register event handlers before firing
  • Loading branch information
alexkolson committed Nov 25, 2021
1 parent 86f8e0d commit d160999
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
29 changes: 7 additions & 22 deletions extension/advanzia-assistant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface ContentScript {
init(): void;
execute(): void;
};

Expand All @@ -16,7 +15,6 @@ export interface WasmExports extends WebAssembly.Exports {
};

export interface ContentScriptEvents {
readonly instantiateWasm: Event;
readonly ready: Event;
readonly done: Event;
readonly error: ErrorEvent;
Expand All @@ -27,6 +25,7 @@ export class Script extends EventTarget implements ContentScript, EventListenerO
private memory: WebAssembly.Memory;
private wasmExports?: WasmExports;


private events: ContentScriptEvents;

status: ContentScriptStatus;
Expand All @@ -38,7 +37,6 @@ export class Script extends EventTarget implements ContentScript, EventListenerO
this.status = ContentScriptStatus.Initializing;
this.memory = new WebAssembly.Memory({ initial: 10 });
this.events = {
instantiateWasm: new Event('instantiatedWasm'),
ready: new Event('ready'),
done: new Event('done'),
error: new ErrorEvent('error')
Expand All @@ -47,13 +45,6 @@ export class Script extends EventTarget implements ContentScript, EventListenerO
this.errors = [];

this.registerEventListeners();

if (location.pathname.indexOf('retail-app') === -1) {
this.dispatchEvent(this.events.done);
return;
}

this.dispatchEvent(this.events.instantiateWasm);
}

private registerEventListeners() {
Expand All @@ -66,9 +57,6 @@ export class Script extends EventTarget implements ContentScript, EventListenerO

handleEvent(e: Event | ErrorEvent) {
switch (e.type) {
case this.events.instantiateWasm.type:
this.init();
break;
case this.events.ready.type:
this.status = ContentScriptStatus.Ready;
break;
Expand All @@ -86,7 +74,12 @@ export class Script extends EventTarget implements ContentScript, EventListenerO
return this;
}

async init() {
async execute() {
if (location.pathname.indexOf('retail-app') === -1) {
this.dispatchEvent(this.events.done);
return;
}

const wasmResponse = await fetch(chrome.runtime.getURL('advanzia-assistant.wasm'));
const wasm = await WebAssembly.instantiateStreaming(wasmResponse, { env: { memory: this.memory } });
this.wasmExports = wasm.instance.exports as WasmExports;
Expand All @@ -96,14 +89,6 @@ export class Script extends EventTarget implements ContentScript, EventListenerO
this.dispatchEvent(this.events.ready);
}

async execute() {
if (this.status !== ContentScriptStatus.Ready) {
this.error(new Error(`Script cannot be executed. Script needs to have ready status but status is ${this.status}`));
}

this.dispatchEvent(this.events.done);
}

private error(e: Error) {
this.errors.push(e);
this.dispatchEvent(this.events.error);
Expand Down
4 changes: 2 additions & 2 deletions extension/content-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Script } from './advanzia-assistant';
const script = new Script();

script
.on('ready', () => script.execute())
.on('done', () => console.log('done'))
.on('error', (e) => console.log({ errors: script.errors, status: script.status }));
.on('error', (e) => console.log({ errors: script.errors, status: script.status }))
.execute();
4 changes: 2 additions & 2 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
},
"scripts": {
"build": "webpack",
"test": "echo 'No unit tests for the chrome extension content script for now..all good'"
"test": "jest"
}
}
}

0 comments on commit d160999

Please sign in to comment.