Skip to content

Commit

Permalink
Merge pull request #40 from alewin/feature/rename-remoteDepsParser-op…
Browse files Browse the repository at this point in the history
…tions

Feature/rename remote deps parser options
  • Loading branch information
alewin authored Apr 26, 2020
2 parents ceb6e7b + c3585a2 commit eb445e7
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion example/src/pages/ExternalScripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function App() {

const [sortWorker, { status: sortWorkerStatus, kill: killWorker }] = useWorker(sortDates, {
autoTerminate: false, // you should manually kill the worker using "killWorker()"
dependencies: [
remoteDependencies: [
"https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js"
],
});
Expand Down
4 changes: 2 additions & 2 deletions src/lib/createWorkerBlobUrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jobRunner from './jobRunner'
import depsParser from './depsParser'
import remoteDepsParser from './remoteDepsParser'

/**
* Converts the "fn" function into the syntax needed to be executed within a web worker
Expand All @@ -16,7 +16,7 @@ import depsParser from './depsParser'
* .catch(postMessage(['ERROR', error])"
*/
const createWorkerBlobUrl = (fn: Function, deps: string[]) => {
const blobCode = `${depsParser(deps)}; onmessage=(${jobRunner})(${fn})`
const blobCode = `${remoteDepsParser(deps)}; onmessage=(${jobRunner})(${fn})`
const blob = new Blob([blobCode], { type: 'text/javascript' })
const url = URL.createObjectURL(blob)
return url
Expand Down
8 changes: 4 additions & 4 deletions src/lib/depsParser.ts → src/lib/remoteDepsParser.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/**
*
* Concatenates the dependencies into a comma separated string.
* Concatenates the remote dependencies into a comma separated string.
* this string will then be passed as an argument to the "importScripts" function
*
* @param {Array.<String>}} deps array of string
* @returns {String} a string composed by the concatenation of the array
* elements "deps" and "importScripts".
*
* @example
* depsParser(['demo1', 'demo2']) // return importScripts('demo1, demo2')
* remoteDepsParser(['http://js.com/1.js', 'http://js.com/2.js']) // return importScripts('http://js.com/1.js, http://js.com/2.js')
*/
const depsParser = (deps: string[]) => {
const remoteDepsParser = (deps: string[]) => {
if (deps.length === 0) return ''

const depsString = (deps.map(dep => `${dep}`)).toString()
return `importScripts('${depsString}')`
}

export default depsParser
export default remoteDepsParser
8 changes: 4 additions & 4 deletions src/useWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ type WorkerController = {

type Options = {
timeout?: number;
dependencies?: string[];
remoteDependencies?: string[];
autoTerminate?: boolean;
}

const PROMISE_RESOLVE = 'resolve'
const PROMISE_REJECT = 'reject'
const DEFAULT_OPTIONS: Options = {
timeout: undefined,
dependencies: [],
remoteDependencies: [],
autoTerminate: true,
}

Expand Down Expand Up @@ -65,11 +65,11 @@ export const useWorker = <T extends (...fnArgs: any[]) => any>(

const generateWorker = useDeepCallback(() => {
const {
dependencies = DEFAULT_OPTIONS.dependencies,
remoteDependencies = DEFAULT_OPTIONS.remoteDependencies,
timeout = DEFAULT_OPTIONS.timeout,
} = options

const blobUrl = createWorkerBlobUrl(fn, dependencies!)
const blobUrl = createWorkerBlobUrl(fn, remoteDependencies!)
const newWorker: Worker & { _url?: string } = new Worker(blobUrl)
newWorker._url = blobUrl

Expand Down
2 changes: 1 addition & 1 deletion website/docs/examples/external.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ title: External Scripts

const [sortWorker, sortWorkerStatus, killWorker] = useWorker(sortDates, {
timeout: 5000,
dependencies: [
remoteDependencies: [
"https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js"
]
});
Expand Down
12 changes: 6 additions & 6 deletions website/docs/useworker.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ to view the values of `WORKER_STATUS` click here: [Status API](./workerstatus.md
import { useWorker } from "@koale/useworker";
const [workerFn, workerStatus, workerTerminate] = useWorker(fn, {
timeout: undefined,
dependencies: []
remoteDependencies: []
});
```

## Options API

| Value | Type | Default | Description |
| ------------ | --------------- | --------- | ------------------------------------------------------------------------- |
| timeout | Number | undefined | the number of milliseconds before killing the worker |
| dependencies | Array of String | [] | an array that contains the external dependencies needed to run the worker |
| Value | Type | Default | Description |
| ------------------ | --------------- | --------- | ------------------------------------------------------------------------- |
| timeout | Number | undefined | the number of milliseconds before killing the worker |
| remoteDependencies | Array of String | [] | an array that contains the remote dependencies needed to run the worker |

## Options Example

Expand All @@ -55,7 +55,7 @@ const fn = dates => dates.sort(dateFns.compareAsc)

const [workerFn, workerStatus, workerTerminate] = useWorker(fn, {
timeout: 50000 // 5 seconds
dependencies: [
remoteDependencies: [
"https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js" // dateFns
]
});
Expand Down

0 comments on commit eb445e7

Please sign in to comment.