-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* convert some .js files to .ts * more * fix
- Loading branch information
1 parent
07c399e
commit 75b3336
Showing
22 changed files
with
308 additions
and
427 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,68 @@ | ||
import type { File } from './types'; | ||
import Worker from './workers/bundler/index.js?worker'; | ||
import type { BundleMessageData } from './workers/workers'; | ||
|
||
const workers = new Map(); | ||
|
||
let uid = 1; | ||
|
||
export default class Bundler { | ||
worker: Worker; | ||
handlers: Map<number, (data: BundleMessageData) => void>; | ||
|
||
constructor({ | ||
packages_url, | ||
svelte_url, | ||
onstatus | ||
}: { | ||
packages_url: string; | ||
svelte_url: string; | ||
onstatus: (val: string | null) => void; | ||
}) { | ||
const hash = `${packages_url}:${svelte_url}`; | ||
|
||
if (!workers.has(hash)) { | ||
const worker = new Worker(); | ||
worker.postMessage({ type: 'init', packages_url, svelte_url }); | ||
workers.set(hash, worker); | ||
} | ||
|
||
this.worker = workers.get(hash); | ||
|
||
this.handlers = new Map(); | ||
|
||
this.worker.addEventListener('message', (event: MessageEvent<BundleMessageData>) => { | ||
const handler = this.handlers.get(event.data.uid); | ||
|
||
if (handler) { | ||
// if no handler, was meant for a different REPL | ||
if (event.data.type === 'status') { | ||
onstatus(event.data.message); | ||
return; | ||
} | ||
|
||
onstatus(null); | ||
handler(event.data); | ||
this.handlers.delete(event.data.uid); | ||
} | ||
}); | ||
} | ||
|
||
bundle(files: File[]) { | ||
return new Promise((fulfil) => { | ||
this.handlers.set(uid, fulfil); | ||
|
||
this.worker.postMessage({ | ||
uid, | ||
type: 'bundle', | ||
files | ||
}); | ||
|
||
uid += 1; | ||
}); | ||
} | ||
|
||
destroy() { | ||
this.worker.terminate(); | ||
} | ||
} |
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
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.