Skip to content

[Documentation] How to actually use BitField / BitArray in schema definitions? #30

@vinerz

Description

@vinerz

Hello! First of all, thank you for the marvelous (and quite unique) way of handling binary data in JavaScript! This library is paving the way for an intra-worker communication I've been developing. Copying/Serializing objects have been an absurd overhead due to the application communication throughput so I started looking into SharedArrayBuffers to transfer data directly.

I already got the gist of BitArrays by themselves, but I'm really struggling to understand the view.create<T> expectations when the schema uses some kind of these special data handlers.

I wrote a basic pseudo-code to explain what I am trying to achieve.
The ??? comments are for sections I am specifically clueless.

interface User {
  username: string
  attributes: number[] // ???
}

const UserView = view.create<User>({
  $id: 'User',
  type: 'object',
  
  properties: {
    username: {
      type: 'string',
      maxLength: 10,
    },

    attributes: {
      type: 'array',
      btype: 'uint32', // ???
      maxLength: 4,
    },
  },
})

const user = UserView.from({
   username: 'vinerz',
   attributes: [0, 1, 0, 1], // ???
 })

// work with the user data
user.getView('attributes').setBit(2, 1) 

// transfer control to worker
worker.postMessage({ user: user.buffer }, [user.buffer])

Could you please shed some light here?
Is it possible or am I thinking in a wrong way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions