diff --git a/README.md b/README.md
index 30af427..ed0481e 100644
--- a/README.md
+++ b/README.md
@@ -65,6 +65,7 @@ Documentation
* [imageData](#imagedata)
* [loadImage](#loadimage)
* [splineTheme](#splinetheme)
+* [uuid](#uuid)
---
@@ -309,4 +310,15 @@ ___
**Returns:** `object`
___
+
+
+### `` uuid
+
+▸ **uuid**(): `string`
+
+*Defined in [utils/random/uuid.ts:7](https://github.com/visual-cortex/imgstry/blob/master/source/utils/random/uuid.ts#L7)*
+
+**Returns:** `string`
+
+___
diff --git a/docs/README.md b/docs/README.md
index bcdf74f..3007578 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -65,6 +65,7 @@ Documentation
* [imageData](#imagedata)
* [loadImage](#loadimage)
* [splineTheme](#splinetheme)
+* [uuid](#uuid)
---
@@ -309,4 +310,15 @@ ___
**Returns:** `object`
___
+
+
+### `` uuid
+
+▸ **uuid**(): `string`
+
+*Defined in [utils/random/uuid.ts:7](https://github.com/visual-cortex/imgstry/blob/master/source/utils/random/uuid.ts#L7)*
+
+**Returns:** `string`
+
+___
diff --git a/docs/classes/imgstry.md b/docs/classes/imgstry.md
index 4342ea2..0add217 100644
--- a/docs/classes/imgstry.md
+++ b/docs/classes/imgstry.md
@@ -3,8 +3,14 @@
# Class: Imgstry
(Exposes image processing methods for html canvas) Processor implementation for the web worker (Exposes image processing methods for html canvas)
+*__export__*:
+
*__class__*: Imgstry
+*__extends__*: {ImgstryEditor}
+
+*__implements__*: {IDisposable}
+
*__export__*:
*__class__*: Imgstry
@@ -23,6 +29,10 @@
**↳ Imgstry**
+## Implements
+
+* [IDisposable](../interfaces/idisposable.md)
+
## Index
### Constructors
@@ -51,6 +61,7 @@
* [clone](imgstry.md#clone)
* [contrast](imgstry.md#contrast)
* [convolve](imgstry.md#convolve)
+* [dispose](imgstry.md#dispose)
* [drawImage](imgstry.md#drawimage)
* [fill](imgstry.md#fill)
* [gamma](imgstry.md#gamma)
@@ -77,7 +88,7 @@
⊕ **new Imgstry**(canvas: *`HTMLCanvasElement`*, _options?: *`Partial`<[ImgstryBrowserOptions](../interfaces/imgstrybrowseroptions.md)>*): [Imgstry](imgstry.md)
-*Defined in [platform/browser/imgstry/imgstry.browser.ts:60](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L60)*
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:69](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L69)*
Creates an instance of Imgstry.
*__constructor__*:
@@ -101,7 +112,7 @@ ___
**● canvas**: *`HTMLCanvasElement`*
-*Defined in [platform/browser/imgstry/imgstry.browser.ts:70](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L70)*
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:79](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L79)*
*Defined in [platform/node/imgstry/imgstry.node.ts:26](https://github.com/visual-cortex/imgstry/blob/master/source/platform/node/imgstry/imgstry.node.ts#L26)*
(specifies the canvas base for imgstry)
@@ -113,7 +124,7 @@ ___
**● context**: *`CanvasRenderingContext2D`*
-*Defined in [platform/browser/imgstry/imgstry.browser.ts:52](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L52)*
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:59](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L59)*
*Defined in [platform/node/imgstry/imgstry.node.ts:25](https://github.com/visual-cortex/imgstry/blob/master/source/platform/node/imgstry/imgstry.node.ts#L25)*
___
@@ -123,7 +134,7 @@ ___
**● getCanvas**: *[getCanvas]()* = getCanvas
-*Defined in [platform/browser/imgstry/imgstry.browser.ts:49](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L49)*
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:56](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L56)*
___
@@ -150,7 +161,7 @@ gethistogram(): `HistogramData`
*Overrides ImgstryProcessor.histogram*
-*Defined in [core/imgstry.processor.ts:95](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L95)*
+*Defined in [core/imgstry.processor.ts:96](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L96)*
Returns the channel histogram of the image.
*__readonly__*:
@@ -207,7 +218,7 @@ ___
*Overrides ImgstryProcessor.batch*
-*Defined in [core/imgstry.processor.ts:126](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L126)*
+*Defined in [core/imgstry.processor.ts:127](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L127)*
Applies a series of filters to the image.
*__memberof__*: ImgstryProcessor
@@ -216,11 +227,11 @@ Applies a series of filters to the image.
| Name | Type | Description |
| ------ | ------ | ------ |
-| options | `OperationOption`[] | the set of operations |
+| options | `OperationOption`[] | The set of operations. |
| `Optional` reset | `boolean` |
**Returns:** `ImgstryProcessor`
-the current processor instance
+The current processor instance
___
@@ -300,7 +311,7 @@ ___
*Overrides ImgstryProcessor.clone*
-*Defined in [platform/browser/imgstry/imgstry.browser.ts:101](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L101)*
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:119](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L119)*
**Parameters:**
@@ -360,6 +371,22 @@ Apply a kernel to the active image
**Returns:** [ImgstryEditor](imgstryeditor.md)
the current editor instance
+___
+
+
+### dispose
+
+▸ **dispose**(): `void`
+
+*Implementation of [IDisposable](../interfaces/idisposable.md).[dispose](../interfaces/idisposable.md#dispose)*
+
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:148](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L148)*
+
+Destroys the thread and clears the canvas of data.
+*__memberof__*: Imgstry
+
+**Returns:** `void`
+
___
@@ -367,7 +394,7 @@ ___
▸ **drawImage**(image: *`HTMLImageElement`*): `void`
-*Defined in [platform/browser/imgstry/imgstry.browser.ts:87](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L87)*
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:97](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L97)*
Draws an image on the canvas.
*__memberof__*: Imgstry
@@ -508,7 +535,7 @@ ___
*Overrides [ImgstryEditor](imgstryeditor.md).[render](imgstryeditor.md#render)*
-*Defined in [platform/browser/imgstry/imgstry.browser.ts:113](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L113)*
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:131](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L131)*
**Returns:** `Promise`<[Imgstry](imgstry.md)>
@@ -540,7 +567,7 @@ ___
*Overrides ImgstryProcessor.reset*
-*Defined in [platform/browser/imgstry/imgstry.browser.ts:96](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L96)*
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:114](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L114)*
**Returns:** `ImgstryProcessor`
@@ -628,7 +655,10 @@ ___
*Overrides ImgstryProcessor.toDataUrl*
-*Defined in [platform/browser/imgstry/imgstry.browser.ts:92](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L92)*
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:110](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L110)*
+
+Returns the content of the current canvas as an image that you can use as a source for another canvas or an HTML element.
+*__memberof__*: Imgstry
**Parameters:**
@@ -637,6 +667,7 @@ ___
| `Default value` type | `string` | "image/png" |
**Returns:** `string`
+The image encoded as a data url.
___
@@ -672,7 +703,7 @@ ___
▸ **loadImage**(src: *`string`*): `Promise`<`HTMLImageElement`>
-*Defined in [platform/browser/imgstry/imgstry.browser.ts:50](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L50)*
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:57](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L57)*
**Parameters:**
diff --git a/docs/classes/imgstryeditor.md b/docs/classes/imgstryeditor.md
index 861ec86..f54f345 100644
--- a/docs/classes/imgstryeditor.md
+++ b/docs/classes/imgstryeditor.md
@@ -101,7 +101,7 @@ gethistogram(): `HistogramData`
*Inherited from ImgstryProcessor.histogram*
-*Defined in [core/imgstry.processor.ts:95](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L95)*
+*Defined in [core/imgstry.processor.ts:96](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L96)*
Returns the channel histogram of the image.
*__readonly__*:
@@ -121,7 +121,7 @@ getimageData(): `ImageData`setimageData(imgData: *`ImageData`*): `any`
*Inherited from ImgstryProcessor.imageData*
-*Defined in [core/imgstry.processor.ts:78](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L78)*
+*Defined in [core/imgstry.processor.ts:79](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L79)*
Gets the image data.
*__abstract__*:
@@ -134,7 +134,7 @@ Gets the image data.
*Inherited from ImgstryProcessor.imageData*
-*Defined in [core/imgstry.processor.ts:86](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L86)*
+*Defined in [core/imgstry.processor.ts:87](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L87)*
Sets the image data.
*__abstract__*:
@@ -161,7 +161,7 @@ ___
*Inherited from ImgstryProcessor.batch*
-*Defined in [core/imgstry.processor.ts:126](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L126)*
+*Defined in [core/imgstry.processor.ts:127](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L127)*
Applies a series of filters to the image.
*__memberof__*: ImgstryProcessor
@@ -170,11 +170,11 @@ Applies a series of filters to the image.
| Name | Type | Description |
| ------ | ------ | ------ |
-| options | `OperationOption`[] | the set of operations |
+| options | `OperationOption`[] | The set of operations. |
| `Optional` reset | `boolean` |
**Returns:** `ImgstryProcessor`
-the current processor instance
+The current processor instance
___
@@ -242,7 +242,7 @@ ___
*Inherited from ImgstryProcessor.clone*
-*Defined in [core/imgstry.processor.ts:70](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L70)*
+*Defined in [core/imgstry.processor.ts:71](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L71)*
Clone image data
*__abstract__*:
@@ -253,10 +253,10 @@ Clone image data
| Name | Type | Description |
| ------ | ------ | ------ |
-| original | `ImageData` | source image data |
+| original | `ImageData` | The source image data. |
**Returns:** `ImageData`
-the cloned canvas image data
+The cloned canvas image data.
___
@@ -438,7 +438,7 @@ ___
*Inherited from ImgstryProcessor.reset*
-*Defined in [core/imgstry.processor.ts:61](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L61)*
+*Defined in [core/imgstry.processor.ts:62](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L62)*
Resets the image to the original state.
*__abstract__*:
@@ -446,7 +446,7 @@ Resets the image to the original state.
*__memberof__*: ImgstryProcessor
**Returns:** `ImgstryProcessor`
-the current processor instance
+The current processor instance.
___
@@ -520,7 +520,7 @@ ___
*Inherited from ImgstryProcessor.toDataUrl*
-*Defined in [core/imgstry.processor.ts:51](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L51)*
+*Defined in [core/imgstry.processor.ts:52](https://github.com/visual-cortex/imgstry/blob/master/source/core/imgstry.processor.ts#L52)*
Encodes the canvas data to a data URI.
*__memberof__*: Imgstry
@@ -529,10 +529,10 @@ Encodes the canvas data to a data URI.
| Name | Type | Description |
| ------ | ------ | ------ |
-| type | `string` | data url type (eg: 'image/png') |
+| type | `string` | The standard MIME type for the image format to return. If you do not specify this parameter, the default value is a PNG format image. |
**Returns:** `string`
-url with the encoded image
+The image encoded as a data url.
___
diff --git a/docs/interfaces/idisposable.md b/docs/interfaces/idisposable.md
index c8c74f5..e670963 100644
--- a/docs/interfaces/idisposable.md
+++ b/docs/interfaces/idisposable.md
@@ -8,6 +8,7 @@
## Implemented by
+* [Imgstry](../classes/imgstry.md)
* [ImgstrySpline](../classes/imgstryspline.md)
## Index
diff --git a/docs/interfaces/imgstrybrowseroptions.md b/docs/interfaces/imgstrybrowseroptions.md
index 38f9510..459d56c 100644
--- a/docs/interfaces/imgstrybrowseroptions.md
+++ b/docs/interfaces/imgstrybrowseroptions.md
@@ -22,7 +22,7 @@
**● thread**: *`ImgstryThreadOptions`*
-*Defined in [platform/browser/imgstry/imgstry.browser.ts:20](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L20)*
+*Defined in [platform/browser/imgstry/imgstry.browser.ts:23](https://github.com/visual-cortex/imgstry/blob/master/source/platform/browser/imgstry/imgstry.browser.ts#L23)*
___
diff --git a/package.json b/package.json
index 9592995..a626f85 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "imgstry",
- "version": "1.0.0-alpha1",
+ "version": "1.0.0-alpha2",
"description": "A JavaScript image editing module.",
"main": "lib/index.js",
"directories": {
diff --git a/source/core/imgstry.processor.ts b/source/core/imgstry.processor.ts
index 9daa57a..060be8c 100644
--- a/source/core/imgstry.processor.ts
+++ b/source/core/imgstry.processor.ts
@@ -44,8 +44,9 @@ export abstract class ImgstryProcessor {
/**
* Encodes the canvas data to a data URI.
*
- * @param {string} type data url type (eg: 'image/png')
- * @returns {string} url with the encoded image
+ * @param {string} type The standard MIME type for the image format to return.
+ * If you do not specify this parameter, the default value is a PNG format image.
+ * @returns {string} The image encoded as a data url.
* @memberof Imgstry
*/
public abstract toDataUrl(type: string): string;
@@ -54,7 +55,7 @@ export abstract class ImgstryProcessor {
* Resets the image to the original state.
*
* @abstract
- * @returns {ImgstryProcessor} the current processor instance
+ * @returns {ImgstryProcessor} The current processor instance.
*
* @memberOf ImgstryProcessor
*/
@@ -63,8 +64,8 @@ export abstract class ImgstryProcessor {
* Clone image data
*
* @abstract
- * @param {ImageData} original source image data
- * @returns {ImageData} the cloned canvas image data
+ * @param {ImageData} original The source image data.
+ * @returns {ImageData} The cloned canvas image data.
* @memberOf ImgstryProcessor
*/
public abstract clone(original: ImageData): ImageData;
@@ -118,9 +119,9 @@ export abstract class ImgstryProcessor {
/**
* Applies a series of filters to the image.
*
- * @param {OperationOption[]} options the set of operations
- * @param {boolean} [reset] if the image should be reset to its original state before applying operations
- * @returns {ImgstryProcessor} the current processor instance
+ * @param {OperationOption[]} options The set of operations.
+ * @param {boolean} [reset] If the image should be reset to its original state before applying operations.
+ * @returns {ImgstryProcessor} The current processor instance
* @memberof ImgstryProcessor
*/
public batch(options: OperationOption[], reset?: boolean): ImgstryProcessor {
diff --git a/source/core/imgstry.thread.ts b/source/core/imgstry.thread.ts
index 48eabe1..b5d82cd 100644
--- a/source/core/imgstry.thread.ts
+++ b/source/core/imgstry.thread.ts
@@ -19,6 +19,7 @@ export interface IThreadData extends IThreadResult {
*/
export interface IThreadResult {
imageData: ImageData;
+ guid?: string;
}
/**
diff --git a/source/platform/browser/imgstry/imgstry.browser.ts b/source/platform/browser/imgstry/imgstry.browser.ts
index 32f715e..eb2cadd 100644
--- a/source/platform/browser/imgstry/imgstry.browser.ts
+++ b/source/platform/browser/imgstry/imgstry.browser.ts
@@ -7,6 +7,7 @@ import {
ImgstryThreadOptions,
} from '~platform/browser/worker';
import {
+ clearCanvas,
drawImage,
fillCanvas,
imageData,
@@ -16,6 +17,8 @@ import {
loadImage,
} from '~utils/dom';
+import { IDisposable } from '~types';
+
export interface ImgstryBrowserOptions {
thread: ImgstryThreadOptions;
}
@@ -40,12 +43,16 @@ const assignDefault = (source: Partial): ImgstryBrowserOp
};
};
+
/**
* (Exposes image processing methods for html canvas)
*
+ * @export
* @class Imgstry
+ * @extends {ImgstryEditor}
+ * @implements {IDisposable}
*/
-export class Imgstry extends ImgstryEditor {
+export class Imgstry extends ImgstryEditor implements IDisposable {
public static getCanvas = getCanvas;
public static loadImage = (src: string) => loadImage(Image, src);
@@ -59,6 +66,8 @@ export class Imgstry extends ImgstryEditor {
return this.canvas.height;
}
+ private _thread: ImgstryThread;
+
/**
* Creates an instance of Imgstry.
*
@@ -75,12 +84,13 @@ export class Imgstry extends ImgstryEditor {
this.context = this.canvas.getContext('2d');
fillCanvas(this.canvas, '');
this.original = this.imageData;
+ this._thread = new ImgstryThread(this._options.thread);
}
/**
* Draws an image on the canvas.
*
- * @param {HTMLImageElement} image the source image that will be drawn on the canvas
+ * @param {HTMLImageElement} image The source image that will be drawn on the canvas.
* @memberof Imgstry
* @returns {void}
*/
@@ -89,6 +99,14 @@ export class Imgstry extends ImgstryEditor {
this.original = this.imageData;
}
+ /**
+ * Returns the content of the current canvas as an image that you can use as a source for another canvas or an HTML element.
+ *
+ * @param {string} [type='image/png'] The standard MIME type for the image format to return.
+ * If you do not specify this parameter, the default value is a PNG format image.
+ * @returns {string} The image encoded as a data url.
+ * @memberof Imgstry
+ */
public toDataUrl(type = 'image/png'): string {
return this.canvas.toDataURL(type);
}
@@ -111,14 +129,25 @@ export class Imgstry extends ImgstryEditor {
}
public async render(): Promise {
- const result = await new ImgstryThread(this._options.thread)
- .run({
- imageData: this.imageData,
- operations: this._operations,
- });
+ const result = await this._thread.run({
+ imageData: this.imageData,
+ operations: this._operations,
+ });
this.imageData = result.imageData;
return this.clear();
}
+
+ /**
+ * Destroys the thread and clears the canvas of data.
+ *
+ * @memberof Imgstry
+ * @returns {void}
+ */
+ public dispose() {
+ this.original = null;
+ this._thread.dispose();
+ clearCanvas(this.canvas);
+ }
}
diff --git a/source/platform/browser/worker/imgstry.thread.ts b/source/platform/browser/worker/imgstry.thread.ts
index 0439d0d..65c4a96 100644
--- a/source/platform/browser/worker/imgstry.thread.ts
+++ b/source/platform/browser/worker/imgstry.thread.ts
@@ -7,9 +7,17 @@ import {
IWorkerData,
IWorkerResult,
} from '~platform/browser/worker/types';
+import {
+ filter,
+ first,
+ throttleTime,
+} from 'rxjs/operators';
+import { IDisposable } from '~types';
import ImgstryWorker from 'worker-loader?inline=true&fallback=false!./imgstry.worker';
import { Logger } from '~utils/logger';
+import { Subject } from 'rxjs';
+import { uuid } from '~utils/random';
/**
* Browser thread option contract.
@@ -28,60 +36,100 @@ export interface ImgstryThreadOptions {
* @export
* @class ImgstryThread
* @implements {IImgstryThread}
+ * @implements {IDisposable}
* @ignore
*/
-export class ImgstryThread implements IImgstryThread {
+export class ImgstryThread implements IImgstryThread, IDisposable {
+ public process$ = new Subject();
+
private _worker: ImgstryWorker;
- private _promise: Promise;
private _logger: Logger;
+ private _work$ = new Subject();
+ /**
+ * Creates an instance of ImgstryThread.
+ * @param {ImgstryThreadOptions} _options The thread options.
+ * @memberof ImgstryThread {isEnabled, isDebugEnabled}
+ * @constructor
+ */
constructor(
_options: ImgstryThreadOptions,
) {
this._logger = new Logger(_options.isDebugEnabled);
this._worker = new ImgstryWorker();
- this._promise = new Promise((resolve, reject) => {
- this._worker.onmessage = (message: MessageEvent) => {
+ this._worker.onmessage = (message: MessageEvent) => {
+ this._logger.info(
+ 'Worker recieved:',
+ message.data,
+ );
+ const { buffer, width, height, guid } = (message.data as IWorkerResult);
+ this.process$.next({
+ imageData: new ImageData(new Uint8ClampedArray(buffer), width, height),
+ guid,
+ });
+ };
+
+ this._worker.onerror = (err: ErrorEvent) => {
+ this._logger.error(err);
+ this.process$.error(err);
+ };
+
+ this._work$
+ .pipe(
+ throttleTime(250),
+ )
+ .subscribe((data) => {
this._logger.info(
- 'Worker recieved:',
- message.data,
+ 'Send to worker: ',
+ data,
);
- const { buffer, width, height } = (message.data as IWorkerResult);
- resolve({
- imageData: new ImageData(new Uint8ClampedArray(buffer), width, height),
- });
- this._worker.terminate();
- };
- this._worker.onerror = (err: ErrorEvent) => {
- this._logger.error(err);
- reject(err);
- this._worker.terminate();
- };
- });
+ this._worker.postMessage(data, [data.buffer]);
+ });
}
+ /**
+ * Queues a new set of operations, throttled @ 250ms.
+ *
+ * @param {IThreadData} data {
+ * imageData,
+ * operations,
+ * }
+ * @returns {Promise} A promise with the resulting image.
+ * @memberof ImgstryThread
+ */
public run({
imageData,
operations,
}: IThreadData): Promise {
- this._post({
+ const identifier = uuid();
+
+ this._work$.next({
buffer: imageData.data.buffer,
width: imageData.width,
height: imageData.height,
operations: operations,
+ guid: identifier,
});
- return this._promise;
+ return this.process$
+ .pipe(
+ filter(response => response.guid === identifier),
+ first(),
+ )
+ .toPromise();
}
- private _post(data: IWorkerData) {
- this._logger.info(
- 'Send to worker: ',
- data,
- );
-
- this._worker.postMessage(data, [data.buffer]);
+ /**
+ * Terminates the current worker thread and completes active streams.
+ *
+ * @memberof ImgstryThread
+ * @returns {void}
+ */
+ public dispose() {
+ this._worker.terminate();
+ this._work$.complete();
+ this.process$.complete();
}
}
diff --git a/source/platform/browser/worker/imgstry.worker.ts b/source/platform/browser/worker/imgstry.worker.ts
index 53834f8..6512b27 100644
--- a/source/platform/browser/worker/imgstry.worker.ts
+++ b/source/platform/browser/worker/imgstry.worker.ts
@@ -12,6 +12,7 @@ worker.onmessage = (message) => {
buffer: processor.imageData.data.buffer,
width: processor.imageData.width,
height: processor.imageData.height,
+ guid: data.guid,
},
[processor.imageData.data.buffer],
);
diff --git a/source/platform/browser/worker/types/index.ts b/source/platform/browser/worker/types/index.ts
index 6722d70..a33a394 100644
--- a/source/platform/browser/worker/types/index.ts
+++ b/source/platform/browser/worker/types/index.ts
@@ -23,4 +23,5 @@ export interface IWorkerResult {
buffer: ArrayBuffer;
width: number;
height: number;
+ guid: string;
}
diff --git a/source/utils/random/index.ts b/source/utils/random/index.ts
new file mode 100644
index 0000000..2f7e3c7
--- /dev/null
+++ b/source/utils/random/index.ts
@@ -0,0 +1 @@
+export * from './uuid';
diff --git a/source/utils/random/uuid.ts b/source/utils/random/uuid.ts
new file mode 100644
index 0000000..9d0f72f
--- /dev/null
+++ b/source/utils/random/uuid.ts
@@ -0,0 +1,11 @@
+export
+ /**
+ * Generates an unique identifier
+ *
+ * @returns {string} the generated UUID
+ */
+ const uuid = (): string =>
+ 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
+ const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
+ return v.toString(16);
+ });