mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-24 18:02:47 +08:00
refactor: move blob related jsb to IPCBlobProvider
This commit is contained in:
@@ -9,7 +9,6 @@ import assert from 'assert';
|
|||||||
import { getLogger } from './logger';
|
import { getLogger } from './logger';
|
||||||
import { BlockSchema } from '@blocksuite/blocks/models';
|
import { BlockSchema } from '@blocksuite/blocks/models';
|
||||||
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
||||||
import { SelfHostedProvider } from './provider/selfhosted';
|
|
||||||
import { TauriIPCProvider } from './provider/tauri-ipc';
|
import { TauriIPCProvider } from './provider/tauri-ipc';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,12 +46,12 @@ export class DataCenter {
|
|||||||
blobs: dc._blobStorage,
|
blobs: dc._blobStorage,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
dc.registerProvider(new SelfHostedProvider());
|
|
||||||
if (typeof window !== 'undefined' && window.CLIENT_APP) {
|
if (typeof window !== 'undefined' && window.CLIENT_APP) {
|
||||||
dc.registerProvider(
|
dc.registerProvider(
|
||||||
new TauriIPCProvider({
|
new TauriIPCProvider({
|
||||||
logger: dc._logger,
|
logger: dc._logger,
|
||||||
workspaces: dc._workspaces.createScope(),
|
workspaces: dc._workspaces.createScope(),
|
||||||
|
blobs: dc._blobStorage,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,38 +6,57 @@ import {
|
|||||||
import * as ipcMethods from '../ipc/methods.js';
|
import * as ipcMethods from '../ipc/methods.js';
|
||||||
import { Signal } from '@blocksuite/store';
|
import { Signal } from '@blocksuite/store';
|
||||||
|
|
||||||
|
// @staticImplements<BlobProviderStatic>()
|
||||||
export class IPCBlobProvider implements BlobProvider {
|
export class IPCBlobProvider implements BlobProvider {
|
||||||
#ipc = ipcMethods;
|
#ipc = ipcMethods;
|
||||||
|
|
||||||
blobs: Set<BlobId>;
|
readonly blobs: Set<string> = new Set();
|
||||||
signals: {
|
readonly signals = {
|
||||||
blobAdded: Signal<BlobId>;
|
blobAdded: new Signal<BlobId>(),
|
||||||
|
blobDeleted: new Signal<BlobId>(),
|
||||||
};
|
};
|
||||||
|
|
||||||
static async init(
|
|
||||||
workspace: string,
|
|
||||||
cloudApi?: string
|
|
||||||
): Promise<IPCBlobProvider> {
|
|
||||||
const provider = new IPCBlobProvider(workspace, cloudApi);
|
|
||||||
await provider._initBlobs();
|
|
||||||
return provider;
|
|
||||||
}
|
|
||||||
|
|
||||||
async get(id: BlobId): Promise<BlobURL | null> {
|
async get(id: BlobId): Promise<BlobURL | null> {
|
||||||
const blobArray = await this.#ipc.getBlob({
|
const blobArray = await this.#ipc.getBlob({
|
||||||
id,
|
id,
|
||||||
});
|
});
|
||||||
// Make a Blob from the bytes
|
// Make a Blob from the bytes
|
||||||
const blob = new Blob([new Uint8Array(blobArray)], { type: 'image/bmp' });
|
const blob = new Blob([new Uint8Array(blobArray)], { type: 'image/bmp' });
|
||||||
|
if (blob) {
|
||||||
|
this.signals.blobAdded.emit(id);
|
||||||
|
this.blobs.add(id);
|
||||||
|
}
|
||||||
return window.URL.createObjectURL(blob);
|
return window.URL.createObjectURL(blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
async set(blob: Blob): Promise<BlobId> {
|
async set(blob: Blob): Promise<BlobId> {
|
||||||
return this.#ipc.putBlob({
|
// TODO: skip if already has
|
||||||
|
const blobID = await this.#ipc.putBlob({
|
||||||
blob: Array.from(new Uint8Array(await blob.arrayBuffer())),
|
blob: Array.from(new Uint8Array(await blob.arrayBuffer())),
|
||||||
});
|
});
|
||||||
|
this.signals.blobAdded.emit(blobID);
|
||||||
|
return blobID;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(id: BlobId): Promise<void>;
|
// TODO: implement getAllKeys in Octobase
|
||||||
clear(): Promise<void>;
|
// private async _initBlobs() {
|
||||||
|
// const entries = await this._database.keys();
|
||||||
|
// for (const key of entries) {
|
||||||
|
// const blobId = key as BlobId;
|
||||||
|
// this.signals.blobAdded.emit(blobId);
|
||||||
|
// this.blobs.add(blobId);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
async delete(id: BlobId): Promise<void> {
|
||||||
|
// TODO: implement blob delete in Octobase
|
||||||
|
this.signals.blobDeleted.emit(id);
|
||||||
|
this.blobs.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
async clear(): Promise<void> {
|
||||||
|
// TODO: implement blob clear in Octobase, need workspace id
|
||||||
|
// await this._database.clear();
|
||||||
|
this.blobs.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export class TauriIPCProvider extends LocalProvider {
|
|||||||
constructor(params: ProviderConstructorParams) {
|
constructor(params: ProviderConstructorParams) {
|
||||||
super(params);
|
super(params);
|
||||||
// TODO: let blocksuite's blob provider get blob receive workspace id. Currently, all blobs are placed together
|
// TODO: let blocksuite's blob provider get blob receive workspace id. Currently, all blobs are placed together
|
||||||
this._blobs = new IPCBlobProvider();
|
this._blobs.addProvider(new IPCBlobProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
async initData() {
|
async initData() {
|
||||||
|
|||||||
Reference in New Issue
Block a user