@vue-lynx-example/main-thread 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -0
- package/dist/background-draggable.lynx.bundle +0 -0
- package/dist/background-draggable.web.bundle +1 -0
- package/dist/cross-thread-calls.lynx.bundle +0 -0
- package/dist/cross-thread-calls.web.bundle +1 -0
- package/dist/main-thread-draggable-raw.lynx.bundle +0 -0
- package/dist/main-thread-draggable-raw.web.bundle +1 -0
- package/dist/main-thread-draggable.lynx.bundle +0 -0
- package/dist/main-thread-draggable.web.bundle +1 -0
- package/dist/shared-module.lynx.bundle +0 -0
- package/dist/shared-module.web.bundle +1 -0
- package/lynx.config.ts +27 -0
- package/package.json +30 -0
- package/src/background-draggable/App.vue +39 -0
- package/src/background-draggable/BackgroundDraggable.vue +20 -0
- package/src/background-draggable/index.ts +6 -0
- package/src/cross-thread-calls/App.vue +80 -0
- package/src/cross-thread-calls/index.ts +22 -0
- package/src/main-thread-draggable/App.vue +62 -0
- package/src/main-thread-draggable/BackgroundDraggable.vue +20 -0
- package/src/main-thread-draggable/MainThreadDraggable.vue +22 -0
- package/src/main-thread-draggable/index.ts +6 -0
- package/src/main-thread-draggable-raw/App.vue +58 -0
- package/src/main-thread-draggable-raw/BackgroundDraggable.vue +20 -0
- package/src/main-thread-draggable-raw/MainThreadDraggable.vue +22 -0
- package/src/main-thread-draggable-raw/index.ts +6 -0
- package/src/shared-module/App.vue +77 -0
- package/src/shared-module/color-utils.ts +21 -0
- package/src/shared-module/index.ts +17 -0
- package/src/shims-vue.d.ts +11 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
<!-- Copyright 2026 Xuan Huang (huxpro). All rights reserved.
|
|
2
|
+
Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
LICENSE file in the root directory of this source tree. -->
|
|
4
|
+
|
|
5
|
+
<!--
|
|
6
|
+
Shared Module Demo
|
|
7
|
+
|
|
8
|
+
Tap the colored box to cycle through colors. The color-utils module is
|
|
9
|
+
imported with `with { runtime: 'shared' }` so both threads can use the
|
|
10
|
+
same `getNextColor()` function — the Main Thread calls it directly in
|
|
11
|
+
the tap handler, and the Background Thread calls it to display the name.
|
|
12
|
+
-->
|
|
13
|
+
<script setup lang="ts">
|
|
14
|
+
import { ref, computed } from 'vue'
|
|
15
|
+
import {
|
|
16
|
+
useMainThreadRef,
|
|
17
|
+
runOnBackground,
|
|
18
|
+
} from 'vue-lynx'
|
|
19
|
+
import { getNextColor, getColorCount } from './color-utils' with { runtime: 'shared' }
|
|
20
|
+
|
|
21
|
+
// --- Background Thread state ---
|
|
22
|
+
const count = ref(0)
|
|
23
|
+
const currentColor = computed(() => getNextColor(count.value))
|
|
24
|
+
|
|
25
|
+
// --- Main Thread function ---
|
|
26
|
+
const boxRef = useMainThreadRef(null)
|
|
27
|
+
|
|
28
|
+
const onTap = () => {
|
|
29
|
+
'main thread'
|
|
30
|
+
// Use shared function directly on the Main Thread to get color
|
|
31
|
+
const idx = (boxRef as unknown as { current?: { __colorIdx?: number } }).current?.__colorIdx ?? 0
|
|
32
|
+
const nextIdx = idx + 1
|
|
33
|
+
const color = getNextColor(nextIdx)
|
|
34
|
+
|
|
35
|
+
const el = (boxRef as unknown as {
|
|
36
|
+
current?: {
|
|
37
|
+
setStyleProperty?(k: string, v: string): void
|
|
38
|
+
__colorIdx?: number
|
|
39
|
+
}
|
|
40
|
+
}).current
|
|
41
|
+
el?.setStyleProperty?.('background-color', color)
|
|
42
|
+
if (el) el.__colorIdx = nextIdx
|
|
43
|
+
|
|
44
|
+
// Also update BG state so the text reflects the change
|
|
45
|
+
runOnBackground(incrementCount)()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function incrementCount() {
|
|
49
|
+
count.value++
|
|
50
|
+
}
|
|
51
|
+
</script>
|
|
52
|
+
|
|
53
|
+
<template>
|
|
54
|
+
<view :style="{ width: '100%', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center', backgroundColor: 'white' }">
|
|
55
|
+
<view
|
|
56
|
+
:main-thread-ref="boxRef"
|
|
57
|
+
:main-thread-bindtap="onTap"
|
|
58
|
+
:style="{
|
|
59
|
+
width: '200px',
|
|
60
|
+
height: '200px',
|
|
61
|
+
backgroundColor: currentColor,
|
|
62
|
+
display: 'flex',
|
|
63
|
+
flexDirection: 'column',
|
|
64
|
+
alignItems: 'center',
|
|
65
|
+
justifyContent: 'center',
|
|
66
|
+
borderRadius: '16px',
|
|
67
|
+
}"
|
|
68
|
+
>
|
|
69
|
+
<text :style="{ fontSize: 16, color: 'white', fontWeight: 'bold' }">
|
|
70
|
+
Tap to cycle
|
|
71
|
+
</text>
|
|
72
|
+
<text :style="{ fontSize: 14, color: 'white', marginTop: '8px' }">
|
|
73
|
+
Color {{ (count % getColorCount()) + 1 }}/{{ getColorCount() }}
|
|
74
|
+
</text>
|
|
75
|
+
</view>
|
|
76
|
+
</view>
|
|
77
|
+
</template>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Copyright 2026 Xuan Huang (huxpro). All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Shared color utilities — available on both Main Thread and Background Thread.
|
|
7
|
+
*
|
|
8
|
+
* This module does NOT contain a 'main thread' directive. It is imported
|
|
9
|
+
* with `with { runtime: 'shared' }` so that its code is included in both
|
|
10
|
+
* thread bundles.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const COLORS = ['#4FC3F7', '#81C784', '#FFB74D', '#E57373', '#BA68C8']
|
|
14
|
+
|
|
15
|
+
export function getNextColor(index: number): string {
|
|
16
|
+
return COLORS[index % COLORS.length]!
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getColorCount(): number {
|
|
20
|
+
return COLORS.length
|
|
21
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright 2026 Xuan Huang (huxpro). All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Shared Module Demo
|
|
7
|
+
*
|
|
8
|
+
* Demonstrates `import ... with { runtime: 'shared' }` to share plain
|
|
9
|
+
* utility functions between the Main Thread and Background Thread.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { createApp } from 'vue-lynx';
|
|
13
|
+
|
|
14
|
+
import App from './App.vue';
|
|
15
|
+
|
|
16
|
+
const app = createApp(App);
|
|
17
|
+
app.mount();
|