@@ -22,6 +22,13 @@ const { validateSuggestion } = require('./suggestion')
2222const { validateProgress } = require ( './progress' )
2323const { validateWidget } = require ( './widget' )
2424
25+ /**
26+ * @typedef SetSharedDataOptions
27+ * @prop {boolean } disk Don't keep this data in memory by writing it to disk
28+ */
29+
30+ /** @typedef {import('../connectors/shared-data').SharedData } SharedData */
31+
2532class PluginApi {
2633 constructor ( { plugins, file, project, lightMode = false } , context ) {
2734 // Context
@@ -453,10 +460,10 @@ class PluginApi {
453460 /* Namespaced */
454461
455462 /**
456- * Retrieve a Shared data value .
463+ * Retrieve a Shared data instance .
457464 *
458465 * @param {string } id Id of the Shared data
459- * @returns {any } Shared data value
466+ * @returns {SharedData } Shared data instance
460467 */
461468 getSharedData ( id ) {
462469 return sharedData . get ( { id, projectId : this . project . id } , this . context )
@@ -467,18 +474,19 @@ class PluginApi {
467474 *
468475 * @param {string } id Id of the Shared data
469476 * @param {any } value Value of the Shared data
477+ * @param {SetSharedDataOptions } options
470478 */
471- setSharedData ( id , value ) {
472- sharedData . set ( { id, projectId : this . project . id , value } , this . context )
479+ async setSharedData ( id , value , { disk = false } = { } ) {
480+ return sharedData . set ( { id, projectId : this . project . id , value, disk } , this . context )
473481 }
474482
475483 /**
476484 * Delete a shared data.
477485 *
478486 * @param {string } id Id of the Shared data
479487 */
480- removeSharedData ( id ) {
481- sharedData . remove ( { id, projectId : this . project . id } , this . context )
488+ async removeSharedData ( id ) {
489+ return sharedData . remove ( { id, projectId : this . project . id } , this . context )
482490 }
483491
484492 /**
@@ -616,24 +624,93 @@ class PluginApi {
616624 * - callAction
617625 *
618626 * @param {string } namespace Prefix to add to the id params
619- * @returns {object } Namespaced methods
620627 */
621628 namespace ( namespace ) {
622629 return {
630+ /**
631+ * Retrieve a Shared data instance.
632+ *
633+ * @param {string } id Id of the Shared data
634+ * @returns {SharedData } Shared data instance
635+ */
623636 getSharedData : ( id ) => this . getSharedData ( namespace + id ) ,
624- setSharedData : ( id , value ) => this . setSharedData ( namespace + id , value ) ,
637+ /**
638+ * Set or update the value of a Shared data
639+ *
640+ * @param {string } id Id of the Shared data
641+ * @param {any } value Value of the Shared data
642+ * @param {SetSharedDataOptions } options
643+ */
644+ setSharedData : ( id , value , options ) => this . setSharedData ( namespace + id , value , options ) ,
645+ /**
646+ * Delete a shared data.
647+ *
648+ * @param {string } id Id of the Shared data
649+ */
625650 removeSharedData : ( id ) => this . removeSharedData ( namespace + id ) ,
651+ /**
652+ * Watch for a value change of a shared data
653+ *
654+ * @param {string } id Id of the Shared data
655+ * @param {function } handler Callback
656+ */
626657 watchSharedData : ( id , handler ) => this . watchSharedData ( namespace + id , handler ) ,
658+ /**
659+ * Delete the watcher of a shared data.
660+ *
661+ * @param {string } id Id of the Shared data
662+ * @param {function } handler Callback
663+ */
627664 unwatchSharedData : ( id , handler ) => this . unwatchSharedData ( namespace + id , handler ) ,
665+ /**
666+ * Listener triggered when a Plugin action is called from a client addon component.
667+ *
668+ * @param {string } id Id of the action to listen
669+ * @param {any } cb Callback (ex: (params) => {} )
670+ */
628671 onAction : ( id , cb ) => this . onAction ( namespace + id , cb ) ,
672+ /**
673+ * Call a Plugin action. This can also listened by client addon components.
674+ *
675+ * @param {string } id Id of the action
676+ * @param {object } params Params object passed as 1st argument to callbacks
677+ * @returns {Promise }
678+ */
629679 callAction : ( id , params ) => this . callAction ( namespace + id , params ) ,
680+ /**
681+ * Retrieve a value from the local DB
682+ *
683+ * @param {string } id Path to the item
684+ * @returns Item value
685+ */
630686 storageGet : ( id ) => this . storageGet ( namespace + id ) ,
687+ /**
688+ * Store a value into the local DB
689+ *
690+ * @param {string } id Path to the item
691+ * @param {any } value Value to be stored (must be serializable in JSON)
692+ */
631693 storageSet : ( id , value ) => this . storageSet ( namespace + id , value ) ,
694+ /**
695+ * Add a suggestion for the user.
696+ *
697+ * @param {object } options Suggestion
698+ */
632699 addSuggestion : ( options ) => {
633700 options . id = namespace + options . id
634701 return this . addSuggestion ( options )
635702 } ,
703+ /**
704+ * Remove a suggestion
705+ *
706+ * @param {string } id Id of the suggestion
707+ */
636708 removeSuggestion : ( id ) => this . removeSuggestion ( namespace + id ) ,
709+ /**
710+ * Register a widget for project dashboard
711+ *
712+ * @param {object } def Widget definition
713+ */
637714 registerWidget : ( def ) => {
638715 def . id = namespace + def . id
639716 return this . registerWidget ( def )
0 commit comments