UNPKG

comctx

Version:

Use RPC to communicate easily across contexts in any JavaScript environment.

32 lines (29 loc) 688 B
import { defineProxy } from 'comctx' export class Counter { private value: number constructor(initialValue: number = 0) { this.value = initialValue } async getValue() { return this.value } async onChange(callback: (value: number) => void) { let oldValue = this.value setInterval(() => { const newValue = this.value if (oldValue !== newValue) { callback(newValue) oldValue = newValue } }) } async increment() { return ++this.value } async decrement() { return --this.value } } export const [provideCounter, injectCounter] = defineProxy(() => new Counter(), { namespace: '__shared-worker-example__' })