mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-19 19:16:29 +08:00
fix: variable naming
This commit is contained in:
@@ -3,29 +3,29 @@ import { Array as YArray, Map as YMap } from 'yjs';
|
||||
import { RemoteKvService } from '@toeverything/datasource/remote-kv';
|
||||
|
||||
export class YjsRemoteBinaries {
|
||||
readonly #binaries: YMap<YArray<ArrayBuffer>>; // binary instance
|
||||
readonly #remote_storage?: RemoteKvService;
|
||||
readonly _binaries: YMap<YArray<ArrayBuffer>>; // binary instance
|
||||
readonly _remoteStorage?: RemoteKvService;
|
||||
|
||||
constructor(binaries: YMap<YArray<ArrayBuffer>>, remote_token?: string) {
|
||||
this.#binaries = binaries;
|
||||
this._binaries = binaries;
|
||||
if (remote_token) {
|
||||
this.#remote_storage = new RemoteKvService(remote_token);
|
||||
this._remoteStorage = new RemoteKvService(remote_token);
|
||||
} else {
|
||||
console.warn(`Remote storage is not ready`);
|
||||
}
|
||||
}
|
||||
|
||||
has(name: string): boolean {
|
||||
return this.#binaries.has(name);
|
||||
return this._binaries.has(name);
|
||||
}
|
||||
|
||||
async get(name: string): Promise<YArray<ArrayBuffer> | undefined> {
|
||||
if (this.#binaries.has(name)) {
|
||||
return this.#binaries.get(name);
|
||||
if (this._binaries.has(name)) {
|
||||
return this._binaries.get(name);
|
||||
} else {
|
||||
// TODO: Remote Load
|
||||
try {
|
||||
const file = await this.#remote_storage?.instance.getBuffData(
|
||||
const file = await this._remoteStorage?.instance.getBuffData(
|
||||
name
|
||||
);
|
||||
console.log(file);
|
||||
@@ -38,16 +38,16 @@ export class YjsRemoteBinaries {
|
||||
}
|
||||
|
||||
async set(name: string, binary: YArray<ArrayBuffer>) {
|
||||
if (!this.#binaries.has(name)) {
|
||||
if (!this._binaries.has(name)) {
|
||||
console.log(name, 'name');
|
||||
if (binary.length === 1) {
|
||||
this.#binaries.set(name, binary);
|
||||
if (this.#remote_storage) {
|
||||
this._binaries.set(name, binary);
|
||||
if (this._remoteStorage) {
|
||||
// TODO: Remote Save, if there is an object with the same name remotely, the upload is skipped, because the file name is the hash of the file content
|
||||
const has_file = this.#remote_storage.instance.exist(name);
|
||||
const has_file = this._remoteStorage.instance.exist(name);
|
||||
if (!has_file) {
|
||||
const upload_file = new File(binary.toArray(), name);
|
||||
await this.#remote_storage.instance
|
||||
await this._remoteStorage.instance
|
||||
.upload(upload_file)
|
||||
.catch(err => {
|
||||
throw new Error(`${err} upload error`);
|
||||
|
||||
@@ -32,49 +32,49 @@ type YjsBlockInstanceProps = {
|
||||
};
|
||||
|
||||
export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
|
||||
readonly #id: string;
|
||||
readonly #block: YMap<unknown>;
|
||||
readonly #binary?: YArray<ArrayBuffer>;
|
||||
readonly #children: YArray<string>;
|
||||
readonly #set_block: (
|
||||
readonly _id: string;
|
||||
readonly _block: YMap<unknown>;
|
||||
readonly _binary?: YArray<ArrayBuffer>;
|
||||
readonly _children: YArray<string>;
|
||||
readonly _setBlock: (
|
||||
id: string,
|
||||
block: BlockItem<YjsContentOperation>
|
||||
) => Promise<void>;
|
||||
readonly #get_updated: (id: string) => number | undefined;
|
||||
readonly #get_creator: (id: string) => string | undefined;
|
||||
readonly #get_block_instance: (id: string) => YjsBlockInstance | undefined;
|
||||
readonly #children_listeners: Map<string, BlockListener>;
|
||||
readonly #content_listeners: Map<string, BlockListener>;
|
||||
readonly _getUpdated: (id: string) => number | undefined;
|
||||
readonly _getCreator: (id: string) => string | undefined;
|
||||
readonly _getBlockInstance: (id: string) => YjsBlockInstance | undefined;
|
||||
readonly _childrenListeners: Map<string, BlockListener>;
|
||||
readonly _contentListeners: Map<string, BlockListener>;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
#children_map: Map<string, number>;
|
||||
_childrenMap: Map<string, number>;
|
||||
|
||||
constructor(props: YjsBlockInstanceProps) {
|
||||
this.#id = props.id;
|
||||
this.#block = props.block;
|
||||
this.#binary = props.binary;
|
||||
this._id = props.id;
|
||||
this._block = props.block;
|
||||
this._binary = props.binary;
|
||||
|
||||
this.#children = props.block.get('children') as YArray<string>;
|
||||
this.#children_map = getMapFromYArray(this.#children);
|
||||
this.#set_block = props.setBlock;
|
||||
this.#get_updated = props.getUpdated;
|
||||
this.#get_creator = props.getCreator;
|
||||
this.#get_block_instance = props.getBlockInstance;
|
||||
this._children = props.block.get('children') as YArray<string>;
|
||||
this._childrenMap = getMapFromYArray(this._children);
|
||||
this._setBlock = props.setBlock;
|
||||
this._getUpdated = props.getUpdated;
|
||||
this._getCreator = props.getCreator;
|
||||
this._getBlockInstance = props.getBlockInstance;
|
||||
|
||||
this.#children_listeners = new Map();
|
||||
this.#content_listeners = new Map();
|
||||
this._childrenListeners = new Map();
|
||||
this._contentListeners = new Map();
|
||||
|
||||
const content = this.#block.get('content') as YMap<unknown>;
|
||||
const content = this._block.get('content') as YMap<unknown>;
|
||||
|
||||
this.#children.observe(event =>
|
||||
ChildrenListenerHandler(this.#children_listeners, event)
|
||||
this._children.observe(event =>
|
||||
ChildrenListenerHandler(this._childrenListeners, event)
|
||||
);
|
||||
content?.observeDeep(events =>
|
||||
ContentListenerHandler(this.#content_listeners, events)
|
||||
ContentListenerHandler(this._contentListeners, events)
|
||||
);
|
||||
// TODO: flavor needs optimization
|
||||
this.#block.observeDeep(events =>
|
||||
ContentListenerHandler(this.#content_listeners, events)
|
||||
this._block.observeDeep(events =>
|
||||
ContentListenerHandler(this._contentListeners, events)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -99,85 +99,85 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
|
||||
}
|
||||
|
||||
addChildrenListener(name: string, listener: BlockListener): void {
|
||||
this.#children_listeners.set(name, listener);
|
||||
this._childrenListeners.set(name, listener);
|
||||
}
|
||||
|
||||
removeChildrenListener(name: string): void {
|
||||
this.#children_listeners.delete(name);
|
||||
this._childrenListeners.delete(name);
|
||||
}
|
||||
|
||||
addContentListener(name: string, listener: BlockListener): void {
|
||||
this.#content_listeners.set(name, listener);
|
||||
this._contentListeners.set(name, listener);
|
||||
}
|
||||
|
||||
removeContentListener(name: string): void {
|
||||
this.#content_listeners.delete(name);
|
||||
this._contentListeners.delete(name);
|
||||
}
|
||||
|
||||
get id() {
|
||||
return this.#id;
|
||||
return this._id;
|
||||
}
|
||||
|
||||
get content(): YjsContentOperation {
|
||||
if (this.type === BlockTypes.block) {
|
||||
const content = this.#block.get('content');
|
||||
const content = this._block.get('content');
|
||||
if (content instanceof YAbstractType) {
|
||||
return new YjsContentOperation(content);
|
||||
} else {
|
||||
throw new Error(`Invalid content type: ${typeof content}`);
|
||||
}
|
||||
} else if (this.type === BlockTypes.binary && this.#binary) {
|
||||
return new YjsContentOperation(this.#binary);
|
||||
} else if (this.type === BlockTypes.binary && this._binary) {
|
||||
return new YjsContentOperation(this._binary);
|
||||
}
|
||||
throw new Error(
|
||||
`Invalid content type: ${this.type}, ${this.#block.get(
|
||||
`Invalid content type: ${this.type}, ${this._block.get(
|
||||
'content'
|
||||
)}, ${this.#binary}`
|
||||
)}, ${this._binary}`
|
||||
);
|
||||
}
|
||||
|
||||
get type(): BlockItem<YjsContentOperation>['type'] {
|
||||
return this.#block.get(
|
||||
return this._block.get(
|
||||
'type'
|
||||
) as BlockItem<YjsContentOperation>['type'];
|
||||
}
|
||||
|
||||
get flavor(): BlockItem<YjsContentOperation>['flavor'] {
|
||||
return this.#block.get(
|
||||
return this._block.get(
|
||||
'flavor'
|
||||
) as BlockItem<YjsContentOperation>['flavor'];
|
||||
}
|
||||
|
||||
// TODO: bad case. Need to optimize.
|
||||
setFlavor(flavor: BlockItem<YjsContentOperation>['flavor']) {
|
||||
this.#block.set('flavor', flavor);
|
||||
this._block.set('flavor', flavor);
|
||||
}
|
||||
|
||||
get created(): BlockItem<YjsContentOperation>['created'] {
|
||||
return this.#block.get(
|
||||
return this._block.get(
|
||||
'created'
|
||||
) as BlockItem<YjsContentOperation>['created'];
|
||||
}
|
||||
|
||||
get updated(): number {
|
||||
return this.#get_updated(this.#id) || this.created;
|
||||
return this._getUpdated(this._id) || this.created;
|
||||
}
|
||||
|
||||
get creator(): string | undefined {
|
||||
return this.#get_creator(this.#id);
|
||||
return this._getCreator(this._id);
|
||||
}
|
||||
|
||||
get children(): string[] {
|
||||
return this.#children.toArray();
|
||||
return this._children.toArray();
|
||||
}
|
||||
|
||||
getChildren(ids?: (string | undefined)[]): YjsBlockInstance[] {
|
||||
const query_ids = ids?.filter((id): id is string => !!id) || [];
|
||||
const exists_ids = this.#children.map(id => id);
|
||||
const exists_ids = this._children.map(id => id);
|
||||
const filter_ids = query_ids.length ? query_ids : exists_ids;
|
||||
return exists_ids
|
||||
.filter(id => filter_ids.includes(id))
|
||||
.map(id => this.#get_block_instance(id))
|
||||
.map(id => this._getBlockInstance(id))
|
||||
.filter((v): v is YjsBlockInstance => !!v);
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
|
||||
return pos;
|
||||
}
|
||||
} else if (before) {
|
||||
const current_pos = this.#children_map.get(before || '');
|
||||
const current_pos = this._childrenMap.get(before || '');
|
||||
if (
|
||||
typeof current_pos === 'number' &&
|
||||
Number.isInteger(current_pos)
|
||||
@@ -207,7 +207,7 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
|
||||
}
|
||||
}
|
||||
} else if (after) {
|
||||
const current_pos = this.#children_map.get(after || '');
|
||||
const current_pos = this._childrenMap.get(after || '');
|
||||
if (
|
||||
typeof current_pos === 'number' &&
|
||||
Number.isInteger(current_pos)
|
||||
@@ -227,44 +227,44 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
|
||||
): Promise<void> {
|
||||
const content = block[GET_BLOCK_ITEM]();
|
||||
if (content) {
|
||||
const lastIndex = this.#children_map.get(block.id);
|
||||
const lastIndex = this._childrenMap.get(block.id);
|
||||
if (typeof lastIndex === 'number') {
|
||||
this.#children.delete(lastIndex);
|
||||
this.#children_map = getMapFromYArray(this.#children);
|
||||
this._children.delete(lastIndex);
|
||||
this._childrenMap = getMapFromYArray(this._children);
|
||||
}
|
||||
|
||||
const position = this.position_calculator(
|
||||
this.#children_map.size,
|
||||
this._childrenMap.size,
|
||||
pos
|
||||
);
|
||||
if (typeof position === 'number') {
|
||||
this.#children.insert(position, [block.id]);
|
||||
this._children.insert(position, [block.id]);
|
||||
} else {
|
||||
this.#children.push([block.id]);
|
||||
this._children.push([block.id]);
|
||||
}
|
||||
await this.#set_block(block.id, content);
|
||||
this.#children_map = getMapFromYArray(this.#children);
|
||||
await this._setBlock(block.id, content);
|
||||
this._childrenMap = getMapFromYArray(this._children);
|
||||
}
|
||||
}
|
||||
|
||||
removeChildren(ids: (string | undefined)[]): Promise<string[]> {
|
||||
return new Promise(resolve => {
|
||||
if (this.#children.doc) {
|
||||
transact(this.#children.doc, () => {
|
||||
if (this._children.doc) {
|
||||
transact(this._children.doc, () => {
|
||||
const failed = [];
|
||||
for (const id of ids) {
|
||||
let idx = -1;
|
||||
for (const block_id of this.#children) {
|
||||
for (const block_id of this._children) {
|
||||
idx += 1;
|
||||
if (block_id === id) {
|
||||
this.#children.delete(idx);
|
||||
this._children.delete(idx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (id) failed.push(id);
|
||||
}
|
||||
|
||||
this.#children_map = getMapFromYArray(this.#children);
|
||||
this._childrenMap = getMapFromYArray(this._children);
|
||||
resolve(failed);
|
||||
});
|
||||
} else {
|
||||
@@ -274,7 +274,7 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
|
||||
}
|
||||
|
||||
public scopedHistory(scope: any[]): HistoryManager {
|
||||
return new YjsHistoryManager(this.#block, scope);
|
||||
return new YjsHistoryManager(this._block, scope);
|
||||
}
|
||||
|
||||
[GET_BLOCK_ITEM]() {
|
||||
@@ -283,7 +283,7 @@ export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
|
||||
return {
|
||||
type: this.type,
|
||||
flavor: this.flavor,
|
||||
children: this.#children.slice(),
|
||||
children: this._children.slice(),
|
||||
created: this.created,
|
||||
content: this.content,
|
||||
};
|
||||
|
||||
@@ -2,35 +2,35 @@ import { Map as YMap } from 'yjs';
|
||||
|
||||
export class GateKeeper {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
#user_id: string;
|
||||
#creators: YMap<string>;
|
||||
#common: YMap<string>;
|
||||
_userId: string;
|
||||
_creators: YMap<string>;
|
||||
_common: YMap<string>;
|
||||
|
||||
constructor(userId: string, creators: YMap<string>, common: YMap<string>) {
|
||||
this.#user_id = userId;
|
||||
this.#creators = creators;
|
||||
this.#common = common;
|
||||
this._userId = userId;
|
||||
this._creators = creators;
|
||||
this._common = common;
|
||||
}
|
||||
|
||||
getCreator(block_id: string): string | undefined {
|
||||
return this.#creators.get(block_id) || this.#common.get(block_id);
|
||||
return this._creators.get(block_id) || this._common.get(block_id);
|
||||
}
|
||||
|
||||
setCreator(block_id: string) {
|
||||
if (!this.#creators.get(block_id)) {
|
||||
this.#creators.set(block_id, this.#user_id);
|
||||
if (!this._creators.get(block_id)) {
|
||||
this._creators.set(block_id, this._userId);
|
||||
}
|
||||
}
|
||||
|
||||
setCommon(block_id: string) {
|
||||
if (!this.#creators.get(block_id) && !this.#common.get(block_id)) {
|
||||
this.#common.set(block_id, this.#user_id);
|
||||
if (!this._creators.get(block_id) && !this._common.get(block_id)) {
|
||||
this._common.set(block_id, this._userId);
|
||||
}
|
||||
}
|
||||
|
||||
private check_delete(block_id: string): boolean {
|
||||
const creator = this.#creators.get(block_id);
|
||||
return creator === this.#user_id || !!this.#common.get(block_id);
|
||||
const creator = this._creators.get(block_id);
|
||||
return creator === this._userId || !!this._common.get(block_id);
|
||||
}
|
||||
|
||||
checkDeleteLists(block_ids: string[]) {
|
||||
@@ -47,7 +47,7 @@ export class GateKeeper {
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.#creators.clear();
|
||||
this.#common.clear();
|
||||
this._creators.clear();
|
||||
this._common.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,34 +5,34 @@ import { HistoryCallback, HistoryManager } from '../../adapter';
|
||||
type StackItem = UndoManager['undoStack'][0];
|
||||
|
||||
export class YjsHistoryManager implements HistoryManager {
|
||||
readonly #blocks: YMap<any>;
|
||||
readonly #history_manager: UndoManager;
|
||||
readonly #push_listeners: Map<string, HistoryCallback<any>>;
|
||||
readonly #pop_listeners: Map<string, HistoryCallback<any>>;
|
||||
readonly _blocks: YMap<any>;
|
||||
readonly _historyManager: UndoManager;
|
||||
readonly _pushListeners: Map<string, HistoryCallback<any>>;
|
||||
readonly _popListeners: Map<string, HistoryCallback<any>>;
|
||||
|
||||
constructor(scope: YMap<any>, tracker?: any[]) {
|
||||
this.#blocks = scope;
|
||||
this.#history_manager = new UndoManager(scope, {
|
||||
this._blocks = scope;
|
||||
this._historyManager = new UndoManager(scope, {
|
||||
trackedOrigins: tracker ? new Set(tracker) : undefined,
|
||||
});
|
||||
|
||||
this.#push_listeners = new Map();
|
||||
this.#history_manager.on(
|
||||
this._pushListeners = new Map();
|
||||
this._historyManager.on(
|
||||
'stack-item-added',
|
||||
(event: { stackItem: StackItem }) => {
|
||||
const meta = event.stackItem.meta;
|
||||
for (const listener of this.#push_listeners.values()) {
|
||||
for (const listener of this._pushListeners.values()) {
|
||||
listener(meta);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
this.#pop_listeners = new Map();
|
||||
this.#history_manager.on(
|
||||
this._popListeners = new Map();
|
||||
this._historyManager.on(
|
||||
'stack-item-popped',
|
||||
(event: { stackItem: StackItem }) => {
|
||||
const meta = event.stackItem.meta;
|
||||
for (const listener of this.#pop_listeners.values()) {
|
||||
for (const listener of this._popListeners.values()) {
|
||||
listener(new Map(meta));
|
||||
}
|
||||
}
|
||||
@@ -40,19 +40,19 @@ export class YjsHistoryManager implements HistoryManager {
|
||||
}
|
||||
|
||||
onPush<T = unknown>(name: string, callback: HistoryCallback<T>): void {
|
||||
this.#push_listeners.set(name, callback);
|
||||
this._pushListeners.set(name, callback);
|
||||
}
|
||||
|
||||
offPush(name: string): boolean {
|
||||
return this.#push_listeners.delete(name);
|
||||
return this._pushListeners.delete(name);
|
||||
}
|
||||
|
||||
onPop<T = unknown>(name: string, callback: HistoryCallback<T>): void {
|
||||
this.#pop_listeners.set(name, callback);
|
||||
this._popListeners.set(name, callback);
|
||||
}
|
||||
|
||||
offPop(name: string): boolean {
|
||||
return this.#pop_listeners.delete(name);
|
||||
return this._popListeners.delete(name);
|
||||
}
|
||||
|
||||
break(): void {
|
||||
@@ -60,14 +60,14 @@ export class YjsHistoryManager implements HistoryManager {
|
||||
}
|
||||
|
||||
undo<T = unknown>(): Map<string, T> | undefined {
|
||||
return this.#history_manager.undo()?.meta;
|
||||
return this._historyManager.undo()?.meta;
|
||||
}
|
||||
|
||||
redo<T = unknown>(): Map<string, T> | undefined {
|
||||
return this.#history_manager.redo()?.meta;
|
||||
return this._historyManager.redo()?.meta;
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
return this.#history_manager.clear();
|
||||
return this._historyManager.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,22 +178,22 @@ export type YjsInitOptions = {
|
||||
};
|
||||
|
||||
export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
readonly #provider: YjsProviders;
|
||||
readonly #doc: Doc; // doc instance
|
||||
readonly #awareness: Awareness; // lightweight state synchronization
|
||||
readonly #gatekeeper: GateKeeper; // Simple access control
|
||||
readonly #history: YjsHistoryManager;
|
||||
readonly _provider: YjsProviders;
|
||||
readonly _doc: Doc; // doc instance
|
||||
readonly _awareness: Awareness; // lightweight state synchronization
|
||||
readonly _gatekeeper: GateKeeper; // Simple access control
|
||||
readonly _history: YjsHistoryManager;
|
||||
|
||||
// Block Collection
|
||||
// key is a randomly generated global id
|
||||
readonly #blocks: YMap<YMap<unknown>>;
|
||||
readonly #block_updated: YMap<number>;
|
||||
readonly _blocks: YMap<YMap<unknown>>;
|
||||
readonly _blockUpdated: YMap<number>;
|
||||
// Maximum cache Block 1024, ttl 10 minutes
|
||||
readonly #block_caches: LRUCache<string, YjsBlockInstance>;
|
||||
readonly _blockCaches: LRUCache<string, YjsBlockInstance>;
|
||||
|
||||
readonly #binaries: YjsRemoteBinaries;
|
||||
readonly _binaries: YjsRemoteBinaries;
|
||||
|
||||
readonly #listener: Map<string, BlockListener<any>>;
|
||||
readonly _listener: Map<string, BlockListener<any>>;
|
||||
|
||||
static async init(
|
||||
workspace: string,
|
||||
@@ -209,30 +209,30 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
}
|
||||
|
||||
private constructor(providers: YjsProviders) {
|
||||
this.#provider = providers;
|
||||
this.#doc = providers.idb.doc;
|
||||
this.#awareness = providers.awareness;
|
||||
this.#gatekeeper = providers.gatekeeper;
|
||||
this._provider = providers;
|
||||
this._doc = providers.idb.doc;
|
||||
this._awareness = providers.awareness;
|
||||
this._gatekeeper = providers.gatekeeper;
|
||||
|
||||
const blocks = this.#doc.getMap<YMap<any>>('blocks');
|
||||
this.#blocks =
|
||||
const blocks = this._doc.getMap<YMap<any>>('blocks');
|
||||
this._blocks =
|
||||
blocks.get('content') || blocks.set('content', new YMap());
|
||||
this.#block_updated =
|
||||
this._blockUpdated =
|
||||
blocks.get('updated') || blocks.set('updated', new YMap());
|
||||
this.#block_caches = new LRUCache({ max: 1024, ttl: 1000 * 60 * 10 });
|
||||
this.#binaries = new YjsRemoteBinaries(
|
||||
this._blockCaches = new LRUCache({ max: 1024, ttl: 1000 * 60 * 10 });
|
||||
this._binaries = new YjsRemoteBinaries(
|
||||
providers.binariesIdb.doc.getMap(),
|
||||
providers.remoteToken
|
||||
);
|
||||
this.#history = new YjsHistoryManager(this.#blocks);
|
||||
this._history = new YjsHistoryManager(this._blocks);
|
||||
|
||||
this.#listener = new Map();
|
||||
this._listener = new Map();
|
||||
|
||||
const ws = providers.ws as any;
|
||||
if (ws) {
|
||||
const workspace = providers.idb.name;
|
||||
const emitState = (connectivity: Connectivity) => {
|
||||
this.#listener.get('connectivity')?.(
|
||||
this._listener.get('connectivity')?.(
|
||||
new Map([[workspace, connectivity]])
|
||||
);
|
||||
};
|
||||
@@ -244,9 +244,9 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
const debounced_editing_notifier = debounce(
|
||||
() => {
|
||||
const listener: BlockListener<Set<string>> | undefined =
|
||||
this.#listener.get('editing');
|
||||
this._listener.get('editing');
|
||||
if (listener) {
|
||||
const mapping = this.#awareness.getStates();
|
||||
const mapping = this._awareness.getStates();
|
||||
const editing_mapping: Record<string, string[]> = {};
|
||||
for (const {
|
||||
userId,
|
||||
@@ -280,11 +280,11 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
{ maxWait: 1000 }
|
||||
);
|
||||
|
||||
this.#awareness.setLocalStateField('userId', providers.userId);
|
||||
this._awareness.setLocalStateField('userId', providers.userId);
|
||||
|
||||
this.#awareness.on('update', debounced_editing_notifier);
|
||||
this._awareness.on('update', debounced_editing_notifier);
|
||||
|
||||
this.#blocks.observeDeep(events => {
|
||||
this._blocks.observeDeep(events => {
|
||||
const now = Date.now();
|
||||
|
||||
const keys = events.flatMap(e => {
|
||||
@@ -300,14 +300,14 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
}
|
||||
});
|
||||
|
||||
EmitEvents(keys, this.#listener.get('updated'));
|
||||
EmitEvents(keys, this._listener.get('updated'));
|
||||
|
||||
transact(this.#doc, () => {
|
||||
transact(this._doc, () => {
|
||||
for (const [key, action] of keys) {
|
||||
if (action === 'delete') {
|
||||
this.#block_updated.delete(key);
|
||||
this._blockUpdated.delete(key);
|
||||
} else {
|
||||
this.#block_updated.set(key, now);
|
||||
this._blockUpdated.set(key, now);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -315,7 +315,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
}
|
||||
|
||||
getUserId(): string {
|
||||
return this.#provider.userId;
|
||||
return this._provider.userId;
|
||||
}
|
||||
|
||||
inspector() {
|
||||
@@ -333,7 +333,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
|
||||
return {
|
||||
save: () => {
|
||||
const binary = encodeStateAsUpdate(this.#doc);
|
||||
const binary = encodeStateAsUpdate(this._doc);
|
||||
saveAs(
|
||||
new Blob([binary]),
|
||||
`affine_workspace_${new Date().toDateString()}.apk`
|
||||
@@ -353,7 +353,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
});
|
||||
const [file] = (await fromEvent(handles)) as File[];
|
||||
const binary = await file.arrayBuffer();
|
||||
await this.#provider.idb.clearData();
|
||||
await this._provider.idb.clearData();
|
||||
const doc = new Doc({ autoLoad: true, shouldLoad: true });
|
||||
let updated = 0;
|
||||
let isUpdated = false;
|
||||
@@ -374,21 +374,21 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
};
|
||||
check();
|
||||
});
|
||||
await new IndexeddbPersistence(this.#provider.idb.name, doc)
|
||||
await new IndexeddbPersistence(this._provider.idb.name, doc)
|
||||
.whenSynced;
|
||||
applyUpdate(doc, new Uint8Array(binary));
|
||||
await update_check;
|
||||
console.log('load success');
|
||||
},
|
||||
parse: () => this.#doc.toJSON(),
|
||||
parse: () => this._doc.toJSON(),
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
parse_page: (page_id: string) => {
|
||||
const blocks = this.#blocks.toJSON();
|
||||
const blocks = this._blocks.toJSON();
|
||||
return resolve_block(blocks, page_id);
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
parse_pages: (resolve = false) => {
|
||||
const blocks = this.#blocks.toJSON();
|
||||
const blocks = this._blocks.toJSON();
|
||||
return Object.fromEntries(
|
||||
Object.entries(blocks)
|
||||
.filter(([, block]) => block.flavor === 'page')
|
||||
@@ -402,21 +402,21 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
);
|
||||
},
|
||||
clear: () => {
|
||||
this.#blocks.clear();
|
||||
this.#block_updated.clear();
|
||||
this.#gatekeeper.clear();
|
||||
this.#doc.getMap('blocks').clear();
|
||||
this.#doc.getMap('gatekeeper').clear();
|
||||
this._blocks.clear();
|
||||
this._blockUpdated.clear();
|
||||
this._gatekeeper.clear();
|
||||
this._doc.getMap('blocks').clear();
|
||||
this._doc.getMap('gatekeeper').clear();
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
clear_old: () => {
|
||||
this.#doc.getMap('block_updated').clear();
|
||||
this.#doc.getMap('blocks').clear();
|
||||
this.#doc.getMap('common').clear();
|
||||
this.#doc.getMap('creators').clear();
|
||||
this._doc.getMap('block_updated').clear();
|
||||
this._doc.getMap('blocks').clear();
|
||||
this._doc.getMap('common').clear();
|
||||
this._doc.getMap('creators').clear();
|
||||
},
|
||||
snapshot: () => {
|
||||
return snapshot(this.#doc);
|
||||
return snapshot(this._doc);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -459,15 +459,15 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
}
|
||||
|
||||
private get_updated(id: string) {
|
||||
return this.#block_updated.get(id);
|
||||
return this._blockUpdated.get(id);
|
||||
}
|
||||
|
||||
private get_creator(id: string) {
|
||||
return this.#gatekeeper.getCreator(id);
|
||||
return this._gatekeeper.getCreator(id);
|
||||
}
|
||||
|
||||
private get_block_sync(id: string): YjsBlockInstance | undefined {
|
||||
const cached = this.#block_caches.get(id);
|
||||
const cached = this._blockCaches.get(id);
|
||||
if (cached) {
|
||||
// Synchronous read cannot read binary
|
||||
if (cached.type === BlockTypes.block) {
|
||||
@@ -476,7 +476,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const block = this.#blocks.get(id);
|
||||
const block = this._blocks.get(id);
|
||||
|
||||
// Synchronous read cannot read binary
|
||||
if (block && block.get('type') === BlockTypes.block) {
|
||||
@@ -496,9 +496,9 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
async getBlock(id: string): Promise<YjsBlockInstance | undefined> {
|
||||
const block_instance = this.get_block_sync(id);
|
||||
if (block_instance) return block_instance;
|
||||
const block = this.#blocks.get(id);
|
||||
const block = this._blocks.get(id);
|
||||
if (block && block.get('type') === BlockTypes.binary) {
|
||||
const binary = await this.#binaries.get(
|
||||
const binary = await this._binaries.get(
|
||||
block.get('hash') as string
|
||||
);
|
||||
if (binary) {
|
||||
@@ -520,7 +520,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
flavor: BlockItem<YjsContentOperation>['flavor']
|
||||
): Promise<string[]> {
|
||||
const keys: string[] = [];
|
||||
this.#blocks.forEach((doc, key) => {
|
||||
this._blocks.forEach((doc, key) => {
|
||||
if (doc.get('flavor') === flavor) {
|
||||
keys.push(key);
|
||||
}
|
||||
@@ -533,7 +533,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
type: BlockItem<YjsContentOperation>['type']
|
||||
): Promise<string[]> {
|
||||
const keys: string[] = [];
|
||||
this.#blocks.forEach((doc, key) => {
|
||||
this._blocks.forEach((doc, key) => {
|
||||
if (doc.get('type') === type) {
|
||||
keys.push(key);
|
||||
}
|
||||
@@ -547,8 +547,8 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
item: BlockItem<YjsContentOperation> & { hash?: string }
|
||||
): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const block = this.#blocks.get(key) || new YMap();
|
||||
transact(this.#doc, () => {
|
||||
const block = this._blocks.get(key) || new YMap();
|
||||
transact(this._doc, () => {
|
||||
// Insert only if the block doesn't exist yet
|
||||
// Other modification operations are done in the block instance
|
||||
let uploaded: Promise<void> | undefined;
|
||||
@@ -568,8 +568,8 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
} else if (item.type === BlockTypes.binary && item.hash) {
|
||||
if (content instanceof YArray) {
|
||||
block.set('hash', item.hash);
|
||||
if (!this.#binaries.has(item.hash)) {
|
||||
uploaded = this.#binaries.set(
|
||||
if (!this._binaries.has(item.hash)) {
|
||||
uploaded = this._binaries.set(
|
||||
item.hash,
|
||||
content
|
||||
);
|
||||
@@ -583,18 +583,18 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
throw new Error('invalid block type: ' + item.type);
|
||||
}
|
||||
|
||||
this.#blocks.set(key, block);
|
||||
this._blocks.set(key, block);
|
||||
}
|
||||
|
||||
if (item.flavor === 'page') {
|
||||
this.#awareness.setLocalStateField('editing', key);
|
||||
this.#awareness.setLocalStateField('updated', Date.now());
|
||||
this._awareness.setLocalStateField('editing', key);
|
||||
this._awareness.setLocalStateField('updated', Date.now());
|
||||
}
|
||||
// References do not add delete restrictions
|
||||
if (item.flavor === 'reference') {
|
||||
this.#gatekeeper.setCommon(key);
|
||||
this._gatekeeper.setCommon(key);
|
||||
} else {
|
||||
this.#gatekeeper.setCreator(key);
|
||||
this._gatekeeper.setCreator(key);
|
||||
}
|
||||
|
||||
if (uploaded) {
|
||||
@@ -613,15 +613,15 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
|
||||
async checkBlocks(keys: string[]): Promise<boolean> {
|
||||
return (
|
||||
keys.filter(key => !!this.#blocks.get(key)).length === keys.length
|
||||
keys.filter(key => !!this._blocks.get(key)).length === keys.length
|
||||
);
|
||||
}
|
||||
|
||||
async deleteBlocks(keys: string[]): Promise<string[]> {
|
||||
const [success, fail] = this.#gatekeeper.checkDeleteLists(keys);
|
||||
transact(this.#doc, () => {
|
||||
const [success, fail] = this._gatekeeper.checkDeleteLists(keys);
|
||||
transact(this._doc, () => {
|
||||
for (const key of success) {
|
||||
this.#blocks.delete(key);
|
||||
this._blocks.delete(key);
|
||||
}
|
||||
});
|
||||
return fail;
|
||||
@@ -631,7 +631,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
key: 'editing' | 'updated' | 'connectivity',
|
||||
listener: BlockListener<S, R>
|
||||
): void {
|
||||
this.#listener.set(key, listener);
|
||||
this._listener.set(key, listener);
|
||||
}
|
||||
|
||||
suspend(suspend: boolean) {
|
||||
@@ -639,6 +639,6 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
|
||||
}
|
||||
|
||||
public history(): HistoryManager {
|
||||
return this.#history;
|
||||
return this._history;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,18 +52,18 @@ function auto_set(root: ContentOperation, key: string, data: BaseTypes): void {
|
||||
}
|
||||
|
||||
export class YjsContentOperation implements ContentOperation {
|
||||
readonly #content: YAbstractType<unknown>;
|
||||
readonly _content: YAbstractType<unknown>;
|
||||
|
||||
constructor(content: YAbstractType<any>) {
|
||||
this.#content = content;
|
||||
this._content = content;
|
||||
}
|
||||
|
||||
get length(): number {
|
||||
if (this.#content instanceof YMap) {
|
||||
return this.#content.size;
|
||||
if (this._content instanceof YMap) {
|
||||
return this._content.size;
|
||||
}
|
||||
if (this.#content instanceof YArray || this.#content instanceof YText) {
|
||||
return this.#content.length;
|
||||
if (this._content instanceof YArray || this._content instanceof YText) {
|
||||
return this._content.length;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -83,8 +83,8 @@ export class YjsContentOperation implements ContentOperation {
|
||||
}
|
||||
|
||||
asText(): YjsTextOperation | undefined {
|
||||
if (this.#content instanceof YText) {
|
||||
return new YjsTextOperation(this.#content);
|
||||
if (this._content instanceof YText) {
|
||||
return new YjsTextOperation(this._content);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -92,8 +92,8 @@ export class YjsContentOperation implements ContentOperation {
|
||||
asArray<T extends ContentTypes = ContentOperation>():
|
||||
| YjsArrayOperation<T>
|
||||
| undefined {
|
||||
if (this.#content instanceof YArray) {
|
||||
return new YjsArrayOperation(this.#content);
|
||||
if (this._content instanceof YArray) {
|
||||
return new YjsArrayOperation(this._content);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -101,8 +101,8 @@ export class YjsContentOperation implements ContentOperation {
|
||||
asMap<T extends ContentTypes = ContentOperation>():
|
||||
| YjsMapOperation<T>
|
||||
| undefined {
|
||||
if (this.#content instanceof YMap) {
|
||||
return new YjsMapOperation(this.#content);
|
||||
if (this._content instanceof YMap) {
|
||||
return new YjsMapOperation(this._content);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -184,24 +184,24 @@ export class YjsContentOperation implements ContentOperation {
|
||||
}
|
||||
|
||||
[INTO_INNER](): YAbstractType<unknown> | undefined {
|
||||
if (this.#content instanceof YAbstractType) {
|
||||
return this.#content;
|
||||
if (this._content instanceof YAbstractType) {
|
||||
return this._content;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
private toJSON() {
|
||||
return this.#content.toJSON();
|
||||
return this._content.toJSON();
|
||||
}
|
||||
}
|
||||
|
||||
class YjsTextOperation extends YjsContentOperation implements TextOperation {
|
||||
readonly #content: YText;
|
||||
readonly _textContent: YText;
|
||||
|
||||
constructor(content: YText) {
|
||||
super(content);
|
||||
this.#content = content;
|
||||
this._textContent = content;
|
||||
}
|
||||
|
||||
insert(
|
||||
@@ -209,7 +209,7 @@ class YjsTextOperation extends YjsContentOperation implements TextOperation {
|
||||
content: string,
|
||||
format?: Record<string, string>
|
||||
): void {
|
||||
this.#content.insert(index, content, format);
|
||||
this._textContent.insert(index, content, format);
|
||||
}
|
||||
|
||||
format(
|
||||
@@ -217,23 +217,23 @@ class YjsTextOperation extends YjsContentOperation implements TextOperation {
|
||||
length: number,
|
||||
format: Record<string, string>
|
||||
): void {
|
||||
this.#content.format(index, length, format);
|
||||
this._textContent.format(index, length, format);
|
||||
}
|
||||
|
||||
delete(index: number, length: number): void {
|
||||
this.#content.delete(index, length);
|
||||
this._textContent.delete(index, length);
|
||||
}
|
||||
|
||||
setAttribute(name: string, value: BaseTypes) {
|
||||
this.#content.setAttribute(name, value);
|
||||
this._textContent.setAttribute(name, value);
|
||||
}
|
||||
|
||||
getAttribute<T extends BaseTypes = string>(name: string): T | undefined {
|
||||
return this.#content.getAttribute(name);
|
||||
return this._textContent.getAttribute(name);
|
||||
}
|
||||
|
||||
override toString(): TextToken[] {
|
||||
return this.#content.toDelta();
|
||||
return this._textContent.toDelta();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,67 +241,69 @@ class YjsArrayOperation<T extends ContentTypes>
|
||||
extends YjsContentOperation
|
||||
implements ArrayOperation<T>
|
||||
{
|
||||
readonly #content: YArray<T>;
|
||||
readonly #listeners: Map<string, BlockListener>;
|
||||
readonly _arrayContent: YArray<T>;
|
||||
readonly _listeners: Map<string, BlockListener>;
|
||||
|
||||
constructor(content: YArray<T>) {
|
||||
super(content);
|
||||
this.#content = content;
|
||||
this.#listeners = new Map();
|
||||
this._arrayContent = content;
|
||||
this._listeners = new Map();
|
||||
|
||||
this.#content.observe(event =>
|
||||
ChildrenListenerHandler(this.#listeners, event)
|
||||
this._arrayContent.observe(event =>
|
||||
ChildrenListenerHandler(this._listeners, event)
|
||||
);
|
||||
}
|
||||
|
||||
on(name: string, listener: BlockListener) {
|
||||
this.#listeners.set(name, listener);
|
||||
this._listeners.set(name, listener);
|
||||
}
|
||||
|
||||
off(name: string) {
|
||||
this.#listeners.delete(name);
|
||||
this._listeners.delete(name);
|
||||
}
|
||||
|
||||
insert(index: number, content: Array<Operable<T>>): void {
|
||||
this.#content.insert(
|
||||
this._arrayContent.insert(
|
||||
index,
|
||||
content.map(v => this.into_inner(v))
|
||||
);
|
||||
}
|
||||
|
||||
delete(index: number, length: number): void {
|
||||
this.#content.delete(index, length);
|
||||
this._arrayContent.delete(index, length);
|
||||
}
|
||||
|
||||
push(content: Array<Operable<T>>): void {
|
||||
this.#content.push(content.map(v => this.into_inner(v)));
|
||||
this._arrayContent.push(content.map(v => this.into_inner(v)));
|
||||
}
|
||||
|
||||
unshift(content: Array<Operable<T>>): void {
|
||||
this.#content.unshift(content.map(v => this.into_inner(v)));
|
||||
this._arrayContent.unshift(content.map(v => this.into_inner(v)));
|
||||
}
|
||||
|
||||
get(index: number): Operable<T> | undefined {
|
||||
const content = this.#content.get(index);
|
||||
const content = this._arrayContent.get(index);
|
||||
if (content) return this.to_operable(content);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private get_internal(index: number): T {
|
||||
return this.#content.get(index);
|
||||
return this._arrayContent.get(index);
|
||||
}
|
||||
|
||||
slice(start?: number, end?: number): Operable<T>[] {
|
||||
return this.#content.slice(start, end).map(v => this.to_operable(v));
|
||||
return this._arrayContent
|
||||
.slice(start, end)
|
||||
.map(v => this.to_operable(v));
|
||||
}
|
||||
|
||||
map<R = unknown>(callback: (value: T, index: number) => R): R[] {
|
||||
return this.#content.map((value, index) => callback(value, index));
|
||||
return this._arrayContent.map((value, index) => callback(value, index));
|
||||
}
|
||||
|
||||
// Traverse, if callback returns false, stop traversing
|
||||
forEach(callback: (value: T, index: number) => boolean) {
|
||||
for (let i = 0; i < this.#content.length; i++) {
|
||||
for (let i = 0; i < this._arrayContent.length; i++) {
|
||||
const ret = callback(this.get_internal(i), i);
|
||||
if (ret === false) {
|
||||
break;
|
||||
@@ -342,47 +344,47 @@ class YjsMapOperation<T extends ContentTypes>
|
||||
extends YjsContentOperation
|
||||
implements MapOperation<T>
|
||||
{
|
||||
readonly #content: YMap<T>;
|
||||
readonly #listeners: Map<string, BlockListener>;
|
||||
readonly _mapContent: YMap<T>;
|
||||
readonly _listeners: Map<string, BlockListener>;
|
||||
|
||||
constructor(content: YMap<T>) {
|
||||
super(content);
|
||||
this.#content = content;
|
||||
this.#listeners = new Map();
|
||||
this._mapContent = content;
|
||||
this._listeners = new Map();
|
||||
|
||||
content?.observeDeep(events =>
|
||||
ContentListenerHandler(this.#listeners, events)
|
||||
ContentListenerHandler(this._listeners, events)
|
||||
);
|
||||
}
|
||||
|
||||
on(name: string, listener: BlockListener) {
|
||||
this.#listeners.set(name, listener);
|
||||
this._listeners.set(name, listener);
|
||||
}
|
||||
|
||||
off(name: string) {
|
||||
this.#listeners.delete(name);
|
||||
this._listeners.delete(name);
|
||||
}
|
||||
|
||||
set(key: string, value: Operable<T>): void {
|
||||
if (value instanceof YjsContentOperation) {
|
||||
const content = value[INTO_INNER]();
|
||||
if (content) this.#content.set(key, content as unknown as T);
|
||||
if (content) this._mapContent.set(key, content as unknown as T);
|
||||
} else {
|
||||
this.#content.set(key, value as T);
|
||||
this._mapContent.set(key, value as T);
|
||||
}
|
||||
}
|
||||
|
||||
get(key: string): Operable<T> | undefined {
|
||||
const content = this.#content.get(key);
|
||||
const content = this._mapContent.get(key);
|
||||
if (content) return this.to_operable(content);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
delete(key: string): void {
|
||||
this.#content.delete(key);
|
||||
this._mapContent.delete(key);
|
||||
}
|
||||
|
||||
has(key: string): boolean {
|
||||
return this.#content.has(key);
|
||||
return this._mapContent.has(key);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user