-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UBERF-7166 Use blob lookup for direct collaborative doc access (#5774)
Signed-off-by: Alexander Onnikov <[email protected]>
- Loading branch information
Showing
3 changed files
with
73 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// Copyright © 2023 Hardcore Engineering Inc. | ||
// | ||
// Licensed under the Eclipse Public License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. You may | ||
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
import { type Blob, type CollaborativeDoc, type Ref, collaborativeDocParse } from '@hcengineering/core' | ||
import { getBlobHref } from '@hcengineering/presentation' | ||
import { ObservableV2 as Observable } from 'lib0/observable' | ||
import { applyUpdate, type Doc as YDoc } from 'yjs' | ||
|
||
interface EVENTS { | ||
synced: (...args: any[]) => void | ||
} | ||
|
||
async function fetchContent (blob: Ref<Blob>, doc: YDoc): Promise<boolean> { | ||
const update = await fetchBlobContent(blob) | ||
if (update !== undefined) { | ||
applyUpdate(doc, update) | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
async function fetchBlobContent (_id: Ref<Blob>): Promise<Uint8Array | undefined> { | ||
try { | ||
const href = await getBlobHref(undefined, _id) | ||
const res = await fetch(href) | ||
|
||
if (res.ok) { | ||
const blob = await res.blob() | ||
const buffer = await blob.arrayBuffer() | ||
return new Uint8Array(buffer) | ||
} | ||
} catch (err: any) { | ||
console.error(err) | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
export class DirectStorageProvider extends Observable<EVENTS> { | ||
loaded: Promise<void> | ||
|
||
constructor (collaborativeDoc: CollaborativeDoc, doc: YDoc) { | ||
super() | ||
|
||
this.loaded = new Promise((resolve) => { | ||
this.on('synced', resolve) | ||
}) | ||
|
||
const { documentId, versionId } = collaborativeDocParse(collaborativeDoc) | ||
|
||
if (versionId === 'HEAD') { | ||
void fetchContent(documentId as Ref<Blob>, doc).then((synced) => { | ||
if (synced) { | ||
this.emit('synced', [this]) | ||
} | ||
}) | ||
} | ||
} | ||
} |