UNPKG

com.hikky.vketcloudsdk

Version:

This is the SDK for creating the world in VketCloud.

55 lines (44 loc) 1.2 kB
const { Subject, zipWith, groupBy, distinctUntilChanged } = rxjs; const toIngame$ = new Subject(); const receiveRequest$ = new Subject(); toIngame$.pipe(zipWith(receiveRequest$)).subscribe(([v, f]) => f(v)); const fromIngame$ = new Subject(); const receiveData = async () => new Promise((resolve) => receiveRequest$.next(resolve)); const sendData = (data) => { fromIngame$.next(data); }; const keyEventStream$ = new Subject(); const keyEventSubs = keyEventStream$ .pipe(groupBy((i) => i.key)) .subscribe((g) => { g.pipe(distinctUntilChanged((p, c) => p.state === c.state)).subscribe( (i) => { toIngame$.next(i); } ); }); function handleKeyEvent(e, state) { if ( e.target && "nodeName" in e.target && e.target.nodeName in ["INPUT", "TEXTAREA"] ) { return; } const item = { key: e.key, state }; keyEventStream$.next(item); } document.addEventListener("keydown", (e) => { handleKeyEvent(e, "down"); }); document.addEventListener("keyup", (e) => { handleKeyEvent(e, "up"); }); fromIngame$.subscribe((data) => console.warn(`Data received from ingame: ${data}`) ); window.heliport.customApi = { sendData, receiveData, };