Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SharedArrayBuffer is not defined (#858) #859

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions packages/orama/src/methods/insert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AnyOrama, PartialSchemaDeep, SortValue, TypedDocument } from '../types.js'
import { isArrayType, isGeoPointType, isVectorType } from '../components.js'
import { isAsyncFunction } from '../utils.js'
import { isAsyncFunction, sleep } from '../utils.js'
import { runMultipleHook, runSingleHook } from '../components/hooks.js'
import { trackInsertion } from '../components/sync-blocking-checker.js'
import { createError } from '../errors.js'
Expand Down Expand Up @@ -316,8 +316,6 @@ async function innerInsertMultipleAsync<T extends AnyOrama>(

const processAllBatches = async (): Promise<void> => {
let currentIndex = 0
const sab = new SharedArrayBuffer(4)
const ia = new Int32Array(sab)

while (currentIndex < docs.length) {
const startTime = Date.now()
Expand All @@ -327,7 +325,7 @@ async function innerInsertMultipleAsync<T extends AnyOrama>(
const elapsedTime = Date.now() - startTime
const waitTime = timeout - elapsedTime
if (waitTime > 0) {
Atomics.wait(ia, 0, 0, waitTime)
sleep(waitTime)
}
}
}
Expand Down Expand Up @@ -369,8 +367,6 @@ function innerInsertMultipleSync<T extends AnyOrama>(

function processAllBatches() {
const startTime = Date.now()
const sab = new SharedArrayBuffer(4)
const ia = new Int32Array(sab)

// eslint-disable-next-line no-constant-condition
while (true) {
Expand All @@ -382,7 +378,7 @@ function innerInsertMultipleSync<T extends AnyOrama>(
if (elapsedTime >= timeout) {
const remainingTime = timeout - (elapsedTime % timeout)
if (remainingTime > 0) {
Atomics.wait(ia, 0, 0, remainingTime)
sleep(remainingTime)
}
}
}
Expand All @@ -398,6 +394,7 @@ function innerInsertMultipleSync<T extends AnyOrama>(
return ids
}


export function innerInsertMultiple<T extends AnyOrama>(
orama: T,
docs: PartialSchemaDeep<TypedDocument<T>>[],
Expand Down
28 changes: 28 additions & 0 deletions packages/orama/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,31 @@ export function setUnion<V>(set1: Set<V> | undefined, set2: Set<V>) {
}
return new Set([...set1, ...set2]);
}

// This code is taken from https://github.com/davidmarkclements/atomic-sleep, MIT licensed at the time of commit b8149d3ca276c84a54fa8fa1478f9cc79aabc15a.
// All credits go to the original author (David Mark Clements, https://github.com/davidmarkclements).
export function sleep(ms: number) {
if (typeof SharedArrayBuffer !== 'undefined' && typeof Atomics !== 'undefined') {
const nil = new Int32Array(new SharedArrayBuffer(4))
const valid = ms > 0 && ms < Infinity
if (valid === false) {
if (typeof ms !== 'number' && typeof ms !== 'bigint') {
throw TypeError('sleep: ms must be a number')
}
throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
}

Atomics.wait(nil, 0, 0, Number(ms))

} else {
const valid = ms > 0 && ms < Infinity
if (valid === false) {
if (typeof ms !== 'number' && typeof ms !== 'bigint') {
throw TypeError('sleep: ms must be a number')
}
throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
}
const target = Date.now() + Number(ms)
while (target > Date.now()){ /* empty */ }
}
}
Loading