From dcf51def633b73123eb2022430fe0c0c3e1e885e Mon Sep 17 00:00:00 2001 From: myckhel Date: Fri, 15 Jan 2021 14:29:14 +0100 Subject: [PATCH 1/6] implemented queue cancelJob This is the commit message #2: added cancel job example fixed ts errors --- example/App.tsx | 23 +++++++++++++++--- src/Queue.ts | 32 +++++++++++++++++++++++-- src/Worker.ts | 64 +++++++++++++++++++++++++++++++++++-------------- tsconfig.json | 1 + 4 files changed, 97 insertions(+), 23 deletions(-) diff --git a/example/App.tsx b/example/App.tsx index 0c1cdf2..ec66261 100644 --- a/example/App.tsx +++ b/example/App.tsx @@ -2,7 +2,7 @@ import * as React from 'react'; import { Button, View } from 'react-native'; import queue from '../src/Queue'; -import { Worker } from '../src/Worker'; +import { Worker, CANCEL } from '../src/Worker'; export interface IAppProps {} @@ -21,12 +21,21 @@ export default class App extends React.Component { }); queue.addWorker( new Worker('testWorker', async (payload) => { - return new Promise((resolve) => { - setTimeout(() => { + let cancel + const promise = new Promise((resolve, reject) => { + const timeout = setTimeout(() => { console.log(payload); resolve(); }, 5000); + + cancel = () => { + clearTimeout(timeout) + reject({message: 'canceled'}) + } }); + + promise[CANCEL] = cancel + return promise }) ); } @@ -39,6 +48,14 @@ export default class App extends React.Component { queue.addJob('testWorker', { id: counter++ }, undefined, false); }} /> +