use-dcp-worker
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -26,3 +26,3 @@ /** | ||
declare class Address { | ||
constructor(address: string | Address); | ||
constructor(address: string | Address | undefined | null); | ||
address: string; | ||
@@ -29,0 +29,0 @@ toString(): string; |
@@ -228,3 +228,3 @@ "use strict"; | ||
worker.start().catch((error) => { | ||
console.error(`useDCPWorker(): starting the worker threw an unexpected error:`, error); | ||
console.error(`use-dcp-worker: starting the worker threw an unexpected error:`, error); | ||
return error; | ||
@@ -234,3 +234,3 @@ }); | ||
else | ||
console.warn('useDCPWorker(): worker is not loaded.'); | ||
console.warn('use-dcp-worker: worker is not loaded.'); | ||
} | ||
@@ -244,3 +244,3 @@ function stopWorker() { | ||
.catch((error) => { | ||
console.error(`useDCPWorker(): stopping the worker threw an unexpected error:`, error); | ||
console.error(`use-dcp-worker: stopping the worker threw an unexpected error:`, error); | ||
return error; | ||
@@ -250,3 +250,3 @@ }); | ||
else { | ||
console.error("useDCPWorker(): failed to stop worker, worker isn't loaded or is null"); | ||
console.error("use-dcp-worker: failed to stop worker, worker isn't loaded or is null."); | ||
} | ||
@@ -272,7 +272,12 @@ } | ||
return null; | ||
for (let prop in newWorkerOptions) | ||
for (const prop in newWorkerOptions) { | ||
if (prop === 'paymentAddress') { | ||
if (typeof newWorkerOptions[prop] === 'string') | ||
newWorkerOptions[prop] = new window.dcp.wallet.Address(newWorkerOptions[prop]); | ||
} | ||
workerOptions[prop] = newWorkerOptions[prop]; | ||
} | ||
}, []); | ||
/** | ||
* Saves the current workerOptions configuration under their email address in | ||
* Saves the current maxWorkingSandboxes and paymentAddress configuration under worker-options in | ||
* their local storage to be loaded when they sign in next time. | ||
@@ -284,3 +289,6 @@ */ | ||
// Save the worker options indexed by the user's Identity | ||
storage[getWorkerOptionsKey()] = workerOptions; | ||
storage[getWorkerOptionsKey()] = { | ||
maxWorkingSandboxes: workerOptions.maxWorkingSandboxes, | ||
paymentAddress: workerOptions.paymentAddress | ||
}; | ||
localStorage.setItem('worker-options', JSON.stringify(storage)); | ||
@@ -305,3 +313,3 @@ }, []); | ||
if (!workerOptions) { | ||
// if worker in state is loaded, pass options reference from supervisor | ||
// if worker in loaded state, pass options reference from supervisor | ||
if (workerState.isLoaded && worker !== null) { | ||
@@ -311,16 +319,21 @@ workerOptions = worker.supervisorOptions; | ||
else { | ||
// useLocalStorage will overide options.workerOptions | ||
if (userWorkerOptions && useLocalStorage) | ||
workerOptions = loadWorkerOptions(); | ||
// if workerOptions doesn't yet exist in localStorage (first time running) || don't use localStorage | ||
// use a default workerConfig overritten with user passed options.workerOptions | ||
if (workerOptions === null || !useLocalStorage) { | ||
workerOptions = (_a = window.dcpConfig.worker) !== null && _a !== void 0 ? _a : defaultWorkerOptions; | ||
if (userWorkerOptions.paymentAddress instanceof window.dcp.wallet.Keystore) | ||
userWorkerOptions.paymentAddress = new window.dcp.wallet.Address(userWorkerOptions.paymentAddress.address); | ||
// we can trust global config | ||
workerOptions = (_a = window.dcpConfig.worker) !== null && _a !== void 0 ? _a : defaultWorkerOptions; | ||
if (userWorkerOptions) | ||
applyWorkerOptions(userWorkerOptions); // apply user passed options | ||
let userOptions = null; | ||
if (useLocalStorage) | ||
userOptions = loadWorkerOptions(); | ||
if (userOptions !== null) | ||
applyWorkerOptions(userOptions); // paymentAddress and maxWorkingSandboxes from localStorage applied onto workerOptions | ||
// ensure that paymentAddress is of type dcp.wallet.Address | ||
try { | ||
if (workerOptions.paymentAddress instanceof window.dcp.wallet.Keystore) | ||
workerOptions.paymentAddress = new window.dcp.wallet.Address(workerOptions.paymentAddress.address); | ||
else if (typeof userWorkerOptions.paymentAddress === 'string') | ||
userWorkerOptions.paymentAddress = new window.dcp.wallet.Address(userWorkerOptions.paymentAddress); | ||
workerOptions.computeGroups = []; | ||
applyWorkerOptions(userWorkerOptions); | ||
workerOptions.paymentAddress = new window.dcp.wallet.Address(workerOptions.paymentAddress); | ||
} | ||
catch (error) { | ||
console.error(`use-dcp-worker: Invalid type of paymentAddress supplied for worker options.`, error); | ||
} | ||
} | ||
@@ -333,7 +346,7 @@ } | ||
if (!window.dcp) { | ||
console.error('useDCPWorker(): Missing dcp-client dependency. '); | ||
console.error('use-dcp-worker: Missing dcp-client dependency. '); | ||
return; | ||
} | ||
if (!Object.prototype.hasOwnProperty.call(workerOptions, 'paymentAddress')) { | ||
console.error('useDCPWorker(): workerOptions must contain a paymentAddress.'); | ||
console.error('use-dcp-worker: workerOptions must contain a paymentAddress.'); | ||
return; | ||
@@ -352,3 +365,3 @@ } | ||
catch (error) { | ||
console.error('useDCPWorker(): something went wrong in the wallet.Worker constructor.', error); | ||
console.error('use-dcp-worker: something went wrong in the wallet.Worker constructor.', error); | ||
return error; | ||
@@ -396,6 +409,9 @@ } | ||
dcpWorker.on('fetchStart', () => { | ||
dispatchWorkerState({ type: 'FETCHING_TRUE ' }); | ||
dispatchWorkerState({ type: 'FETCHING_TRUE' }); | ||
}); | ||
dcpWorker.on('fetchEnd', () => { | ||
dispatchWorkerState({ type: 'FETCHING_FALSE ' }); | ||
// extra delay for cleaner UI visual updates between quick fetching states | ||
setTimeout(() => { | ||
dispatchWorkerState({ type: 'FETCHING_FALSE' }); | ||
}, 1000); | ||
}); | ||
@@ -402,0 +418,0 @@ dcpWorker.on('fetchError', (error) => { |
{ | ||
"name": "use-dcp-worker", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Embed the DCP Worker in a React app", | ||
@@ -5,0 +5,0 @@ "main": "lib/useDCPWorker.js", |
@@ -96,3 +96,3 @@ # use-dcp-worker | ||
- `sandboxes: object`: List of Sandbox objects of sandboxes currently working. Sandbox objects consist of the properties: `id`, `isWorking`, `public`, `sliceStartTime`, and `progress`. | ||
- `setWorkerOptions(): function`: This method updates the `workerOptions` object. The method accepts an object as a parameter and does a leaf merge on the original `workerOptions` object, however, only on the first layer of properties. For example, `setWorkerOptions({ paymentAddress: 'some address' })` will only update the `paymentAddress` property of `workerOptions` and preserve the rest of the object. `setWorkerOptions({ allowOrigins: { any: ['origin'] } })` will update the entirety of `allowOrigins` instead of just `allowOrigins.any`. | ||
- `setWorkerOptions: function`: This method updates the `workerOptions` object. The method accepts an object as a parameter and does a leaf merge on the original `workerOptions` object, however, only on the first layer of properties. For example, `setWorkerOptions({ paymentAddress: 'some address' })` will only update the `paymentAddress` property of `workerOptions` and preserve the rest of the object. `setWorkerOptions({ allowOrigins: { any: ['origin'] } })` will update the entirety of `allowOrigins` instead of just `allowOrigins.any`. | ||
- `startWorker: function`: This method starts the worker. | ||
@@ -99,0 +99,0 @@ - `stopWorker: function`: This method stops the worker. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66283
1218