style: enable rxjs/finnish (#6276)

chore(infra): use finnish notation for observables

do rename
This commit is contained in:
EYHN
2024-03-24 17:04:51 +00:00
parent c6676fd074
commit 2b42a75e5a
104 changed files with 797 additions and 591 deletions

View File

@@ -32,7 +32,7 @@ export class CollectionService {
return this.setting.get(COLLECTIONS_TRASH_KEY) as YArray<DeletedCollection>;
}
readonly collections = LiveData.from(
readonly collections$ = LiveData.from(
new Observable<Collection[]>(subscriber => {
subscriber.next(this.collectionsYArray?.toArray() ?? []);
const fn = () => {
@@ -46,7 +46,7 @@ export class CollectionService {
[]
);
readonly collectionsTrash = LiveData.from(
readonly collectionsTrash$ = LiveData.from(
new Observable<DeletedCollection[]>(subscriber => {
subscriber.next(this.collectionsTrashYArray?.toArray() ?? []);
const fn = () => {
@@ -148,7 +148,7 @@ export class CollectionService {
deletePagesFromCollections(ids: string[]) {
const idSet = new Set(ids);
this.doc.transact(() => {
this.collections.value.forEach(collection => {
this.collections$.value.forEach(collection => {
this.deletePagesFromCollection(collection, idSet);
});
});

View File

@@ -16,7 +16,7 @@ export const GlobalScopeProvider: React.FC<
});
const workspaceProvider = useLiveData(
currentWorkspaceService.currentWorkspace
currentWorkspaceService.currentWorkspace$
)?.services;
return (

View File

@@ -57,8 +57,8 @@ const PageItem = ({
className,
...attrs
}: PageItemProps) => {
const title = useLiveData(pageRecord.title);
const mode = useLiveData(pageRecord.mode);
const title = useLiveData(pageRecord.title$);
const mode = useLiveData(pageRecord.mode$);
const workspace = useService(Workspace);
const { isJournal } = useJournalInfoHelper(
workspace.docCollection,
@@ -171,7 +171,7 @@ const sortPagesByDate = (
return [...pages].sort((a, b) => {
return (
(order === 'asc' ? 1 : -1) *
dayjs(b.meta.value[field]).diff(dayjs(a.meta.value[field]))
dayjs(b.meta$.value[field]).diff(dayjs(a.meta$.value[field]))
);
});
};
@@ -193,7 +193,7 @@ const JournalDailyCountBlock = ({ date }: JournalBlockProps) => {
const t = useAFFiNEI18N();
const [activeItem, setActiveItem] = useState<NavItemName>('createdToday');
const pageRecordList = useService(PageRecordList);
const pageRecords = useLiveData(pageRecordList.records);
const pageRecords = useLiveData(pageRecordList.records$);
const navigateHelper = useNavigateHelper();
@@ -201,10 +201,10 @@ const JournalDailyCountBlock = ({ date }: JournalBlockProps) => {
(field: 'createDate' | 'updatedDate') => {
return sortPagesByDate(
pageRecords.filter(pageRecord => {
if (pageRecord.meta.value.trash) return false;
if (pageRecord.meta$.value.trash) return false;
return (
pageRecord.meta.value[field] &&
dayjs(pageRecord.meta.value[field]).isSame(date, 'day')
pageRecord.meta$.value[field] &&
dayjs(pageRecord.meta$.value[field]).isSame(date, 'day')
);
}),
field
@@ -321,7 +321,7 @@ const ConflictList = ({
setTrashModal({
open: true,
pageIds: [pageRecord.id],
pageTitles: [pageRecord.meta.value.title],
pageTitles: [pageRecord.meta$.value.title],
});
},
[setTrashModal]
@@ -363,7 +363,7 @@ const JournalConflictBlock = ({ date }: JournalBlockProps) => {
const pageRecordList = useService(PageRecordList);
const journalHelper = useJournalHelper(workspace.docCollection);
const docs = journalHelper.getJournalsByDate(date.format('YYYY-MM-DD'));
const pageRecords = useLiveData(pageRecordList.records).filter(v => {
const pageRecords = useLiveData(pageRecordList.records$).filter(v => {
return docs.some(doc => doc.id === v.id);
});

View File

@@ -7,12 +7,12 @@ import type { Workbench } from '../../workbench';
export class Navigator {
constructor(private readonly workbench: Workbench) {}
private readonly history = this.workbench.activeView.map(
private readonly history$ = this.workbench.activeView$.map(
view => view.history
);
private readonly location = LiveData.from(
this.history.pipe(
private readonly location$ = LiveData.from(
this.history$.pipe(
switchMap(
history =>
new Observable<{ index: number; entries: Location[] }>(subscriber => {
@@ -29,11 +29,11 @@ export class Navigator {
{ index: 0, entries: [] }
);
readonly backable = this.location.map(
readonly backable$ = this.location$.map(
({ index, entries }) => index > 0 && entries.length > 1
);
readonly forwardable = this.location.map(
readonly forwardable$ = this.location$.map(
({ index, entries }) => index < entries.length - 1
);
@@ -41,7 +41,7 @@ export class Navigator {
if (!environment.isDesktop) {
window.history.back();
} else {
this.history.value.back();
this.history$.value.back();
}
}
@@ -49,7 +49,7 @@ export class Navigator {
if (!environment.isDesktop) {
window.history.forward();
} else {
this.history.value.forward();
this.history$.value.forward();
}
}
}

View File

@@ -33,8 +33,8 @@ export const NavigationButtons = () => {
const navigator = useService(Navigator);
const backable = useLiveData(navigator.backable);
const forwardable = useLiveData(navigator.forwardable);
const backable = useLiveData(navigator.backable$);
const forwardable = useLiveData(navigator.forwardable$);
const handleBack = useCallback(() => {
navigator.back();

View File

@@ -7,22 +7,22 @@ const RIGHT_SIDEBAR_KEY = 'app:settings:rightsidebar';
export class RightSidebar {
constructor(private readonly globalState: GlobalState) {}
readonly isOpen = LiveData.from(
readonly isOpen$ = LiveData.from(
this.globalState.watch<boolean>(RIGHT_SIDEBAR_KEY),
false
).map(Boolean);
readonly views = new LiveData<RightSidebarView[]>([]);
readonly front = this.views.map(
readonly views$ = new LiveData<RightSidebarView[]>([]);
readonly front$ = this.views$.map(
stack => stack[0] as RightSidebarView | undefined
);
readonly hasViews = this.views.map(stack => stack.length > 0);
readonly hasViews$ = this.views$.map(stack => stack.length > 0);
open() {
this._set(true);
}
toggle() {
this._set(!this.isOpen.value);
this._set(!this.isOpen$.value);
}
close() {
@@ -37,15 +37,15 @@ export class RightSidebar {
* @private use `RightSidebarViewIsland` instead
*/
_append(view: RightSidebarView) {
this.views.next([...this.views.value, view]);
this.views$.next([...this.views$.value, view]);
}
/**
* @private use `RightSidebarViewIsland` instead
*/
_moveToFront(view: RightSidebarView) {
if (this.views.value.includes(view)) {
this.views.next([view, ...this.views.value.filter(v => v !== view)]);
if (this.views$.value.includes(view)) {
this.views$.next([view, ...this.views$.value.filter(v => v !== view)]);
}
}
@@ -53,6 +53,6 @@ export class RightSidebar {
* @private use `RightSidebarViewIsland` instead
*/
_remove(view: RightSidebarView) {
this.views.next(this.views.value.filter(v => v !== view));
this.views$.next(this.views$.value.filter(v => v !== view));
}
}

View File

@@ -19,8 +19,8 @@ export const RightSidebarContainer = () => {
const [resizing, setResizing] = useState(false);
const rightSidebar = useService(RightSidebar);
const frontView = useLiveData(rightSidebar.front);
const open = useLiveData(rightSidebar.isOpen) && frontView !== undefined;
const frontView = useLiveData(rightSidebar.front$);
const open = useLiveData(rightSidebar.isOpen$) && frontView !== undefined;
const [floating, setFloating] = useState(false);
const appSidebarOpened = useAtomValue(appSidebarOpenAtom);

View File

@@ -10,24 +10,24 @@ export class Tag {
private readonly pageRecordList: PageRecordList
) {}
private readonly tagOption = this.properties.tagOptions$.map(
private readonly tagOption$ = this.properties.tagOptions$.map(
tags => tags.find(tag => tag.id === this.id) as TagSchema
);
value = this.tagOption.map(tag => tag?.value || '');
value$ = this.tagOption$.map(tag => tag?.value || '');
color = this.tagOption.map(tag => tag?.color || '');
color$ = this.tagOption$.map(tag => tag?.color || '');
createDate = this.tagOption.map(tag => tag?.createDate || Date.now());
createDate$ = this.tagOption$.map(tag => tag?.createDate || Date.now());
updateDate = this.tagOption.map(tag => tag?.updateDate || Date.now());
updateDate$ = this.tagOption$.map(tag => tag?.updateDate || Date.now());
rename(value: string) {
this.properties.updateTagOption(this.id, {
id: this.id,
value,
color: this.color.value,
createDate: this.createDate.value,
color: this.color$.value,
createDate: this.createDate$.value,
updateDate: Date.now(),
});
}
@@ -35,37 +35,37 @@ export class Tag {
changeColor(color: string) {
this.properties.updateTagOption(this.id, {
id: this.id,
value: this.value.value,
value: this.value$.value,
color,
createDate: this.createDate.value,
createDate: this.createDate$.value,
updateDate: Date.now(),
});
}
tag(pageId: string) {
const pageRecord = this.pageRecordList.record(pageId).value;
const pageRecord = this.pageRecordList.record$(pageId).value;
if (!pageRecord) {
return;
}
pageRecord?.setMeta({
tags: [...pageRecord.meta.value.tags, this.id],
tags: [...pageRecord.meta$.value.tags, this.id],
});
}
untag(pageId: string) {
const pageRecord = this.pageRecordList.record(pageId).value;
const pageRecord = this.pageRecordList.record$(pageId).value;
if (!pageRecord) {
return;
}
pageRecord?.setMeta({
tags: pageRecord.meta.value.tags.filter(tagId => tagId !== this.id),
tags: pageRecord.meta$.value.tags.filter(tagId => tagId !== this.id),
});
}
readonly pageIds = LiveData.computed(get => {
const pages = get(this.pageRecordList.records);
readonly pageIds$ = LiveData.computed(get => {
const pages = get(this.pageRecordList.records$);
return pages
.filter(page => get(page.meta).tags.includes(this.id))
.filter(page => get(page.meta$).tags.includes(this.id))
.map(page => page.id);
});
}

View File

@@ -10,7 +10,7 @@ export class TagService {
private readonly pageRecordList: PageRecordList
) {}
readonly tags = this.properties.tagOptions$.map(tags =>
readonly tags$ = this.properties.tagOptions$.map(tags =>
tags.map(tag => new Tag(tag.id, this.properties, this.pageRecordList))
);
@@ -34,33 +34,33 @@ export class TagService {
this.properties.removeTagOption(tagId);
}
tagsByPageId(pageId: string) {
tagsByPageId$(pageId: string) {
return LiveData.computed(get => {
const pageRecord = get(this.pageRecordList.record(pageId));
const pageRecord = get(this.pageRecordList.record$(pageId));
if (!pageRecord) return [];
const tagIds = get(pageRecord.meta).tags;
const tagIds = get(pageRecord.meta$).tags;
return get(this.tags).filter(tag => tagIds.includes(tag.id));
return get(this.tags$).filter(tag => tagIds.includes(tag.id));
});
}
tagIdsByPageId(pageId: string) {
return this.tagsByPageId(pageId).map(tags => tags.map(tag => tag.id));
tagIdsByPageId$(pageId: string) {
return this.tagsByPageId$(pageId).map(tags => tags.map(tag => tag.id));
}
tagByTagId(tagId?: string) {
return this.tags.map(tags => tags.find(tag => tag.id === tagId));
tagByTagId$(tagId?: string) {
return this.tags$.map(tags => tags.find(tag => tag.id === tagId));
}
tagMetas = LiveData.computed(get => {
return get(this.tags).map(tag => {
tagMetas$ = LiveData.computed(get => {
return get(this.tags$).map(tag => {
return {
id: tag.id,
title: get(tag.value),
color: get(tag.color),
pageCount: get(tag.pageIds).length,
createDate: get(tag.createDate),
updatedDate: get(tag.updateDate),
title: get(tag.value$),
color: get(tag.color$),
pageCount: get(tag.pageIds$).length,
createDate: get(tag.createDate$),
updatedDate: get(tag.updateDate$),
};
});
});
@@ -71,15 +71,17 @@ export class TagService {
return trimmedValue.includes(trimmedQuery);
}
filterTagsByName(name: string) {
filterTagsByName$(name: string) {
return LiveData.computed(get => {
return get(this.tags).filter(tag => this.filterFn(get(tag.value), name));
return get(this.tags$).filter(tag =>
this.filterFn(get(tag.value$), name)
);
});
}
tagByTagValue(value: string) {
tagByTagValue$(value: string) {
return LiveData.computed(get => {
return get(this.tags).find(tag => this.filterFn(get(tag.value), value));
return get(this.tags$).find(tag => this.filterFn(get(tag.value$), value));
});
}
}

View File

@@ -17,11 +17,11 @@ export const DeleteTagConfirmModal = ({
}) => {
const t = useAFFiNEI18N();
const tagService = useService(TagService);
const tags = useLiveData(tagService.tags);
const tags = useLiveData(tagService.tags$);
const selectedTags = useMemo(() => {
return tags.filter(tag => selectedTagIds.includes(tag.id));
}, [selectedTagIds, tags]);
const tagName = useLiveData(selectedTags[0]?.value || '');
const tagName = useLiveData(selectedTags[0]?.value$ || '');
const handleDelete = useCallback(() => {
selectedTagIds.forEach(tagId => {

View File

@@ -21,7 +21,7 @@ export class View {
initialIndex: 0,
});
location = LiveData.from<Location>(
location$ = LiveData.from<Location>(
new Observable(subscriber => {
subscriber.next(this.history.location);
return this.history.listen(update => {
@@ -31,7 +31,7 @@ export class View {
this.history.location
);
entries = LiveData.from<Location[]>(
entries$ = LiveData.from<Location[]>(
new Observable(subscriber => {
subscriber.next(this.history.entries);
return this.history.listen(() => {
@@ -41,7 +41,7 @@ export class View {
this.history.entries
);
size = new LiveData(100);
size$ = new LiveData(100);
header = createIsland();
body = createIsland();
@@ -59,6 +59,6 @@ export class View {
}
setSize(size?: number) {
this.size.next(size ?? 100);
this.size$.next(size ?? 100);
}
}

View File

@@ -13,32 +13,32 @@ interface WorkbenchOpenOptions {
}
export class Workbench {
readonly views = new LiveData([new View()]);
readonly views$ = new LiveData([new View()]);
activeViewIndex = new LiveData(0);
activeView = LiveData.from(
combineLatest([this.views, this.activeViewIndex]).pipe(
activeViewIndex$ = new LiveData(0);
activeView$ = LiveData.from(
combineLatest([this.views$, this.activeViewIndex$]).pipe(
map(([views, index]) => views[index])
),
this.views.value[this.activeViewIndex.value]
this.views$.value[this.activeViewIndex$.value]
);
basename = new LiveData('/');
basename$ = new LiveData('/');
location = LiveData.from(
this.activeView.pipe(switchMap(view => view.location)),
this.views.value[this.activeViewIndex.value].history.location
location$ = LiveData.from(
this.activeView$.pipe(switchMap(view => view.location$)),
this.views$.value[this.activeViewIndex$.value].history.location
);
active(index: number) {
this.activeViewIndex.next(index);
this.activeViewIndex$.next(index);
}
createView(at: WorkbenchPosition = 'beside', defaultLocation: To) {
const view = new View(defaultLocation);
const newViews = [...this.views.value];
const newViews = [...this.views$.value];
newViews.splice(this.indexAt(at), 0, view);
this.views.next(newViews);
this.views$.next(newViews);
return newViews.indexOf(view);
}
@@ -91,33 +91,33 @@ export class Workbench {
}
viewAt(positionIndex: WorkbenchPosition): View | undefined {
return this.views.value[this.indexAt(positionIndex)];
return this.views$.value[this.indexAt(positionIndex)];
}
close(view: View) {
if (this.views.value.length === 1) return;
const index = this.views.value.indexOf(view);
if (this.views$.value.length === 1) return;
const index = this.views$.value.indexOf(view);
if (index === -1) return;
const newViews = [...this.views.value];
const newViews = [...this.views$.value];
newViews.splice(index, 1);
const activeViewIndex = this.activeViewIndex.value;
const activeViewIndex = this.activeViewIndex$.value;
if (activeViewIndex !== 0 && activeViewIndex >= index) {
this.active(activeViewIndex - 1);
}
this.views.next(newViews);
this.views$.next(newViews);
}
closeOthers(view: View) {
view.size.next(100);
this.views.next([view]);
view.size$.next(100);
this.views$.next([view]);
this.active(0);
}
moveView(from: number, to: number) {
const views = [...this.views.value];
const views = [...this.views$.value];
const [removed] = views.splice(from, 1);
views.splice(to, 0, removed);
this.views.next(views);
this.views$.next(views);
this.active(to);
}
@@ -128,18 +128,18 @@ export class Workbench {
* @returns
*/
resize(index: number, percent: number) {
const view = this.views.value[index];
const nextView = this.views.value[index + 1];
const view = this.views$.value[index];
const nextView = this.views$.value[index + 1];
if (!nextView) return;
const totalViewSize = this.views.value.reduce(
(sum, v) => sum + v.size.value,
const totalViewSize = this.views$.value.reduce(
(sum, v) => sum + v.size$.value,
0
);
const percentOfTotal = totalViewSize * percent;
const newSize = Number((view.size.value + percentOfTotal).toFixed(4));
const newSize = Number((view.size$.value + percentOfTotal).toFixed(4));
const newNextSize = Number(
(nextView.size.value - percentOfTotal).toFixed(4)
(nextView.size$.value - percentOfTotal).toFixed(4)
);
// TODO: better strategy to limit size
if (newSize / totalViewSize < 0.2 || newNextSize / totalViewSize < 0.2)
@@ -150,16 +150,16 @@ export class Workbench {
private indexAt(positionIndex: WorkbenchPosition): number {
if (positionIndex === 'active') {
return this.activeViewIndex.value;
return this.activeViewIndex$.value;
}
if (positionIndex === 'beside') {
return this.activeViewIndex.value + 1;
return this.activeViewIndex$.value + 1;
}
if (positionIndex === 'head') {
return 0;
}
if (positionIndex === 'tail') {
return this.views.value.length;
return this.views$.value.length;
}
return positionIndex;
}

View File

@@ -29,7 +29,7 @@ export function useBindWorkbenchToBrowserRouter(
const navigate = useNavigate();
const browserLocation = useLocation();
const view = useLiveData(workbench.activeView);
const view = useLiveData(workbench.activeView$);
useEffect(() => {
return view.history.listen(update => {

View File

@@ -31,9 +31,9 @@ export function useBindWorkbenchToDesktopRouter(
return;
}
if (
workbench.location.value.pathname === newLocation.pathname &&
workbench.location.value.search === newLocation.search &&
workbench.location.value.hash === newLocation.hash
workbench.location$.value.pathname === newLocation.pathname &&
workbench.location$.value.search === newLocation.search &&
workbench.location$.value.hash === newLocation.hash
) {
return;
}

View File

@@ -48,8 +48,8 @@ export const RouteContainer = ({ route }: Props) => {
const viewPosition = useViewPosition();
const leftSidebarOpen = useAtomValue(appSidebarOpenAtom);
const rightSidebar = useService(RightSidebar);
const rightSidebarOpen = useLiveData(rightSidebar.isOpen);
const rightSidebarHasViews = useLiveData(rightSidebar.hasViews);
const rightSidebarOpen = useLiveData(rightSidebar.isOpen$);
const rightSidebarHasViews = useLiveData(rightSidebar.hasViews$);
const handleToggleRightSidebar = useCallback(() => {
rightSidebar.toggle();
}, [rightSidebar]);

View File

@@ -45,10 +45,10 @@ export const SplitViewPanel = memo(function SplitViewPanel({
}: SplitViewPanelProps) {
const [indicatorPressed, setIndicatorPressed] = useState(false);
const ref = useRef<HTMLDivElement>(null);
const size = useLiveData(view.size);
const size = useLiveData(view.size$);
const workbench = useService(Workbench);
const activeView = useLiveData(workbench.activeView);
const views = useLiveData(workbench.views);
const activeView = useLiveData(workbench.activeView$);
const views = useLiveData(workbench.views$);
const isLast = views[views.length - 1] === view;
const {
@@ -116,7 +116,7 @@ export const SplitViewPanel = memo(function SplitViewPanel({
const SplitViewMenu = ({ view }: { view: View }) => {
const t = useAFFiNEI18N();
const workbench = useService(Workbench);
const views = useLiveData(workbench.views);
const views = useLiveData(workbench.views$);
const viewIndex = views.findIndex(v => v === view);

View File

@@ -7,6 +7,6 @@ import { useView } from './use-view';
export function useIsActiveView() {
const workbench = useService(Workbench);
const currentView = useView();
const activeView = useLiveData(workbench.activeView);
const activeView = useLiveData(workbench.activeView$);
return currentView === activeView;
}

View File

@@ -10,11 +10,11 @@ export const useViewPosition = () => {
const view = useView();
const [position, setPosition] = useState(() =>
calcPosition(view, workbench.views.value)
calcPosition(view, workbench.views$.value)
);
useEffect(() => {
const subscription = workbench.views.subscribe(views => {
const subscription = workbench.views$.subscribe(views => {
setPosition(calcPosition(view, views));
});
return () => {

View File

@@ -33,7 +33,7 @@ const warpedRoutes = viewRoutes.map(({ path, lazy }) => {
export const ViewRoot = ({ view }: { view: View }) => {
const viewRouter = useMemo(() => createMemoryRouter(warpedRoutes), []);
const location = useLiveData(view.location);
const location = useLiveData(view.location$);
useEffect(() => {
viewRouter.navigate(location).catch(err => {

View File

@@ -16,7 +16,7 @@ export const WorkbenchLink = ({
>) => {
const workbench = useService(Workbench);
const { appSettings } = useAppSettingHelper();
const basename = useLiveData(workbench.basename);
const basename = useLiveData(workbench.basename$);
const handleClick = useCallback(
(event: React.MouseEvent<HTMLAnchorElement>) => {
event.preventDefault();

View File

@@ -21,7 +21,7 @@ export const WorkbenchRoot = () => {
// for debugging
(window as any).workbench = workbench;
const views = useLiveData(workbench.views);
const views = useLiveData(workbench.views$);
const location = useLocation();
const basename = location.pathname.match(/\/workspace\/[^/]+/g)?.[0] ?? '/';
@@ -40,8 +40,8 @@ export const WorkbenchRoot = () => {
);
useEffect(() => {
workbench.basename.next(basename);
}, [basename, workbench.basename]);
workbench.basename$.next(basename);
}, [basename, workbench.basename$]);
return (
<SplitView

View File

@@ -5,20 +5,20 @@ import { LiveData } from '@toeverything/infra/livedata';
* service to manage current workspace
*/
export class CurrentWorkspaceService {
currentWorkspace = new LiveData<Workspace | null>(null);
currentWorkspace$ = new LiveData<Workspace | null>(null);
/**
* open workspace, current workspace will be set to the workspace
* @param workspace
*/
openWorkspace(workspace: Workspace) {
this.currentWorkspace.next(workspace);
this.currentWorkspace$.next(workspace);
}
/**
* close current workspace, current workspace will be null
*/
closeWorkspace() {
this.currentWorkspace.next(null);
this.currentWorkspace$.next(null);
}
}