feat(editor): replace slot with rxjs subject (#10768)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { Slot } from '@blocksuite/global/slot';
|
||||
import type { Subject } from 'rxjs';
|
||||
import { assert, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { applyUpdate, type Doc, encodeStateAsUpdate } from 'yjs';
|
||||
|
||||
@@ -36,8 +36,13 @@ function serializCollection(doc: Doc): Record<string, any> {
|
||||
};
|
||||
}
|
||||
|
||||
function waitOnce<T>(slot: Slot<T>) {
|
||||
return new Promise<T>(resolve => slot.once(val => resolve(val)));
|
||||
function waitOnce<T>(slot: Subject<T>) {
|
||||
return new Promise<T>(resolve => {
|
||||
const subscription = slot.subscribe(val => {
|
||||
subscription.unsubscribe();
|
||||
resolve(val);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function createRoot(doc: Store) {
|
||||
@@ -150,8 +155,8 @@ describe('basic', () => {
|
||||
|
||||
const readyCallback = vi.fn();
|
||||
const rootAddedCallback = vi.fn();
|
||||
doc.slots.ready.on(readyCallback);
|
||||
doc.slots.rootAdded.on(rootAddedCallback);
|
||||
doc.slots.ready.subscribe(readyCallback);
|
||||
doc.slots.rootAdded.subscribe(rootAddedCallback);
|
||||
|
||||
doc.load(() => {
|
||||
const rootId = doc.addBlock('affine:page', {
|
||||
@@ -428,7 +433,7 @@ describe('addBlock', () => {
|
||||
);
|
||||
|
||||
let called = false;
|
||||
collection.slots.docListUpdated.on(() => {
|
||||
collection.slots.docListUpdated.subscribe(() => {
|
||||
called = true;
|
||||
});
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ test('trigger props updated', () => {
|
||||
expect(rootModel).not.toBeNull();
|
||||
|
||||
const onPropsUpdated = vi.fn();
|
||||
rootModel.propsUpdated.on(onPropsUpdated);
|
||||
rootModel.propsUpdated.subscribe(onPropsUpdated);
|
||||
|
||||
const getColor = () =>
|
||||
(rootModel.yBlock.get('prop:style') as Y.Map<string>).get('color');
|
||||
@@ -101,7 +101,7 @@ test('stash and pop', () => {
|
||||
expect(rootModel).not.toBeNull();
|
||||
|
||||
const onPropsUpdated = vi.fn();
|
||||
rootModel.propsUpdated.on(onPropsUpdated);
|
||||
rootModel.propsUpdated.subscribe(onPropsUpdated);
|
||||
|
||||
const getCount = () => rootModel.yBlock.get('prop:count');
|
||||
const getColor = () =>
|
||||
@@ -171,7 +171,7 @@ test('always get latest value in onChange', () => {
|
||||
expect(rootModel).not.toBeNull();
|
||||
|
||||
let value: unknown;
|
||||
rootModel.propsUpdated.on(({ key }) => {
|
||||
rootModel.propsUpdated.subscribe(({ key }) => {
|
||||
// @ts-expect-error ignore
|
||||
value = rootModel[key];
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Slot } from '@blocksuite/global/slot';
|
||||
import { Subject } from 'rxjs';
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import * as Y from 'yjs';
|
||||
|
||||
@@ -184,7 +184,7 @@ test('flat', () => {
|
||||
map.set('prop:col.c.d', 3);
|
||||
map.set('prop:col.c.e', 4);
|
||||
|
||||
const reactive = new ReactiveFlatYMap(map, new Slot());
|
||||
const reactive = new ReactiveFlatYMap(map, new Subject());
|
||||
const proxy = reactive.proxy as Record<string, any>;
|
||||
proxy.col.c.d = 200;
|
||||
expect(map.get('prop:col.c.d')).toBe(200);
|
||||
|
||||
Reference in New Issue
Block a user