Files
AFFiNE-Mirror/blocksuite/framework/store/src/reactive/base-reactive-data.ts
T
EYHN 2d1600fa00 refactor(core): implement doc created/updated by service (#12150)
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Documents now automatically track and display "created by" and "updated by" user information.
  - Document creation and update timestamps are now managed and shown more accurately.
  - Workspace and document metadata (name, avatar) updates are more responsive and reliable.
  - Document creation supports middleware for customizing properties and behavior.

- **Improvements**
  - Simplified and unified event handling for document list updates, reducing redundant event subscriptions.
  - Enhanced integration of editor and theme settings into the document creation process.
  - Explicit Yjs document initialization for improved workspace stability and reliability.
  - Consolidated journal-related metadata display in document icons and titles for clarity.

- **Bug Fixes**
  - Fixed inconsistencies in how workspace and document names are set and updated.
  - Improved accuracy of "last updated" indicators by handling timestamps automatically.

- **Refactor**
  - Removed deprecated event subjects and direct metadata manipulation in favor of more robust, reactive patterns.
  - Streamlined document creation logic across various features (quick search, journal, recording, etc.).
  - Simplified user avatar display components and removed cloud metadata dependencies.
  - Removed legacy editor setting and theme service dependencies from multiple modules.

- **Chores**
  - Updated internal APIs and interfaces to support new metadata and event handling mechanisms.
  - Cleaned up unused code and dependencies related to editor settings and theme services.
  - Skipped flaky end-to-end test to improve test suite stability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-05-08 07:53:33 +00:00

77 lines
1.7 KiB
TypeScript

import * as Y from 'yjs';
import type { ProxyOptions } from './types';
export abstract class BaseReactiveYData<
T,
YSource extends Y.AbstractType<any>,
> {
protected _getOrigin = (
doc: Y.Doc
): {
doc: Y.Doc;
proxy: true;
target: BaseReactiveYData<any, any>;
} => {
return {
doc,
proxy: true,
target: this,
};
};
protected _onObserve = (event: Y.YEvent<any>, handler: () => void) => {
if (
event.transaction.origin?.force === true ||
(event.transaction.origin?.proxy !== true &&
(!event.transaction.local ||
event.transaction.origin instanceof Y.UndoManager))
) {
handler();
}
const isLocal =
!event.transaction.origin ||
!this._ySource.doc ||
event.transaction.origin instanceof Y.UndoManager ||
event.transaction.origin.proxy
? true
: event.transaction.origin === this._ySource.doc.clientID;
this._options?.onChange?.(this._proxy, isLocal);
};
protected abstract readonly _options?: ProxyOptions<T>;
protected abstract readonly _proxy: T;
protected _skipNext = false;
protected abstract readonly _source: T;
protected readonly _stashed = new Set<string | number>();
protected _transact = (doc: Y.Doc, fn: () => void) => {
doc.transact(fn, this._getOrigin(doc));
};
protected _updateWithSkip = (fn: () => void) => {
if (this._skipNext) {
return;
}
this._skipNext = true;
fn();
this._skipNext = false;
};
protected abstract readonly _ySource: YSource;
get proxy() {
return this._proxy;
}
abstract pop(prop: string | number): void;
abstract stash(prop: string | number): void;
}