|
| 1 | +# Seer API |
| 2 | + |
| 3 | +This library provides an abstraction around the [Window.postMessage API](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) |
| 4 | +to interact with the Seer extension. |
| 5 | +You could use this module if you have a framework or application that wants to display debugging |
| 6 | +information in the Seer Chrome extension. |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +Simply download the package from the npm registry |
| 11 | + |
| 12 | + yarn add seer |
| 13 | + |
| 14 | +## Notes |
| 15 | + |
| 16 | +The extension will declare a `__SEER_INITIALIZED__` boolean on the window, |
| 17 | +that you can use to check if the extension is installed and prevent any useless |
| 18 | +processing in production or for real-users. |
| 19 | + |
| 20 | +## Internal |
| 21 | + |
| 22 | +How the communication is done exactly relies on the bridge, that you can checkout |
| 23 | +in its dedicated [directory](../src/bridge). The following schema represent the |
| 24 | +complete data flow: |
| 25 | + |
| 26 | +<img src="https://cdn.pbrd.co/images/92al0O7cY.png" height="300" /> |
| 27 | + |
| 28 | +## Functions |
| 29 | + |
| 30 | +<dl> |
| 31 | +<dt><a href="#isReady">isReady()</a> ⇒ <code>Boolean</code></dt> |
| 32 | +<dd><p>Ready check for Seer initialization</p> |
| 33 | +</dd> |
| 34 | +<dt><a href="#throttle">throttle(key, delay)</a> ⇒ <code>Boolean</code></dt> |
| 35 | +<dd><p>Utility method allowing to throttle a user action based on a key and a minimun delay.</p> |
| 36 | +</dd> |
| 37 | +<dt><a href="#send">send(type, payload)</a></dt> |
| 38 | +<dd><p>Low-level api leveraging window.postMessage</p> |
| 39 | +</dd> |
| 40 | +<dt><a href="#init">init()</a></dt> |
| 41 | +<dd><p>Initilize window listener. There will be only one for the whole process |
| 42 | +to prevent too many registrations.</p> |
| 43 | +<p>This method will be called automatically if you use the <code>listenFor</code> method.</p> |
| 44 | +</dd> |
| 45 | +<dt><a href="#clean">clean()</a></dt> |
| 46 | +<dd><p>Clean listener. Can be useful in case you want to unregister upcoming events |
| 47 | +or liberate memory.</p> |
| 48 | +</dd> |
| 49 | +<dt><a href="#listenFor">listenFor(key, cb)</a></dt> |
| 50 | +<dd><p>Create a listener that will be called upon events of the given key.</p> |
| 51 | +</dd> |
| 52 | +<dt><a href="#removeListener">removeListener(cb)</a></dt> |
| 53 | +<dd><p>Remove an identity listener</p> |
| 54 | +</dd> |
| 55 | +<dt><a href="#list">list(key, data)</a></dt> |
| 56 | +<dd><p>Creates a new indexed list. |
| 57 | +It works by index to get O(1) accessing and performance.</p> |
| 58 | +</dd> |
| 59 | +<dt><a href="#listItem">listItem(key, itemKey, data)</a></dt> |
| 60 | +<dd><p>Creates an element in the indexed list, based on the itemKey.</p> |
| 61 | +</dd> |
| 62 | +<dt><a href="#updateItem">updateItem(key, itemKey, path, data)</a></dt> |
| 63 | +<dd><p>Update an item property, can be deeply nested.</p> |
| 64 | +</dd> |
| 65 | +<dt><a href="#multiUpdate">multiUpdate(key, itemKey, array)</a></dt> |
| 66 | +<dd><p>Similar to updateItem, but allows to pass an array with {path,data} pairs for |
| 67 | +multiple update of the same item without having to send multiple messages.</p> |
| 68 | +</dd> |
| 69 | +<dt><a href="#deleteItem">deleteItem(key, itemKey)</a></dt> |
| 70 | +<dd><p>Remove a specific item in a specific tab.</p> |
| 71 | +</dd> |
| 72 | +<dt><a href="#addLog">addLog(key, itemKey, msg)</a></dt> |
| 73 | +<dd><p>Will create a log message to an item, that will be displayde with the current time.</p> |
| 74 | +</dd> |
| 75 | +</dl> |
| 76 | + |
| 77 | +<a name="isReady"></a> |
| 78 | + |
| 79 | +### isReady() ⇒ <code>Boolean</code> |
| 80 | +Ready check for Seer initialization |
| 81 | + |
| 82 | +**Kind**: global function |
| 83 | +<a name="throttle"></a> |
| 84 | + |
| 85 | +### throttle(key, delay) ⇒ <code>Boolean</code> |
| 86 | +Utility method allowing to throttle a user action based on a key and a minimun delay. |
| 87 | + |
| 88 | +**Kind**: global function |
| 89 | + |
| 90 | +| Param | Type | Description | |
| 91 | +| --- | --- | --- | |
| 92 | +| key | <code>String</code> | A unique key | |
| 93 | +| delay | <code>Number</code> | The minimal delay to throttle | |
| 94 | + |
| 95 | +<a name="send"></a> |
| 96 | + |
| 97 | +### send(type, payload) |
| 98 | +Low-level api leveraging window.postMessage |
| 99 | + |
| 100 | +**Kind**: global function |
| 101 | + |
| 102 | +| Param | Type | Description | |
| 103 | +| --- | --- | --- | |
| 104 | +| type | <code>String</code> | The action type | |
| 105 | +| payload | <code>Any</code> | The action payload | |
| 106 | + |
| 107 | +<a name="init"></a> |
| 108 | + |
| 109 | +### init() |
| 110 | +Initilize window listener. There will be only one for the whole process |
| 111 | +to prevent too many registrations. |
| 112 | + |
| 113 | +This method will be called automatically if you use the `listenFor` method. |
| 114 | + |
| 115 | +**Kind**: global function |
| 116 | +<a name="clean"></a> |
| 117 | + |
| 118 | +### clean() |
| 119 | +Clean listener. Can be useful in case you want to unregister upcoming events |
| 120 | +or liberate memory. |
| 121 | + |
| 122 | +**Kind**: global function |
| 123 | +<a name="listenFor"></a> |
| 124 | + |
| 125 | +### listenFor(key, cb) |
| 126 | +Create a listener that will be called upon events of the given key. |
| 127 | + |
| 128 | +**Kind**: global function |
| 129 | + |
| 130 | +| Param | Type | Description | |
| 131 | +| --- | --- | --- | |
| 132 | +| key | <code>String</code> | The unique tab key | |
| 133 | +| cb | <code>function</code> | A callback that will receive the message payload | |
| 134 | + |
| 135 | +<a name="removeListener"></a> |
| 136 | + |
| 137 | +### removeListener(cb) |
| 138 | +Remove an identity listener |
| 139 | + |
| 140 | +**Kind**: global function |
| 141 | + |
| 142 | +| Param | Type | Description | |
| 143 | +| --- | --- | --- | |
| 144 | +| cb | <code>function</code> | The callback to remove | |
| 145 | + |
| 146 | +<a name="list"></a> |
| 147 | + |
| 148 | +### list(key, data) |
| 149 | +Creates a new indexed list. |
| 150 | +It works by index to get O(1) accessing and performance. |
| 151 | + |
| 152 | +**Kind**: global function |
| 153 | + |
| 154 | +| Param | Type | Description | |
| 155 | +| --- | --- | --- | |
| 156 | +| key | <code>String</code> | The key of the tab | |
| 157 | +| data | <code>Object</code> | The indexed object | |
| 158 | + |
| 159 | +<a name="listItem"></a> |
| 160 | + |
| 161 | +### listItem(key, itemKey, data) |
| 162 | +Creates an element in the indexed list, based on the itemKey. |
| 163 | + |
| 164 | +**Kind**: global function |
| 165 | + |
| 166 | +| Param | Type | Description | |
| 167 | +| --- | --- | --- | |
| 168 | +| key | <code>String</code> | The key of the tab | |
| 169 | +| itemKey | <code>String</code> | The key of the item | |
| 170 | +| data | <code>Any</code> | The value of the item | |
| 171 | + |
| 172 | +<a name="updateItem"></a> |
| 173 | + |
| 174 | +### updateItem(key, itemKey, path, data) |
| 175 | +Update an item property, can be deeply nested. |
| 176 | + |
| 177 | +**Kind**: global function |
| 178 | + |
| 179 | +| Param | Type | Description | |
| 180 | +| --- | --- | --- | |
| 181 | +| key | <code>String</code> | The key of the tab | |
| 182 | +| itemKey | <code>String</code> | The key of the item | |
| 183 | +| path | <code>String</code> | The path of the variable you want to update | |
| 184 | +| data | <code>Object</code> | The new value | |
| 185 | + |
| 186 | +<a name="multiUpdate"></a> |
| 187 | + |
| 188 | +### multiUpdate(key, itemKey, array) |
| 189 | +Similar to updateItem, but allows to pass an array with {path,data} pairs for |
| 190 | +multiple update of the same item without having to send multiple messages. |
| 191 | + |
| 192 | +**Kind**: global function |
| 193 | + |
| 194 | +| Param | Type | Description | |
| 195 | +| --- | --- | --- | |
| 196 | +| key | <code>String</code> | The key of the tab | |
| 197 | +| itemKey | <code>String</code> | The key of the item | |
| 198 | +| array | <code>Array</code> | The array of updates | |
| 199 | +| array.path | <code>String</code> | The path for this update | |
| 200 | +| array.data | <code>Object</code> | The value of this update | |
| 201 | + |
| 202 | +<a name="deleteItem"></a> |
| 203 | + |
| 204 | +### deleteItem(key, itemKey) |
| 205 | +Remove a specific item in a specific tab. |
| 206 | + |
| 207 | +**Kind**: global function |
| 208 | + |
| 209 | +| Param | Type | Description | |
| 210 | +| --- | --- | --- | |
| 211 | +| key | <code>String</code> | They key of the tab | |
| 212 | +| itemKey | <code>String</code> | The key of the item | |
| 213 | + |
| 214 | +<a name="addLog"></a> |
| 215 | + |
| 216 | +### addLog(key, itemKey, msg) |
| 217 | +Will create a log message to an item, that will be displayde with the current time. |
| 218 | + |
| 219 | +**Kind**: global function |
| 220 | + |
| 221 | +| Param | Type | Description | |
| 222 | +| --- | --- | --- | |
| 223 | +| key | <code>String</code> | The key of the tab | |
| 224 | +| itemKey | <code>String</code> | The key of the item | |
| 225 | +| msg | <code>String</code> | The message to display | |
| 226 | + |
0 commit comments