Coder Social home page Coder Social logo

dropbox / componentbox Goto Github PK

View Code? Open in Web Editor NEW
257.0 12.0 5.0 39.28 MB

Reactive server-driven UI for iOS, Android, and web

Home Page: https://dropbox.github.io/componentbox/

License: Apache License 2.0

Kotlin 100.00%
kotlin-multiplatform server-driven-ui android ios compose jetpack react swift-ui

componentbox's Introduction

componentbox

Component Box

Note

No longer active!

After internal experimentation and discussion, we've decided to move forward with other options.

Sample

Model (server)

class Counter : ComposableModel<Int, CounterEvent>(0) {
    private fun increment() {
        withState {
            setState(state.value + 1)
        }
    }

    private fun decrement() {
        withState {
            setState(state.value - 1)
        }
    }

    override fun on(event: CounterEvent) = when (event) {
        Increment -> increment()
        Decrement -> decrement()
    }
}

UI Representation (server)

Static
@SerializableComponentBox
fun main() = componentBox {
        tree {
            lazyColumn<CounterEvent>(
                verticalArrangement = Arrangement.SpaceEvenly(2.dp),
                horizontalAlignment = Alignment.Start
            ) {
                child(header)
                child(count)
                child(incrementButton)
                child(decrementButton)
            }
        }
    }

val header = text(
    text = "Component Box Counter",
    style = TextStyle(fontWeight = FontWeight.ExtraBold)
)

val count = text(
    text = "Count: \${COUNTER_STATE}",
    style = TextStyle(color = Color.Hex("#FF0000"))
)

val incrementButton = textButton(
    text = "+1",
    onClick = semantic { Increment }
)

val decrementButton = textButton(
    text = "-1",
    onClick = semantic { Decrement }
)
Dynamic
@Composable
@ComponentBoxExport
fun main() = statefulComponentBoxGraph(init = null) {
        Graph(start = CounterOnboardingFlow.value) {
            componentBox(CounterLoginScreen.value, LoginScreen())
            componentBox(CounterOnboardingFlow.value, OnboardingFlow())
            componentBox(CounterScreen.Home.value, HomeScreen())
        }
    }
@Composable
fun LoginScreen() = Forest {
        tree("heading", Tree { LoginHeading() })
        tree("button", Tree { LoginButton() })
    }
@Composable
fun OnboardingFlow() = Trail {
        node(WelcomeScreen())
        node(FeatureDiscoveryScreen())
        node(HomeScreen())
    }
@Composable
fun HomeScreen() = ComponentBox {
        Tree {
            LazyColumn(
                verticalArrangement = Arrangement.SpaceEvenly(2.dp),
                horizontalAlignment = Alignment.Start
            ) {
                child(header)
                child(Count())
                child(IncrementButton())
                child(DecrementButton())
            }
        }
    }

@Composable
fun IncrementButton() = StatefulComposable<Counter> {
    ContainedButton(onClick = lambda { it.on(Increment) }) {
        text(text = "+1")
    }
}

@Composable
fun DecrementButton() = StatefulComposable<Counter> {
    ContainedButton(onClick = lambda { it.on(Decrement) }) {
        text(text = "-1")
    }
}

@Composable
fun Count() = StatefulComposable<Counter> {
    text(
        text = "Count: ${it.state.value}",
        style = TextStyle(color = Color.Hex("#FF0000"))
    )
}

Binaries (server)

./gradlew componentBoxJs
./gradlew componentBoxJson

Jetpack Compose (Android)

Activity
class ComponentBoxActivity : ComponentActivity() {
    private val scope = CoroutineScope(Dispatchers.Default)
    private val service = ComponentBoxService(scope)
    private val componentBox = service.componentBox
    private val render = RenderingEngine()

    override fun onStart() {
        super.onStart()
        service.launch(MANIFEST_URL)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            val root = componentBox.collectAsState()
            render {
                root.value
            }
        }
    }
}
Composable
@Composable
fun ComponentBoxView(componentBox: StateFlow<Component?>, render: RenderingEngine) {
    val root = componentBox.collectAsState()
    render {
        root.value
    }
}

React (web)

export default function ComponentBoxView(props: {manifestUrl: string}) {
  const [root, setRoot] = useState<Component | null>(null);
  const service = new ComponentBoxService();
  const render = new RenderingEngine();

  useEffect(() => {
    async function launch(manifestUrl: string): Tree {
        const componentBox = await service.launch(manifestUrl)
        setRoot(componentBox.root)
    }
    
    launch(props.manifestUrl)
 
  }, [props.manifestUrl]);
  
  return render(root)
}

SwiftUI (iOS)

struct ComponentBoxView: View {
    @StateObject private var service = ComponentBoxService()
    @State private var root: Component?
    
    var body: some View {
        render {
            root
        }
        .onAppear {
            service.launch(from: MANIFEST_URL) { result in
                switch result {
                case .success(let componentBox):
                    DispatchQueue.main.async {
                        self.root = componentBox.root
                    }
            }
        }
    }
}

Snapshots

Snapshots are available in Sonatype's snapshots repository.

License

Copyright (c) 2023 Dropbox, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Acknowledgments

cashapp Thanks to our friends at Cash App for Zipline

componentbox's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

componentbox's Issues

https://componentbox.io/ doesn't load

https://componentbox.io/ and related links in the README such as

You can find Component Box documentation [on the website](https://componentbox.io).

do not load, seems the domain doesn't point to anything

$ dig componentbox.io

; <<>> DiG 9.16.1-Ubuntu <<>> componentbox.io
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34169
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;componentbox.io.               IN      A

;; AUTHORITY SECTION:
componentbox.io.        300     IN      SOA     ns-cloud-a1.googledomains.com. cloud-dns-hostmaster.google.com. 3 21600 3600 259200 300

;; Query time: 116 msec
;; SERVER: 192.168.8.1#53(192.168.8.1)
;; WHEN: Fri Mar 04 19:17:45 PST 2022
;; MSG SIZE  rcvd: 137

Unable to load Kotlin classes using URLClassLoader in separate module

Description

I'm using the componentbox-gradle-plugin module to load Kotlin classes from the main source directory of a project. However, when I try to load classes in a separate test module, the URLClassLoader fails to load any classes.

Steps taken

  • I've verified that the URLs and class names are correct and that the classes exist in the specified directories.
  • I've tried using ClassLoader.getSystemClassLoader() instead of URLClassLoader to load the classes, but that also fails.
  • I've checked that the componentbox-gradle-plugin module is being properly included in the classpath of the test module.

Expected behavior

The URLClassLoader should be able to load the Kotlin classes from the main source directory of the project even when invoked from a separate test module.

Actual behavior

The URLClassLoader fails to load any classes from the main source directory when invoked from a separate test module.

Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb4000073957bc580 in tid 11408 (DefaultDispatch), pid 11383 (mples.campaigns)

Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb4000073957bc580 in tid 11408 (DefaultDispatch), pid 11383 (mples.campaigns)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A  Cmdline: com.dropbox.componentbox.samples.campaigns
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A  pid: 11383, tid: 11408, name: DefaultDispatch  >>> com.dropbox.componentbox.samples.campaigns <<<
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #00 pc 0000000000027524  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/base.apk!libquickjs.so (BuildId: e223aa6b91b8fe2f75ad53f138b2fb4a5751dd5d)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #01 pc 000000000003b9cc  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/base.apk!libquickjs.so (BuildId: e223aa6b91b8fe2f75ad53f138b2fb4a5751dd5d)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #02 pc 0000000000018bcc  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/base.apk!libquickjs.so (BuildId: e223aa6b91b8fe2f75ad53f138b2fb4a5751dd5d)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #05 pc 00000000004879d2  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (app.cash.zipline.QuickJs.execute+14)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #07 pc 0000000000489c7e  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (app.cash.zipline.internal.QuickJsExtensionsKt.loadJsModule+94)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #09 pc 00000000004883f6  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (app.cash.zipline.Zipline.loadJsModule+26)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #11 pc 00000000004964d0  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (app.cash.zipline.loader.internal.InternalJniKt.multiplatformLoadJsModule+32)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #13 pc 000000000049b6c2  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (app.cash.zipline.loader.internal.receiver.ZiplineLoadReceiver.receive+50)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #15 pc 0000000000491d28  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (app.cash.zipline.loader.ZiplineLoader$ModuleJob$run$3.invokeSuspend+112)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #17 pc 0000000000491c9c  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (app.cash.zipline.loader.ZiplineLoader$ModuleJob$run$3.invoke+16)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #19 pc 0000000000491c70  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (app.cash.zipline.loader.ZiplineLoader$ModuleJob$run$3.invoke+8)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #21 pc 00000000005c2ef0  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn+28)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #23 pc 0000000000583b90  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext+128)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #25 pc 0000000000583d68  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (kotlinx.coroutines.BuildersKt.withContext+0)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #27 pc 0000000000491f4e  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (app.cash.zipline.loader.ZiplineLoader$ModuleJob.run+370)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #29 pc 0000000000491c30  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (app.cash.zipline.loader.ZiplineLoader$ModuleJob$run$1.invokeSuspend+28)
2022-12-14 23:52:40.392 11430-11430 DEBUG                   crash_dump64                         A        #31 pc 0000000000543d46  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith+42)
2022-12-14 23:52:40.393 11430-11430 DEBUG                   crash_dump64                         A        #33 pc 0000000000588dec  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (kotlinx.coroutines.DispatchedTask.run+444)
2022-12-14 23:52:40.393 11430-11430 DEBUG                   crash_dump64                         A        #35 pc 0000000000f0532e  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely+2)
2022-12-14 23:52:40.393 11430-11430 DEBUG                   crash_dump64                         A        #37 pc 0000000000f0403e  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask+34)
2022-12-14 23:52:40.393 11430-11430 DEBUG                   crash_dump64                         A        #39 pc 0000000000f0416c  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker+56)
2022-12-14 23:52:40.393 11430-11430 DEBUG                   crash_dump64                         A        #41 pc 0000000000f0411c  /data/app/~~D6MquTqnlmLVHIRFHs5nKQ==/com.dropbox.componentbox.samples.campaigns-MRzoVeIn4E-fdqpPIvRctQ==/oat/arm64/base.vdex (kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run+0)

Sample Android app crashes on Plans screen

E/AndroidRuntime: FATAL EXCEPTION: Zipline
    Process: com.dropbox.componentbox.discovery, PID: 3219
    app.cash.zipline.QuickJsException: Only KClass supported as classifier, got C
        at JavaScript.vc(zipline)
        at JavaScript.ph(zipline)
        at JavaScript.b$(zipline)
        at JavaScript.kk(zipline)
        at JavaScript.hk(zipline)
        at JavaScript.<anonymous>(zipline)
        at JavaScript.kk(zipline)
        at JavaScript.hk(zipline)
        at JavaScript.AC(zipline)
        at JavaScript.<anonymous>(zipline)
        at JavaScript.<anonymous>(zipline)
        at JavaScript.<anonymous>(zipline)
        at JavaScript.<anonymous>(zipline)
        at JavaScript.tj(zipline)
        at JavaScript.<anonymous>(zipline)
        at JavaScript.<anonymous>(zipline)
        at JavaScript.<eval>(?)
        at app.cash.zipline.QuickJs.evaluate(Native Method)
        at app.cash.zipline.QuickJs.evaluate(QuickJs.kt:110)
        at app.cash.zipline.QuickJs.evaluate$default(QuickJs.kt:109)
        at com.dropbox.componentbox.discovery.zipline.ComponentBoxZipline$produceModelsIn$job$1.invokeSuspend(ComponentBoxZipline.kt:41)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.