Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for promises #4

Merged
merged 6 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ SWorker.run(() => 'SWorker run 1: Function in other thread')
SWorker.run((arg1, arg2) => `SWorker run 2: ${arg1} ${arg2}`, ['Another', 'function in other thread'])
.then(console.log) // logs 'SWorker run 2: Another function in other thread'
.catch(console.error) // logs any possible error

// Any function that returns a promise (e.g. fetch) works
SWorker.run((arg1, arg2) => new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`SWorker run 3: ${arg1} ${arg2}`);
}, 100);
}), ['Another', 'function in other thread after 100ms'])
.then(console.log) // logs 'SWorker run 3: Another function in other thread after 100ms'
.catch(console.error) // logs any possible error
```

### SWorker.create(_[actions]?_)
Expand All @@ -70,7 +79,8 @@ const actions = [
{ message: 'func1', func: () => `Worker 1: Working on func1` },
{ message: 'func2', func: arg => `Worker 2: ${arg}` },
{ message: 'func3', func: arg => `Worker 3: ${arg}` },
{ message: 'func4', func: (arg = 'Working on func4') => `Worker 4: ${arg}` }
{ message: 'func4', func: (arg = 'Working on func4') => `Worker 4: ${arg}` },
{ message: 'func5', func: (arg = 500) => new Promise((resolve, reject) => { setTimeout(() => resolve(`Worker 5: ${arg}ms delay`), arg) }) }
]

let worker = SWorker.create(actions)
Expand All @@ -95,7 +105,8 @@ const actions = [
{ message: 'func1', func: () => `Worker 1: Working on func1` },
{ message: 'func2', func: arg => `Worker 2: ${arg}` },
{ message: 'func3', func: arg => `Worker 3: ${arg}` },
{ message: 'func4', func: (arg = 'Working on func4') => `Worker 4: ${arg}` }
{ message: 'func4', func: (arg = 'Working on func4') => `Worker 4: ${arg}` },
{ message: 'func5', func: (arg = 500) => new Promise((resolve, reject) => { setTimeout(() => resolve(`Worker 5: ${arg}ms delay`), arg) }) }
]

let worker = SWorker.create(actions)
Expand Down Expand Up @@ -123,6 +134,14 @@ worker.postMessage('func4')
worker.postMessage('func4', ['Overwrited argument'])
.then(console.log) // logs 'Worker 4: Overwrited argument'
.catch(console.error) // logs any possible error

worker.postMessage('func5')
.then(console.log) // logs 'Worker 5: 500ms delay'
.catch(console.error) // logs any possible error

worker.postMessage('func5', [1000])
.then(console.log) // logs 'Worker 5: 1000ms delay'
.catch(console.error) // logs any possible error
```

### <worker\>.postAll(_[message1,... || {message: message1, args: [args1]},... || [args1],...]?_)
Expand All @@ -149,13 +168,14 @@ const actions = [
{ message: 'func1', func: () => `Worker 1: Working on func1` },
{ message: 'func2', func: arg => `Worker 2: ${arg}` },
{ message: 'func3', func: arg => `Worker 3: ${arg}` },
{ message: 'func4', func: (arg = 'Working on func4') => `Worker 4: ${arg}` }
{ message: 'func4', func: (arg = 'Working on func4') => `Worker 4: ${arg}` },
{ message: 'func5', func: (arg = 500) => new Promise((resolve, reject) => { setTimeout(() => resolve(`Worker 5: ${arg}ms delay`), arg) }) }
]

let worker = SWorker.create(actions)

worker.postAll()
.then(console.log) // logs ['Worker 1: Working on func1', 'Worker 2: undefined', 'Worker 3: undefined', 'Worker 4: Working on func4']
.then(console.log) // logs ['Worker 1: Working on func1', 'Worker 2: undefined', 'Worker 3: undefined', 'Worker 4: Working on func4', 'Worker 5: 50ms delay']
.catch(console.error) // logs any possible error

worker.postAll(['func1', 'func3'])
Expand Down
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

## Unreleased
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved my previous changes in the README for changelog updates to the changelog file under a new Unreleased header since I'm not sure under what version you'd release this under, and it didn't feel right being presumptuous about the version being made in the README since I'm not the one producing a new package.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll release under v1.2.1, you were right before 😄
But I do understand why you did change. I'll ask you to maintain like that, because I do intend to make some other changes, and I'll change it back to 1.2.1 when it's time to merge at master. Thank you!

* Add support for input functions returning Promises.
## **1.2.0**

* Added tests
Expand All @@ -23,4 +25,4 @@

## **0.1.0**

* First release (beta).
* First release (beta).
2 changes: 1 addition & 1 deletion dist/sww.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading