Skip to content

Commit

Permalink
fix(core): do not ensure properties on read (toeverything#6263)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Mar 23, 2024
1 parent 532d655 commit 62a6075
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type { WorkspacePropertiesAdapter } from '@affine/core/modules/workspace';
import type {
PageInfoCustomProperty,
PageInfoCustomPropertyMeta,
TagOption,
} from '@affine/core/modules/workspace/properties/schema';
import { PagePropertyType } from '@affine/core/modules/workspace/properties/schema';
import { DebugLogger } from '@affine/debug';
Expand Down Expand Up @@ -41,20 +41,16 @@ export const newPropertyTypes: PagePropertyType[] = [
export class PagePropertiesMetaManager {
constructor(private readonly adapter: WorkspacePropertiesAdapter) {}

get tagOptions() {
return this.adapter.tagOptions;
}

get propertiesSchema() {
return this.adapter.schema.pageProperties;
return this.adapter.schema?.pageProperties ?? {};
}

get systemPropertiesSchema() {
return this.adapter.schema.pageProperties.system;
return this.adapter.schema?.pageProperties.system ?? {};
}

get customPropertiesSchema() {
return this.adapter.schema.pageProperties.custom;
return this.adapter.schema?.pageProperties.custom ?? {};
}

getOrderedPropertiesSchema() {
Expand Down Expand Up @@ -126,9 +122,11 @@ export class PagePropertiesMetaManager {
const mapping = new Map<string, Set<string>>();
for (const page of this.adapter.workspace.docCollection.docs.values()) {
const properties = this.adapter.getPageProperties(page.id);
for (const id of Object.keys(properties.custom)) {
if (!mapping.has(id)) mapping.set(id, new Set());
mapping.get(id)?.add(page.id);
if (properties) {
for (const id of Object.keys(properties.custom)) {
if (!mapping.has(id)) mapping.set(id, new Set());
mapping.get(id)?.add(page.id);
}
}
}
return mapping;
Expand All @@ -145,13 +143,14 @@ export class PagePropertiesManager {
private readonly adapter: WorkspacePropertiesAdapter,
public readonly pageId: string
) {
this.adapter.ensurePageProperties(this.pageId);
this.metaManager = new PagePropertiesMetaManager(this.adapter);
this.ensureRequiredProperties();
}

// prevent infinite loop
private ensuring = false;
ensureRequiredProperties() {
this.adapter.ensurePageProperties(this.pageId);
if (this.ensuring) return;
this.ensuring = true;
this.transact(() => {
Expand Down Expand Up @@ -184,36 +183,25 @@ export class PagePropertiesManager {
return this.intrinsicMeta?.createDate;
}

get pageTags() {
return this.adapter.getPageTags(this.pageId);
}

get properties() {
this.ensureRequiredProperties();
return this.adapter.getPageProperties(this.pageId);
}

get readonly() {
return !!this.page?.readonly;
}

addPageTag(pageId: string, tag: TagOption | string) {
this.adapter.addPageTag(pageId, tag);
}

removePageTag(pageId: string, tag: TagOption | string) {
this.adapter.removePageTag(pageId, tag);
}

/**
* get custom properties (filter out properties that are not in schema)
*/
getCustomProperties(): Record<string, PageInfoCustomProperty> {
return Object.fromEntries(
Object.entries(this.properties.custom).filter(([id]) =>
this.metaManager.checkPropertyExists(id)
)
);
return this.properties
? Object.fromEntries(
Object.entries(this.properties.custom).filter(([id]) =>
this.metaManager.checkPropertyExists(id)
)
)
: {};
}

getOrderedCustomProperties() {
Expand All @@ -231,10 +219,11 @@ export class PagePropertiesManager {
}

getCustomProperty(id: string) {
return this.properties.custom[id];
return this.properties?.custom[id];
}

addCustomProperty(id: string, value?: any) {
this.ensureRequiredProperties();
if (!this.metaManager.checkPropertyExists(id)) {
logger.warn(`property ${id} not found`);
return;
Expand All @@ -246,11 +235,11 @@ export class PagePropertiesManager {
}

const newOrder = generateKeyBetween(this.largestOrder(), null);
if (this.properties.custom[id]) {
if (this.properties!.custom[id]) {
logger.warn(`custom property ${id} already exists`);
}

this.properties.custom[id] = {
this.properties!.custom[id] = {
id,
value,
order: newOrder,
Expand All @@ -259,6 +248,7 @@ export class PagePropertiesManager {
}

moveCustomProperty(from: number, to: number) {
this.ensureRequiredProperties();
// move from -> to means change from's order to a new order between to and to -1/+1
const properties = this.getOrderedCustomProperties();
const fromProperty = properties[from];
Expand All @@ -269,19 +259,21 @@ export class PagePropertiesManager {
? [toProperty.order, toNextProperty?.order ?? null]
: [toNextProperty?.order ?? null, toProperty.order];
const newOrder = generateKeyBetween(...args);
this.properties.custom[fromProperty.id].order = newOrder;
this.properties!.custom[fromProperty.id].order = newOrder;
}

hasCustomProperty(id: string) {
return !!this.properties.custom[id];
return !!this.properties?.custom[id];
}

removeCustomProperty(id: string) {
delete this.properties.custom[id];
this.ensureRequiredProperties();
delete this.properties!.custom[id];
}

updateCustomProperty(id: string, opt: Partial<PageInfoCustomProperty>) {
if (!this.properties.custom[id]) {
this.ensureRequiredProperties();
if (!this.properties?.custom[id]) {
logger.warn(`custom property ${id} not found`);
return;
}
Expand Down
73 changes: 19 additions & 54 deletions packages/frontend/core/src/modules/workspace/properties/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
// the adapter is to bridge the workspace rootdoc & native js bindings

import { createYProxy, type Y } from '@blocksuite/store';
Expand All @@ -7,7 +8,6 @@ import { defaultsDeep } from 'lodash-es';
import {
PagePropertyType,
PageSystemPropertyId,
type TagOption,
type WorkspaceAffineProperties,
type WorkspaceFavoriteItem,
} from './schema';
Expand All @@ -28,17 +28,21 @@ export class WorkspacePropertiesAdapter {
public readonly proxy: WorkspaceAffineProperties;
public readonly properties: Y.Map<any>;

private ensuredRoot = false;
private ensuredPages = {} as Record<string, boolean>;

constructor(public readonly workspace: Workspace) {
// check if properties exists, if not, create one
const rootDoc = workspace.docCollection.doc;
this.properties = rootDoc.getMap(AFFINE_PROPERTIES_ID);
this.proxy = createYProxy(this.properties);

// fixme: deal with migration issue?
this.ensureRootProperties();
}

private ensureRootProperties() {
if (this.ensuredRoot) {
return;
}
this.ensuredRoot = true;
// todo: deal with schema change issue
// fixme: may not to be called every time
defaultsDeep(this.proxy, {
Expand Down Expand Up @@ -70,6 +74,11 @@ export class WorkspacePropertiesAdapter {
}

ensurePageProperties(pageId: string) {
this.ensureRootProperties();
if (this.ensuredPages[pageId]) {
return;
}
this.ensuredPages[pageId] = true;
// fixme: may not to be called every time
defaultsDeep(this.proxy.pageProperties, {
[pageId]: {
Expand Down Expand Up @@ -108,65 +117,21 @@ export class WorkspacePropertiesAdapter {
// ====== utilities ======

getPageProperties(pageId: string) {
this.ensurePageProperties(pageId);
return this.pageProperties[pageId];
return this.pageProperties?.[pageId] ?? null;
}

isFavorite(id: string, type: WorkspaceFavoriteItem['type']) {
return this.favorites[id]?.type === type;
return this.favorites?.[id]?.type === type;
}

getJournalPageDateString(id: string) {
return this.pageProperties[id]?.system[PageSystemPropertyId.Journal]?.value;
return this.pageProperties?.[id]?.system[PageSystemPropertyId.Journal]
?.value;
}

setJournalPageDateString(id: string, date: string) {
this.ensurePageProperties(id);
const pageProperties = this.pageProperties[id];
pageProperties.system[PageSystemPropertyId.Journal].value = date;
}

get tagOptions() {
return this.schema.pageProperties.system[PageSystemPropertyId.Tags].options;
}

// page tags could be reactive
getPageTags(pageId: string) {
const tags =
this.getPageProperties(pageId)?.system[PageSystemPropertyId.Tags].value ??
[];
const optionsMap = Object.fromEntries(this.tagOptions.map(o => [o.id, o]));
return tags.map(tag => optionsMap[tag]).filter((t): t is TagOption => !!t);
}

addPageTag(pageId: string, tag: TagOption | string) {
this.ensurePageProperties(pageId);
const tags = this.getPageTags(pageId);
const tagId = typeof tag === 'string' ? tag : tag.id;
if (tags.some(t => t.id === tagId)) {
return;
}
// add tag option if not exist
if (!this.tagOptions.some(t => t.id === tagId)) {
if (typeof tag === 'string') {
throw new Error(`Tag ${tag} does not exist`);
} else {
this.tagOptions.push(tag);
}
}
const pageProperties = this.pageProperties[pageId];
pageProperties.system[PageSystemPropertyId.Tags].value.push(tagId);
}

removePageTag(pageId: string, tag: TagOption | string) {
this.ensurePageProperties(pageId);
const tags = this.getPageTags(pageId);
const tagId = typeof tag === 'string' ? tag : tag.id;
const index = tags.findIndex(t => t.id === tagId);
if (index === -1) {
return;
}
const pageProperties = this.pageProperties[pageId];
pageProperties.system[PageSystemPropertyId.Tags].value.splice(index, 1);
const pageProperties = this.pageProperties?.[id];
pageProperties!.system[PageSystemPropertyId.Journal].value = date;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ const WorkspacePagePropertiesSchema = z.object({
});

export const WorkspaceAffinePropertiesSchema = z.object({
schema: WorkspaceAffinePropertiesSchemaSchema,
favorites: z.record(WorkspaceFavoriteItemSchema),
pageProperties: z.record(WorkspacePagePropertiesSchema),
schema: WorkspaceAffinePropertiesSchemaSchema.optional(),
favorites: z.record(WorkspaceFavoriteItemSchema).optional(),
pageProperties: z.record(WorkspacePagePropertiesSchema).optional(),
});

export type PageInfoCustomPropertyMeta = z.infer<
Expand Down

0 comments on commit 62a6075

Please sign in to comment.