Skip to content

Banou26/osra

Repository files navigation

osra

what that? https://github.com/GoogleChromeLabs/comlink but nicer to use. thats about it

how to? register your functions in the other context like so

import { makeCallListener, registerListener } from 'osra'

const resolvers = {
  'test': makeCallListener(async (myData) => {
    // do work...
    return {
      foo: 1,
      bar: 'bar',
      baz: () => true
    }
  })
}

registerListener({ target: globalThis, resolvers })

and on your current context you can call it easily like

import { call } from 'osra'

const worker = new Worker('/worker.js', { type: 'module' })

call(worker)('test', { theDataThatWillBeSentToTheOtherContext: 1 })
      .then(({ foo, bar, baz }) => {
        // foo === 1
        // bar === 'bar'
        // baz === callable function that will return a promise with its response
      })

all types of data supported by osra that will be correctly proxied/sent to the other context in addition of functions:

export type TransferableObject =
  ArrayBuffer | MessagePort | ReadableStream | WritableStream |
  TransformStream | /* AudioData | */ ImageBitmap /* | VideoFrame | OffscreenCanvas */

export interface StructuredCloneObject {
  [key: string | number | symbol]: StructuredCloneType
}

export type StructuredCloneType =
  boolean | null | undefined | number | BigInt | string | Date | RegExp | Blob | File | FileList | ArrayBuffer | ArrayBufferView |
  ImageBitmap | ImageData | Array<StructuredCloneType> | StructuredCloneObject | Map<StructuredCloneType, StructuredCloneType> | Set<StructuredCloneType>

export interface StructuredCloneTransferableObject {
  [key: string | number | symbol]: StructuredCloneTransferableType
}

export type StructuredCloneTransferableType =
  StructuredCloneType | TransferableObject | Array<StructuredCloneTransferableType> | StructuredCloneTransferableObject |
  Map<StructuredCloneTransferableType, StructuredCloneTransferableType> | Set<StructuredCloneTransferableType>

About

A nicer comlink

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy