fix(core): collections initialized logic (#5310)
Collections YArray should be initialized only when the user operates on it, local state can't be trusted
This commit is contained in:
@@ -193,9 +193,9 @@ export const pageCollectionBaseAtom =
|
|||||||
collections: workspaceSetting.collections,
|
collections: workspaceSetting.collections,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
workspaceSetting.collectionsYArray.observe(fn);
|
workspaceSetting.setting.observeDeep(fn);
|
||||||
group.add(() => {
|
group.add(() => {
|
||||||
workspaceSetting.collectionsYArray.unobserve(fn);
|
workspaceSetting.setting.unobserveDeep(fn);
|
||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
|||||||
@@ -23,29 +23,24 @@ export class WorkspaceSetting {
|
|||||||
return this.workspace.doc.getMap(SETTING_KEY);
|
return this.workspace.doc.getMap(SETTING_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
get collectionsYArray() {
|
get collectionsYArray(): YArray<Collection> | undefined {
|
||||||
if (!this.setting.has(COLLECTIONS_KEY)) {
|
|
||||||
this.setting.set(COLLECTIONS_KEY, new YArray());
|
|
||||||
}
|
|
||||||
return this.setting.get(COLLECTIONS_KEY) as YArray<Collection>;
|
return this.setting.get(COLLECTIONS_KEY) as YArray<Collection>;
|
||||||
}
|
}
|
||||||
|
|
||||||
get collectionsTrashYArray() {
|
get collectionsTrashYArray(): YArray<DeletedCollection> | undefined {
|
||||||
if (!this.setting.has(COLLECTIONS_TRASH_KEY)) {
|
|
||||||
this.setting.set(COLLECTIONS_TRASH_KEY, new YArray());
|
|
||||||
}
|
|
||||||
return this.setting.get(COLLECTIONS_TRASH_KEY) as YArray<DeletedCollection>;
|
return this.setting.get(COLLECTIONS_TRASH_KEY) as YArray<DeletedCollection>;
|
||||||
}
|
}
|
||||||
|
|
||||||
get collections(): Collection[] {
|
get collections(): Collection[] {
|
||||||
return this.collectionsYArray.toArray() ?? [];
|
return this.collectionsYArray?.toArray() ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
get collectionsTrash(): DeletedCollection[] {
|
get collectionsTrash(): DeletedCollection[] {
|
||||||
return this.collectionsTrashYArray.toArray() ?? [];
|
return this.collectionsTrashYArray?.toArray() ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
updateCollection(id: string, updater: (value: Collection) => Collection) {
|
updateCollection(id: string, updater: (value: Collection) => Collection) {
|
||||||
|
if (this.collectionsYArray) {
|
||||||
updateFirstOfYArray(
|
updateFirstOfYArray(
|
||||||
this.collectionsYArray,
|
this.collectionsYArray,
|
||||||
v => v.id === id,
|
v => v.id === id,
|
||||||
@@ -54,19 +49,27 @@ export class WorkspaceSetting {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
addCollection(...collections: Collection[]) {
|
addCollection(...collections: Collection[]) {
|
||||||
|
if (!this.setting.has(COLLECTIONS_KEY)) {
|
||||||
|
this.setting.set(COLLECTIONS_KEY, new YArray());
|
||||||
|
}
|
||||||
this.doc.transact(() => {
|
this.doc.transact(() => {
|
||||||
this.collectionsYArray.insert(0, collections);
|
this.collectionsYArray?.insert(0, collections);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteCollection(info: DeleteCollectionInfo, ...ids: string[]) {
|
deleteCollection(info: DeleteCollectionInfo, ...ids: string[]) {
|
||||||
|
const collectionsYArray = this.collectionsYArray;
|
||||||
|
if (!collectionsYArray) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const set = new Set(ids);
|
const set = new Set(ids);
|
||||||
this.workspace.doc.transact(() => {
|
this.workspace.doc.transact(() => {
|
||||||
const indexList: number[] = [];
|
const indexList: number[] = [];
|
||||||
const list: Collection[] = [];
|
const list: Collection[] = [];
|
||||||
this.collectionsYArray.forEach((collection, i) => {
|
collectionsYArray.forEach((collection, i) => {
|
||||||
if (set.has(collection.id)) {
|
if (set.has(collection.id)) {
|
||||||
set.delete(collection.id);
|
set.delete(collection.id);
|
||||||
indexList.unshift(i);
|
indexList.unshift(i);
|
||||||
@@ -74,9 +77,16 @@ export class WorkspaceSetting {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
indexList.forEach(i => {
|
indexList.forEach(i => {
|
||||||
this.collectionsYArray.delete(i);
|
collectionsYArray.delete(i);
|
||||||
});
|
});
|
||||||
this.collectionsTrashYArray.insert(
|
if (!this.collectionsTrashYArray) {
|
||||||
|
this.setting.set(COLLECTIONS_TRASH_KEY, new YArray());
|
||||||
|
}
|
||||||
|
const collectionsTrashYArray = this.collectionsTrashYArray;
|
||||||
|
if (!collectionsTrashYArray) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
collectionsTrashYArray.insert(
|
||||||
0,
|
0,
|
||||||
list.map(collection => ({
|
list.map(collection => ({
|
||||||
userId: info?.userId,
|
userId: info?.userId,
|
||||||
@@ -84,11 +94,8 @@ export class WorkspaceSetting {
|
|||||||
collection,
|
collection,
|
||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
if (this.collectionsTrashYArray.length > 10) {
|
if (collectionsTrashYArray.length > 10) {
|
||||||
this.collectionsTrashYArray.delete(
|
collectionsTrashYArray.delete(10, collectionsTrashYArray.length - 10);
|
||||||
10,
|
|
||||||
this.collectionsTrashYArray.length - 10
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user