|
| 1 | +<a name="PerPluginStorage"></a> |
| 2 | + |
| 3 | +## PerPluginStorage |
| 4 | +**Kind**: interface |
| 5 | + |
| 6 | +**Since**: XD 29 |
| 7 | + |
| 8 | +Stores metadata accessible to multiple plugins, separated into silos by plugin ID. Your plugin can read & write the storage for its own plugin ID, but storage for other plugin IDs is *read-only*. |
| 9 | + |
| 10 | +Each per-plugin storage silo is a collection of key-value pairs. Keys and values must both be strings. |
| 11 | + |
| 12 | +*Each* scenenode has its own metadata storage, accessed via [`SceneNode.sharedPluginData`](scenegraph.md#SceneNode-sharedPluginData). To store general metadata that is not specific to one scenenode, use `sharedPluginData` on the [document's scenegraph root](scenegraph.md#module_scenegraph-root). |
| 13 | + |
| 14 | +**Example** |
| 15 | +```js |
| 16 | +// This example shows how to save & retrieve rich JSON data in shared metadata storage. |
| 17 | +// See below for simpler examples of using individual APIs. |
| 18 | +const PLUGIN_ID = "<your manifest's plugin ID here>"; |
| 19 | + |
| 20 | +let richObject = { |
| 21 | + list: [2, 4, 6], |
| 22 | + name: "Hello world" |
| 23 | +}; |
| 24 | +node.sharedPluginData.setItem(PLUGIN_ID, "richData", JSON.stringify(richObject)); |
| 25 | + |
| 26 | +// Later on... |
| 27 | +// (This could be in a different plugin, if it passes the original plugin's ID here) |
| 28 | +let jsonString = node.sharedPluginData.getItem(PLUGIN_ID, "richData"); |
| 29 | +if (jsonString) { // may be undefined |
| 30 | + let richObjectCopy = JSON.parse(jsonString); |
| 31 | + console.log(richObjectCopy.list.length); // 3 |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +* [PerPluginStorage](#PerPluginStorage) |
| 36 | + * [.getAll()](#getAll) ⇒ <code>!Object< string, !Object<string, string> ></code> |
| 37 | + * [.getForPluginId(pluginId)](#getForPluginId) ⇒ <code>!Object<string, string></code> |
| 38 | + * [.keys(pluginId)](#keys) ⇒ <code>!Array<string></code> |
| 39 | + * [.getItem(pluginId, key)](#getItem) ⇒ <code>?string</code> |
| 40 | + * [.setItem(pluginId, key, value)](#setItem) |
| 41 | + * [.removeItem(pluginId, key)](#removeItem) |
| 42 | + * [.toString()](#toString) ⇒ <code>string</code> |
| 43 | + * [.toJSON()](#toJSON) ⇒ <code>!Object</code> |
| 44 | + |
| 45 | + |
| 46 | +<a name="getAll"></a> |
| 47 | +## perPluginStorage.getAll() ⇒ <code>!Object< string, !Object<string, string> ></code> |
| 48 | + |
| 49 | +Returns a map where key is plugin ID and value is a nested map containing all the shared metadata for that plugin ID (i.e. the result of calling `getForPluginId()` with that ID). |
| 50 | + |
| 51 | +This map is a clone of the stored metadata, so modifying it has no effect. |
| 52 | + |
| 53 | +**Example** |
| 54 | +```js |
| 55 | +let allSharedMetadata = node.sharedPluginData.getAll(); |
| 56 | +console.log("Plugin A's 'foo' value:", |
| 57 | + allSharedMetadata["A"] && allSharedMetadata["A"].foo); |
| 58 | +console.log("All of plugin B's shared metadata on this node:", |
| 59 | + allSharedMetadata["B"]); |
| 60 | +console.log("List of plugins storing shared metadata on this node:", |
| 61 | + Object.keys(allSharedMetadata)); |
| 62 | +``` |
| 63 | + |
| 64 | + |
| 65 | +* * * |
| 66 | + |
| 67 | +<a name="getForPluginId"></a> |
| 68 | +## perPluginStorage.getForPluginId(pluginId) ⇒ <code>!Object<string, string></code> |
| 69 | + |
| 70 | +Returns a map of key-value string pairs containing all shared metadata stored on this node by the given plugin. May be an empty object (zero keys), but is never null. |
| 71 | + |
| 72 | +This map is a clone of the stored metadata, so modifying it has no effect. |
| 73 | + |
| 74 | +| Param | Type | |
| 75 | +| ----- | ---- | |
| 76 | +| pluginId | `string` | |
| 77 | + |
| 78 | +**Example** |
| 79 | +```js |
| 80 | +const MY_PLUGIN_ID = "<your manifest's plugin ID here>"; |
| 81 | +let mySharedMetadata = node.sharedPluginData.getForPluginId(MY_PLUGIN_ID); |
| 82 | +console.log("My shared 'foo' & 'bar' values:", |
| 83 | + mySharedMetadata.foo, mySharedMetadata.bar); |
| 84 | + |
| 85 | +console.log("Plugin B's shared 'foo' value:", |
| 86 | + node.sharedPluginData.getForPluginId("B").foo); |
| 87 | +``` |
| 88 | + |
| 89 | + |
| 90 | +* * * |
| 91 | + |
| 92 | +<a name="keys"></a> |
| 93 | +## perPluginStorage.keys(pluginId) ⇒ <code>!Array<string></code> |
| 94 | + |
| 95 | +Returns a list of all keys stored on this node by the given plugin. May be empty (length zero), but is never null. |
| 96 | + |
| 97 | +| Param | Type | |
| 98 | +| ----- | ---- | |
| 99 | +| pluginId | `string` | |
| 100 | + |
| 101 | + |
| 102 | +**Example** |
| 103 | +```js |
| 104 | +console.log("All properties stored by plugin A on this node:", |
| 105 | + node.sharedPluginData.keys("A")); |
| 106 | +``` |
| 107 | + |
| 108 | + |
| 109 | +* * * |
| 110 | + |
| 111 | +<a name="getItem"></a> |
| 112 | +## perPluginStorage.getItem(pluginId, key) ⇒ <code>?string</code> |
| 113 | + |
| 114 | +Returns the value stored under the given key on this node by the given plugin, or undefined if the plugin hasn't stored anything under the given key. |
| 115 | + |
| 116 | +Because metadata is stored separately per plugin, two plugins can store two different values under the same key. |
| 117 | + |
| 118 | +| Param | Type | |
| 119 | +| ----- | ---- | |
| 120 | +| pluginId | `string` | |
| 121 | +| key | `string` | |
| 122 | + |
| 123 | + |
| 124 | +**Example** |
| 125 | +```js |
| 126 | +// These are two different values, stored independently per plugin |
| 127 | +console.log("Plugin A's 'foo' value:", node.sharedPluginData.getItem("A", "foo")); |
| 128 | +console.log("Plugin B's 'foo' value:", node.sharedPluginData.getItem("B", "foo")); |
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | +* * * |
| 133 | + |
| 134 | +<a name="setItem"></a> |
| 135 | +## perPluginStorage.setItem(pluginId, key, value) |
| 136 | + |
| 137 | +Set a metadata key which can be read by any other plugin. |
| 138 | + |
| 139 | +| Param | Type | Description | |
| 140 | +| ----- | ---- | ----------- | |
| 141 | +| pluginId | `string` | *Must* be equal to your plugin's ID. | |
| 142 | +| key | `string` | | |
| 143 | +| value | `string` or `undefined` | If undefined, behaves as if you'd called `removeItem()` instead. | |
| 144 | + |
| 145 | + |
| 146 | +**Example** |
| 147 | +```js |
| 148 | +const MY_PLUGIN_ID = "<your manifest's plugin ID here>"; |
| 149 | +node.sharedPluginData.setItem(MY_PLUGIN_ID, "foo", "42"); |
| 150 | + |
| 151 | +node.sharedPluginData.setItem("other_plugin_id", "foo", "42"); |
| 152 | +// ^ ERROR: other plugin's metadata is read-only |
| 153 | + |
| 154 | +console.log(node.sharedPluginData.getItem(MY_PLUGIN_ID, "foo")); // "42" |
| 155 | +``` |
| 156 | + |
| 157 | + |
| 158 | +* * * |
| 159 | + |
| 160 | +<a name="removeItem"></a> |
| 161 | +## perPluginStorage.removeItem(pluginId, key) |
| 162 | + |
| 163 | +Clears a shared metadata key stored by your plugin. |
| 164 | + |
| 165 | +| Param | Type | Description | |
| 166 | +| ----- | ---- | ----------- | |
| 167 | +| pluginId | `string` | *Must* be equal to your plugin's ID. | |
| 168 | +| key | `string` | | |
| 169 | + |
| 170 | + |
| 171 | +**Example** |
| 172 | +```js |
| 173 | +const MY_PLUGIN_ID = "<your manifest's plugin ID here>"; |
| 174 | +node.sharedPluginData.setItem(MY_PLUGIN_ID, "foo", "42"); |
| 175 | +console.log(node.sharedPluginData.getItem(MY_PLUGIN_ID, "foo")); // "42" |
| 176 | + |
| 177 | +node.sharedPluginData.removeItem(MY_PLUGIN_ID, "foo"); |
| 178 | +console.log(node.sharedPluginData.getItem(MY_PLUGIN_ID, "foo")); // undefined |
| 179 | +``` |
| 180 | + |
| 181 | + |
| 182 | +* * * |
| 183 | + |
| 184 | +<a name="toString"></a> |
| 185 | +## perPluginStorage.toString() ⇒ <code>string</code> |
| 186 | + |
| 187 | +Provided for convenience: you can `console.log(node.sharedPluginData)` to see the value of `getAll()`. |
| 188 | + |
| 189 | + |
| 190 | +* * * |
| 191 | + |
| 192 | +<a name="toJSON"></a> |
| 193 | +## perPluginStorage.toJSON() ⇒ <code>!Object</code> |
| 194 | + |
| 195 | +Provided for convenience: you can include a PerPluginStorage object inside data you are going to convert to JSON, even though it is not a plain JavaScript object. Returns the same value as `getAll()`. |
| 196 | + |
| 197 | +**Example** |
| 198 | +```js |
| 199 | +let myData = { |
| 200 | + foo: 42, |
| 201 | + bar: "Some other data", |
| 202 | + metadata: node.sharedPluginData |
| 203 | +}; |
| 204 | +let jsonString = JSON.stringify(myData); |
| 205 | +``` |
0 commit comments