-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Smart difference detection #89
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a7e2dc7
add dependencies
Quramy edfb3df
add diff invoker thread
Quramy 868a0a4
[wip] add entry for workerjs
Quramy fdb9cb6
add worker client
Quramy 6c8c82d
setup worker and wasm
Quramy 3dd84fb
[wip] add diff marking view
Quramy e864433
fix response sequence
Quramy 9f6b17b
refactor modal names
Quramy 9a7a32e
wip
Quramy 6fd712e
add option
Quramy ec8026d
modify setup scripts
Quramy 56fc05d
rename worker resources
Quramy 8cb5f7b
draw straying rectangulars
Quramy e5ee772
remove debug console logging
Quramy 30fc123
fix dependencies
Quramy b7b83c7
fix pointed outs
Quramy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
assets/cv-wasm_browser.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const instantiateCachedURL = require('./util.js'); | ||
|
||
class ModuleClass { | ||
constructor(opt){ | ||
this._initCb = opt.init; | ||
this._version = opt.version; | ||
this._wasmUrl = opt.wasmUrl; | ||
} | ||
|
||
locateFile(baseName) { | ||
return self.location.pathname.replace(/\/[^\/]*$/, '/') + baseName; | ||
} | ||
|
||
instantiateWasm(imports, callback) { | ||
instantiateCachedURL(this._version, this._wasmUrl, imports) | ||
.then(instance => callback(instance)); | ||
return { }; | ||
} | ||
|
||
onInit(cb) { | ||
this._initCb = cb; | ||
} | ||
|
||
onRuntimeInitialized() { | ||
if (this._initCb) { | ||
return this._initCb(this); | ||
} | ||
} | ||
} | ||
|
||
module.exports = { ModuleClass }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// 1. +++ fetchAndInstantiate() +++ // | ||
|
||
// This library function fetches the wasm module at 'url', instantiates it with | ||
// the given 'importObject', and returns the instantiated object instance | ||
|
||
function fetchAndInstantiate(url, importObject) { | ||
return fetch(url).then(response => | ||
response.arrayBuffer() | ||
).then(bytes => | ||
WebAssembly.instantiate(bytes, importObject) | ||
).then(results => | ||
results.instance | ||
); | ||
} | ||
|
||
// 2. +++ instantiateCachedURL() +++ // | ||
|
||
// This library function fetches the wasm Module at 'url', instantiates it with | ||
// the given 'importObject', and returns a Promise resolving to the finished | ||
// wasm Instance. Additionally, the function attempts to cache the compiled wasm | ||
// Module in IndexedDB using 'url' as the key. The entire site's wasm cache (not | ||
// just the given URL) is versioned by dbVersion and any change in dbVersion on | ||
// any call to instantiateCachedURL() will conservatively clear out the entire | ||
// cache to avoid stale modules. | ||
function instantiateCachedURL(dbVersion, url, importObject) { | ||
const dbName = 'wasm-cache'; | ||
const storeName = 'wasm-cache'; | ||
|
||
// This helper function Promise-ifies the operation of opening an IndexedDB | ||
// database and clearing out the cache when the version changes. | ||
function openDatabase() { | ||
return new Promise((resolve, reject) => { | ||
var request = indexedDB.open(dbName, dbVersion); | ||
request.onerror = reject.bind(null, 'Error opening wasm cache database'); | ||
request.onsuccess = () => { resolve(request.result) }; | ||
request.onupgradeneeded = event => { | ||
var db = request.result; | ||
if (db.objectStoreNames.contains(storeName)) { | ||
console.log(`Clearing out version ${event.oldVersion} wasm cache`); | ||
db.deleteObjectStore(storeName); | ||
} | ||
console.log(`Creating version ${event.newVersion} wasm cache`); | ||
db.createObjectStore(storeName) | ||
}; | ||
}); | ||
} | ||
|
||
// This helper function Promise-ifies the operation of looking up 'url' in the | ||
// given IDBDatabase. | ||
function lookupInDatabase(db) { | ||
return new Promise((resolve, reject) => { | ||
var store = db.transaction([storeName]).objectStore(storeName); | ||
var request = store.get(url); | ||
request.onerror = reject.bind(null, `Error getting wasm module ${url}`); | ||
request.onsuccess = event => { | ||
if (request.result) | ||
resolve(request.result); | ||
else | ||
reject(`Module ${url} was not found in wasm cache`); | ||
} | ||
}); | ||
} | ||
|
||
// This helper function fires off an async operation to store the given wasm | ||
// Module in the given IDBDatabase. | ||
function storeInDatabase(db, module) { | ||
var store = db.transaction([storeName], 'readwrite').objectStore(storeName); | ||
try { | ||
var request = store.put(module, url); | ||
request.onerror = err => { console.log(`Failed to store in wasm cache: ${err}`) }; | ||
request.onsuccess = err => { console.log(`Successfully stored ${url} in wasm cache`) }; | ||
} catch (e) { | ||
console.warn('An error was thrown... in storing wasm cache...'); | ||
console.warn(e); | ||
} | ||
} | ||
|
||
// This helper function fetches 'url', compiles it into a Module, | ||
// instantiates the Module with the given import object. | ||
function fetchAndInstantiate() { | ||
return fetch(url).then(response => | ||
response.arrayBuffer() | ||
).then(buffer => | ||
WebAssembly.instantiate(buffer, importObject) | ||
) | ||
} | ||
|
||
// With all the Promise helper functions defined, we can now express the core | ||
// logic of an IndexedDB cache lookup. We start by trying to open a database. | ||
return openDatabase().then(db => { | ||
// Now see if we already have a compiled Module with key 'url' in 'db': | ||
return lookupInDatabase(db).then(module => { | ||
// We do! Instantiate it with the given import object. | ||
console.log(`Found ${url} in wasm cache`); | ||
return WebAssembly.instantiate(module, importObject); | ||
}, errMsg => { | ||
// Nope! Compile from scratch and then store the compiled Module in 'db' | ||
// with key 'url' for next time. | ||
console.log(errMsg); | ||
return fetchAndInstantiate().then(results => { | ||
setTimeout(() => storeInDatabase(db, results.module), 0); | ||
return results.instance; | ||
}); | ||
}) | ||
}, | ||
errMsg => { | ||
// If opening the database failed (due to permissions or quota), fall back | ||
// to simply fetching and compiling the module and don't try to store the | ||
// results. | ||
console.log(errMsg); | ||
return fetchAndInstantiate().then(results => | ||
results.instance | ||
); | ||
}); | ||
} | ||
|
||
module.exports = instantiateCachedURL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[question] Is this file copied from MDN?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I copied and modify a little(added some exception handling).