Skip to content

Commit 0d5494a

Browse files
authored
added @State, @binding, lifecycle and global events (#9)
* added basic @State system * bit of clean up * lifecycle, global events, refactored swiftle * updated readme
1 parent 7b90ca2 commit 0d5494a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2065
-175
lines changed

.github/workflows/deploy-example.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ jobs:
2929
- name: Checkout
3030
uses: actions/checkout@v4
3131
- name: Build
32-
working-directory: Examples/Embedded
32+
working-directory: Examples/Swiftle
3333
run: ./build.sh
3434
- name: Optimize Wasm
3535
uses: NiklasEi/wasm-opt-action@v2
3636
with:
37-
file: Examples/Embedded/Public/app.wasm
38-
output: Examples/Embedded/Public/app.wasm
37+
file: Examples/Swiftle/Public/app.wasm
38+
output: Examples/Swiftle/Public/app.wasm
3939
- name: Setup Pages
4040
uses: actions/configure-pages@v5
4141
- name: Upload artifact
4242
uses: actions/upload-pages-artifact@v3
4343
with:
44-
path: "Examples/Embedded/Public"
44+
path: "Examples/Swiftle/Public"
4545
- name: Deploy to GitHub Pages
4646
id: deployment
4747
uses: actions/deploy-pages@v4
File renamed without changes.

Examples/Embedded/Package.swift renamed to Examples/Basic/Package.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.10
1+
// swift-tools-version:6.0
22
import PackageDescription
33

44
let shouldBuildForEmbedded =
@@ -10,7 +10,7 @@ let extraDependencies: [Target.Dependency] = shouldBuildForEmbedded
1010

1111
let package = Package(
1212
name: "Embedded",
13-
platforms: [.macOS(.v14)],
13+
platforms: [.macOS(.v15)],
1414
dependencies: [
1515
.package(name: "ElementaryDOM", path: "../../"),
1616
.package(url: "https://github.com/swiftwasm/swift-dlmalloc", from: "0.1.0"),
@@ -24,21 +24,22 @@ let package = Package(
2424
.product(name: "JavaScriptKit", package: "JavaScriptKit"),
2525
] + extraDependencies,
2626
cSettings: [.unsafeFlags(["-fdeclspec"])],
27-
swiftSettings: [
27+
swiftSettings: shouldBuildForEmbedded ? [
2828
.enableExperimentalFeature("Embedded"),
2929
.enableExperimentalFeature("Extern"),
3030
.unsafeFlags([
3131
"-Xfrontend", "-gnone",
3232
"-Xfrontend", "-disable-stack-protector",
3333
]),
34-
],
35-
linkerSettings: [
34+
] : nil,
35+
linkerSettings: shouldBuildForEmbedded ? [
3636
.unsafeFlags([
3737
"-Xclang-linker", "-nostdlib",
3838
"-Xlinker", "--no-entry",
3939
"-Xlinker", "--export-if-defined=__main_argc_argv",
4040
]),
41-
]
41+
] : nil
4242
),
43-
]
43+
],
44+
swiftLanguageModes: [.v5]
4445
)

Examples/Basic/Public/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Basic Embedded Elementary demo</title>
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
</head>
7+
8+
<body>
9+
<script type="module">
10+
const [response, jsk] = await Promise.all([
11+
fetch(`app.wasm`),
12+
import(`./javascript-kit/index.mjs`),
13+
]);
14+
15+
const swift = new jsk.SwiftRuntime();
16+
const { instance } = await WebAssembly.instantiateStreaming(response, {
17+
javascript_kit: swift.wasmImports,
18+
});
19+
swift.setInstance(instance);
20+
swift.main();
21+
</script>
22+
</body>
23+
</html>
File renamed without changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import ElementaryDOM
2+
3+
@View
4+
struct App {
5+
@State var counters: [Int] = [1]
6+
@State var nextCounterName = 1
7+
8+
var content: some View {
9+
for (index, counter) in counters.enumerated() {
10+
h3 { "Counter \(counter)" }
11+
Counter(count: counter)
12+
br()
13+
button { "Remove counter" }
14+
.onClick { _ in
15+
counters.remove(at: index)
16+
}
17+
hr()
18+
}
19+
button { "Add counter" }
20+
.onClick { _ in
21+
nextCounterName += 1
22+
counters.append(nextCounterName)
23+
}
24+
}
25+
}
26+
27+
@View
28+
struct Counter {
29+
@State var count: Int = 0
30+
31+
var content: some View {
32+
div {
33+
button { "-" }
34+
.onClick { _ in count -= 1 }
35+
span { " \(count) " }
36+
button { "+" }
37+
.onClick { _ in count += 1 }
38+
}
39+
.onMount {
40+
print("Counter \(count) mounted")
41+
}
42+
.onUnmount {
43+
print("Counter \(count) dismounted")
44+
}
45+
}
46+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import JavaScriptKit
2+
3+
App().mount(in: JSObject.global.document.body.object!)

0 commit comments

Comments
 (0)