@atlaskit/node-data-provider 4.1.2 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/dist/cjs/index.js +1 -232
- package/dist/es2019/index.js +0 -191
- package/dist/esm/index.js +0 -227
- package/dist/types/index.d.ts +0 -134
- package/dist/types-ts4.5/index.d.ts +0 -134
- package/package.json +2 -10
- package/cache/package.json +0 -15
- package/content/package.json +0 -15
- package/dist/cjs/cache.js +0 -145
- package/dist/cjs/consumption/_global-ndp-caches.js +0 -21
- package/dist/cjs/consumption/_internal-context.js +0 -77
- package/dist/cjs/consumption/_lru-cache.js +0 -45
- package/dist/cjs/consumption/content.js +0 -56
- package/dist/cjs/get-providers/confluence-page.js +0 -15
- package/dist/cjs/internal-types.js +0 -5
- package/dist/cjs/plugin-hooks.js +0 -100
- package/dist/cjs/providers/emoji.js +0 -54
- package/dist/es2019/cache.js +0 -96
- package/dist/es2019/consumption/_global-ndp-caches.js +0 -13
- package/dist/es2019/consumption/_internal-context.js +0 -71
- package/dist/es2019/consumption/_lru-cache.js +0 -30
- package/dist/es2019/consumption/content.js +0 -49
- package/dist/es2019/get-providers/confluence-page.js +0 -10
- package/dist/es2019/internal-types.js +0 -1
- package/dist/es2019/plugin-hooks.js +0 -69
- package/dist/es2019/providers/emoji.js +0 -26
- package/dist/esm/cache.js +0 -141
- package/dist/esm/consumption/_global-ndp-caches.js +0 -13
- package/dist/esm/consumption/_internal-context.js +0 -71
- package/dist/esm/consumption/_lru-cache.js +0 -38
- package/dist/esm/consumption/content.js +0 -49
- package/dist/esm/get-providers/confluence-page.js +0 -9
- package/dist/esm/internal-types.js +0 -1
- package/dist/esm/plugin-hooks.js +0 -93
- package/dist/esm/providers/emoji.js +0 -47
- package/dist/types/cache.d.ts +0 -61
- package/dist/types/consumption/_global-ndp-caches.d.ts +0 -8
- package/dist/types/consumption/_internal-context.d.ts +0 -32
- package/dist/types/consumption/_lru-cache.d.ts +0 -7
- package/dist/types/consumption/content.d.ts +0 -65
- package/dist/types/get-providers/confluence-page.d.ts +0 -6
- package/dist/types/internal-types.d.ts +0 -2
- package/dist/types/plugin-hooks.d.ts +0 -32
- package/dist/types/providers/emoji.d.ts +0 -10
- package/dist/types-ts4.5/cache.d.ts +0 -61
- package/dist/types-ts4.5/consumption/_global-ndp-caches.d.ts +0 -8
- package/dist/types-ts4.5/consumption/_internal-context.d.ts +0 -32
- package/dist/types-ts4.5/consumption/_lru-cache.d.ts +0 -7
- package/dist/types-ts4.5/consumption/content.d.ts +0 -65
- package/dist/types-ts4.5/get-providers/confluence-page.d.ts +0 -6
- package/dist/types-ts4.5/internal-types.d.ts +0 -2
- package/dist/types-ts4.5/plugin-hooks.d.ts +0 -32
- package/dist/types-ts4.5/providers/emoji.d.ts +0 -10
- package/emoji-provider/package.json +0 -15
- package/get-confluence-page-providers/package.json +0 -15
- package/plugin-hooks/package.json +0 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @atlaskit/node-data-provider
|
|
2
2
|
|
|
3
|
+
## 4.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#188356](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/188356)
|
|
8
|
+
[`5a2110350abd3`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/5a2110350abd3) -
|
|
9
|
+
[https://product-fabric.atlassian.net/browse/ED-28554](ED-28554) - clean up EmojiNodeDataProvider
|
|
10
|
+
spike
|
|
11
|
+
|
|
3
12
|
## 4.1.2
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,232 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.NodeDataProvider = void 0;
|
|
8
|
-
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
9
|
-
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
10
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
11
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
12
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
14
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
15
|
-
/**
|
|
16
|
-
* This is the base class for creating a node data provider for an editor plugin.
|
|
17
|
-
*
|
|
18
|
-
* ## Usage
|
|
19
|
-
*
|
|
20
|
-
* ### Create a provider
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```ts
|
|
24
|
-
* class EmojiNodeDataProvider extends NodeDataProvider<
|
|
25
|
-
* { attrs: EmojiAttributes },
|
|
26
|
-
* { resolvedData: string }
|
|
27
|
-
* > {
|
|
28
|
-
* constructor({ existingCache }?: { existingCache: Record<string, { resolvedData: string }> }) {
|
|
29
|
-
* super({ existingCache, nodeName: 'emoji' });
|
|
30
|
-
* }
|
|
31
|
-
* nodeToKey(node: { attrs: EmojiAttributes }): string {
|
|
32
|
-
* return `${node.attrs.shortName}-${node.attrs.text}-${node.attrs.id}`;
|
|
33
|
-
* }
|
|
34
|
-
* resolve(node: { attrs: EmojiAttributes }, _?: { signal: AbortSignal }) {
|
|
35
|
-
* return Promise.resolve({ resolvedData: 'resolved' });
|
|
36
|
-
* }
|
|
37
|
-
* }
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* ### Use the provider
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```ts
|
|
44
|
-
* const emojiNodeDataProvider = new EmojiNodeDataProvider();
|
|
45
|
-
* ```
|
|
46
|
-
*
|
|
47
|
-
* ### Caching
|
|
48
|
-
*
|
|
49
|
-
* @see {@link buildCaches} for more information on building caches.
|
|
50
|
-
*
|
|
51
|
-
* #### Load an existing provider with a cache
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```
|
|
55
|
-
* await buildCaches({
|
|
56
|
-
* adf: docFromSomewhere,
|
|
57
|
-
* nodeDataProviders: [emojiNodeDataProvider],
|
|
58
|
-
* signal: AbortSignal.timeout(5000),
|
|
59
|
-
* });
|
|
60
|
-
* emojiNodeDataProvider // { 'key': 'value' }
|
|
61
|
-
* ```
|
|
62
|
-
*
|
|
63
|
-
* ### Load an new provider with an existing cache
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```
|
|
67
|
-
* const provider1 = new ExampleNodeDataProvider();
|
|
68
|
-
* await buildCaches({adf, nodeDataProviders: [provider1]})
|
|
69
|
-
* provider1.cache // { 'key': 'value' }
|
|
70
|
-
*
|
|
71
|
-
* const provider2 = new ExampleNodeDataProvider({existingCache: provider1.cache});
|
|
72
|
-
* ```
|
|
73
|
-
*/
|
|
74
|
-
var NodeDataProvider = exports.NodeDataProvider = /*#__PURE__*/function () {
|
|
75
|
-
/**
|
|
76
|
-
* This takes a node and returns a key that can be used to cache the result of the resolve function.
|
|
77
|
-
*/
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* This returns the information required to render a node.
|
|
81
|
-
*
|
|
82
|
-
* If unresolvable, this method will throw an error.
|
|
83
|
-
*
|
|
84
|
-
* If signal is aborted, this method will return undefined.
|
|
85
|
-
*/
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* The adf node name this provider is responsible for.
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
function NodeDataProvider(_ref) {
|
|
92
|
-
var _ref$existingCache = _ref.existingCache,
|
|
93
|
-
existingCache = _ref$existingCache === void 0 ? {} : _ref$existingCache,
|
|
94
|
-
nodeName = _ref.nodeName,
|
|
95
|
-
nodeToKey = _ref.nodeToKey,
|
|
96
|
-
resolve = _ref.resolve;
|
|
97
|
-
(0, _classCallCheck2.default)(this, NodeDataProvider);
|
|
98
|
-
/**
|
|
99
|
-
* This is added to ease building types
|
|
100
|
-
*/
|
|
101
|
-
// @ts-ignore
|
|
102
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
103
|
-
(0, _defineProperty2.default)(this, "__node", {});
|
|
104
|
-
(0, _defineProperty2.default)(this, "pending", {});
|
|
105
|
-
this.__cache = existingCache;
|
|
106
|
-
this.nodeName = nodeName;
|
|
107
|
-
this.nodeToKey = nodeToKey;
|
|
108
|
-
this.resolve = resolve;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Updates the providers cache.
|
|
113
|
-
*
|
|
114
|
-
* Useful in scenarios such as SSR where the cache is built on the server and then passed to the client.
|
|
115
|
-
*
|
|
116
|
-
* Avoids the need to provide the cache to the constructor (to allow decoupling creation of node data providers from cache building),
|
|
117
|
-
* and allow for caching to be managed at a group level across multiple providers.
|
|
118
|
-
*
|
|
119
|
-
* This is not expected to be used by consumers, for internal consumption examples;
|
|
120
|
-
* @see {@link buildCaches}
|
|
121
|
-
*
|
|
122
|
-
*/
|
|
123
|
-
return (0, _createClass2.default)(NodeDataProvider, [{
|
|
124
|
-
key: "updateCache",
|
|
125
|
-
value: function updateCache(cache, options) {
|
|
126
|
-
switch (options.strategy) {
|
|
127
|
-
case 'merge-override':
|
|
128
|
-
this.__cache = _objectSpread(_objectSpread({}, this.__cache), cache);
|
|
129
|
-
return;
|
|
130
|
-
case 'replace':
|
|
131
|
-
this.__cache = cache;
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* This is the cache for the provider.
|
|
138
|
-
*/
|
|
139
|
-
}, {
|
|
140
|
-
key: "cache",
|
|
141
|
-
get: function get() {
|
|
142
|
-
return this.__cache;
|
|
143
|
-
}
|
|
144
|
-
}, {
|
|
145
|
-
key: "get",
|
|
146
|
-
value: function get(node, _) {
|
|
147
|
-
var _this = this;
|
|
148
|
-
var key = this.nodeToKey(node);
|
|
149
|
-
var cached = this.cache[key];
|
|
150
|
-
if (cached) {
|
|
151
|
-
return cached;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// Get could be called from a variety of sources;
|
|
155
|
-
// - a Node Data Provider
|
|
156
|
-
// - a cache build
|
|
157
|
-
// - something else
|
|
158
|
-
//
|
|
159
|
-
// We want to avoid triggering multiple resolves for the same node -- so we keep track of pending requests
|
|
160
|
-
// and share them across any overlapping gets.
|
|
161
|
-
//
|
|
162
|
-
// When a get is cancelled -- we only want to cancel the shared resolve if all signals are aborted
|
|
163
|
-
// so we keep track of all signals that are not aborted.
|
|
164
|
-
|
|
165
|
-
var originalSignal = (_ === null || _ === void 0 ? void 0 : _.signal) || new AbortController().signal;
|
|
166
|
-
if (!this.pending[key]) {
|
|
167
|
-
var abortController = new AbortController();
|
|
168
|
-
this.pending[key] = {
|
|
169
|
-
// eslint-disable-next-line no-async-promise-executor
|
|
170
|
-
resolving: new Promise( /*#__PURE__*/function () {
|
|
171
|
-
var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(res, rej) {
|
|
172
|
-
var result;
|
|
173
|
-
return _regenerator.default.wrap(function _callee$(_context) {
|
|
174
|
-
while (1) switch (_context.prev = _context.next) {
|
|
175
|
-
case 0:
|
|
176
|
-
_context.prev = 0;
|
|
177
|
-
_context.next = 3;
|
|
178
|
-
return _this.resolve(node, {
|
|
179
|
-
signal: abortController.signal
|
|
180
|
-
}).catch(function (res) {
|
|
181
|
-
return res(undefined);
|
|
182
|
-
});
|
|
183
|
-
case 3:
|
|
184
|
-
result = _context.sent;
|
|
185
|
-
res(result);
|
|
186
|
-
_context.next = 10;
|
|
187
|
-
break;
|
|
188
|
-
case 7:
|
|
189
|
-
_context.prev = 7;
|
|
190
|
-
_context.t0 = _context["catch"](0);
|
|
191
|
-
res(undefined);
|
|
192
|
-
case 10:
|
|
193
|
-
case "end":
|
|
194
|
-
return _context.stop();
|
|
195
|
-
}
|
|
196
|
-
}, _callee, null, [[0, 7]]);
|
|
197
|
-
}));
|
|
198
|
-
return function (_x, _x2) {
|
|
199
|
-
return _ref2.apply(this, arguments);
|
|
200
|
-
};
|
|
201
|
-
}()),
|
|
202
|
-
abortController: abortController,
|
|
203
|
-
activeSignals: [originalSignal]
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
var handleAbort = function handleAbort() {
|
|
207
|
-
_this.pending[key].activeSignals = _this.pending[key].activeSignals.filter(function (activeSignal) {
|
|
208
|
-
return activeSignal !== originalSignal;
|
|
209
|
-
});
|
|
210
|
-
if (_this.pending[key].activeSignals.length === 0) {
|
|
211
|
-
// abort the resolution if all signals are aborted
|
|
212
|
-
_this.pending[key].abortController.abort();
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
|
|
217
|
-
originalSignal === null || originalSignal === void 0 || originalSignal.addEventListener('abort', handleAbort);
|
|
218
|
-
this.pending[key].resolving.then(function (resolvedValue) {
|
|
219
|
-
if (resolvedValue) {
|
|
220
|
-
_this.cache[key] = resolvedValue;
|
|
221
|
-
}
|
|
222
|
-
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
|
|
223
|
-
originalSignal === null || originalSignal === void 0 || originalSignal.removeEventListener('abort', handleAbort);
|
|
224
|
-
return resolvedValue;
|
|
225
|
-
});
|
|
226
|
-
return this.pending[key].resolving;
|
|
227
|
-
}
|
|
228
|
-
}]);
|
|
229
|
-
}(); // The purpose of this type is to ensure that either a DocNode or a PMNode is passed in
|
|
230
|
-
// to the provider.
|
|
231
|
-
// It is not opinionated about which nodes are used, so `any` is used here to allow
|
|
232
|
-
// compatibility with both DocNodes and PMNodes.
|
|
1
|
+
"use strict";
|
package/dist/es2019/index.js
CHANGED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
|
-
/**
|
|
3
|
-
* This is the base class for creating a node data provider for an editor plugin.
|
|
4
|
-
*
|
|
5
|
-
* ## Usage
|
|
6
|
-
*
|
|
7
|
-
* ### Create a provider
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* class EmojiNodeDataProvider extends NodeDataProvider<
|
|
12
|
-
* { attrs: EmojiAttributes },
|
|
13
|
-
* { resolvedData: string }
|
|
14
|
-
* > {
|
|
15
|
-
* constructor({ existingCache }?: { existingCache: Record<string, { resolvedData: string }> }) {
|
|
16
|
-
* super({ existingCache, nodeName: 'emoji' });
|
|
17
|
-
* }
|
|
18
|
-
* nodeToKey(node: { attrs: EmojiAttributes }): string {
|
|
19
|
-
* return `${node.attrs.shortName}-${node.attrs.text}-${node.attrs.id}`;
|
|
20
|
-
* }
|
|
21
|
-
* resolve(node: { attrs: EmojiAttributes }, _?: { signal: AbortSignal }) {
|
|
22
|
-
* return Promise.resolve({ resolvedData: 'resolved' });
|
|
23
|
-
* }
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* ### Use the provider
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```ts
|
|
31
|
-
* const emojiNodeDataProvider = new EmojiNodeDataProvider();
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* ### Caching
|
|
35
|
-
*
|
|
36
|
-
* @see {@link buildCaches} for more information on building caches.
|
|
37
|
-
*
|
|
38
|
-
* #### Load an existing provider with a cache
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```
|
|
42
|
-
* await buildCaches({
|
|
43
|
-
* adf: docFromSomewhere,
|
|
44
|
-
* nodeDataProviders: [emojiNodeDataProvider],
|
|
45
|
-
* signal: AbortSignal.timeout(5000),
|
|
46
|
-
* });
|
|
47
|
-
* emojiNodeDataProvider // { 'key': 'value' }
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
50
|
-
* ### Load an new provider with an existing cache
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```
|
|
54
|
-
* const provider1 = new ExampleNodeDataProvider();
|
|
55
|
-
* await buildCaches({adf, nodeDataProviders: [provider1]})
|
|
56
|
-
* provider1.cache // { 'key': 'value' }
|
|
57
|
-
*
|
|
58
|
-
* const provider2 = new ExampleNodeDataProvider({existingCache: provider1.cache});
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
export class NodeDataProvider {
|
|
62
|
-
/**
|
|
63
|
-
* This takes a node and returns a key that can be used to cache the result of the resolve function.
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* This returns the information required to render a node.
|
|
68
|
-
*
|
|
69
|
-
* If unresolvable, this method will throw an error.
|
|
70
|
-
*
|
|
71
|
-
* If signal is aborted, this method will return undefined.
|
|
72
|
-
*/
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* The adf node name this provider is responsible for.
|
|
76
|
-
*/
|
|
77
|
-
|
|
78
|
-
constructor({
|
|
79
|
-
existingCache = {},
|
|
80
|
-
nodeName,
|
|
81
|
-
nodeToKey,
|
|
82
|
-
resolve
|
|
83
|
-
}) {
|
|
84
|
-
/**
|
|
85
|
-
* This is added to ease building types
|
|
86
|
-
*/
|
|
87
|
-
// @ts-ignore
|
|
88
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
89
|
-
_defineProperty(this, "__node", {});
|
|
90
|
-
_defineProperty(this, "pending", {});
|
|
91
|
-
this.__cache = existingCache;
|
|
92
|
-
this.nodeName = nodeName;
|
|
93
|
-
this.nodeToKey = nodeToKey;
|
|
94
|
-
this.resolve = resolve;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Updates the providers cache.
|
|
99
|
-
*
|
|
100
|
-
* Useful in scenarios such as SSR where the cache is built on the server and then passed to the client.
|
|
101
|
-
*
|
|
102
|
-
* Avoids the need to provide the cache to the constructor (to allow decoupling creation of node data providers from cache building),
|
|
103
|
-
* and allow for caching to be managed at a group level across multiple providers.
|
|
104
|
-
*
|
|
105
|
-
* This is not expected to be used by consumers, for internal consumption examples;
|
|
106
|
-
* @see {@link buildCaches}
|
|
107
|
-
*
|
|
108
|
-
*/
|
|
109
|
-
updateCache(cache, options) {
|
|
110
|
-
switch (options.strategy) {
|
|
111
|
-
case 'merge-override':
|
|
112
|
-
this.__cache = {
|
|
113
|
-
...this.__cache,
|
|
114
|
-
...cache
|
|
115
|
-
};
|
|
116
|
-
return;
|
|
117
|
-
case 'replace':
|
|
118
|
-
this.__cache = cache;
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* This is the cache for the provider.
|
|
125
|
-
*/
|
|
126
|
-
get cache() {
|
|
127
|
-
return this.__cache;
|
|
128
|
-
}
|
|
129
|
-
get(node, _) {
|
|
130
|
-
const key = this.nodeToKey(node);
|
|
131
|
-
const cached = this.cache[key];
|
|
132
|
-
if (cached) {
|
|
133
|
-
return cached;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Get could be called from a variety of sources;
|
|
137
|
-
// - a Node Data Provider
|
|
138
|
-
// - a cache build
|
|
139
|
-
// - something else
|
|
140
|
-
//
|
|
141
|
-
// We want to avoid triggering multiple resolves for the same node -- so we keep track of pending requests
|
|
142
|
-
// and share them across any overlapping gets.
|
|
143
|
-
//
|
|
144
|
-
// When a get is cancelled -- we only want to cancel the shared resolve if all signals are aborted
|
|
145
|
-
// so we keep track of all signals that are not aborted.
|
|
146
|
-
|
|
147
|
-
const originalSignal = (_ === null || _ === void 0 ? void 0 : _.signal) || new AbortController().signal;
|
|
148
|
-
if (!this.pending[key]) {
|
|
149
|
-
const abortController = new AbortController();
|
|
150
|
-
this.pending[key] = {
|
|
151
|
-
// eslint-disable-next-line no-async-promise-executor
|
|
152
|
-
resolving: new Promise(async (res, rej) => {
|
|
153
|
-
try {
|
|
154
|
-
const result = await this.resolve(node, {
|
|
155
|
-
signal: abortController.signal
|
|
156
|
-
}).catch(res => res(undefined));
|
|
157
|
-
res(result);
|
|
158
|
-
} catch {
|
|
159
|
-
res(undefined);
|
|
160
|
-
}
|
|
161
|
-
}),
|
|
162
|
-
abortController,
|
|
163
|
-
activeSignals: [originalSignal]
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
const handleAbort = () => {
|
|
167
|
-
this.pending[key].activeSignals = this.pending[key].activeSignals.filter(activeSignal => activeSignal !== originalSignal);
|
|
168
|
-
if (this.pending[key].activeSignals.length === 0) {
|
|
169
|
-
// abort the resolution if all signals are aborted
|
|
170
|
-
this.pending[key].abortController.abort();
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
|
|
175
|
-
originalSignal === null || originalSignal === void 0 ? void 0 : originalSignal.addEventListener('abort', handleAbort);
|
|
176
|
-
this.pending[key].resolving.then(resolvedValue => {
|
|
177
|
-
if (resolvedValue) {
|
|
178
|
-
this.cache[key] = resolvedValue;
|
|
179
|
-
}
|
|
180
|
-
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
|
|
181
|
-
originalSignal === null || originalSignal === void 0 ? void 0 : originalSignal.removeEventListener('abort', handleAbort);
|
|
182
|
-
return resolvedValue;
|
|
183
|
-
});
|
|
184
|
-
return this.pending[key].resolving;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// The purpose of this type is to ensure that either a DocNode or a PMNode is passed in
|
|
189
|
-
// to the provider.
|
|
190
|
-
// It is not opinionated about which nodes are used, so `any` is used here to allow
|
|
191
|
-
// compatibility with both DocNodes and PMNodes.
|
package/dist/esm/index.js
CHANGED
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
2
|
-
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
3
|
-
import _createClass from "@babel/runtime/helpers/createClass";
|
|
4
|
-
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
5
|
-
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
6
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
7
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
8
|
-
/**
|
|
9
|
-
* This is the base class for creating a node data provider for an editor plugin.
|
|
10
|
-
*
|
|
11
|
-
* ## Usage
|
|
12
|
-
*
|
|
13
|
-
* ### Create a provider
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* class EmojiNodeDataProvider extends NodeDataProvider<
|
|
18
|
-
* { attrs: EmojiAttributes },
|
|
19
|
-
* { resolvedData: string }
|
|
20
|
-
* > {
|
|
21
|
-
* constructor({ existingCache }?: { existingCache: Record<string, { resolvedData: string }> }) {
|
|
22
|
-
* super({ existingCache, nodeName: 'emoji' });
|
|
23
|
-
* }
|
|
24
|
-
* nodeToKey(node: { attrs: EmojiAttributes }): string {
|
|
25
|
-
* return `${node.attrs.shortName}-${node.attrs.text}-${node.attrs.id}`;
|
|
26
|
-
* }
|
|
27
|
-
* resolve(node: { attrs: EmojiAttributes }, _?: { signal: AbortSignal }) {
|
|
28
|
-
* return Promise.resolve({ resolvedData: 'resolved' });
|
|
29
|
-
* }
|
|
30
|
-
* }
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* ### Use the provider
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```ts
|
|
37
|
-
* const emojiNodeDataProvider = new EmojiNodeDataProvider();
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* ### Caching
|
|
41
|
-
*
|
|
42
|
-
* @see {@link buildCaches} for more information on building caches.
|
|
43
|
-
*
|
|
44
|
-
* #### Load an existing provider with a cache
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```
|
|
48
|
-
* await buildCaches({
|
|
49
|
-
* adf: docFromSomewhere,
|
|
50
|
-
* nodeDataProviders: [emojiNodeDataProvider],
|
|
51
|
-
* signal: AbortSignal.timeout(5000),
|
|
52
|
-
* });
|
|
53
|
-
* emojiNodeDataProvider // { 'key': 'value' }
|
|
54
|
-
* ```
|
|
55
|
-
*
|
|
56
|
-
* ### Load an new provider with an existing cache
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```
|
|
60
|
-
* const provider1 = new ExampleNodeDataProvider();
|
|
61
|
-
* await buildCaches({adf, nodeDataProviders: [provider1]})
|
|
62
|
-
* provider1.cache // { 'key': 'value' }
|
|
63
|
-
*
|
|
64
|
-
* const provider2 = new ExampleNodeDataProvider({existingCache: provider1.cache});
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export var NodeDataProvider = /*#__PURE__*/function () {
|
|
68
|
-
/**
|
|
69
|
-
* This takes a node and returns a key that can be used to cache the result of the resolve function.
|
|
70
|
-
*/
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* This returns the information required to render a node.
|
|
74
|
-
*
|
|
75
|
-
* If unresolvable, this method will throw an error.
|
|
76
|
-
*
|
|
77
|
-
* If signal is aborted, this method will return undefined.
|
|
78
|
-
*/
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* The adf node name this provider is responsible for.
|
|
82
|
-
*/
|
|
83
|
-
|
|
84
|
-
function NodeDataProvider(_ref) {
|
|
85
|
-
var _ref$existingCache = _ref.existingCache,
|
|
86
|
-
existingCache = _ref$existingCache === void 0 ? {} : _ref$existingCache,
|
|
87
|
-
nodeName = _ref.nodeName,
|
|
88
|
-
nodeToKey = _ref.nodeToKey,
|
|
89
|
-
resolve = _ref.resolve;
|
|
90
|
-
_classCallCheck(this, NodeDataProvider);
|
|
91
|
-
/**
|
|
92
|
-
* This is added to ease building types
|
|
93
|
-
*/
|
|
94
|
-
// @ts-ignore
|
|
95
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
96
|
-
_defineProperty(this, "__node", {});
|
|
97
|
-
_defineProperty(this, "pending", {});
|
|
98
|
-
this.__cache = existingCache;
|
|
99
|
-
this.nodeName = nodeName;
|
|
100
|
-
this.nodeToKey = nodeToKey;
|
|
101
|
-
this.resolve = resolve;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Updates the providers cache.
|
|
106
|
-
*
|
|
107
|
-
* Useful in scenarios such as SSR where the cache is built on the server and then passed to the client.
|
|
108
|
-
*
|
|
109
|
-
* Avoids the need to provide the cache to the constructor (to allow decoupling creation of node data providers from cache building),
|
|
110
|
-
* and allow for caching to be managed at a group level across multiple providers.
|
|
111
|
-
*
|
|
112
|
-
* This is not expected to be used by consumers, for internal consumption examples;
|
|
113
|
-
* @see {@link buildCaches}
|
|
114
|
-
*
|
|
115
|
-
*/
|
|
116
|
-
return _createClass(NodeDataProvider, [{
|
|
117
|
-
key: "updateCache",
|
|
118
|
-
value: function updateCache(cache, options) {
|
|
119
|
-
switch (options.strategy) {
|
|
120
|
-
case 'merge-override':
|
|
121
|
-
this.__cache = _objectSpread(_objectSpread({}, this.__cache), cache);
|
|
122
|
-
return;
|
|
123
|
-
case 'replace':
|
|
124
|
-
this.__cache = cache;
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* This is the cache for the provider.
|
|
131
|
-
*/
|
|
132
|
-
}, {
|
|
133
|
-
key: "cache",
|
|
134
|
-
get: function get() {
|
|
135
|
-
return this.__cache;
|
|
136
|
-
}
|
|
137
|
-
}, {
|
|
138
|
-
key: "get",
|
|
139
|
-
value: function get(node, _) {
|
|
140
|
-
var _this = this;
|
|
141
|
-
var key = this.nodeToKey(node);
|
|
142
|
-
var cached = this.cache[key];
|
|
143
|
-
if (cached) {
|
|
144
|
-
return cached;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Get could be called from a variety of sources;
|
|
148
|
-
// - a Node Data Provider
|
|
149
|
-
// - a cache build
|
|
150
|
-
// - something else
|
|
151
|
-
//
|
|
152
|
-
// We want to avoid triggering multiple resolves for the same node -- so we keep track of pending requests
|
|
153
|
-
// and share them across any overlapping gets.
|
|
154
|
-
//
|
|
155
|
-
// When a get is cancelled -- we only want to cancel the shared resolve if all signals are aborted
|
|
156
|
-
// so we keep track of all signals that are not aborted.
|
|
157
|
-
|
|
158
|
-
var originalSignal = (_ === null || _ === void 0 ? void 0 : _.signal) || new AbortController().signal;
|
|
159
|
-
if (!this.pending[key]) {
|
|
160
|
-
var abortController = new AbortController();
|
|
161
|
-
this.pending[key] = {
|
|
162
|
-
// eslint-disable-next-line no-async-promise-executor
|
|
163
|
-
resolving: new Promise( /*#__PURE__*/function () {
|
|
164
|
-
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(res, rej) {
|
|
165
|
-
var result;
|
|
166
|
-
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
167
|
-
while (1) switch (_context.prev = _context.next) {
|
|
168
|
-
case 0:
|
|
169
|
-
_context.prev = 0;
|
|
170
|
-
_context.next = 3;
|
|
171
|
-
return _this.resolve(node, {
|
|
172
|
-
signal: abortController.signal
|
|
173
|
-
}).catch(function (res) {
|
|
174
|
-
return res(undefined);
|
|
175
|
-
});
|
|
176
|
-
case 3:
|
|
177
|
-
result = _context.sent;
|
|
178
|
-
res(result);
|
|
179
|
-
_context.next = 10;
|
|
180
|
-
break;
|
|
181
|
-
case 7:
|
|
182
|
-
_context.prev = 7;
|
|
183
|
-
_context.t0 = _context["catch"](0);
|
|
184
|
-
res(undefined);
|
|
185
|
-
case 10:
|
|
186
|
-
case "end":
|
|
187
|
-
return _context.stop();
|
|
188
|
-
}
|
|
189
|
-
}, _callee, null, [[0, 7]]);
|
|
190
|
-
}));
|
|
191
|
-
return function (_x, _x2) {
|
|
192
|
-
return _ref2.apply(this, arguments);
|
|
193
|
-
};
|
|
194
|
-
}()),
|
|
195
|
-
abortController: abortController,
|
|
196
|
-
activeSignals: [originalSignal]
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
var handleAbort = function handleAbort() {
|
|
200
|
-
_this.pending[key].activeSignals = _this.pending[key].activeSignals.filter(function (activeSignal) {
|
|
201
|
-
return activeSignal !== originalSignal;
|
|
202
|
-
});
|
|
203
|
-
if (_this.pending[key].activeSignals.length === 0) {
|
|
204
|
-
// abort the resolution if all signals are aborted
|
|
205
|
-
_this.pending[key].abortController.abort();
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
|
|
210
|
-
originalSignal === null || originalSignal === void 0 || originalSignal.addEventListener('abort', handleAbort);
|
|
211
|
-
this.pending[key].resolving.then(function (resolvedValue) {
|
|
212
|
-
if (resolvedValue) {
|
|
213
|
-
_this.cache[key] = resolvedValue;
|
|
214
|
-
}
|
|
215
|
-
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
|
|
216
|
-
originalSignal === null || originalSignal === void 0 || originalSignal.removeEventListener('abort', handleAbort);
|
|
217
|
-
return resolvedValue;
|
|
218
|
-
});
|
|
219
|
-
return this.pending[key].resolving;
|
|
220
|
-
}
|
|
221
|
-
}]);
|
|
222
|
-
}();
|
|
223
|
-
|
|
224
|
-
// The purpose of this type is to ensure that either a DocNode or a PMNode is passed in
|
|
225
|
-
// to the provider.
|
|
226
|
-
// It is not opinionated about which nodes are used, so `any` is used here to allow
|
|
227
|
-
// compatibility with both DocNodes and PMNodes.
|