init: the first public commit for AFFiNE

This commit is contained in:
DarkSky
2022-07-22 15:49:21 +08:00
commit e3e3741393
1451 changed files with 108124 additions and 0 deletions
+161
View File
@@ -0,0 +1,161 @@
import { AbstractType as YAbstractType } from 'yjs';
import { BlockItem } from '../types';
export type ChangedStateKeys = 'add' | 'update' | 'delete';
export type ChangedStates<S = ChangedStateKeys> = Map<string, S>;
export type BlockListener<S = ChangedStateKeys, R = unknown> = (
states: ChangedStates<S>
) => Promise<R> | R;
export type Operable<T, Base = YAbstractType<any>> = T extends Base
? ContentOperation
: T;
export interface InternalPlainObject {}
export type BaseTypes = string | number | boolean | InternalPlainObject;
export type ContentTypes = BaseTypes | ContentOperation;
interface ContentOperation {
get length(): number;
createText(): TextOperation;
createArray<T extends ContentTypes = ContentOperation>(): ArrayOperation<T>;
createMap<T extends ContentTypes = ContentOperation>(): MapOperation<T>;
asText(): TextOperation | undefined;
asArray<T extends ContentTypes = ContentOperation>():
| ArrayOperation<T>
| undefined;
asMap<T extends ContentTypes = ContentOperation>():
| MapOperation<T>
| undefined;
autoGet(
root: ThisType<ContentOperation> | Record<string, unknown>,
path: string[]
): unknown | undefined;
autoSet(
root: ThisType<ContentOperation>,
path: string[],
data: unknown,
partial?: boolean
): void;
}
type TextAttributes = Record<string, string>;
export type TextToken = {
insert: string;
attributes?: TextAttributes;
};
export interface TextOperation extends ContentOperation {
insert(
index: number,
content: string,
format?: Record<string, string>
): void;
format(index: number, length: number, format: Record<string, string>): void;
delete(index: number, length: number): void;
setAttribute(name: string, value: BaseTypes): void;
getAttribute<T extends BaseTypes = string>(name: string): T | undefined;
toString(): TextToken[];
}
export interface ArrayOperation<T extends ContentTypes = ContentOperation>
extends ContentOperation {
insert(index: number, content: Array<Operable<T>>): void;
delete(index: number, length: number): void;
push(content: Array<Operable<T>>): void;
unshift(content: Array<Operable<T>>): void;
get(index: number): Operable<T> | undefined;
slice(start?: number, end?: number): Array<Operable<T>>;
map<R = unknown>(callback: (value: T, index: number) => R): Array<R>;
forEach(callback: (value: T, index: number) => boolean | void): void;
find<R = unknown>(
callback: (value: T, index: number) => boolean
): R | undefined;
findIndex(callback: (value: T, index: number) => boolean): number;
}
export interface MapOperation<T extends ContentTypes = ContentOperation>
extends ContentOperation {
set(key: string, value: Operable<T>): void;
get(key: string): Operable<T> | undefined;
delete(key: string): void;
has(key: string): boolean;
}
export type HistoryCallback<T = unknown> = (map: Map<string, T>) => void;
export interface HistoryManager {
onPush<T = unknown>(name: string, callback: HistoryCallback<T>): void;
offPush(name: string): boolean;
onPop<T = unknown>(name: string, callback: HistoryCallback<T>): void;
offPop(name: string): boolean;
undo<T = unknown>(): Map<string, T> | undefined;
redo<T = unknown>(): Map<string, T> | undefined;
clear(): void;
}
type BlockPosition = { pos?: number; before?: string; after?: string };
interface BlockInstance<C extends ContentOperation> {
get id(): string;
get type(): BlockItem<C>['type'];
get flavor(): BlockItem<C>['flavor'];
// TODO: flavor needs optimization
setFlavor(flavor: BlockItem<C>['flavor']): void;
get created(): BlockItem<C>['created'];
get updated(): number; // update time, UTC timestamp, read only
get creator(): string | undefined; // creator id
get children(): string[];
getChildren(block_ids: (string | undefined)[]): BlockInstance<C>[];
hasChildren(block_id: string): boolean;
insertChildren(
block: ThisType<BlockInstance<C>>,
pos?: BlockPosition
): void;
removeChildren(block_ids: (string | undefined)[]): void;
get content(): BlockItem<C>['content'];
on(
key: 'children' | 'content',
name: string,
listener: BlockListener
): void;
off(key: 'children' | 'content', name: string): void;
addChildrenListener(name: string, listener: BlockListener): void;
removeChildrenListener(name: string): void;
addContentListener(name: string, listener: BlockListener): void;
removeContentListener(name: string): void;
scopedHistory(scope: any[]): HistoryManager;
}
interface AsyncDatabaseAdapter<C extends ContentOperation> {
inspector(): Record<string, any>;
createBlock(
options: Pick<BlockItem<C>, 'type' | 'flavor'> & {
binary?: ArrayBuffer;
uuid?: string;
}
): Promise<BlockInstance<C>>;
getBlock(id: string): Promise<BlockInstance<C> | undefined>;
getBlockByFlavor(flavor: BlockItem<C>['flavor']): Promise<string[]>;
getBlockByType(type: BlockItem<C>['type']): Promise<string[]>;
checkBlocks(keys: string[]): Promise<boolean>;
deleteBlocks(keys: string[]): Promise<string[]>;
on<S, R>(key: 'editing' | 'updated', listener: BlockListener<S, R>): void;
suspend(suspend: boolean): void;
history(): HistoryManager;
getUserId(): string;
}
export type {
AsyncDatabaseAdapter,
BlockPosition,
BlockInstance,
ContentOperation,
};
export type { YjsInitOptions, YjsContentOperation } from './yjs';
export { YjsAdapter } from './yjs';
@@ -0,0 +1,66 @@
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;
constructor(binaries: YMap<YArray<ArrayBuffer>>, remote_token?: string) {
this.#binaries = binaries;
if (remote_token) {
this.#remote_storage = new RemoteKvService(remote_token);
} else {
console.warn(`Remote storage is not ready`);
}
}
has(name: string): boolean {
return this.#binaries.has(name);
}
async get(name: string): Promise<YArray<ArrayBuffer> | undefined> {
if (this.#binaries.has(name)) {
return this.#binaries.get(name);
} else {
// TODO: Remote Load
try {
const file = await this.#remote_storage?.instance.getBuffData(
name
);
console.log(file);
// return file;
} catch (e) {
throw new Error(`Binary ${name} not found`);
}
return undefined;
}
}
async set(name: string, binary: YArray<ArrayBuffer>) {
if (!this.#binaries.has(name)) {
console.log(name, 'name');
if (binary.length === 1) {
this.#binaries.set(name, binary);
if (this.#remote_storage) {
// 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);
if (!has_file) {
const upload_file = new File(binary.toArray(), name);
await this.#remote_storage.instance
.upload(upload_file)
.catch(err => {
throw new Error(`${err} upload error`);
});
}
} else {
console.warn(`Remote storage is not ready`);
}
return;
} else {
console.log('err');
}
throw new Error(`Binary ${name} is invalid`);
}
}
}
@@ -0,0 +1,293 @@
import {
AbstractType as YAbstractType,
Array as YArray,
Map as YMap,
transact,
} from 'yjs';
import { BlockInstance, BlockListener, HistoryManager } from '../index';
import { BlockItem, BlockTypes } from '../../types';
import { YjsContentOperation } from './operation';
import { ChildrenListenerHandler, ContentListenerHandler } from './listener';
import { YjsHistoryManager } from './history';
const GET_BLOCK_ITEM = Symbol('GET_BLOCK_ITEM');
// eslint-disable-next-line @typescript-eslint/naming-convention
const getMapFromYArray = (array: YArray<string>) =>
new Map(array.map((child, index) => [child, index]));
type YjsBlockInstanceProps = {
id: string;
block: YMap<unknown>;
binary?: YArray<ArrayBuffer>;
setBlock: (
id: string,
block: BlockItem<YjsContentOperation>
) => Promise<void>;
getUpdated: (id: string) => number | undefined;
getCreator: (id: string) => string | undefined;
getBlockInstance: (id: string) => YjsBlockInstance | undefined;
};
export class YjsBlockInstance implements BlockInstance<YjsContentOperation> {
readonly #id: string;
readonly #block: YMap<unknown>;
readonly #binary?: YArray<ArrayBuffer>;
readonly #children: YArray<string>;
readonly #set_block: (
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>;
// eslint-disable-next-line @typescript-eslint/naming-convention
#children_map: Map<string, number>;
constructor(props: YjsBlockInstanceProps) {
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_listeners = new Map();
this.#content_listeners = new Map();
const content = this.#block.get('content') as YMap<unknown>;
this.#children.observe(event =>
ChildrenListenerHandler(this.#children_listeners, event)
);
content?.observeDeep(events =>
ContentListenerHandler(this.#content_listeners, events)
);
// TODO: flavor needs optimization
this.#block.observeDeep(events =>
ContentListenerHandler(this.#content_listeners, events)
);
}
on(
key: 'children' | 'content',
name: string,
listener: BlockListener
): void {
if (key === 'children') {
this.addChildrenListener(name, listener);
} else if (key === 'content') {
this.addContentListener(name, listener);
}
}
off(key: 'children' | 'content', name: string): void {
if (key === 'children') {
this.removeChildrenListener(name);
} else if (key === 'content') {
this.removeContentListener(name);
}
}
addChildrenListener(name: string, listener: BlockListener): void {
this.#children_listeners.set(name, listener);
}
removeChildrenListener(name: string): void {
this.#children_listeners.delete(name);
}
addContentListener(name: string, listener: BlockListener): void {
this.#content_listeners.set(name, listener);
}
removeContentListener(name: string): void {
this.#content_listeners.delete(name);
}
get id() {
return this.#id;
}
get content(): YjsContentOperation {
if (this.type === BlockTypes.block) {
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);
}
throw new Error(
`Invalid content type: ${this.type}, ${this.#block.get(
'content'
)}, ${this.#binary}`
);
}
get type(): BlockItem<YjsContentOperation>['type'] {
return this.#block.get(
'type'
) as BlockItem<YjsContentOperation>['type'];
}
get flavor(): BlockItem<YjsContentOperation>['flavor'] {
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);
}
get created(): BlockItem<YjsContentOperation>['created'] {
return this.#block.get(
'created'
) as BlockItem<YjsContentOperation>['created'];
}
get updated(): number {
return this.#get_updated(this.#id) || this.created;
}
get creator(): string | undefined {
return this.#get_creator(this.#id);
}
get children(): string[] {
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 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))
.filter((v): v is YjsBlockInstance => !!v);
}
hasChildren(id: string): boolean {
if (this.children.includes(id)) return true;
return this.getChildren().some(block => block.hasChildren(id));
}
private position_calculator(
max_pos: number,
position?: { pos?: number; before?: string; after?: string }
) {
const { pos, before, after } = position || {};
if (typeof pos === 'number' && Number.isInteger(pos)) {
if (pos >= 0 && pos < max_pos) {
return pos;
}
} else if (before) {
const current_pos = this.#children_map.get(before || '');
if (
typeof current_pos === 'number' &&
Number.isInteger(current_pos)
) {
const prev_pos = current_pos;
if (prev_pos >= 0 && prev_pos < max_pos) {
return prev_pos;
}
}
} else if (after) {
const current_pos = this.#children_map.get(after || '');
if (
typeof current_pos === 'number' &&
Number.isInteger(current_pos)
) {
const next_pos = current_pos + 1;
if (next_pos >= 0 && next_pos < max_pos) {
return next_pos;
}
}
}
return undefined;
}
async insertChildren(
block: YjsBlockInstance,
pos?: { pos?: number; before?: string; after?: string }
): Promise<void> {
const content = block[GET_BLOCK_ITEM]();
if (content) {
const lastIndex = this.#children_map.get(block.id);
if (typeof lastIndex === 'number') {
this.#children.delete(lastIndex);
this.#children_map = getMapFromYArray(this.#children);
}
const position = this.position_calculator(
this.#children_map.size,
pos
);
if (typeof position === 'number') {
this.#children.insert(position, [block.id]);
} else {
this.#children.push([block.id]);
}
await this.#set_block(block.id, content);
this.#children_map = getMapFromYArray(this.#children);
}
}
removeChildren(ids: (string | undefined)[]): Promise<string[]> {
return new Promise(resolve => {
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) {
idx += 1;
if (block_id === id) {
this.#children.delete(idx);
break;
}
}
if (id) failed.push(id);
}
this.#children_map = getMapFromYArray(this.#children);
resolve(failed);
});
} else {
resolve(ids.filter((id): id is string => !!id));
}
});
}
public scopedHistory(scope: any[]): HistoryManager {
return new YjsHistoryManager(this.#block, scope);
}
[GET_BLOCK_ITEM]() {
// check null & undefined
if (this.content != null) {
return {
type: this.type,
flavor: this.flavor,
children: this.#children.slice(),
created: this.created,
content: this.content,
};
}
return undefined;
}
}
@@ -0,0 +1,53 @@
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>;
constructor(userId: string, creators: YMap<string>, common: YMap<string>) {
this.#user_id = userId;
this.#creators = creators;
this.#common = common;
}
getCreator(block_id: string): string | undefined {
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);
}
}
setCommon(block_id: string) {
if (!this.#creators.get(block_id) && !this.#common.get(block_id)) {
this.#common.set(block_id, this.#user_id);
}
}
private check_delete(block_id: string): boolean {
const creator = this.#creators.get(block_id);
return creator === this.#user_id || !!this.#common.get(block_id);
}
checkDeleteLists(block_ids: string[]) {
const success = [];
const fail = [];
for (const block_id of block_ids) {
if (this.check_delete(block_id)) {
success.push(block_id);
} else {
fail.push(block_id);
}
}
return [success, fail];
}
clear() {
this.#creators.clear();
this.#common.clear();
}
}
@@ -0,0 +1,73 @@
import { Map as YMap, UndoManager } from 'yjs';
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>>;
constructor(scope: YMap<any>, tracker?: any[]) {
this.#blocks = scope;
this.#history_manager = new UndoManager(scope, {
trackedOrigins: tracker ? new Set(tracker) : undefined,
});
this.#push_listeners = new Map();
this.#history_manager.on(
'stack-item-added',
(event: { stackItem: StackItem }) => {
const meta = event.stackItem.meta;
for (const listener of this.#push_listeners.values()) {
listener(meta);
}
}
);
this.#pop_listeners = new Map();
this.#history_manager.on(
'stack-item-popped',
(event: { stackItem: StackItem }) => {
const meta = event.stackItem.meta;
for (const listener of this.#pop_listeners.values()) {
listener(new Map(meta));
}
}
);
}
onPush<T = unknown>(name: string, callback: HistoryCallback<T>): void {
this.#push_listeners.set(name, callback);
}
offPush(name: string): boolean {
return this.#push_listeners.delete(name);
}
onPop<T = unknown>(name: string, callback: HistoryCallback<T>): void {
this.#pop_listeners.set(name, callback);
}
offPop(name: string): boolean {
return this.#pop_listeners.delete(name);
}
break(): void {
// this.#history_manager.
}
undo<T = unknown>(): Map<string, T> | undefined {
return this.#history_manager.undo()?.meta;
}
redo<T = unknown>(): Map<string, T> | undefined {
return this.#history_manager.redo()?.meta;
}
clear(): void {
return this.#history_manager.clear();
}
}
@@ -0,0 +1,605 @@
/* eslint-disable max-lines */
/* eslint-disable @typescript-eslint/no-unused-vars */
/// <reference types="wicg-file-system-access" />
import { Buffer } from 'buffer';
import { saveAs } from 'file-saver';
import { fromEvent } from 'file-selector';
import LRUCache from 'lru-cache';
import { debounce } from 'ts-debounce';
import { nanoid } from 'nanoid';
import { IndexeddbPersistence } from 'y-indexeddb';
import { Awareness } from 'y-protocols/awareness.js';
import {
Doc,
Array as YArray,
Map as YMap,
transact,
encodeStateAsUpdate,
applyUpdate,
} from 'yjs';
import { WebsocketProvider } from '@toeverything/datasource/jwt-rpc';
import {
AsyncDatabaseAdapter,
BlockListener,
ChangedStateKeys,
HistoryManager,
} from '../../adapter';
import { BucketBackend, BlockItem, BlockTypes } from '../../types';
import { getLogger, sha3, sleep } from '../../utils';
import { YjsRemoteBinaries } from './binary';
import { YjsBlockInstance } from './block';
import { GateKeeper } from './gatekeeper';
import {
YjsContentOperation,
DO_NOT_USE_THIS_OR_YOU_WILL_BE_FIRED_SYMBOL_INTO_INNER as INTO_INNER,
} from './operation';
import { EmitEvents, Suspend } from './listener';
import { YjsHistoryManager } from './history';
declare const JWT_DEV: boolean;
const logger = getLogger('BlockDB:yjs');
type YjsProviders = {
awareness: Awareness;
idb: IndexeddbPersistence;
binariesIdb: IndexeddbPersistence;
ws?: WebsocketProvider;
backend: string;
gatekeeper: GateKeeper;
userId: string;
remoteToken?: string; // remote storage token
};
const _yjsDatabaseInstance = new Map<string, YjsProviders>();
async function _initWebsocketProvider(
url: string,
room: string,
doc: Doc,
token?: string,
params?: YjsInitOptions['params']
): Promise<[Awareness, WebsocketProvider | undefined]> {
const awareness = new Awareness(doc);
if (token && !process.env['NX_FREE_LOGIN']) {
const ws = new WebsocketProvider(token, url, room, doc, {
awareness,
params,
}) as any; // TODO: type is erased after cascading references
// Wait for ws synchronization to complete, otherwise the data will be modified in reverse, which can be optimized later
return new Promise((resolve, reject) => {
// TODO: synced will also be triggered on reconnection after losing sync
// There needs to be an event mechanism to emit the synchronization state to the upper layer
ws.once('synced', () => resolve([awareness, ws]));
ws.once('lost-connection', () => resolve([awareness, ws]));
ws.on('connection-error', reject);
});
} else {
return [awareness, undefined];
}
}
const _asyncInitLoading = new Set<string>();
const _waitLoading = async (workspace: string) => {
while (_asyncInitLoading.has(workspace)) {
await sleep();
}
};
async function _initYjsDatabase(
backend: string,
workspace: string,
options: {
params: YjsInitOptions['params'];
userId: string;
token?: string;
}
): Promise<YjsProviders> {
if (_asyncInitLoading.has(workspace)) {
await _waitLoading(workspace);
}
const instance = _yjsDatabaseInstance.get(workspace);
// tTODO:odo temporarily handle this
if (
instance &&
(instance.userId === options.userId || options.userId === 'default')
) {
return instance;
}
// if (instance) return instance;
_asyncInitLoading.add(workspace);
const { params, userId, token: remoteToken } = options;
const doc = new Doc({ autoLoad: true, shouldLoad: true });
const idb = await new IndexeddbPersistence(workspace, doc).whenSynced;
const [awareness, ws] = await _initWebsocketProvider(
backend,
workspace,
doc,
remoteToken,
params
);
const binaries = new Doc({ autoLoad: true, shouldLoad: true });
const binariesIdb = await new IndexeddbPersistence(
`${workspace}_binaries`,
binaries
).whenSynced;
const gatekeeper = new GateKeeper(
userId,
doc.getMap('creators'),
doc.getMap('common')
);
_yjsDatabaseInstance.set(workspace, {
awareness,
idb,
binariesIdb,
ws,
backend,
gatekeeper,
userId,
remoteToken,
});
_asyncInitLoading.delete(workspace);
return {
awareness,
idb,
binariesIdb,
ws,
backend,
gatekeeper,
userId,
remoteToken,
};
}
export type { YjsBlockInstance } from './block';
export type { YjsContentOperation } from './operation';
export type YjsInitOptions = {
backend: typeof BucketBackend[keyof typeof BucketBackend];
params?: Record<string, string>;
userId?: string;
token?: string;
};
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;
// Block Collection
// key is a randomly generated global id
readonly #blocks: YMap<YMap<unknown>>;
readonly #block_updated: YMap<number>;
// Maximum cache Block 1024, ttl 10 minutes
readonly #block_caches: LRUCache<string, YjsBlockInstance>;
readonly #binaries: YjsRemoteBinaries;
readonly #listener: Map<string, BlockListener<any>>;
static async init(
workspace: string,
options: YjsInitOptions
): Promise<YjsAdapter> {
const { backend, params = {}, userId = 'default', token } = options;
const providers = await _initYjsDatabase(backend, workspace, {
params,
userId,
token,
});
return new YjsAdapter(providers);
}
private constructor(providers: YjsProviders) {
this.#provider = providers;
this.#doc = providers.idb.doc;
this.#awareness = providers.awareness;
this.#gatekeeper = providers.gatekeeper;
this.#blocks = this.#doc.getMap('blocks');
this.#block_updated = this.#doc.getMap('block_updated');
this.#block_caches = 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.#listener = new Map();
const debounced_editing_notifier = debounce(
() => {
const listener: BlockListener<Set<string>> | undefined =
this.#listener.get('editing');
if (listener) {
const mapping = this.#awareness.getStates();
const editing_mapping: Record<string, string[]> = {};
for (const {
userId,
editing,
updated,
} of mapping.values()) {
// Only return the status with refresh time within 10 seconds
if (
userId &&
editing &&
updated &&
typeof updated === 'number' &&
updated + 1000 * 10 > Date.now()
) {
if (!editing_mapping[editing])
editing_mapping[editing] = [];
editing_mapping[editing].push(userId);
}
}
listener(
new Map(
Object.entries(editing_mapping).map(([k, v]) => [
k,
new Set(v),
])
)
);
}
},
200,
{ maxWait: 1000 }
);
this.#awareness.setLocalStateField('userId', providers.userId);
this.#awareness.on('update', debounced_editing_notifier);
this.#blocks.observeDeep(events => {
const now = Date.now();
const keys = events.flatMap(e => {
if ((e.path?.length | 0) > 0) {
return [
[e.path[0], 'update'] as [string, ChangedStateKeys],
];
} else {
return Array.from(e.changes.keys.entries()).map(
([k, { action }]) =>
[k, action] as [string, ChangedStateKeys]
);
}
});
EmitEvents(keys, this.#listener.get('updated'));
transact(this.#doc, () => {
for (const [key, action] of keys) {
if (action === 'delete') {
this.#block_updated.delete(key);
} else {
this.#block_updated.set(key, now);
}
}
});
});
}
getUserId(): string {
return this.#provider.userId;
}
inspector() {
const resolve_block = (blocks: Record<string, any>, id: string) => {
const block = blocks[id];
if (block) {
return {
...block,
children: block.children.map((id: string) =>
resolve_block(blocks, id)
),
};
}
};
return {
save: () => {
const binary = encodeStateAsUpdate(this.#doc);
saveAs(
new Blob([binary]),
`affine_workspace_${new Date().toDateString()}.apk`
);
},
load: async () => {
const handles = await window.showOpenFilePicker({
types: [
{
description: 'Affine Package',
accept: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'application/affine': ['.apk'],
},
},
],
});
const [file] = (await fromEvent(handles)) as File[];
const binary = await file.arrayBuffer();
await this.#provider.idb.clearData();
const doc = new Doc({ autoLoad: true, shouldLoad: true });
let updated = 0;
let isUpdated = false;
doc.on('update', () => {
isUpdated = true;
updated += 1;
});
setInterval(() => {
if (updated > 0) updated -= 1;
}, 500);
const update_check = new Promise<void>(resolve => {
const check = async () => {
while (!isUpdated || updated > 0) {
await sleep();
}
resolve();
};
check();
});
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(),
// eslint-disable-next-line @typescript-eslint/naming-convention
parse_page: (page_id: string) => {
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();
return Object.fromEntries(
Object.entries(blocks)
.filter(([, block]) => block.flavor === 'page')
.map(([key, block]) => {
if (resolve) {
return resolve_block(blocks, key);
} else {
return [key, block];
}
})
);
},
clear: () => {
this.#blocks.clear();
this.#block_updated.clear();
this.#gatekeeper.clear();
},
};
}
async createBlock(
options: Pick<BlockItem<YjsContentOperation>, 'type' | 'flavor'> & {
uuid?: string;
binary?: ArrayBufferLike;
}
): Promise<YjsBlockInstance> {
const uuid = options.uuid || `affine${nanoid(16)}`;
if (options.type === BlockTypes.binary) {
if (options.binary && options.binary instanceof ArrayBuffer) {
const array = new YArray();
array.insert(0, [options.binary]);
const block = {
type: options.type,
flavor: options.flavor,
children: [] as string[],
created: Date.now(),
content: new YjsContentOperation(array),
hash: sha3(Buffer.from(options.binary)),
};
await this.set_block(uuid, block);
return (await this.getBlock(uuid))!;
} else {
throw new Error(`Invalid binary type: ${options.binary}`);
}
} else {
const block = {
type: options.type,
flavor: options.flavor,
children: [] as string[],
created: Date.now(),
content: new YjsContentOperation(new YMap()),
};
await this.set_block(uuid, block);
return (await this.getBlock(uuid))!;
}
}
private get_updated(id: string) {
return this.#block_updated.get(id);
}
private get_creator(id: string) {
return this.#gatekeeper.getCreator(id);
}
private get_block_sync(id: string): YjsBlockInstance | undefined {
const cached = this.#block_caches.get(id);
if (cached) {
// Synchronous read cannot read binary
if (cached.type === BlockTypes.block) {
return cached;
}
return undefined;
}
const block = this.#blocks.get(id);
// Synchronous read cannot read binary
if (block && block.get('type') === BlockTypes.block) {
return new YjsBlockInstance({
id,
block,
setBlock: this.set_block.bind(this),
getUpdated: this.get_updated.bind(this),
getCreator: this.get_creator.bind(this),
getBlockInstance: this.get_block_sync.bind(this),
});
}
return undefined;
}
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);
if (block && block.get('type') === BlockTypes.binary) {
const binary = await this.#binaries.get(
block.get('hash') as string
);
if (binary) {
return new YjsBlockInstance({
id,
block,
binary,
setBlock: this.set_block.bind(this),
getUpdated: this.get_updated.bind(this),
getCreator: this.get_creator.bind(this),
getBlockInstance: this.get_block_sync.bind(this),
});
}
}
return undefined;
}
async getBlockByFlavor(
flavor: BlockItem<YjsContentOperation>['flavor']
): Promise<string[]> {
const keys: string[] = [];
this.#blocks.forEach((doc, key) => {
if (doc.get('flavor') === flavor) {
keys.push(key);
}
});
return keys;
}
async getBlockByType(
type: BlockItem<YjsContentOperation>['type']
): Promise<string[]> {
const keys: string[] = [];
this.#blocks.forEach((doc, key) => {
if (doc.get('type') === type) {
keys.push(key);
}
});
return keys;
}
private async set_block(
key: string,
item: BlockItem<YjsContentOperation> & { hash?: string }
): Promise<void> {
return new Promise<void>((resolve, reject) => {
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;
if (!block.size) {
const content = item.content[INTO_INNER]();
if (!content) return reject();
const children = new YArray();
children.push(item.children);
block.set('type', item.type);
block.set('flavor', item.flavor);
block.set('children', children);
block.set('created', item.created);
if (item.type === BlockTypes.block) {
block.set('content', content);
} 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(
item.hash,
content
);
}
} else {
throw new Error(
'binary content must be an buffer yarray'
);
}
} else {
throw new Error('invalid block type: ' + item.type);
}
this.#blocks.set(key, block);
}
if (item.flavor === 'page') {
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);
} else {
this.#gatekeeper.setCreator(key);
}
if (uploaded) {
// TODO: there should be a mechanism to retry the upload
uploaded.catch(err => {
// undo set on failure
console.error('Failed to upload object: ', err);
this.deleteBlocks([key]);
reject(err);
});
}
resolve();
});
});
}
async checkBlocks(keys: string[]): Promise<boolean> {
return (
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, () => {
for (const key of success) {
this.#blocks.delete(key);
}
});
return fail;
}
on<S, R>(key: 'editing' | 'updated', listener: BlockListener<S, R>): void {
this.#listener.set(key, listener);
}
suspend(suspend: boolean) {
Suspend(suspend);
}
public history(): HistoryManager {
return this.#history;
}
}
@@ -0,0 +1,96 @@
import { produce } from 'immer';
import { debounce } from 'ts-debounce';
import { YEvent } from 'yjs';
import { BlockListener, ChangedStateKeys } from '../index';
let listener_suspend = false;
let listener_map = new Map<BlockListener, [string, ChangedStateKeys][]>();
const debounced_suspend_notifier = debounce(
(listener?: BlockListener) => {
if (listener) {
listener_map = produce(listener_map, draft => {
const events = draft.get(listener);
if (events) {
listener(new Map(events));
draft.delete(listener);
}
});
}
},
500,
{ maxWait: 2000 }
);
/**
* Suspend instant update event dispatch, extend to at least 500ms once, and up to 2000ms once when triggered continuously
* @param suspend true: suspend monitoring, false: resume monitoring
*/
export function Suspend(suspend: boolean) {
listener_suspend = produce(listener_suspend, draft => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
draft = suspend;
});
if (!suspend && listener_map.size) {
listener_map = produce(listener_map, draft => {
for (const [listener, events] of draft) {
listener(new Map(events));
}
draft.clear();
});
}
}
export function EmitEvents(
events: [string, ChangedStateKeys][],
listener?: BlockListener
) {
if (listener) {
if (listener_suspend) {
listener_map = produce(listener_map, draft => {
const old_events = listener_map.get(listener) || [];
draft.set(listener, [...old_events, ...events]);
});
debounced_suspend_notifier(listener);
} else {
listener(new Map(events));
}
}
}
export function ChildrenListenerHandler(
listeners: Map<string, BlockListener>,
event: YEvent<any>
) {
if (listeners.size) {
const keys = Array.from(event.keys.entries()).map(
([key, { action }]) => [key, action] as [string, ChangedStateKeys]
);
for (const listener of listeners.values()) {
EmitEvents(keys, listener);
}
}
}
export function ContentListenerHandler(
listeners: Map<string, BlockListener>,
events: YEvent<any>[]
) {
if (listeners.size) {
const keys = events.flatMap(e => {
if ((e.path?.length | 0) > 0) {
return [[e.path[0], 'update'] as [string, 'update']];
} else {
return Array.from(e.changes.keys.entries()).map(
([k, { action }]) => [k, action] as [string, typeof action]
);
}
});
if (keys.length) {
for (const listener of listeners.values()) {
EmitEvents(keys, listener);
}
}
}
}
@@ -0,0 +1,388 @@
/* eslint-disable max-lines */
import {
AbstractType as YAbstractType,
Array as YArray,
Map as YMap,
Text as YText,
} from 'yjs';
import {
ArrayOperation,
BaseTypes,
BlockListener,
ContentOperation,
ContentTypes,
MapOperation,
Operable,
TextOperation,
TextToken,
} from '../index';
import { ChildrenListenerHandler, ContentListenerHandler } from './listener';
const INTO_INNER = Symbol('INTO_INNER');
export const DO_NOT_USE_THIS_OR_YOU_WILL_BE_FIRED_SYMBOL_INTO_INNER: typeof INTO_INNER =
INTO_INNER;
function auto_get(root: ContentOperation, key: string): unknown | undefined {
const array = root.asArray();
if (array && !Number.isNaN(Number(key))) return array.get(Number(key));
const map = root.asMap();
if (map) return map.get(key);
const text = root.asText();
if (text) return text.toString();
console.error('auto_get unknown root', root, key);
return undefined;
}
function auto_set(root: ContentOperation, key: string, data: BaseTypes): void {
const array = root.asArray<BaseTypes>();
if (array && !Number.isNaN(Number(key))) {
return array.insert(Number(key), [data]);
}
const map = root.asMap<BaseTypes>();
if (map) {
return map.set(key, data);
}
const text = root.asText();
if (text && !Number.isNaN(Number(key)) && typeof data === 'string') {
return text.insert(Number(key), data);
}
console.error('autoSet unknown root or path', root, key, data);
}
export class YjsContentOperation implements ContentOperation {
readonly #content: YAbstractType<unknown>;
constructor(content: YAbstractType<any>) {
this.#content = content;
}
get length(): number {
if (this.#content instanceof YMap) {
return this.#content.size;
}
if (this.#content instanceof YArray || this.#content instanceof YText) {
return this.#content.length;
}
return 0;
}
createText(): YjsTextOperation {
return new YjsTextOperation(new YText());
}
createArray<
T extends ContentTypes = ContentOperation
>(): YjsArrayOperation<T> {
return new YjsArrayOperation(new YArray());
}
createMap<T extends ContentTypes = ContentOperation>(): YjsMapOperation<T> {
return new YjsMapOperation(new YMap());
}
asText(): YjsTextOperation | undefined {
if (this.#content instanceof YText) {
return new YjsTextOperation(this.#content);
}
return undefined;
}
asArray<T extends ContentTypes = ContentOperation>():
| YjsArrayOperation<T>
| undefined {
if (this.#content instanceof YArray) {
return new YjsArrayOperation(this.#content);
}
return undefined;
}
asMap<T extends ContentTypes = ContentOperation>():
| YjsMapOperation<T>
| undefined {
if (this.#content instanceof YMap) {
return new YjsMapOperation(this.#content);
}
return undefined;
}
autoGet(
root: ThisType<ContentOperation> | Record<string, unknown>,
path: string[]
): unknown | undefined {
if (root) {
if (path.length === 0) {
return root;
} else if (root instanceof YjsContentOperation) {
const [key, ...rest] = path;
const new_root = auto_get(root, key);
if (new_root) {
return this.autoGet(new_root as typeof root, rest);
}
} else if (typeof root === 'object') {
throw new Error(
'autoGet must not get a non-value type, this is a deprecated behavior'
);
}
}
console.error('autoGet unknown root', root, path);
return undefined;
}
autoSet(
root: ThisType<ContentOperation>,
path: string[],
data: BaseTypes,
partial?: boolean
): void {
if (root) {
if (path.length === 0) {
if (data && typeof data === 'object') {
throw new Error(
'autoSet must not set a non-value type, this is a deprecated behavior'
);
} else {
console.error('autoSet unknown data', root, path, data);
}
return;
}
if (root instanceof YjsContentOperation) {
if (path.length === 1) {
const [key] = path;
if (key) return auto_set(root, key, data);
console.error('autoSet unknown path', root, path, data);
return;
}
const [key, ...rest] = path;
const new_root = auto_get(root, key);
if (new_root && new_root instanceof YjsContentOperation) {
return this.autoSet(new_root, rest, data, partial);
} else {
throw new Error(
'autoSet must not set a non-value type, this is a deprecated behavior'
);
}
}
}
console.error('autoSet unknown root', root, path);
}
protected into_inner<T>(content: Operable<T>): T {
if (content instanceof YjsContentOperation) {
return content[INTO_INNER]() as unknown as T;
} else {
return content as T;
}
}
protected to_operable<T>(content: T): Operable<T> {
if (content instanceof YAbstractType) {
return new YjsContentOperation(content) as unknown as Operable<T>;
}
return content as Operable<T>;
}
[INTO_INNER](): YAbstractType<unknown> | undefined {
if (this.#content instanceof YAbstractType) {
return this.#content;
}
return undefined;
}
// eslint-disable-next-line @typescript-eslint/naming-convention
private toJSON() {
return this.#content.toJSON();
}
}
class YjsTextOperation extends YjsContentOperation implements TextOperation {
readonly #content: YText;
constructor(content: YText) {
super(content);
this.#content = content;
}
insert(
index: number,
content: string,
format?: Record<string, string>
): void {
this.#content.insert(index, content, format);
}
format(
index: number,
length: number,
format: Record<string, string>
): void {
this.#content.format(index, length, format);
}
delete(index: number, length: number): void {
this.#content.delete(index, length);
}
setAttribute(name: string, value: BaseTypes) {
this.#content.setAttribute(name, value);
}
getAttribute<T extends BaseTypes = string>(name: string): T | undefined {
return this.#content.getAttribute(name);
}
override toString(): TextToken[] {
return this.#content.toDelta();
}
}
class YjsArrayOperation<T extends ContentTypes>
extends YjsContentOperation
implements ArrayOperation<T>
{
readonly #content: YArray<T>;
readonly #listeners: Map<string, BlockListener>;
constructor(content: YArray<T>) {
super(content);
this.#content = content;
this.#listeners = new Map();
this.#content.observe(event =>
ChildrenListenerHandler(this.#listeners, event)
);
}
on(name: string, listener: BlockListener) {
this.#listeners.set(name, listener);
}
off(name: string) {
this.#listeners.delete(name);
}
insert(index: number, content: Array<Operable<T>>): void {
this.#content.insert(
index,
content.map(v => this.into_inner(v))
);
}
delete(index: number, length: number): void {
this.#content.delete(index, length);
}
push(content: Array<Operable<T>>): void {
this.#content.push(content.map(v => this.into_inner(v)));
}
unshift(content: Array<Operable<T>>): void {
this.#content.unshift(content.map(v => this.into_inner(v)));
}
get(index: number): Operable<T> | undefined {
const content = this.#content.get(index);
if (content) return this.to_operable(content);
return undefined;
}
private get_internal(index: number): T {
return this.#content.get(index);
}
slice(start?: number, end?: number): Operable<T>[] {
return this.#content.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));
}
// Traverse, if callback returns false, stop traversing
forEach(callback: (value: T, index: number) => boolean) {
for (let i = 0; i < this.#content.length; i++) {
const ret = callback(this.get_internal(i), i);
if (ret === false) {
break;
}
}
}
find<R = unknown>(
callback: (value: T, index: number) => boolean
): R | undefined {
let result: R | undefined = undefined;
this.forEach((value, i) => {
const found = callback(value, i);
if (found) {
result = value as unknown as R;
return false;
}
return true;
});
return result;
}
findIndex(callback: (value: T, index: number) => boolean): number {
let position = -1;
this.forEach((value, i) => {
const found = callback(value, i);
if (found) {
position = i;
return false;
}
return true;
});
return position;
}
}
class YjsMapOperation<T extends ContentTypes>
extends YjsContentOperation
implements MapOperation<T>
{
readonly #content: YMap<T>;
readonly #listeners: Map<string, BlockListener>;
constructor(content: YMap<T>) {
super(content);
this.#content = content;
this.#listeners = new Map();
content?.observeDeep(events =>
ContentListenerHandler(this.#listeners, events)
);
}
on(name: string, listener: BlockListener) {
this.#listeners.set(name, listener);
}
off(name: string) {
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);
} else {
this.#content.set(key, value as T);
}
}
get(key: string): Operable<T> | undefined {
const content = this.#content.get(key);
if (content) return this.to_operable(content);
return undefined;
}
delete(key: string): void {
this.#content.delete(key);
}
has(key: string): boolean {
return this.#content.has(key);
}
}