@bnaya/objectbuffer
Basic usage (Browser):
[[include:docs/codeExamples/basicUsage.js]]
- acquireLock
- acquireLockWait
- createObjectBuffer
- getUnderlyingArrayBuffer
- loadObjectBuffer
- memoryStats
- processQueuedReclaims
- queueReclaim
- reclaim
- releaseLock
- unstable_replaceUnderlyingArrayBuffer
- unstable_resizeObjectBuffer
- updateObjectBufferSettings
▸ acquireLock(agentId
, objectBuffer
): boolean
Tries to acquire a lock on the given objectBuffer, as the given agentId.
Name | Type |
---|---|
agentId |
number |
objectBuffer |
any |
boolean
▸ acquireLockWait(agentId
, objectBuffer
, timeout
): "have-lock"
| "miss-lock"
| "timed-out"
| "no-lock"
Try to get a lock on a the given objectBuffer, or wait until timeout
Will Not work on the main thread.
Use only on workers
Only when "have-lock"
returned you actually got the lock
Uses https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/wait under the hood
Name | Type |
---|---|
agentId |
number |
objectBuffer |
any |
timeout |
number |
"have-lock"
| "miss-lock"
| "timed-out"
| "no-lock"
▸ createObjectBuffer<T>(size
, initialValue
, settings?
, arrayBufferKind?
): T
Create a new objectBuffer, with the given initial value
Name | Type |
---|---|
T |
T = any |
Name | Type | Default value | Description |
---|---|---|---|
size |
number |
undefined |
The size of the ArrayBuffer to create (heap size) |
initialValue |
T |
undefined |
|
settings |
ObjectBufferSettings | {} | |
arrayBufferKind |
ArrayBufferKind |
"vanilla" | - |
T
▸ getUnderlyingArrayBuffer(objectBuffer
): ArrayBuffer
| SharedArrayBuffer
Name | Type |
---|---|
objectBuffer |
unknown |
ArrayBuffer
| SharedArrayBuffer
▸ loadObjectBuffer<T>(arrayBuffer
, settings?
): T
Create objectBuffer object from the given ArrayBuffer
The given ArrayBuffer is expected to be one obtained via getUnderlyingArrayBuffer This operation doesn't change any value in the ArrayBuffer
Name | Type |
---|---|
T |
T = any |
Name | Type |
---|---|
arrayBuffer |
ArrayBuffer | SharedArrayBuffer |
settings |
ObjectBufferSettings |
T
▸ memoryStats(objectBuffer
): MemoryStats
Return the number of free & used bytes left in the given objectBuffer
Name | Type |
---|---|
objectBuffer |
unknown |
MemoryStats
▸ processQueuedReclaims(objectBuffer
): void
Free all the addresses collected using FinalizationRegistry When no FinalizationRegistry/WeakRef available, use disposeWrapperObject https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
This is not called automatic by FinalizationRegistry, because It's only safe to call it when you have a lock/similar (As any other operation) And FinalizationRegistry might run when ever
Name | Type |
---|---|
objectBuffer |
unknown |
void
▸ queueReclaim(objectBufferPart
): void
Similar to reclaim, Dispose the given objectBuffer part, make it not usable anymore, but only queue the memory for later reclaiming
see
processQueuedReclaims for actually processing the reclaim
The given part should not be top level ObjectBuffer
Name | Type |
---|---|
objectBufferPart |
unknown |
void
▸ reclaim(objectBufferPart
): boolean
Dispose the given objectBuffer part, make it not usable anymore, immediately reclaiming memory if applicable (arc = 0 etc) To be used on systems that does not support FinalizationRegistry or for immediate and not eventual memory reclaiming
Name | Type |
---|---|
objectBufferPart |
unknown |
boolean
▸ releaseLock(agentId
, objectBuffer
): boolean
Try to release a lock acquired by acquireLock or acquireLockWait
Name | Type |
---|---|
agentId |
number |
objectBuffer |
any |
boolean
▸ unstable_replaceUnderlyingArrayBuffer(objectBuffer
, newArrayBuffer
): void
Replace the Underlying array buffer with the given one. The given ArrayBuffer is expected to be a copy of the prev ArrayBuffer, just bigger or smaller (less free space)
Consider using resizeObjectBuffer
unstable
Due to possible issues with future support of typed arrays,
and the upcoming proposal, this api function may be removed
https://github.com/tc39/proposal-resizablearraybuffer
Name | Type |
---|---|
objectBuffer |
unknown |
newArrayBuffer |
ArrayBuffer | SharedArrayBuffer |
void
▸ unstable_resizeObjectBuffer(objectBuffer
, newSize
): ArrayBuffer
Grow or shrink the underlying ArrayBuffer
unstable
Due to possible issues with future support of typed arrays,
and the upcoming proposal, this api function may be removed
https://github.com/tc39/proposal-resizablearraybuffer
Name | Type |
---|---|
objectBuffer |
unknown |
newSize |
number |
ArrayBuffer
▸ updateObjectBufferSettings(objectBuffer
, options
): void
Update the settings of the given ObjectBuffer
Name | Type |
---|---|
objectBuffer |
unknown |
options |
ObjectBufferSettings |
void