Skip to content

Commit e507fa4

Browse files
committed
docs: Add usage notes with plenty of warnings.
1 parent 205d5a4 commit e507fa4

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

README.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ into the toolchain only for wasm targets.
2323

2424
Ideally, with either approach, this repository would transfer ownership to the swiftlang organization.
2525

26+
In the interim, to move wasm support forward, portions of DispatchAsync may be inlined (copy-pasted)
27+
into various libraries to enable wasm support. DispatchAsync is designed for this purpose, and has
28+
special `#if` handling to ensure that existing temporary usages will be elided without breakage
29+
the moment SwiftWasm adds support for `Dispatch` into the toolchain.
30+
2631
# DispatchSemaphore Limitations
2732

2833
The current implementation of `DispatchSemaphore` has some limitations. Blocking threads goes against the design goals of Swift Concurrency.
@@ -37,4 +42,94 @@ be needed for multi-threaded execution. This makes the implementation much easie
3742
- For wasm targets, calls to `signal` and `wait` must be balanced. An assertion triggers if `wait` is called more times than `signal`.
3843
- DispatchSemaphore is deprecated for wasm targets, and AsyncSemaphore is encouraged as the replacement.
3944
- For non-wasm targets, DispatchSemaphore is simply a typealias for `AsyncSemaphore`, and provides only a non-blocking async `wait`
40-
function. This reduces potential issues that can arise from wait being a thread-blocking function.
45+
function. This reduces potential issues that can arise from wait being a thread-blocking function.
46+
47+
# Usage
48+
49+
If you've scrolled this far, you probably saw the warning. But just to make sure…
50+
51+
> ⚠️ WARNING - This is an 🧪experimental🧪 repository and should not be adopted at large.
52+
53+
PassiveLogic is [actively working](https://github.com/PassiveLogic/swift-web-examples/issues/1) to mainstream this into the SwiftWasm
54+
toolchain. But if you can't wait, here are some tips.
55+
56+
## 1. Only use this for WASI platforms, and only if Dispatch cannot be imported.
57+
58+
Use `#if os(WASI) && !canImport(Dispatch)` to elide usages outside of WASI platforms:
59+
60+
```swift
61+
#if os(WASI) && !canImport(Dispatch)
62+
import DispatchAsync
63+
#else
64+
import Dispatch
65+
#endif
66+
67+
// Use Dispatch API's the same way you normal would.
68+
```
69+
70+
## 2. If you really want to use DispatchAsync as a pure swift Dispatch alternative for non-wasm targets
71+
72+
Stop. Are you sure? If you do this, you'll need to be '
73+
74+
1. Add the dependency to your package:
75+
76+
```swift
77+
let package = Package(
78+
name: "MyPackage",
79+
products: [
80+
// Products define the executables and libraries a package produces, making them visible to other packages.
81+
.library(
82+
name: "MyPackage",
83+
targets: [
84+
"MyPackage"
85+
]
86+
),
87+
],
88+
dependencies: [
89+
.package(
90+
url: "https://github.com/PassiveLogic/dispatch-async.git",
91+
from: "0.0.1"
92+
),
93+
],
94+
targets: [
95+
.target(
96+
name: "MyPackage"
97+
dependencies: [
98+
"DispatchAsync"
99+
]
100+
),
101+
]
102+
)
103+
```
104+
105+
2. Import and use DispatchAsync in place of Dispatch like this:
106+
107+
```swift
108+
#if os(WASI) && !canImport(Dispatch)
109+
import DispatchAsync
110+
#else
111+
// Non-WASI platforms have to explicitly bring in DispatchAsync
112+
// by using `@_spi`.
113+
@_spi(DispatchAsync) import DispatchAsync
114+
#endif
115+
116+
// Not allowed:
117+
// import Dispatch
118+
119+
// Also Not allowed:
120+
// import Foundation
121+
122+
// You'll need to use scoped Foundation imports:
123+
import struct Foundation.URL // Ok. Doesn't bring in Dispatch
124+
125+
// If you ignore the above notes, but do the following, be prepared for namespace
126+
// collisions between the toolchain's Dispatch and DispatchAsync:
127+
128+
private typealias DispatchQueue = DispatchAsync.DispatchQueue
129+
130+
// Ok. If you followed everything above, you can now do the following, using pure swift
131+
// under the hood! 🎉
132+
DispatchQueue.main.async {
133+
// Run your code here…
134+
}
135+
```

0 commit comments

Comments
 (0)