feat(editor): replace slot with rxjs subject (#10768)

This commit is contained in:
Mirone
2025-03-12 11:29:24 +09:00
committed by GitHub
parent 19f978d9aa
commit cd63e0ed8b
302 changed files with 1405 additions and 1251 deletions

View File

@@ -1,5 +1,5 @@
import { Slot } from '@blocksuite/global/slot';
import { signal } from '@preact/signals-core';
import { Subject } from 'rxjs';
import * as Y from 'yjs';
import type { YBlock } from '../model/block/types.js';
@@ -34,7 +34,7 @@ export class TestDoc implements Doc {
private readonly _historyObserver = () => {
this._updateCanUndoRedoSignals();
this.slots.historyUpdated.emit();
this.slots.historyUpdated.next();
};
private readonly _initSubDoc = () => {
@@ -45,7 +45,7 @@ export class TestDoc implements Doc {
});
this.rootDoc.getMap('spaces').set(this.id, subDoc);
this._loaded = true;
this._onLoadSlot.emit();
this._onLoadSlot.next();
} else {
this._loaded = false;
this.rootDoc.on('subdocs', this._onSubdocEvent);
@@ -56,7 +56,7 @@ export class TestDoc implements Doc {
private _loaded!: boolean;
private readonly _onLoadSlot = new Slot();
private readonly _onLoadSlot = new Subject<void>();
private readonly _onSubdocEvent = ({
loaded,
@@ -71,7 +71,7 @@ export class TestDoc implements Doc {
}
this.rootDoc.off('subdocs', this._onSubdocEvent);
this._loaded = true;
this._onLoadSlot.emit();
this._onLoadSlot.next();
};
/** Indicate whether the block tree is ready */
@@ -105,8 +105,8 @@ export class TestDoc implements Doc {
readonly rootDoc: Y.Doc;
readonly slots = {
historyUpdated: new Slot(),
yBlockUpdated: new Slot<
historyUpdated: new Subject<void>(),
yBlockUpdated: new Subject<
| {
type: 'add';
id: string;
@@ -186,11 +186,11 @@ export class TestDoc implements Doc {
}
private _handleYBlockAdd(id: string) {
this.slots.yBlockUpdated.emit({ type: 'add', id });
this.slots.yBlockUpdated.next({ type: 'add', id });
}
private _handleYBlockDelete(id: string) {
this.slots.yBlockUpdated.emit({ type: 'delete', id });
this.slots.yBlockUpdated.next({ type: 'delete', id });
}
private _handleYEvent(event: Y.YEvent<YBlock | Y.Text | Y.Array<unknown>>) {
@@ -244,12 +244,12 @@ export class TestDoc implements Doc {
private _destroy() {
this._ySpaceDoc.destroy();
this._onLoadSlot.dispose();
this._onLoadSlot.complete();
this._loaded = false;
}
dispose() {
this.slots.historyUpdated.dispose();
this.slots.historyUpdated.complete();
if (this.ready) {
this._yBlocks.unobserveDeep(this._handleYEvents);

View File

@@ -1,4 +1,4 @@
import { Slot } from '@blocksuite/global/slot';
import { Subject } from 'rxjs';
import type * as Y from 'yjs';
import type {
@@ -45,15 +45,15 @@ export class TestMeta implements WorkspaceMeta {
DocCollectionMetaState[keyof DocCollectionMetaState]
>;
commonFieldsUpdated = new Slot();
commonFieldsUpdated = new Subject<void>();
readonly doc: Y.Doc;
docMetaAdded = new Slot<string>();
docMetaAdded = new Subject<string>();
docMetaRemoved = new Slot<string>();
docMetaRemoved = new Subject<string>();
docMetaUpdated = new Slot();
docMetaUpdated = new Subject<void>();
readonly id: string = 'meta';
@@ -103,7 +103,7 @@ export class TestMeta implements WorkspaceMeta {
}
private _handleCommonFieldsEvent() {
this.commonFieldsUpdated.emit();
this.commonFieldsUpdated.next();
}
private _handleDocMetaEvent() {
@@ -113,7 +113,7 @@ export class TestMeta implements WorkspaceMeta {
docMetas.forEach(docMeta => {
if (!_prevDocs.has(docMeta.id)) {
this.docMetaAdded.emit(docMeta.id);
this.docMetaAdded.next(docMeta.id);
}
newDocs.add(docMeta.id);
});
@@ -121,13 +121,13 @@ export class TestMeta implements WorkspaceMeta {
_prevDocs.forEach(prevDocId => {
const isRemoved = newDocs.has(prevDocId) === false;
if (isRemoved) {
this.docMetaRemoved.emit(prevDocId);
this.docMetaRemoved.next(prevDocId);
}
});
this._prevDocs = newDocs;
this.docMetaUpdated.emit();
this.docMetaUpdated.next();
}
addDocMeta(doc: DocMeta, index?: number) {
@@ -204,6 +204,6 @@ export class TestMeta implements WorkspaceMeta {
setProperties(meta: DocsPropertiesMeta) {
this._proxy.properties = meta;
this.docMetaUpdated.emit();
this.docMetaUpdated.next();
}
}

View File

@@ -1,5 +1,4 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { Slot } from '@blocksuite/global/slot';
import { NoopLogger } from '@blocksuite/global/utils';
import {
AwarenessEngine,
@@ -11,6 +10,7 @@ import {
MemoryBlobSource,
NoopDocSource,
} from '@blocksuite/sync';
import { Subject } from 'rxjs';
import { Awareness } from 'y-protocols/awareness.js';
import * as Y from 'yjs';
@@ -67,9 +67,9 @@ export class TestWorkspace implements Workspace {
meta: WorkspaceMeta;
slots = {
docListUpdated: new Slot(),
docRemoved: new Slot<string>(),
docCreated: new Slot<string>(),
docListUpdated: new Subject<void>(),
docRemoved: new Subject<string>(),
docCreated: new Subject<string>(),
};
get docs() {
@@ -116,7 +116,7 @@ export class TestWorkspace implements Workspace {
}
private _bindDocMetaEvents() {
this.meta.docMetaAdded.on(docId => {
this.meta.docMetaAdded.subscribe(docId => {
const doc = new TestDoc({
id: docId,
collection: this,
@@ -126,14 +126,14 @@ export class TestWorkspace implements Workspace {
this.blockCollections.set(doc.id, doc);
});
this.meta.docMetaUpdated.on(() => this.slots.docListUpdated.emit());
this.meta.docMetaUpdated.subscribe(() => this.slots.docListUpdated.next());
this.meta.docMetaRemoved.on(id => {
this.meta.docMetaRemoved.subscribe(id => {
const space = this.getBlockCollection(id);
if (!space) return;
this.blockCollections.delete(id);
space.remove();
this.slots.docRemoved.emit(id);
this.slots.docRemoved.next(id);
});
}
@@ -174,7 +174,7 @@ export class TestWorkspace implements Workspace {
createDate: Date.now(),
tags: [],
});
this.slots.docCreated.emit(docId);
this.slots.docCreated.next(docId);
return this.getDoc(docId, {
id: docId,
query,