@@ -37,6 +37,10 @@ import {
3737 type SimpleInlineButtonDeclaration ,
3838} from 'packages/core/src/parsers/ButtonParser' ;
3939import { JsViewFieldParser } from 'packages/core/src/parsers/viewFieldParser/JsViewFieldParser' ;
40+ import { Signal } from 'packages/core/src/utils/Signal' ;
41+ import { parsePropPath } from 'packages/core/src/utils/prop/PropParser' ;
42+ import { type BindTargetDeclaration } from 'packages/core/src/parsers/bindTargetParser/BindTargetDeclaration' ;
43+ import { type MetadataSubscription } from 'packages/core/src/metadata/MetadataSubscription' ;
4044
4145export enum FieldType {
4246 INPUT_FIELD = 'INPUT_FIELD' ,
@@ -410,4 +414,167 @@ export abstract class API<Plugin extends IPlugin> {
410414
411415 return undefined ;
412416 }
417+
418+ public createSignal < T > ( value : T ) : Signal < T > {
419+ return new Signal < T > ( value ) ;
420+ }
421+
422+ /**
423+ * Sets a property in meta binds metadata cache.
424+ *
425+ * @param storageType
426+ * @param storagePath
427+ * @param property the property path a.b.c = ['a', 'b', 'c']
428+ * @param value
429+ */
430+ public setMetadata ( storageType : string , storagePath : string , property : string [ ] , value : unknown ) : void {
431+ this . plugin . metadataManager . write ( value , {
432+ storageType : storageType ,
433+ storagePath : storagePath ,
434+ storageProp : parsePropPath ( property ) ,
435+ listenToChildren : false ,
436+ } ) ;
437+ }
438+
439+ /**
440+ * Sets a property in meta binds metadata cache.
441+ *
442+ * @param bindTarget
443+ * @param value
444+ */
445+ public setMetadataWithBindTarget ( bindTarget : BindTargetDeclaration , value : unknown ) : void {
446+ this . plugin . metadataManager . write ( value , bindTarget ) ;
447+ }
448+
449+ /**
450+ * Reads a property from meta binds metadata cache.
451+ * If the value is not present in the cache, it will check the underlying source. E.g. Obsidians metadata cache.
452+ *
453+ * @param storageType
454+ * @param storagePath
455+ * @param property the property path a.b.c = ['a', 'b', 'c']
456+ */
457+ public getMetadata ( storageType : string , storagePath : string , property : string [ ] ) : unknown {
458+ return this . plugin . metadataManager . read ( {
459+ storageType : storageType ,
460+ storagePath : storagePath ,
461+ storageProp : parsePropPath ( property ) ,
462+ listenToChildren : false ,
463+ } ) ;
464+ }
465+
466+ /**
467+ * Reads a property from meta binds metadata cache.
468+ * If the value is not present in the cache, it will check the underlying source. E.g. Obsidians metadata cache.
469+ *
470+ * @param bindTarget
471+ */
472+ public getMetadataWithBindTarget ( bindTarget : BindTargetDeclaration ) : unknown {
473+ return this . plugin . metadataManager . read ( bindTarget ) ;
474+ }
475+
476+ /**
477+ * Updates a property in meta binds metadata cache.
478+ *
479+ * @param storageType
480+ * @param storagePath
481+ * @param property the property path a.b.c = ['a', 'b', 'c']
482+ * @param updateFn a function that takes the current value and returns the new value
483+ */
484+ public updateMetadata (
485+ storageType : string ,
486+ storagePath : string ,
487+ property : string [ ] ,
488+ updateFn : ( value : unknown ) => unknown ,
489+ ) : void {
490+ const bindTarget : BindTargetDeclaration = {
491+ storageType : storageType ,
492+ storagePath : storagePath ,
493+ storageProp : parsePropPath ( property ) ,
494+ listenToChildren : false ,
495+ } ;
496+
497+ const value = this . plugin . metadataManager . read ( bindTarget ) ;
498+ const newValue = updateFn ( value ) ;
499+ this . plugin . metadataManager . write ( newValue , bindTarget ) ;
500+ }
501+
502+ /**
503+ * Updates a property in meta binds metadata cache.
504+ *
505+ * @param bindTarget
506+ * @param updateFn a function that takes the current value and returns the new value
507+ */
508+ public updateMetadataWithBindTarget (
509+ bindTarget : BindTargetDeclaration ,
510+ updateFn : ( value : unknown ) => unknown ,
511+ ) : void {
512+ const value = this . plugin . metadataManager . read ( bindTarget ) ;
513+ const newValue = updateFn ( value ) ;
514+ this . plugin . metadataManager . write ( newValue , bindTarget ) ;
515+ }
516+
517+ /**
518+ * Subscribes to a property in meta binds metadata cache.
519+ * This returns a subscription that can be used to unsubscribe as well as update the cache.
520+ * IF YOU DON'T CALL `unsubscribe` THE SUBSCRIPTION WILL LEAK MEMORY.
521+ *
522+ * @param storageType
523+ * @param storagePath
524+ * @param property the property path a.b.c = ['a', 'b', 'c']
525+ * @param listenToChildren
526+ * @param callback
527+ */
528+ public subscribeToMetadata (
529+ storageType : string ,
530+ storagePath : string ,
531+ property : string [ ] ,
532+ listenToChildren : boolean ,
533+ callback : ( value : unknown ) => void ,
534+ ) : MetadataSubscription {
535+ const uuid = getUUID ( ) ;
536+ const signal = new Signal < unknown > ( undefined ) ;
537+
538+ signal . registerListener ( {
539+ callback : callback ,
540+ } ) ;
541+
542+ return this . plugin . metadataManager . subscribe (
543+ uuid ,
544+ signal ,
545+ {
546+ storageType : storageType ,
547+ storagePath : storagePath ,
548+ storageProp : parsePropPath ( property ) ,
549+ listenToChildren : listenToChildren ,
550+ } ,
551+ ( ) : void => {
552+ signal . unregisterAllListeners ( ) ;
553+ } ,
554+ ) ;
555+ }
556+
557+ /**
558+ * Subscribes to a property in meta binds metadata cache.
559+ * This returns a subscription that can be used to unsubscribe as well as update the cache.
560+ * IF YOU DON'T CALL `unsubscribe` THE SUBSCRIPTION WILL LEAK MEMORY.
561+ *
562+ * @param bindTarget
563+ * @param callback
564+ */
565+ public subscribeToMetadataWithBindTarget (
566+ bindTarget : BindTargetDeclaration ,
567+ callback : ( value : unknown ) => void ,
568+ ) : MetadataSubscription {
569+ const uuid = getUUID ( ) ;
570+ const signal = new Signal < unknown > ( undefined ) ;
571+
572+ signal . registerListener ( {
573+ callback : callback ,
574+ } ) ;
575+
576+ return this . plugin . metadataManager . subscribe ( uuid , signal , bindTarget , ( ) : void => {
577+ signal . unregisterAllListeners ( ) ;
578+ } ) ;
579+ }
413580}
0 commit comments