From ba11ccbd23dc44affaddce99e4271377fb7eafa2 Mon Sep 17 00:00:00 2001 From: "Joshua A. Horton" Date: Thu, 15 Nov 2018 09:37:19 +0700 Subject: [PATCH] Prototype of a directly-embedded WebWorker. --- common/predictive-text/testing/index.html | 1 + .../one-stage-embedded-webworker/.gitignore | 2 + .../one-stage-embedded-webworker/build.sh | 65 +++++++++++++++++++ .../one-stage-embedded-webworker/index.html | 42 ++++++++++++ .../one-stage-embedded-webworker/main.ts | 52 +++++++++++++++ .../tsconfig.json | 13 ++++ common/predictive-text/tsconfig.json | 4 ++ 7 files changed, 179 insertions(+) create mode 100644 common/predictive-text/testing/one-stage-embedded-webworker/.gitignore create mode 100644 common/predictive-text/testing/one-stage-embedded-webworker/build.sh create mode 100644 common/predictive-text/testing/one-stage-embedded-webworker/index.html create mode 100644 common/predictive-text/testing/one-stage-embedded-webworker/main.ts create mode 100644 common/predictive-text/testing/one-stage-embedded-webworker/tsconfig.json diff --git a/common/predictive-text/testing/index.html b/common/predictive-text/testing/index.html index 61201e36ad..5e57fa398a 100644 --- a/common/predictive-text/testing/index.html +++ b/common/predictive-text/testing/index.html @@ -21,5 +21,6 @@

Language-Modeling Layer module testing

A simple basic WebWorker.

Designed as a baseline functionality test we can run against various platforms as a first-stage canary of sorts. +

A simple basic WebWorker.

A prototype for directly embedding a WebWorker's code within a "master" script. diff --git a/common/predictive-text/testing/one-stage-embedded-webworker/.gitignore b/common/predictive-text/testing/one-stage-embedded-webworker/.gitignore new file mode 100644 index 0000000000..df6361deda --- /dev/null +++ b/common/predictive-text/testing/one-stage-embedded-webworker/.gitignore @@ -0,0 +1,2 @@ +main.js +main.js.map \ No newline at end of file diff --git a/common/predictive-text/testing/one-stage-embedded-webworker/build.sh b/common/predictive-text/testing/one-stage-embedded-webworker/build.sh new file mode 100644 index 0000000000..64e1628d7e --- /dev/null +++ b/common/predictive-text/testing/one-stage-embedded-webworker/build.sh @@ -0,0 +1,65 @@ +#! /bin/bash +# +# Compiles the Language Modeling Layer for common use in predictive text and autocorrective applications. +# Designed for optimal compatibility with the Keyman Suite. +# + +display_usage ( ) { + echo "build.sh [-clean]" + echo + echo " -clean to erase pre-existing build products before a re-build" +} + +# Fails the build if a specified file does not exist. +assert ( ) { + if ! [ -f $1 ]; then + fail "Build failed." + exit 1 + fi +} + +# Prints a nice, common error message. +fail ( ) { + FAILURE_MSG="$1" + if [[ "$FAILURE_MSG" == "" ]]; then + FAILURE_MSG="Unknown failure." + fi + echo "${ERROR_RED}$FAILURE_MSG${NORMAL}" + exit 1 +} + +SOURCE="testing/one-stage-embedded-webworker" + +echo "Node.js + dependencies check" +npm install --no-optional + +if [ $? -ne 0 ]; then + fail "Build environment setup error detected! Please ensure Node.js is installed!" +fi + +# A nice, extensible method for -clean operations. Add to this as necessary. +clean ( ) { + rm -rf "./*.js" + if [ $? -ne 0 ]; then + fail "Failed to erase the prior build." + fi +} + +# Process command-line arguments +while [[ $# -gt 0 ]] ; do + key="$1" + case $key in + -clean) + clean + ;; + esac + shift # past the processed argument +done + +npm run tsc -- -p $SOURCE/tsconfig.json + +if [ $? -ne 0 ]; then + fail "Compilation failed." +fi + +echo "Typescript compilation successful." \ No newline at end of file diff --git a/common/predictive-text/testing/one-stage-embedded-webworker/index.html b/common/predictive-text/testing/one-stage-embedded-webworker/index.html new file mode 100644 index 0000000000..244f4aa283 --- /dev/null +++ b/common/predictive-text/testing/one-stage-embedded-webworker/index.html @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + Embedded WebWorker test + + + + + + + + + +

LM Layer Testing - One-Stage Embedded WebWorkers

+

This page serves as a prototype for embedding a WebWorker's code within its "master" script + across the TypeScript transpilation boundary.

+
+ + + +

Return to testing home page

+ + diff --git a/common/predictive-text/testing/one-stage-embedded-webworker/main.ts b/common/predictive-text/testing/one-stage-embedded-webworker/main.ts new file mode 100644 index 0000000000..5d15ae88b5 --- /dev/null +++ b/common/predictive-text/testing/one-stage-embedded-webworker/main.ts @@ -0,0 +1,52 @@ +// Useful for passing class constructors +type Workable = { + new (...args: any[]): T; + // Requires a static implementation. + onmessage(e: any): void; +}; + +class A { + a(x: number, y: number):number { + return x + y; + } +} + +var WorkerGlobals = { + counter: 0, + a: A +} + +class WorkerCore { + static WorkerGlobals = WorkerGlobals; + //static counter: number; + + static onmessage(e: any) { + WorkerGlobals.counter++; + + console.log("Message received from main page: ", e.data); + + // Forces TypeScript to interpret this line as plain JavaScript, as it uses a non-Worker definition. + // @ts-ignore + postMessage(WorkerGlobals.counter); + } +} + +function createWorkerFromClasses(globals: object, fn: Workable): Worker { + var sep = ";\n"; + let glb = "var WorkerGlobals = " + JSON.stringify(globals); + let wc = "var onmessage = " + fn.onmessage.toString(); + var blob = new Blob([glb, sep, wc], { type: 'text/javascript' }); + let url = URL.createObjectURL(blob); + + return new Worker(url); +} + +var canaryWorker = createWorkerFromClasses(WorkerGlobals, WorkerCore); + +canaryWorker.onmessage = function(e) { + var counter = e.data; // Number of times the WebWorker has been messaged. + console.log("Received message from the WebWorker: " + e.data); + + var txtFeedback = document.getElementById("txtFeedback"); + txtFeedback.value = counter + " click(s)"; +} diff --git a/common/predictive-text/testing/one-stage-embedded-webworker/tsconfig.json b/common/predictive-text/testing/one-stage-embedded-webworker/tsconfig.json new file mode 100644 index 0000000000..f1e9be8600 --- /dev/null +++ b/common/predictive-text/testing/one-stage-embedded-webworker/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "allowJs": false, + "module": "none", + "outDir": "./", + "inlineSources": true, + "sourceMap": true, + "target": "es5" + }, + "files" : [ + "main.ts" + ] +} diff --git a/common/predictive-text/tsconfig.json b/common/predictive-text/tsconfig.json index 5a4cecf904..da0761196c 100644 --- a/common/predictive-text/tsconfig.json +++ b/common/predictive-text/tsconfig.json @@ -10,5 +10,9 @@ }, "include" : [ "./*.ts" + ], + "exclude" : [ + "node_modules", + "testing" ] }