Skip to content

Commit

Permalink
fix #185
Browse files Browse the repository at this point in the history
  • Loading branch information
mProjectsCode committed Jan 26, 2024
1 parent 3be4589 commit 9d52dd9
Show file tree
Hide file tree
Showing 24 changed files with 1,395 additions and 640 deletions.
Binary file modified bun.lockb
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
playerCount: 6
playerLevel: 6
playerLevel: 4
enemy:
- name: Somthing
level: 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
text: aaa
text: aasdasd
locked: false
---

Expand All @@ -8,23 +8,28 @@ Locked: `INPUT[toggle:locked]`
```js-engine
const mb = engine.getPlugin('obsidian-meta-bind-plugin').api;
const signal = mb.createSignal(undefined)
component.register(mb.listenToMetadata(signal, context.file.path, ['locked']))
const signal = mb.createSignal(undefined);
component.register(mb.listenToMetadata(signal, context.file.path, ['locked']));
const comp = new obsidian.Component(component);
function render() {
comp.unload()
comp.load()
container.empty();
if (signal.get()) {
mb.createViewFieldFromString("VIEW[{text}][text]", "INLINE", context.file.path, container, component);
mb.createViewFieldFromString("VIEW[{text}][text]", "inline", context.file.path, container, comp);
} else {
mb.createInputFieldFromString("INPUT[text:text]", "INLINE", context.file.path, container, component);
mb.createInputFieldFromString("INPUT[text:text]", "inline", context.file.path, container, comp);
}
}
const reactive = engine.reactive(render)
const reactive = engine.reactive(render);
signal.registerListener({
callback: () => reactive.refresh(),
})
});
return reactive;
```
Expand Down
4 changes: 2 additions & 2 deletions exampleVault/Examples.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
slider1: 8
slider1: 7
suggest: test
toggle1: false
Domestic_tasks:
Expand All @@ -13,7 +13,7 @@ inlineSelect: 0
nested:
object: test
number1: 2
number2: 16
number2: 14
---

## Fields Work Everywhere
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dev-publish": "bun run esbuild.publish.config.mjs",
"build-publish": "tsc -noEmit -skipLibCheck && node esbuild.publish.config.mjs production",
"tsc": "tsc -noEmit -skipLibCheck",
"test": "bun test",
"test": "LOG_TESTS=false bun test",
"test:log": "LOG_TESTS=true bun test",
"format": "prettier --write --plugin prettier-plugin-svelte .",
"format:check": "prettier --check --plugin prettier-plugin-svelte .",
Expand All @@ -28,7 +28,7 @@
"@codemirror/language": "6.9.2",
"@codemirror/state": "6.3.1",
"@codemirror/view": "6.22.0",
"@happy-dom/global-registrator": "^12.10.3",
"@happy-dom/global-registrator": "^13.3.1",
"@tsconfig/svelte": "^5.0.0",
"@types/bun": "^1.0.2",
"@typescript-eslint/eslint-plugin": "^6.0.0",
Expand Down
59 changes: 42 additions & 17 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createMarkdownRenderChildWidgetEditorPlugin } from './cm6/Cm6_ViewPlugi
import { MDRCManager } from './MDRCManager';
import { DEFAULT_SETTINGS, type MetaBindPluginSettings } from './settings/Settings';
import { type IPlugin } from './IPlugin';
import { ObsidianMetadataAdapter } from './metadata/ObsidianMetadataAdapter';
// import { ObsidianMetadataAdapter } from './metadata/ObsidianMetadataAdapter';
import { FaqView, MB_FAQ_VIEW_TYPE } from './faq/FaqView';
import { EMBED_MAX_DEPTH, EmbedMDRC } from './renderChildren/EmbedMDRC';
import { getUUID } from './utils/Utils';
Expand All @@ -20,6 +20,9 @@ import { registerCm5HLModes } from './cm6/Cm5_Modes';
import { DependencyManager } from './utils/dependencies/DependencyManager';
import { Version } from './utils/dependencies/Version';
import { createEditorMenu } from './EditorMenu';
import { BindTargetStorageType } from './parsers/bindTargetParser/BindTargetDeclaration';
import { GlobalMetadataSource, InternalMetadataSource } from './metadata/InternalMetadataSources';
import { ObsidianMetadataSource } from './metadata/ObsidianMetadataSource';

export enum MetaBindBuild {
DEV = 'dev',
Expand Down Expand Up @@ -79,22 +82,9 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
this.api = new API(this);
this.internal = new ObsidianAPIAdapter(this);
this.mdrcManager = new MDRCManager();
const metadataAdapter = new ObsidianMetadataAdapter(this);
this.metadataManager = new MetadataManager(metadataAdapter);
// const metadataAdapter = new ObsidianMetadataAdapter(this);

this.registerEvent(
this.app.vault.on('rename', (_file, oldPath) => {
this.mdrcManager.unloadFile(oldPath);
metadataAdapter.onFileRename(oldPath);
}),
);

this.registerEvent(
this.app.vault.on('delete', file => {
this.mdrcManager.unloadFile(file.path);
metadataAdapter.onFileDelete(file.path);
}),
);
this.setUpMetadataManager();

this.loadTemplates();

Expand All @@ -121,7 +111,6 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
onunload(): void {
console.log(`meta-bind | Main >> unload`);
this.mdrcManager.unload();
this.metadataManager.unload();
}

// TODO: move to internal API
Expand All @@ -135,6 +124,42 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
}
}

setUpMetadataManager(): void {
this.metadataManager = new MetadataManager();

const obsidianMetadataSource = new ObsidianMetadataSource(
this,
BindTargetStorageType.FRONTMATTER,
this.metadataManager,
);
this.metadataManager.registerSource(obsidianMetadataSource);

const memoryMetadataSource = new InternalMetadataSource(BindTargetStorageType.MEMORY, this.metadataManager);
this.metadataManager.registerSource(memoryMetadataSource);

const globalMemoryMetadataSource = new GlobalMetadataSource(
BindTargetStorageType.GLOBAL_MEMORY,
this.metadataManager,
);
this.metadataManager.registerSource(globalMemoryMetadataSource);

this.registerEvent(
this.app.vault.on('rename', (file, oldPath) => {
this.mdrcManager.unloadFile(oldPath);
this.metadataManager.onStoragePathRenamed(oldPath, file.path);
}),
);

this.registerEvent(
this.app.vault.on('delete', file => {
this.mdrcManager.unloadFile(file.path);
this.metadataManager.onStoragePathDeleted(file.path);
}),
);

this.registerInterval(window.setInterval(() => this.metadataManager.cycle(), this.settings.syncInterval));
}

addPostProcessors(): void {
this.registerMarkdownPostProcessor((el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
const codeBlocks = el.querySelectorAll('code');
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/ComputedMetadataSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class ComputedMetadataSubscription implements IMetadataSubscription {
const values = this.dependencySubscriptions.map(x => x.callbackSignal.get());
const value = await this.computeFunction(values);
this.callbackSignal.set(value);
this.metadataManager.updateCache(value, this);
this.metadataManager.update(value, this);
}

/**
Expand Down
15 changes: 0 additions & 15 deletions src/metadata/IMetadataAdapter.ts

This file was deleted.

103 changes: 103 additions & 0 deletions src/metadata/InternalMetadataSources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { type IMetadataSource, MapMetadataSource, type Metadata } from './MetadataSource';
import { type GlobalMetadataCacheItem, type MapMetadataCacheItem } from './MetadataCacheItem';
import { type MetadataManager } from './MetadataManager';
import { type BindTargetDeclaration } from '../parsers/bindTargetParser/BindTargetDeclaration';
import { type PropPath } from '../utils/prop/PropPath';
import { type IMetadataSubscription } from './IMetadataSubscription';
import { PropUtils } from '../utils/prop/PropUtils';
import { ErrorLevel, MetaBindInternalError } from '../utils/errors/MetaBindErrors';

export class InternalMetadataSource extends MapMetadataSource<MapMetadataCacheItem> {
public getDefaultCacheItem(storagePath: string): MapMetadataCacheItem {
return {
data: {},
storagePath: storagePath,
...this.manager.getDefaultCacheItem(),
};
}

public async syncExternal(_cacheItem: MapMetadataCacheItem): Promise<void> {
// Do nothing
}
}

export class GlobalMetadataSource implements IMetadataSource<GlobalMetadataCacheItem> {
public readonly id: string;
public readonly manager: MetadataManager;
public readonly cache: GlobalMetadataCacheItem;

constructor(id: string, manager: MetadataManager) {
this.id = id;
this.manager = manager;

this.cache = {
data: {},
...this.manager.getDefaultCacheItem(),
};
}

public delete(_cacheItem: GlobalMetadataCacheItem): void {
// noop
}

public getCacheItemForStoragePath(_storagePath: string): GlobalMetadataCacheItem | undefined {
return this.cache;
}

public iterateCacheItems(): IterableIterator<GlobalMetadataCacheItem> {
return [this.cache][Symbol.iterator]();
}

public onCycle(_cacheItem: GlobalMetadataCacheItem): void {
// noop
}

public readCache(_bindTarget: BindTargetDeclaration): unknown {
return this.readCacheItem(this.cache, _bindTarget.storageProp);
}

public readCacheItem(cacheItem: GlobalMetadataCacheItem, propPath: PropPath): unknown {
return PropUtils.tryGet(cacheItem.data, propPath);
}

public shouldDelete(_cacheItem: GlobalMetadataCacheItem): boolean {
return false;
}

public subscribe(subscription: IMetadataSubscription): GlobalMetadataCacheItem {
this.cache.subscriptions.push(subscription);
return this.cache;
}

public syncExternal(_cacheItem: GlobalMetadataCacheItem): void {
// noop
}

public unsubscribe(subscription: IMetadataSubscription): GlobalMetadataCacheItem {
this.cache.subscriptions = this.cache.subscriptions.filter(x => x.uuid !== subscription.uuid);

return this.cache;
}

public update(value: unknown, subscription: IMetadataSubscription): GlobalMetadataCacheItem {
if (subscription.bindTarget === undefined) {
throw new MetaBindInternalError({
errorLevel: ErrorLevel.CRITICAL,
effect: 'can not update metadata',
cause: 'subscription bind target undefined',
});
}

PropUtils.setAndCreate(this.cache.data, subscription.bindTarget.storageProp, value);

return this.cache;
}

public updateEntireCache(value: Metadata, cacheItem: GlobalMetadataCacheItem): void {
cacheItem.data = value;
}

public readEntireCacheItem(cacheItem: GlobalMetadataCacheItem): Metadata {
return cacheItem.data;
}
}
33 changes: 33 additions & 0 deletions src/metadata/MetadataCacheItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { type IMetadataSubscription } from './IMetadataSubscription';

import { type Metadata } from './MetadataSource';

export interface IMetadataCacheItem {
subscriptions: IMetadataSubscription[];

/**
* The cycles since the last change to the cache by the plugin.
*/
cyclesSinceInternalChange: number;
/**
* Whether the cache was changed by the plugin. If this is true, the frontmatter should be updated.
*/
pendingInternalChange: boolean;
/**
* The cycles that the cache has been inactive, meaning no listener registered to it.
*/
cyclesSinceInactive: number;
/**
* Whether the there are no subscribers to the cache.
*/
inactive: boolean;
}

export interface MapMetadataCacheItem extends IMetadataCacheItem {
storagePath: string;
data: Metadata;
}

export interface GlobalMetadataCacheItem extends IMetadataCacheItem {
data: Metadata;
}
Loading

0 comments on commit 9d52dd9

Please sign in to comment.