Skip to content

Instantly share code, notes, and snippets.

@iacore
Created February 5, 2023 14:15
Show Gist options
  • Save iacore/2ad815895435c7eb9bd5cabc06848ffa to your computer and use it in GitHub Desktop.
Save iacore/2ad815895435c7eb9bd5cabc06848ffa to your computer and use it in GitHub Desktop.
Qwik + RxJs
import {
component$,
useSignal,
$,
} from "@builder.io/qwik"
import { loader$, DocumentHead } from "@builder.io/qwik-city"
import { scan, Subject } from "rxjs"
export const timeLoader = loader$(() => new Date())
export default component$(() => {
const count = useSignal(0)
const now = timeLoader.use()
const ctrl = $(
useSubject<null>((s) => {
s.pipe(scan((count) => count + 1, 0)).subscribe((count) =>
alert(`Clicked ${count} times`)
)
})
)
return (
<a>
Now: {now.value.toISOString()}
{count.value}
<button onClick$={async () => (await ctrl.resolve()).next(null)}>
+
</button>
</a>
)
})
export const head: DocumentHead = {
title: "RxJs test",
}
export function useSubject<T>(fn: (subject: Subject<T>) => void): Subject<T> {
const subject = new Subject<null>()
fn(subject)
return subject
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment