vestjs-runtime 0.1.0-dev-ae6b14 → 0.1.0-next-25c20e

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/README.md CHANGED
@@ -1,3 +1,3 @@
1
1
  # vestjs-runtime
2
2
 
3
- Internal runtime module used by Vest. This module is not intended to be used directly, but rather used by the `vest` package.
3
+ Internal Runtime module used by Vest. This module is not intended to be used directly, but rather used by the `vest` package.
@@ -3,6 +3,37 @@
3
3
  var vestUtils = require('vest-utils');
4
4
  var context = require('context');
5
5
 
6
+ class IsolateMutator {
7
+ static setParent(isolate, parent) {
8
+ isolate.parent = parent;
9
+ return isolate;
10
+ }
11
+ static saveOutput(isolate, output) {
12
+ isolate.output = output;
13
+ return isolate;
14
+ }
15
+ static setKey(isolate, key) {
16
+ isolate.key = key;
17
+ return isolate;
18
+ }
19
+ static addChild(isolate, child) {
20
+ vestUtils.invariant(isolate.children);
21
+ isolate.children.push(child);
22
+ IsolateMutator.setParent(child, isolate);
23
+ }
24
+ static removeChild(isolate, node) {
25
+ var _a, _b;
26
+ isolate.children =
27
+ (_b = (_a = isolate.children) === null || _a === void 0 ? void 0 : _a.filter(child => child !== node)) !== null && _b !== void 0 ? _b : null;
28
+ }
29
+ static slice(isolate, at) {
30
+ if (vestUtils.isNullish(isolate.children)) {
31
+ return;
32
+ }
33
+ isolate.children.length = at;
34
+ }
35
+ }
36
+
6
37
  // eslint-disable-next-line
7
38
  function walk(startNode, callback, visitOnly) {
8
39
  // If the startNode has no children, there is nothing to walk.
@@ -84,7 +115,7 @@ function every(startNode, predicate, visitOnly) {
84
115
  function pluck(startNode, predicate, visitOnly) {
85
116
  walk(startNode, node => {
86
117
  if (predicate(node) && node.parent) {
87
- node.parent.removeChild(node);
118
+ IsolateMutator.removeChild(node.parent, node);
88
119
  }
89
120
  }, visitOnly);
90
121
  }
@@ -92,12 +123,12 @@ function pluck(startNode, predicate, visitOnly) {
92
123
  //startNode that satisfies the given predicate function.
93
124
  function closest(startNode, predicate) {
94
125
  let current = startNode;
95
- while (current.parent) {
126
+ do {
96
127
  if (predicate(current)) {
97
128
  return current;
98
129
  }
99
130
  current = current.parent;
100
- }
131
+ } while (current);
101
132
  return null;
102
133
  }
103
134
  // This function returns true if the closest ancestor Isolates of the
@@ -118,6 +149,46 @@ var IsolateWalker = /*#__PURE__*/Object.freeze({
118
149
  walk: walk
119
150
  });
120
151
 
152
+ class IsolateInspector {
153
+ static at(isolate, at) {
154
+ var _a, _b;
155
+ if (vestUtils.isNullish(isolate)) {
156
+ return null;
157
+ }
158
+ return (_b = (_a = isolate.children) === null || _a === void 0 ? void 0 : _a[at]) !== null && _b !== void 0 ? _b : null;
159
+ }
160
+ static cursor(isolate) {
161
+ var _a, _b;
162
+ if (vestUtils.isNullish(isolate)) {
163
+ return 0;
164
+ }
165
+ return (_b = (_a = isolate.children) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
166
+ }
167
+ static shouldAllowReorder(isolate) {
168
+ if (vestUtils.isNullish(isolate)) {
169
+ return false;
170
+ }
171
+ return closestExists(isolate, node => node.allowReorder);
172
+ }
173
+ static usesKey(isolate) {
174
+ if (vestUtils.isNullish(isolate)) {
175
+ return false;
176
+ }
177
+ return vestUtils.isNotNullish(isolate.key);
178
+ }
179
+ static dump(isolate) {
180
+ if (vestUtils.isNullish(isolate)) {
181
+ return '';
182
+ }
183
+ return JSON.stringify(isolate, (key, value) => {
184
+ if (key === 'parent') {
185
+ return undefined;
186
+ }
187
+ return value;
188
+ });
189
+ }
190
+ }
191
+
121
192
  var ErrorStrings;
122
193
  (function (ErrorStrings) {
123
194
  ErrorStrings["NO_ACTIVE_ISOLATE"] = "Not within an active isolate";
@@ -142,30 +213,14 @@ const PersistedContext = context.createCascade((stateRef, parentContext) => {
142
213
  const Run = PersistedContext.run;
143
214
  const RuntimeApi = {
144
215
  Run,
216
+ addNodeToHistory,
145
217
  createRef,
146
218
  persist,
147
219
  reset,
148
220
  useAvailableRoot,
149
- useBus,
150
221
  useCurrentCursor,
151
- useEmit,
152
- usePrepareEmitter,
153
222
  useXAppData,
154
223
  };
155
- function useBus() {
156
- return useX().stateRef.Bus;
157
- }
158
- /*
159
- Returns an emitter, but it also has a shortcut for emitting an event immediately
160
- by passing an event name.
161
- */
162
- function useEmit() {
163
- return persist(useBus().emit);
164
- }
165
- function usePrepareEmitter(event) {
166
- const emit = useEmit();
167
- return (arg) => emit(event, arg);
168
- }
169
224
  function useXAppData() {
170
225
  return useX().stateRef.appData;
171
226
  }
@@ -193,6 +248,16 @@ function useHistoryRoot() {
193
248
  function useHistoryNode() {
194
249
  return useX().historyNode;
195
250
  }
251
+ function addNodeToHistory(node) {
252
+ const parent = useIsolate();
253
+ if (parent) {
254
+ useSetNextIsolateChild(node);
255
+ }
256
+ else {
257
+ useSetHistory(node);
258
+ }
259
+ IsolateMutator.setParent(node, parent);
260
+ }
196
261
  function useSetHistory(history) {
197
262
  const [, setHistoryRoot] = useHistoryRoot();
198
263
  setHistoryRoot(history);
@@ -210,8 +275,8 @@ function useIsolate() {
210
275
  return (_a = useX().runtimeNode) !== null && _a !== void 0 ? _a : null;
211
276
  }
212
277
  function useCurrentCursor() {
213
- var _a, _b;
214
- return (_b = (_a = useIsolate()) === null || _a === void 0 ? void 0 : _a.cursor()) !== null && _b !== void 0 ? _b : 0;
278
+ const isolate = useIsolate();
279
+ return isolate ? IsolateInspector.cursor(isolate) : 0;
215
280
  }
216
281
  function useRuntimeRoot() {
217
282
  return useX().runtimeRoot;
@@ -219,7 +284,7 @@ function useRuntimeRoot() {
219
284
  function useSetNextIsolateChild(child) {
220
285
  const currentIsolate = useIsolate();
221
286
  vestUtils.invariant(currentIsolate, ErrorStrings.NO_ACTIVE_ISOLATE);
222
- currentIsolate.addChild(child);
287
+ IsolateMutator.addChild(currentIsolate, child);
223
288
  }
224
289
  function useSetIsolateKey(key, value) {
225
290
  if (!key) {
@@ -254,15 +319,13 @@ function BaseReconciler(currentNode, historicNode) {
254
319
  }
255
320
  class Reconciler {
256
321
  static reconcile(reconciler, node, callback) {
257
- var _a;
258
322
  const parent = useIsolate();
259
323
  const historyNode = useHistoryNode();
260
324
  let localHistoryNode = historyNode;
261
325
  if (parent) {
262
326
  // If we have a parent, we need to get the history node from the parent's children
263
327
  // We take the history node from the cursor of the active node's children
264
- localHistoryNode =
265
- (_a = historyNode === null || historyNode === void 0 ? void 0 : historyNode.at(useCurrentCursor())) !== null && _a !== void 0 ? _a : null;
328
+ localHistoryNode = IsolateInspector.at(historyNode, IsolateInspector.cursor(parent));
266
329
  }
267
330
  const nextNode = reconciler(node, localHistoryNode);
268
331
  vestUtils.invariant(nextNode);
@@ -277,12 +340,13 @@ class Reconciler {
277
340
  if (!historyNode || !testIsolate) {
278
341
  // This is probably unreachable, but TS is not convinced.
279
342
  // Let's play it safe.
343
+ /* istanbul ignore next */
280
344
  return;
281
345
  }
282
- historyNode.slice(useCurrentCursor());
346
+ IsolateMutator.slice(historyNode, IsolateInspector.cursor(testIsolate));
283
347
  }
284
348
  static handleIsolateNodeWithKey(node) {
285
- vestUtils.invariant(node.usesKey());
349
+ vestUtils.invariant(IsolateInspector.usesKey(node));
286
350
  const prevNodeByKey = useHistoryKey(node.key);
287
351
  let nextNode = node;
288
352
  if (!vestUtils.isNullish(prevNodeByKey)) {
@@ -309,80 +373,49 @@ class Isolate {
309
373
  this.parent = null;
310
374
  this.key = null;
311
375
  this.allowReorder = false;
312
- }
313
- setParent(parent) {
314
- this.parent = parent;
315
- return this;
316
- }
317
- saveOutput(output) {
318
- this.output = output;
319
- return this;
320
- }
321
- setKey(key) {
322
- this.key = key;
323
- return this;
324
- }
325
- usesKey() {
326
- return vestUtils.isNotNullish(this.key);
327
- }
328
- addChild(child) {
329
- vestUtils.invariant(this.children);
330
- this.children.push(child);
331
- }
332
- removeChild(node) {
333
- var _a, _b;
334
- this.children = (_b = (_a = this.children) === null || _a === void 0 ? void 0 : _a.filter(child => child !== node)) !== null && _b !== void 0 ? _b : null;
335
- }
336
- slice(at) {
337
- if (vestUtils.isNullish(this.children)) {
338
- return;
339
- }
340
- this.children.length = at;
341
- }
342
- at(at) {
343
- var _a, _b;
344
- return (_b = (_a = this.children) === null || _a === void 0 ? void 0 : _a[at]) !== null && _b !== void 0 ? _b : null;
345
- }
346
- cursor() {
347
- var _a, _b;
348
- return (_b = (_a = this.children) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
349
- }
350
- shouldAllowReorder() {
351
- var _a;
352
- return (_a = closestExists(this, node => node.allowReorder)) !== null && _a !== void 0 ? _a : false;
353
- }
354
- get rootNode() {
355
- var _a;
356
- return (_a = closest(this, node => vestUtils.isNullish(node.parent))) !== null && _a !== void 0 ? _a : this;
376
+ this.type = 'Isolate';
357
377
  }
358
378
  static create(callback, data) {
359
- return this.createImplementation(callback, data);
360
- }
361
- static createImplementation(callback, data) {
362
379
  const parent = useIsolate();
363
- const newCreatedNode = new this(data).setParent(parent);
380
+ const newCreatedNode = IsolateMutator.setParent(new this(data), parent);
364
381
  const [nextIsolateChild, output] = Reconciler.reconcile(this.reconciler, newCreatedNode, callback);
365
- nextIsolateChild.saveOutput(output);
366
- this.setNode(nextIsolateChild);
382
+ IsolateMutator.saveOutput(nextIsolateChild, output);
383
+ addNodeToHistory(nextIsolateChild);
367
384
  return nextIsolateChild;
368
385
  }
369
- static setNode(node) {
370
- const parent = useIsolate();
371
- if (parent) {
372
- useSetNextIsolateChild(node);
373
- }
374
- else {
375
- useSetHistory(node);
376
- }
377
- node.setParent(parent);
378
- }
379
- static is(node) {
380
- return node instanceof Isolate;
381
- }
382
386
  }
383
387
  Isolate.reconciler = BaseReconciler;
384
388
 
389
+ function useBus() {
390
+ return useX().stateRef.Bus;
391
+ }
392
+ /*
393
+ Returns an emitter, but it also has a shortcut for emitting an event immediately
394
+ by passing an event name.
395
+ */
396
+ function useEmit(event, data) {
397
+ const emit = useBus().emit;
398
+ if (!vestUtils.isNullish(event)) {
399
+ emit(event, data);
400
+ }
401
+ return persist(emit);
402
+ }
403
+ function usePrepareEmitter(event) {
404
+ const emit = useEmit();
405
+ return (arg) => emit(event, arg);
406
+ }
407
+
408
+ var Bus = /*#__PURE__*/Object.freeze({
409
+ __proto__: null,
410
+ useBus: useBus,
411
+ useEmit: useEmit,
412
+ usePrepareEmitter: usePrepareEmitter
413
+ });
414
+
415
+ exports.Bus = Bus;
385
416
  exports.Isolate = Isolate;
417
+ exports.IsolateInspector = IsolateInspector;
418
+ exports.IsolateMutator = IsolateMutator;
386
419
  exports.Reconciler = Reconciler;
387
420
  exports.VestRuntime = RuntimeApi;
388
421
  exports.Walker = IsolateWalker;
@@ -1,5 +1,4 @@
1
1
  'use strict'
2
-
3
2
  if (process.env.NODE_ENV === 'production') {
4
3
  module.exports = require('./vestjs-runtime.production.js');
5
4
  } else {
@@ -1 +1 @@
1
- "use strict";var t=require("vest-utils"),e=require("context");function n(e,r,i){if(t.isNullish(e.children))return;let s=!1;for(const o of e.children){if(s)return;if((t.isNullish(i)||t.optionalFunctionValue(i,o))&&r(o,u),s)return;n(o,((t,e)=>{r(t,(()=>{e(),u()}))}),i)}function u(){s=!0}}function r(t,e,r){let i=!1;return n(t,((t,n)=>{e(t)&&(n(),i=!0)}),r),i}function i(t,e){let n=t;for(;n.parent;){if(e(n))return n;n=n.parent}return null}function s(t,e){return!!i(t,e)}var u,o=Object.freeze({__proto__:null,closest:i,closestExists:s,every:function(t,e,r){let i=!0;return n(t,((t,n)=>{e(t)||(n(),i=!1)}),r),i},find:function(t,e,r){let i=null;return n(t,((t,n)=>{e(t)&&(n(),i=t)}),r),i},has:function(t,e){return r(t,(()=>!0),e)},pluck:function(t,e,r){n(t,(t=>{e(t)&&t.parent&&t.parent.removeChild(t)}),r)},some:r,walk:n});!function(t){t.NO_ACTIVE_ISOLATE="Not within an active isolate",t.ENCOUNTERED_THE_SAME_KEY_TWICE='Encountered the same test key "{key}" twice. This may lead to tests overriding each other\'s results, or to tests being unexpectedly omitted.'}(u||(u={}));const l=e.createCascade(((e,n)=>{if(n)return null;t.invariant(e.historyRoot);const[r]=e.historyRoot(),i={};return t.assign(i,{historyNode:r,runtimeNode:null,runtimeRoot:null,stateRef:e}),i})),c=l.run,a={Run:c,createRef:function(e){return Object.freeze({historyRoot:t.tinyState.createTinyState(null),Bus:t.bus.createBus(),appData:t.optionalFunctionValue(e)})},persist:f,reset:function(){const[,,t]=p();t()},useAvailableRoot:function(){const t=R();if(t)return t;const[e]=p();return e},useBus:h,useCurrentCursor:E,useEmit:d,usePrepareEmitter:function(t){const e=d();return n=>e(t,n)},useXAppData:function(){return v().stateRef.appData}};function h(){return v().stateRef.Bus}function d(){return f(h().emit)}function f(t){const e=l.useX();return(...n)=>{var r;const i=null!==(r=l.use())&&void 0!==r?r:e;return l.run(i.stateRef,(()=>t(...n)))}}function v(){return l.useX()}function p(){return v().stateRef.historyRoot()}function N(){return v().historyNode}function y(){var t;return null!==(t=v().runtimeNode)&&void 0!==t?t:null}function E(){var t,e;return null!==(e=null===(t=y())||void 0===t?void 0:t.cursor())&&void 0!==e?e:0}function R(){return v().runtimeRoot}class m{static reconcile(e,n,r){var i;const s=y(),u=N();let o=u;s&&(o=null!==(i=null==u?void 0:u.at(E()))&&void 0!==i?i:null);const l=e(n,o);return t.invariant(l),Object.is(l,n)?[n,_(o,n,r)]:[l,l.output]}static removeAllNextNodesInIsolate(){const t=y(),e=N();e&&t&&e.slice(E())}static handleIsolateNodeWithKey(e){t.invariant(e.usesKey());const n=function(e){var n;if(t.isNullish(e))return null;const r=v().historyNode;return null!==(n=null==r?void 0:r.keys[e])&&void 0!==n?n:null}(e.key);let r=e;return t.isNullish(n)||(r=n),function(e,n){if(!e)return;const r=y();t.invariant(r,u.NO_ACTIVE_ISOLATE),t.isNullish(r.keys[e])?r.keys[e]=n:t.deferThrow(t.text(u.ENCOUNTERED_THE_SAME_KEY_TWICE,{key:e}))}(e.key,e),r}}function _(t,e,n){const r=R(),i=c(Object.assign({historyNode:t,runtimeNode:e},!r&&{runtimeRoot:e}),(()=>n(e)));return e.output=i,i}class k{constructor(t){this.children=[],this.keys={},this.parent=null,this.key=null,this.allowReorder=!1}setParent(t){return this.parent=t,this}saveOutput(t){return this.output=t,this}setKey(t){return this.key=t,this}usesKey(){return t.isNotNullish(this.key)}addChild(e){t.invariant(this.children),this.children.push(e)}removeChild(t){var e,n;this.children=null!==(n=null===(e=this.children)||void 0===e?void 0:e.filter((e=>e!==t)))&&void 0!==n?n:null}slice(e){t.isNullish(this.children)||(this.children.length=e)}at(t){var e,n;return null!==(n=null===(e=this.children)||void 0===e?void 0:e[t])&&void 0!==n?n:null}cursor(){var t,e;return null!==(e=null===(t=this.children)||void 0===t?void 0:t.length)&&void 0!==e?e:0}shouldAllowReorder(){var t;return null!==(t=s(this,(t=>t.allowReorder)))&&void 0!==t&&t}get rootNode(){var e;return null!==(e=i(this,(e=>t.isNullish(e.parent))))&&void 0!==e?e:this}static create(t,e){return this.createImplementation(t,e)}static createImplementation(t,e){const n=y(),r=new this(e).setParent(n),[i,s]=m.reconcile(this.reconciler,r,t);return i.saveOutput(s),this.setNode(i),i}static setNode(e){const n=y();n?function(e){const n=y();t.invariant(n,u.NO_ACTIVE_ISOLATE),n.addChild(e)}(e):function(t){const[,e]=p();e(t)}(e),e.setParent(n)}static is(t){return t instanceof k}}k.reconciler=function(e,n){return t.isNullish(n),e},exports.Isolate=k,exports.Reconciler=m,exports.VestRuntime=a,exports.Walker=o;
1
+ "use strict";var t=require("vest-utils"),e=require("context");class n{static setParent(t,e){return t.parent=e,t}static saveOutput(t,e){return t.output=e,t}static setKey(t,e){return t.key=e,t}static addChild(e,r){t.invariant(e.children),e.children.push(r),n.setParent(r,e)}static removeChild(t,e){var n,r;t.children=null!==(r=null===(n=t.children)||void 0===n?void 0:n.filter((t=>t!==e)))&&void 0!==r?r:null}static slice(e,n){t.isNullish(e.children)||(e.children.length=n)}}function r(e,n,i){if(t.isNullish(e.children))return;let s=!1;for(const o of e.children){if(s)return;if((t.isNullish(i)||t.optionalFunctionValue(i,o))&&n(o,u),s)return;r(o,((t,e)=>{n(t,(()=>{e(),u()}))}),i)}function u(){s=!0}}function i(t,e,n){let i=!1;return r(t,((t,n)=>{e(t)&&(n(),i=!0)}),n),i}function s(t,e){let n=t;do{if(e(n))return n;n=n.parent}while(n);return null}function u(t,e){return!!s(t,e)}var o,l=Object.freeze({__proto__:null,closest:s,closestExists:u,every:function(t,e,n){let i=!0;return r(t,((t,n)=>{e(t)||(n(),i=!1)}),n),i},find:function(t,e,n){let i=null;return r(t,((t,n)=>{e(t)&&(n(),i=t)}),n),i},has:function(t,e){return i(t,(()=>!0),e)},pluck:function(t,e,i){r(t,(t=>{e(t)&&t.parent&&n.removeChild(t.parent,t)}),i)},some:i,walk:r});class c{static at(e,n){var r,i;return t.isNullish(e)?null:null!==(i=null===(r=e.children)||void 0===r?void 0:r[n])&&void 0!==i?i:null}static cursor(e){var n,r;return t.isNullish(e)?0:null!==(r=null===(n=e.children)||void 0===n?void 0:n.length)&&void 0!==r?r:0}static shouldAllowReorder(e){return!t.isNullish(e)&&u(e,(t=>t.allowReorder))}static usesKey(e){return!t.isNullish(e)&&t.isNotNullish(e.key)}static dump(e){return t.isNullish(e)?"":JSON.stringify(e,((t,e)=>{if("parent"!==t)return e}))}}!function(t){t.NO_ACTIVE_ISOLATE="Not within an active isolate",t.ENCOUNTERED_THE_SAME_KEY_TWICE='Encountered the same test key "{key}" twice. This may lead to tests overriding each other\'s results, or to tests being unexpectedly omitted.'}(o||(o={}));const a=e.createCascade(((e,n)=>{if(n)return null;t.invariant(e.historyRoot);const[r]=e.historyRoot(),i={};return t.assign(i,{historyNode:r,runtimeNode:null,runtimeRoot:null,stateRef:e}),i})),d=a.run,h={Run:d,addNodeToHistory:y,createRef:function(e){return Object.freeze({historyRoot:t.tinyState.createTinyState(null),Bus:t.bus.createBus(),appData:t.optionalFunctionValue(e)})},persist:f,reset:function(){const[,,t]=p();t()},useAvailableRoot:function(){const t=R();if(t)return t;const[e]=p();return e},useCurrentCursor:function(){const t=E();return t?c.cursor(t):0},useXAppData:function(){return v().stateRef.appData}};function f(t){const e=a.useX();return(...n)=>{var r;const i=null!==(r=a.use())&&void 0!==r?r:e;return a.run(i.stateRef,(()=>t(...n)))}}function v(){return a.useX()}function p(){return v().stateRef.historyRoot()}function N(){return v().historyNode}function y(e){const r=E();r?function(e){const r=E();t.invariant(r,o.NO_ACTIVE_ISOLATE),n.addChild(r,e)}(e):function(t){const[,e]=p();e(t)}(e),n.setParent(e,r)}function E(){var t;return null!==(t=v().runtimeNode)&&void 0!==t?t:null}function R(){return v().runtimeRoot}class _{static reconcile(e,n,r){const i=E(),s=N();let u=s;i&&(u=c.at(s,c.cursor(i)));const o=e(n,u);return t.invariant(o),Object.is(o,n)?[n,m(u,n,r)]:[o,o.output]}static removeAllNextNodesInIsolate(){const t=E(),e=N();e&&t&&n.slice(e,c.cursor(t))}static handleIsolateNodeWithKey(e){t.invariant(c.usesKey(e));const n=function(e){var n;if(t.isNullish(e))return null;const r=v().historyNode;return null!==(n=null==r?void 0:r.keys[e])&&void 0!==n?n:null}(e.key);let r=e;return t.isNullish(n)||(r=n),function(e,n){if(!e)return;const r=E();t.invariant(r,o.NO_ACTIVE_ISOLATE),t.isNullish(r.keys[e])?r.keys[e]=n:t.deferThrow(t.text(o.ENCOUNTERED_THE_SAME_KEY_TWICE,{key:e}))}(e.key,e),r}}function m(t,e,n){const r=R(),i=d(Object.assign({historyNode:t,runtimeNode:e},!r&&{runtimeRoot:e}),(()=>n(e)));return e.output=i,i}class I{constructor(t){this.children=[],this.keys={},this.parent=null,this.key=null,this.allowReorder=!1,this.type="Isolate"}static create(t,e){const r=E(),i=n.setParent(new this(e),r),[s,u]=_.reconcile(this.reconciler,i,t);return n.saveOutput(s,u),y(s),s}}function O(){return v().stateRef.Bus}function T(e,n){const r=O().emit;return t.isNullish(e)||r(e,n),f(r)}I.reconciler=function(e,n){return t.isNullish(n),e};var k=Object.freeze({__proto__:null,useBus:O,useEmit:T,usePrepareEmitter:function(t){const e=T();return n=>e(t,n)}});exports.Bus=k,exports.Isolate=I,exports.IsolateInspector=c,exports.IsolateMutator=n,exports.Reconciler=_,exports.VestRuntime=h,exports.Walker=l;
@@ -1,6 +1,37 @@
1
- import { isNullish, optionalFunctionValue, invariant, assign, tinyState, bus, deferThrow, text, isNotNullish } from 'vest-utils';
1
+ import { invariant, isNullish, optionalFunctionValue, isNotNullish, assign, tinyState, bus, deferThrow, text } from 'vest-utils';
2
2
  import { createCascade } from 'context';
3
3
 
4
+ class IsolateMutator {
5
+ static setParent(isolate, parent) {
6
+ isolate.parent = parent;
7
+ return isolate;
8
+ }
9
+ static saveOutput(isolate, output) {
10
+ isolate.output = output;
11
+ return isolate;
12
+ }
13
+ static setKey(isolate, key) {
14
+ isolate.key = key;
15
+ return isolate;
16
+ }
17
+ static addChild(isolate, child) {
18
+ invariant(isolate.children);
19
+ isolate.children.push(child);
20
+ IsolateMutator.setParent(child, isolate);
21
+ }
22
+ static removeChild(isolate, node) {
23
+ var _a, _b;
24
+ isolate.children =
25
+ (_b = (_a = isolate.children) === null || _a === void 0 ? void 0 : _a.filter(child => child !== node)) !== null && _b !== void 0 ? _b : null;
26
+ }
27
+ static slice(isolate, at) {
28
+ if (isNullish(isolate.children)) {
29
+ return;
30
+ }
31
+ isolate.children.length = at;
32
+ }
33
+ }
34
+
4
35
  // eslint-disable-next-line
5
36
  function walk(startNode, callback, visitOnly) {
6
37
  // If the startNode has no children, there is nothing to walk.
@@ -82,7 +113,7 @@ function every(startNode, predicate, visitOnly) {
82
113
  function pluck(startNode, predicate, visitOnly) {
83
114
  walk(startNode, node => {
84
115
  if (predicate(node) && node.parent) {
85
- node.parent.removeChild(node);
116
+ IsolateMutator.removeChild(node.parent, node);
86
117
  }
87
118
  }, visitOnly);
88
119
  }
@@ -90,12 +121,12 @@ function pluck(startNode, predicate, visitOnly) {
90
121
  //startNode that satisfies the given predicate function.
91
122
  function closest(startNode, predicate) {
92
123
  let current = startNode;
93
- while (current.parent) {
124
+ do {
94
125
  if (predicate(current)) {
95
126
  return current;
96
127
  }
97
128
  current = current.parent;
98
- }
129
+ } while (current);
99
130
  return null;
100
131
  }
101
132
  // This function returns true if the closest ancestor Isolates of the
@@ -116,6 +147,46 @@ var IsolateWalker = /*#__PURE__*/Object.freeze({
116
147
  walk: walk
117
148
  });
118
149
 
150
+ class IsolateInspector {
151
+ static at(isolate, at) {
152
+ var _a, _b;
153
+ if (isNullish(isolate)) {
154
+ return null;
155
+ }
156
+ return (_b = (_a = isolate.children) === null || _a === void 0 ? void 0 : _a[at]) !== null && _b !== void 0 ? _b : null;
157
+ }
158
+ static cursor(isolate) {
159
+ var _a, _b;
160
+ if (isNullish(isolate)) {
161
+ return 0;
162
+ }
163
+ return (_b = (_a = isolate.children) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
164
+ }
165
+ static shouldAllowReorder(isolate) {
166
+ if (isNullish(isolate)) {
167
+ return false;
168
+ }
169
+ return closestExists(isolate, node => node.allowReorder);
170
+ }
171
+ static usesKey(isolate) {
172
+ if (isNullish(isolate)) {
173
+ return false;
174
+ }
175
+ return isNotNullish(isolate.key);
176
+ }
177
+ static dump(isolate) {
178
+ if (isNullish(isolate)) {
179
+ return '';
180
+ }
181
+ return JSON.stringify(isolate, (key, value) => {
182
+ if (key === 'parent') {
183
+ return undefined;
184
+ }
185
+ return value;
186
+ });
187
+ }
188
+ }
189
+
119
190
  var ErrorStrings;
120
191
  (function (ErrorStrings) {
121
192
  ErrorStrings["NO_ACTIVE_ISOLATE"] = "Not within an active isolate";
@@ -140,30 +211,14 @@ const PersistedContext = createCascade((stateRef, parentContext) => {
140
211
  const Run = PersistedContext.run;
141
212
  const RuntimeApi = {
142
213
  Run,
214
+ addNodeToHistory,
143
215
  createRef,
144
216
  persist,
145
217
  reset,
146
218
  useAvailableRoot,
147
- useBus,
148
219
  useCurrentCursor,
149
- useEmit,
150
- usePrepareEmitter,
151
220
  useXAppData,
152
221
  };
153
- function useBus() {
154
- return useX().stateRef.Bus;
155
- }
156
- /*
157
- Returns an emitter, but it also has a shortcut for emitting an event immediately
158
- by passing an event name.
159
- */
160
- function useEmit() {
161
- return persist(useBus().emit);
162
- }
163
- function usePrepareEmitter(event) {
164
- const emit = useEmit();
165
- return (arg) => emit(event, arg);
166
- }
167
222
  function useXAppData() {
168
223
  return useX().stateRef.appData;
169
224
  }
@@ -191,6 +246,16 @@ function useHistoryRoot() {
191
246
  function useHistoryNode() {
192
247
  return useX().historyNode;
193
248
  }
249
+ function addNodeToHistory(node) {
250
+ const parent = useIsolate();
251
+ if (parent) {
252
+ useSetNextIsolateChild(node);
253
+ }
254
+ else {
255
+ useSetHistory(node);
256
+ }
257
+ IsolateMutator.setParent(node, parent);
258
+ }
194
259
  function useSetHistory(history) {
195
260
  const [, setHistoryRoot] = useHistoryRoot();
196
261
  setHistoryRoot(history);
@@ -208,8 +273,8 @@ function useIsolate() {
208
273
  return (_a = useX().runtimeNode) !== null && _a !== void 0 ? _a : null;
209
274
  }
210
275
  function useCurrentCursor() {
211
- var _a, _b;
212
- return (_b = (_a = useIsolate()) === null || _a === void 0 ? void 0 : _a.cursor()) !== null && _b !== void 0 ? _b : 0;
276
+ const isolate = useIsolate();
277
+ return isolate ? IsolateInspector.cursor(isolate) : 0;
213
278
  }
214
279
  function useRuntimeRoot() {
215
280
  return useX().runtimeRoot;
@@ -217,7 +282,7 @@ function useRuntimeRoot() {
217
282
  function useSetNextIsolateChild(child) {
218
283
  const currentIsolate = useIsolate();
219
284
  invariant(currentIsolate, ErrorStrings.NO_ACTIVE_ISOLATE);
220
- currentIsolate.addChild(child);
285
+ IsolateMutator.addChild(currentIsolate, child);
221
286
  }
222
287
  function useSetIsolateKey(key, value) {
223
288
  if (!key) {
@@ -252,15 +317,13 @@ function BaseReconciler(currentNode, historicNode) {
252
317
  }
253
318
  class Reconciler {
254
319
  static reconcile(reconciler, node, callback) {
255
- var _a;
256
320
  const parent = useIsolate();
257
321
  const historyNode = useHistoryNode();
258
322
  let localHistoryNode = historyNode;
259
323
  if (parent) {
260
324
  // If we have a parent, we need to get the history node from the parent's children
261
325
  // We take the history node from the cursor of the active node's children
262
- localHistoryNode =
263
- (_a = historyNode === null || historyNode === void 0 ? void 0 : historyNode.at(useCurrentCursor())) !== null && _a !== void 0 ? _a : null;
326
+ localHistoryNode = IsolateInspector.at(historyNode, IsolateInspector.cursor(parent));
264
327
  }
265
328
  const nextNode = reconciler(node, localHistoryNode);
266
329
  invariant(nextNode);
@@ -275,12 +338,13 @@ class Reconciler {
275
338
  if (!historyNode || !testIsolate) {
276
339
  // This is probably unreachable, but TS is not convinced.
277
340
  // Let's play it safe.
341
+ /* istanbul ignore next */
278
342
  return;
279
343
  }
280
- historyNode.slice(useCurrentCursor());
344
+ IsolateMutator.slice(historyNode, IsolateInspector.cursor(testIsolate));
281
345
  }
282
346
  static handleIsolateNodeWithKey(node) {
283
- invariant(node.usesKey());
347
+ invariant(IsolateInspector.usesKey(node));
284
348
  const prevNodeByKey = useHistoryKey(node.key);
285
349
  let nextNode = node;
286
350
  if (!isNullish(prevNodeByKey)) {
@@ -307,77 +371,43 @@ class Isolate {
307
371
  this.parent = null;
308
372
  this.key = null;
309
373
  this.allowReorder = false;
310
- }
311
- setParent(parent) {
312
- this.parent = parent;
313
- return this;
314
- }
315
- saveOutput(output) {
316
- this.output = output;
317
- return this;
318
- }
319
- setKey(key) {
320
- this.key = key;
321
- return this;
322
- }
323
- usesKey() {
324
- return isNotNullish(this.key);
325
- }
326
- addChild(child) {
327
- invariant(this.children);
328
- this.children.push(child);
329
- }
330
- removeChild(node) {
331
- var _a, _b;
332
- this.children = (_b = (_a = this.children) === null || _a === void 0 ? void 0 : _a.filter(child => child !== node)) !== null && _b !== void 0 ? _b : null;
333
- }
334
- slice(at) {
335
- if (isNullish(this.children)) {
336
- return;
337
- }
338
- this.children.length = at;
339
- }
340
- at(at) {
341
- var _a, _b;
342
- return (_b = (_a = this.children) === null || _a === void 0 ? void 0 : _a[at]) !== null && _b !== void 0 ? _b : null;
343
- }
344
- cursor() {
345
- var _a, _b;
346
- return (_b = (_a = this.children) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
347
- }
348
- shouldAllowReorder() {
349
- var _a;
350
- return (_a = closestExists(this, node => node.allowReorder)) !== null && _a !== void 0 ? _a : false;
351
- }
352
- get rootNode() {
353
- var _a;
354
- return (_a = closest(this, node => isNullish(node.parent))) !== null && _a !== void 0 ? _a : this;
374
+ this.type = 'Isolate';
355
375
  }
356
376
  static create(callback, data) {
357
- return this.createImplementation(callback, data);
358
- }
359
- static createImplementation(callback, data) {
360
377
  const parent = useIsolate();
361
- const newCreatedNode = new this(data).setParent(parent);
378
+ const newCreatedNode = IsolateMutator.setParent(new this(data), parent);
362
379
  const [nextIsolateChild, output] = Reconciler.reconcile(this.reconciler, newCreatedNode, callback);
363
- nextIsolateChild.saveOutput(output);
364
- this.setNode(nextIsolateChild);
380
+ IsolateMutator.saveOutput(nextIsolateChild, output);
381
+ addNodeToHistory(nextIsolateChild);
365
382
  return nextIsolateChild;
366
383
  }
367
- static setNode(node) {
368
- const parent = useIsolate();
369
- if (parent) {
370
- useSetNextIsolateChild(node);
371
- }
372
- else {
373
- useSetHistory(node);
374
- }
375
- node.setParent(parent);
376
- }
377
- static is(node) {
378
- return node instanceof Isolate;
379
- }
380
384
  }
381
385
  Isolate.reconciler = BaseReconciler;
382
386
 
383
- export { Isolate, Reconciler, RuntimeApi as VestRuntime, IsolateWalker as Walker };
387
+ function useBus() {
388
+ return useX().stateRef.Bus;
389
+ }
390
+ /*
391
+ Returns an emitter, but it also has a shortcut for emitting an event immediately
392
+ by passing an event name.
393
+ */
394
+ function useEmit(event, data) {
395
+ const emit = useBus().emit;
396
+ if (!isNullish(event)) {
397
+ emit(event, data);
398
+ }
399
+ return persist(emit);
400
+ }
401
+ function usePrepareEmitter(event) {
402
+ const emit = useEmit();
403
+ return (arg) => emit(event, arg);
404
+ }
405
+
406
+ var Bus = /*#__PURE__*/Object.freeze({
407
+ __proto__: null,
408
+ useBus: useBus,
409
+ useEmit: useEmit,
410
+ usePrepareEmitter: usePrepareEmitter
411
+ });
412
+
413
+ export { Bus, Isolate, IsolateInspector, IsolateMutator, Reconciler, RuntimeApi as VestRuntime, IsolateWalker as Walker };
@@ -1 +1 @@
1
- import{isNullish as t,optionalFunctionValue as e,invariant as n,assign as r,tinyState as o,bus as i,deferThrow as u,text as s,isNotNullish as l}from"vest-utils";import{createCascade as c}from"context";function a(n,r,o){if(t(n.children))return;let i=!1;for(const s of n.children){if(i)return;if((t(o)||e(o,s))&&r(s,u),i)return;a(s,((t,e)=>{r(t,(()=>{e(),u()}))}),o)}function u(){i=!0}}function d(t,e,n){let r=!1;return a(t,((t,n)=>{e(t)&&(n(),r=!0)}),n),r}function h(t,e){let n=t;for(;n.parent;){if(e(n))return n;n=n.parent}return null}function f(t,e){return!!h(t,e)}var v,p=Object.freeze({__proto__:null,closest:h,closestExists:f,every:function(t,e,n){let r=!0;return a(t,((t,n)=>{e(t)||(n(),r=!1)}),n),r},find:function(t,e,n){let r=null;return a(t,((t,n)=>{e(t)&&(n(),r=t)}),n),r},has:function(t,e){return d(t,(()=>!0),e)},pluck:function(t,e,n){a(t,(t=>{e(t)&&t.parent&&t.parent.removeChild(t)}),n)},some:d,walk:a});!function(t){t.NO_ACTIVE_ISOLATE="Not within an active isolate",t.ENCOUNTERED_THE_SAME_KEY_TWICE='Encountered the same test key "{key}" twice. This may lead to tests overriding each other\'s results, or to tests being unexpectedly omitted.'}(v||(v={}));const y=c(((t,e)=>{if(e)return null;n(t.historyRoot);const[o]=t.historyRoot(),i={};return r(i,{historyNode:o,runtimeNode:null,runtimeRoot:null,stateRef:t}),i})),m=y.run,E={Run:m,createRef:function(t){return Object.freeze({historyRoot:o.createTinyState(null),Bus:i.createBus(),appData:e(t)})},persist:_,reset:function(){const[,,t]=O();t()},useAvailableRoot:function(){const t=A();if(t)return t;const[e]=O();return e},useBus:N,useCurrentCursor:I,useEmit:R,usePrepareEmitter:function(t){const e=R();return n=>e(t,n)},useXAppData:function(){return k().stateRef.appData}};function N(){return k().stateRef.Bus}function R(){return _(N().emit)}function _(t){const e=y.useX();return(...n)=>{var r;const o=null!==(r=y.use())&&void 0!==r?r:e;return y.run(o.stateRef,(()=>t(...n)))}}function k(){return y.useX()}function O(){return k().stateRef.historyRoot()}function T(){return k().historyNode}function C(){var t;return null!==(t=k().runtimeNode)&&void 0!==t?t:null}function I(){var t,e;return null!==(e=null===(t=C())||void 0===t?void 0:t.cursor())&&void 0!==e?e:0}function A(){return k().runtimeRoot}class w{static reconcile(t,e,r){var o;const i=C(),u=T();let s=u;i&&(s=null!==(o=null==u?void 0:u.at(I()))&&void 0!==o?o:null);const l=t(e,s);return n(l),Object.is(l,e)?[e,b(s,e,r)]:[l,l.output]}static removeAllNextNodesInIsolate(){const t=C(),e=T();e&&t&&e.slice(I())}static handleIsolateNodeWithKey(e){n(e.usesKey());const r=function(e){var n;if(t(e))return null;const r=k().historyNode;return null!==(n=null==r?void 0:r.keys[e])&&void 0!==n?n:null}(e.key);let o=e;return t(r)||(o=r),function(e,r){if(!e)return;const o=C();n(o,v.NO_ACTIVE_ISOLATE),t(o.keys[e])?o.keys[e]=r:u(s(v.ENCOUNTERED_THE_SAME_KEY_TWICE,{key:e}))}(e.key,e),o}}function b(t,e,n){const r=A(),o=m(Object.assign({historyNode:t,runtimeNode:e},!r&&{runtimeRoot:e}),(()=>n(e)));return e.output=o,o}class g{constructor(t){this.children=[],this.keys={},this.parent=null,this.key=null,this.allowReorder=!1}setParent(t){return this.parent=t,this}saveOutput(t){return this.output=t,this}setKey(t){return this.key=t,this}usesKey(){return l(this.key)}addChild(t){n(this.children),this.children.push(t)}removeChild(t){var e,n;this.children=null!==(n=null===(e=this.children)||void 0===e?void 0:e.filter((e=>e!==t)))&&void 0!==n?n:null}slice(e){t(this.children)||(this.children.length=e)}at(t){var e,n;return null!==(n=null===(e=this.children)||void 0===e?void 0:e[t])&&void 0!==n?n:null}cursor(){var t,e;return null!==(e=null===(t=this.children)||void 0===t?void 0:t.length)&&void 0!==e?e:0}shouldAllowReorder(){var t;return null!==(t=f(this,(t=>t.allowReorder)))&&void 0!==t&&t}get rootNode(){var e;return null!==(e=h(this,(e=>t(e.parent))))&&void 0!==e?e:this}static create(t,e){return this.createImplementation(t,e)}static createImplementation(t,e){const n=C(),r=new this(e).setParent(n),[o,i]=w.reconcile(this.reconciler,r,t);return o.saveOutput(i),this.setNode(o),o}static setNode(t){const e=C();e?function(t){const e=C();n(e,v.NO_ACTIVE_ISOLATE),e.addChild(t)}(t):function(t){const[,e]=O();e(t)}(t),t.setParent(e)}static is(t){return t instanceof g}}g.reconciler=function(e,n){return t(n),e};export{g as Isolate,w as Reconciler,E as VestRuntime,p as Walker};
1
+ import{invariant as t,isNullish as e,optionalFunctionValue as n,isNotNullish as r,assign as o,tinyState as u,bus as i,deferThrow as s,text as c}from"vest-utils";import{createCascade as l}from"context";class a{static setParent(t,e){return t.parent=e,t}static saveOutput(t,e){return t.output=e,t}static setKey(t,e){return t.key=e,t}static addChild(e,n){t(e.children),e.children.push(n),a.setParent(n,e)}static removeChild(t,e){var n,r;t.children=null!==(r=null===(n=t.children)||void 0===n?void 0:n.filter((t=>t!==e)))&&void 0!==r?r:null}static slice(t,n){e(t.children)||(t.children.length=n)}}function d(t,r,o){if(e(t.children))return;let u=!1;for(const s of t.children){if(u)return;if((e(o)||n(o,s))&&r(s,i),u)return;d(s,((t,e)=>{r(t,(()=>{e(),i()}))}),o)}function i(){u=!0}}function f(t,e,n){let r=!1;return d(t,((t,n)=>{e(t)&&(n(),r=!0)}),n),r}function h(t,e){let n=t;do{if(e(n))return n;n=n.parent}while(n);return null}function v(t,e){return!!h(t,e)}var y,p=Object.freeze({__proto__:null,closest:h,closestExists:v,every:function(t,e,n){let r=!0;return d(t,((t,n)=>{e(t)||(n(),r=!1)}),n),r},find:function(t,e,n){let r=null;return d(t,((t,n)=>{e(t)&&(n(),r=t)}),n),r},has:function(t,e){return f(t,(()=>!0),e)},pluck:function(t,e,n){d(t,(t=>{e(t)&&t.parent&&a.removeChild(t.parent,t)}),n)},some:f,walk:d});class E{static at(t,n){var r,o;return e(t)?null:null!==(o=null===(r=t.children)||void 0===r?void 0:r[n])&&void 0!==o?o:null}static cursor(t){var n,r;return e(t)?0:null!==(r=null===(n=t.children)||void 0===n?void 0:n.length)&&void 0!==r?r:0}static shouldAllowReorder(t){return!e(t)&&v(t,(t=>t.allowReorder))}static usesKey(t){return!e(t)&&r(t.key)}static dump(t){return e(t)?"":JSON.stringify(t,((t,e)=>{if("parent"!==t)return e}))}}!function(t){t.NO_ACTIVE_ISOLATE="Not within an active isolate",t.ENCOUNTERED_THE_SAME_KEY_TWICE='Encountered the same test key "{key}" twice. This may lead to tests overriding each other\'s results, or to tests being unexpectedly omitted.'}(y||(y={}));const m=l(((e,n)=>{if(n)return null;t(e.historyRoot);const[r]=e.historyRoot(),u={};return o(u,{historyNode:r,runtimeNode:null,runtimeRoot:null,stateRef:e}),u})),_=m.run,R={Run:_,addNodeToHistory:C,createRef:function(t){return Object.freeze({historyRoot:u.createTinyState(null),Bus:i.createBus(),appData:n(t)})},persist:N,reset:function(){const[,,t]=k();t()},useAvailableRoot:function(){const t=I();if(t)return t;const[e]=k();return e},useCurrentCursor:function(){const t=A();return t?E.cursor(t):0},useXAppData:function(){return O().stateRef.appData}};function N(t){const e=m.useX();return(...n)=>{var r;const o=null!==(r=m.use())&&void 0!==r?r:e;return m.run(o.stateRef,(()=>t(...n)))}}function O(){return m.useX()}function k(){return O().stateRef.historyRoot()}function T(){return O().historyNode}function C(e){const n=A();n?function(e){const n=A();t(n,y.NO_ACTIVE_ISOLATE),a.addChild(n,e)}(e):function(t){const[,e]=k();e(t)}(e),a.setParent(e,n)}function A(){var t;return null!==(t=O().runtimeNode)&&void 0!==t?t:null}function I(){return O().runtimeRoot}class w{static reconcile(e,n,r){const o=A(),u=T();let i=u;o&&(i=E.at(u,E.cursor(o)));const s=e(n,i);return t(s),Object.is(s,n)?[n,b(i,n,r)]:[s,s.output]}static removeAllNextNodesInIsolate(){const t=A(),e=T();e&&t&&a.slice(e,E.cursor(t))}static handleIsolateNodeWithKey(n){t(E.usesKey(n));const r=function(t){var n;if(e(t))return null;const r=O().historyNode;return null!==(n=null==r?void 0:r.keys[t])&&void 0!==n?n:null}(n.key);let o=n;return e(r)||(o=r),function(n,r){if(!n)return;const o=A();t(o,y.NO_ACTIVE_ISOLATE),e(o.keys[n])?o.keys[n]=r:s(c(y.ENCOUNTERED_THE_SAME_KEY_TWICE,{key:n}))}(n.key,n),o}}function b(t,e,n){const r=I(),o=_(Object.assign({historyNode:t,runtimeNode:e},!r&&{runtimeRoot:e}),(()=>n(e)));return e.output=o,o}class S{constructor(t){this.children=[],this.keys={},this.parent=null,this.key=null,this.allowReorder=!1,this.type="Isolate"}static create(t,e){const n=A(),r=a.setParent(new this(e),n),[o,u]=w.reconcile(this.reconciler,r,t);return a.saveOutput(o,u),C(o),o}}function g(){return O().stateRef.Bus}function K(t,n){const r=g().emit;return e(t)||r(t,n),N(r)}S.reconciler=function(t,n){return e(n),t};var j=Object.freeze({__proto__:null,useBus:g,useEmit:K,usePrepareEmitter:function(t){const e=K();return n=>e(t,n)}});export{j as Bus,S as Isolate,E as IsolateInspector,a as IsolateMutator,w as Reconciler,R as VestRuntime,p as Walker};
@@ -4,6 +4,37 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["vestjs-runtime"] = {}, global["vest-utils"], global.context));
5
5
  })(this, (function (exports, vestUtils, context) { 'use strict';
6
6
 
7
+ class IsolateMutator {
8
+ static setParent(isolate, parent) {
9
+ isolate.parent = parent;
10
+ return isolate;
11
+ }
12
+ static saveOutput(isolate, output) {
13
+ isolate.output = output;
14
+ return isolate;
15
+ }
16
+ static setKey(isolate, key) {
17
+ isolate.key = key;
18
+ return isolate;
19
+ }
20
+ static addChild(isolate, child) {
21
+ vestUtils.invariant(isolate.children);
22
+ isolate.children.push(child);
23
+ IsolateMutator.setParent(child, isolate);
24
+ }
25
+ static removeChild(isolate, node) {
26
+ var _a, _b;
27
+ isolate.children =
28
+ (_b = (_a = isolate.children) === null || _a === void 0 ? void 0 : _a.filter(child => child !== node)) !== null && _b !== void 0 ? _b : null;
29
+ }
30
+ static slice(isolate, at) {
31
+ if (vestUtils.isNullish(isolate.children)) {
32
+ return;
33
+ }
34
+ isolate.children.length = at;
35
+ }
36
+ }
37
+
7
38
  // eslint-disable-next-line
8
39
  function walk(startNode, callback, visitOnly) {
9
40
  // If the startNode has no children, there is nothing to walk.
@@ -85,7 +116,7 @@
85
116
  function pluck(startNode, predicate, visitOnly) {
86
117
  walk(startNode, node => {
87
118
  if (predicate(node) && node.parent) {
88
- node.parent.removeChild(node);
119
+ IsolateMutator.removeChild(node.parent, node);
89
120
  }
90
121
  }, visitOnly);
91
122
  }
@@ -93,12 +124,12 @@
93
124
  //startNode that satisfies the given predicate function.
94
125
  function closest(startNode, predicate) {
95
126
  let current = startNode;
96
- while (current.parent) {
127
+ do {
97
128
  if (predicate(current)) {
98
129
  return current;
99
130
  }
100
131
  current = current.parent;
101
- }
132
+ } while (current);
102
133
  return null;
103
134
  }
104
135
  // This function returns true if the closest ancestor Isolates of the
@@ -119,6 +150,46 @@
119
150
  walk: walk
120
151
  });
121
152
 
153
+ class IsolateInspector {
154
+ static at(isolate, at) {
155
+ var _a, _b;
156
+ if (vestUtils.isNullish(isolate)) {
157
+ return null;
158
+ }
159
+ return (_b = (_a = isolate.children) === null || _a === void 0 ? void 0 : _a[at]) !== null && _b !== void 0 ? _b : null;
160
+ }
161
+ static cursor(isolate) {
162
+ var _a, _b;
163
+ if (vestUtils.isNullish(isolate)) {
164
+ return 0;
165
+ }
166
+ return (_b = (_a = isolate.children) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
167
+ }
168
+ static shouldAllowReorder(isolate) {
169
+ if (vestUtils.isNullish(isolate)) {
170
+ return false;
171
+ }
172
+ return closestExists(isolate, node => node.allowReorder);
173
+ }
174
+ static usesKey(isolate) {
175
+ if (vestUtils.isNullish(isolate)) {
176
+ return false;
177
+ }
178
+ return vestUtils.isNotNullish(isolate.key);
179
+ }
180
+ static dump(isolate) {
181
+ if (vestUtils.isNullish(isolate)) {
182
+ return '';
183
+ }
184
+ return JSON.stringify(isolate, (key, value) => {
185
+ if (key === 'parent') {
186
+ return undefined;
187
+ }
188
+ return value;
189
+ });
190
+ }
191
+ }
192
+
122
193
  var ErrorStrings;
123
194
  (function (ErrorStrings) {
124
195
  ErrorStrings["NO_ACTIVE_ISOLATE"] = "Not within an active isolate";
@@ -143,30 +214,14 @@
143
214
  const Run = PersistedContext.run;
144
215
  const RuntimeApi = {
145
216
  Run,
217
+ addNodeToHistory,
146
218
  createRef,
147
219
  persist,
148
220
  reset,
149
221
  useAvailableRoot,
150
- useBus,
151
222
  useCurrentCursor,
152
- useEmit,
153
- usePrepareEmitter,
154
223
  useXAppData,
155
224
  };
156
- function useBus() {
157
- return useX().stateRef.Bus;
158
- }
159
- /*
160
- Returns an emitter, but it also has a shortcut for emitting an event immediately
161
- by passing an event name.
162
- */
163
- function useEmit() {
164
- return persist(useBus().emit);
165
- }
166
- function usePrepareEmitter(event) {
167
- const emit = useEmit();
168
- return (arg) => emit(event, arg);
169
- }
170
225
  function useXAppData() {
171
226
  return useX().stateRef.appData;
172
227
  }
@@ -194,6 +249,16 @@
194
249
  function useHistoryNode() {
195
250
  return useX().historyNode;
196
251
  }
252
+ function addNodeToHistory(node) {
253
+ const parent = useIsolate();
254
+ if (parent) {
255
+ useSetNextIsolateChild(node);
256
+ }
257
+ else {
258
+ useSetHistory(node);
259
+ }
260
+ IsolateMutator.setParent(node, parent);
261
+ }
197
262
  function useSetHistory(history) {
198
263
  const [, setHistoryRoot] = useHistoryRoot();
199
264
  setHistoryRoot(history);
@@ -211,8 +276,8 @@
211
276
  return (_a = useX().runtimeNode) !== null && _a !== void 0 ? _a : null;
212
277
  }
213
278
  function useCurrentCursor() {
214
- var _a, _b;
215
- return (_b = (_a = useIsolate()) === null || _a === void 0 ? void 0 : _a.cursor()) !== null && _b !== void 0 ? _b : 0;
279
+ const isolate = useIsolate();
280
+ return isolate ? IsolateInspector.cursor(isolate) : 0;
216
281
  }
217
282
  function useRuntimeRoot() {
218
283
  return useX().runtimeRoot;
@@ -220,7 +285,7 @@
220
285
  function useSetNextIsolateChild(child) {
221
286
  const currentIsolate = useIsolate();
222
287
  vestUtils.invariant(currentIsolate, ErrorStrings.NO_ACTIVE_ISOLATE);
223
- currentIsolate.addChild(child);
288
+ IsolateMutator.addChild(currentIsolate, child);
224
289
  }
225
290
  function useSetIsolateKey(key, value) {
226
291
  if (!key) {
@@ -255,15 +320,13 @@
255
320
  }
256
321
  class Reconciler {
257
322
  static reconcile(reconciler, node, callback) {
258
- var _a;
259
323
  const parent = useIsolate();
260
324
  const historyNode = useHistoryNode();
261
325
  let localHistoryNode = historyNode;
262
326
  if (parent) {
263
327
  // If we have a parent, we need to get the history node from the parent's children
264
328
  // We take the history node from the cursor of the active node's children
265
- localHistoryNode =
266
- (_a = historyNode === null || historyNode === void 0 ? void 0 : historyNode.at(useCurrentCursor())) !== null && _a !== void 0 ? _a : null;
329
+ localHistoryNode = IsolateInspector.at(historyNode, IsolateInspector.cursor(parent));
267
330
  }
268
331
  const nextNode = reconciler(node, localHistoryNode);
269
332
  vestUtils.invariant(nextNode);
@@ -278,12 +341,13 @@
278
341
  if (!historyNode || !testIsolate) {
279
342
  // This is probably unreachable, but TS is not convinced.
280
343
  // Let's play it safe.
344
+ /* istanbul ignore next */
281
345
  return;
282
346
  }
283
- historyNode.slice(useCurrentCursor());
347
+ IsolateMutator.slice(historyNode, IsolateInspector.cursor(testIsolate));
284
348
  }
285
349
  static handleIsolateNodeWithKey(node) {
286
- vestUtils.invariant(node.usesKey());
350
+ vestUtils.invariant(IsolateInspector.usesKey(node));
287
351
  const prevNodeByKey = useHistoryKey(node.key);
288
352
  let nextNode = node;
289
353
  if (!vestUtils.isNullish(prevNodeByKey)) {
@@ -310,80 +374,49 @@
310
374
  this.parent = null;
311
375
  this.key = null;
312
376
  this.allowReorder = false;
313
- }
314
- setParent(parent) {
315
- this.parent = parent;
316
- return this;
317
- }
318
- saveOutput(output) {
319
- this.output = output;
320
- return this;
321
- }
322
- setKey(key) {
323
- this.key = key;
324
- return this;
325
- }
326
- usesKey() {
327
- return vestUtils.isNotNullish(this.key);
328
- }
329
- addChild(child) {
330
- vestUtils.invariant(this.children);
331
- this.children.push(child);
332
- }
333
- removeChild(node) {
334
- var _a, _b;
335
- this.children = (_b = (_a = this.children) === null || _a === void 0 ? void 0 : _a.filter(child => child !== node)) !== null && _b !== void 0 ? _b : null;
336
- }
337
- slice(at) {
338
- if (vestUtils.isNullish(this.children)) {
339
- return;
340
- }
341
- this.children.length = at;
342
- }
343
- at(at) {
344
- var _a, _b;
345
- return (_b = (_a = this.children) === null || _a === void 0 ? void 0 : _a[at]) !== null && _b !== void 0 ? _b : null;
346
- }
347
- cursor() {
348
- var _a, _b;
349
- return (_b = (_a = this.children) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
350
- }
351
- shouldAllowReorder() {
352
- var _a;
353
- return (_a = closestExists(this, node => node.allowReorder)) !== null && _a !== void 0 ? _a : false;
354
- }
355
- get rootNode() {
356
- var _a;
357
- return (_a = closest(this, node => vestUtils.isNullish(node.parent))) !== null && _a !== void 0 ? _a : this;
377
+ this.type = 'Isolate';
358
378
  }
359
379
  static create(callback, data) {
360
- return this.createImplementation(callback, data);
361
- }
362
- static createImplementation(callback, data) {
363
380
  const parent = useIsolate();
364
- const newCreatedNode = new this(data).setParent(parent);
381
+ const newCreatedNode = IsolateMutator.setParent(new this(data), parent);
365
382
  const [nextIsolateChild, output] = Reconciler.reconcile(this.reconciler, newCreatedNode, callback);
366
- nextIsolateChild.saveOutput(output);
367
- this.setNode(nextIsolateChild);
383
+ IsolateMutator.saveOutput(nextIsolateChild, output);
384
+ addNodeToHistory(nextIsolateChild);
368
385
  return nextIsolateChild;
369
386
  }
370
- static setNode(node) {
371
- const parent = useIsolate();
372
- if (parent) {
373
- useSetNextIsolateChild(node);
374
- }
375
- else {
376
- useSetHistory(node);
377
- }
378
- node.setParent(parent);
379
- }
380
- static is(node) {
381
- return node instanceof Isolate;
382
- }
383
387
  }
384
388
  Isolate.reconciler = BaseReconciler;
385
389
 
390
+ function useBus() {
391
+ return useX().stateRef.Bus;
392
+ }
393
+ /*
394
+ Returns an emitter, but it also has a shortcut for emitting an event immediately
395
+ by passing an event name.
396
+ */
397
+ function useEmit(event, data) {
398
+ const emit = useBus().emit;
399
+ if (!vestUtils.isNullish(event)) {
400
+ emit(event, data);
401
+ }
402
+ return persist(emit);
403
+ }
404
+ function usePrepareEmitter(event) {
405
+ const emit = useEmit();
406
+ return (arg) => emit(event, arg);
407
+ }
408
+
409
+ var Bus = /*#__PURE__*/Object.freeze({
410
+ __proto__: null,
411
+ useBus: useBus,
412
+ useEmit: useEmit,
413
+ usePrepareEmitter: usePrepareEmitter
414
+ });
415
+
416
+ exports.Bus = Bus;
386
417
  exports.Isolate = Isolate;
418
+ exports.IsolateInspector = IsolateInspector;
419
+ exports.IsolateMutator = IsolateMutator;
387
420
  exports.Reconciler = Reconciler;
388
421
  exports.VestRuntime = RuntimeApi;
389
422
  exports.Walker = IsolateWalker;
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("vest-utils"),require("context")):"function"==typeof define&&define.amd?define(["exports","vest-utils","context"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["vestjs-runtime"]={},t["vest-utils"],t.context)}(this,(function(t,e,n){"use strict";function i(t,n,r){if(e.isNullish(t.children))return;let o=!1;for(const u of t.children){if(o)return;if((e.isNullish(r)||e.optionalFunctionValue(r,u))&&n(u,s),o)return;i(u,((t,e)=>{n(t,(()=>{e(),s()}))}),r)}function s(){o=!0}}function r(t,e,n){let r=!1;return i(t,((t,n)=>{e(t)&&(n(),r=!0)}),n),r}function o(t,e){let n=t;for(;n.parent;){if(e(n))return n;n=n.parent}return null}function s(t,e){return!!o(t,e)}var u,l=Object.freeze({__proto__:null,closest:o,closestExists:s,every:function(t,e,n){let r=!0;return i(t,((t,n)=>{e(t)||(n(),r=!1)}),n),r},find:function(t,e,n){let r=null;return i(t,((t,n)=>{e(t)&&(n(),r=t)}),n),r},has:function(t,e){return r(t,(()=>!0),e)},pluck:function(t,e,n){i(t,(t=>{e(t)&&t.parent&&t.parent.removeChild(t)}),n)},some:r,walk:i});!function(t){t.NO_ACTIVE_ISOLATE="Not within an active isolate",t.ENCOUNTERED_THE_SAME_KEY_TWICE='Encountered the same test key "{key}" twice. This may lead to tests overriding each other\'s results, or to tests being unexpectedly omitted.'}(u||(u={}));const c=n.createCascade(((t,n)=>{if(n)return null;e.invariant(t.historyRoot);const[i]=t.historyRoot(),r={};return e.assign(r,{historyNode:i,runtimeNode:null,runtimeRoot:null,stateRef:t}),r})),a=c.run,h={Run:a,createRef:function(t){return Object.freeze({historyRoot:e.tinyState.createTinyState(null),Bus:e.bus.createBus(),appData:e.optionalFunctionValue(t)})},persist:v,reset:function(){const[,,t]=y();t()},useAvailableRoot:function(){const t=R();if(t)return t;const[e]=y();return e},useBus:d,useCurrentCursor:E,useEmit:f,usePrepareEmitter:function(t){const e=f();return n=>e(t,n)},useXAppData:function(){return p().stateRef.appData}};function d(){return p().stateRef.Bus}function f(){return v(d().emit)}function v(t){const e=c.useX();return(...n)=>{var i;const r=null!==(i=c.use())&&void 0!==i?i:e;return c.run(r.stateRef,(()=>t(...n)))}}function p(){return c.useX()}function y(){return p().stateRef.historyRoot()}function N(){return p().historyNode}function m(){var t;return null!==(t=p().runtimeNode)&&void 0!==t?t:null}function E(){var t,e;return null!==(e=null===(t=m())||void 0===t?void 0:t.cursor())&&void 0!==e?e:0}function R(){return p().runtimeRoot}class _{static reconcile(t,n,i){var r;const o=m(),s=N();let u=s;o&&(u=null!==(r=null==s?void 0:s.at(E()))&&void 0!==r?r:null);const l=t(n,u);return e.invariant(l),Object.is(l,n)?[n,T(u,n,i)]:[l,l.output]}static removeAllNextNodesInIsolate(){const t=m(),e=N();e&&t&&e.slice(E())}static handleIsolateNodeWithKey(t){e.invariant(t.usesKey());const n=function(t){var n;if(e.isNullish(t))return null;const i=p().historyNode;return null!==(n=null==i?void 0:i.keys[t])&&void 0!==n?n:null}(t.key);let i=t;return e.isNullish(n)||(i=n),function(t,n){if(!t)return;const i=m();e.invariant(i,u.NO_ACTIVE_ISOLATE),e.isNullish(i.keys[t])?i.keys[t]=n:e.deferThrow(e.text(u.ENCOUNTERED_THE_SAME_KEY_TWICE,{key:t}))}(t.key,t),i}}function T(t,e,n){const i=R(),r=a(Object.assign({historyNode:t,runtimeNode:e},!i&&{runtimeRoot:e}),(()=>n(e)));return e.output=r,r}class k{constructor(t){this.children=[],this.keys={},this.parent=null,this.key=null,this.allowReorder=!1}setParent(t){return this.parent=t,this}saveOutput(t){return this.output=t,this}setKey(t){return this.key=t,this}usesKey(){return e.isNotNullish(this.key)}addChild(t){e.invariant(this.children),this.children.push(t)}removeChild(t){var e,n;this.children=null!==(n=null===(e=this.children)||void 0===e?void 0:e.filter((e=>e!==t)))&&void 0!==n?n:null}slice(t){e.isNullish(this.children)||(this.children.length=t)}at(t){var e,n;return null!==(n=null===(e=this.children)||void 0===e?void 0:e[t])&&void 0!==n?n:null}cursor(){var t,e;return null!==(e=null===(t=this.children)||void 0===t?void 0:t.length)&&void 0!==e?e:0}shouldAllowReorder(){var t;return null!==(t=s(this,(t=>t.allowReorder)))&&void 0!==t&&t}get rootNode(){var t;return null!==(t=o(this,(t=>e.isNullish(t.parent))))&&void 0!==t?t:this}static create(t,e){return this.createImplementation(t,e)}static createImplementation(t,e){const n=m(),i=new this(e).setParent(n),[r,o]=_.reconcile(this.reconciler,i,t);return r.saveOutput(o),this.setNode(r),r}static setNode(t){const n=m();n?function(t){const n=m();e.invariant(n,u.NO_ACTIVE_ISOLATE),n.addChild(t)}(t):function(t){const[,e]=y();e(t)}(t),t.setParent(n)}static is(t){return t instanceof k}}k.reconciler=function(t,n){return e.isNullish(n),t},t.Isolate=k,t.Reconciler=_,t.VestRuntime=h,t.Walker=l}));
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("vest-utils"),require("context")):"function"==typeof define&&define.amd?define(["exports","vest-utils","context"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["vestjs-runtime"]={},t["vest-utils"],t.context)}(this,(function(t,e,n){"use strict";class r{static setParent(t,e){return t.parent=e,t}static saveOutput(t,e){return t.output=e,t}static setKey(t,e){return t.key=e,t}static addChild(t,n){e.invariant(t.children),t.children.push(n),r.setParent(n,t)}static removeChild(t,e){var n,r;t.children=null!==(r=null===(n=t.children)||void 0===n?void 0:n.filter((t=>t!==e)))&&void 0!==r?r:null}static slice(t,n){e.isNullish(t.children)||(t.children.length=n)}}function i(t,n,r){if(e.isNullish(t.children))return;let s=!1;for(const o of t.children){if(s)return;if((e.isNullish(r)||e.optionalFunctionValue(r,o))&&n(o,u),s)return;i(o,((t,e)=>{n(t,(()=>{e(),u()}))}),r)}function u(){s=!0}}function s(t,e,n){let r=!1;return i(t,((t,n)=>{e(t)&&(n(),r=!0)}),n),r}function u(t,e){let n=t;do{if(e(n))return n;n=n.parent}while(n);return null}function o(t,e){return!!u(t,e)}var l,c=Object.freeze({__proto__:null,closest:u,closestExists:o,every:function(t,e,n){let r=!0;return i(t,((t,n)=>{e(t)||(n(),r=!1)}),n),r},find:function(t,e,n){let r=null;return i(t,((t,n)=>{e(t)&&(n(),r=t)}),n),r},has:function(t,e){return s(t,(()=>!0),e)},pluck:function(t,e,n){i(t,(t=>{e(t)&&t.parent&&r.removeChild(t.parent,t)}),n)},some:s,walk:i});class a{static at(t,n){var r,i;return e.isNullish(t)?null:null!==(i=null===(r=t.children)||void 0===r?void 0:r[n])&&void 0!==i?i:null}static cursor(t){var n,r;return e.isNullish(t)?0:null!==(r=null===(n=t.children)||void 0===n?void 0:n.length)&&void 0!==r?r:0}static shouldAllowReorder(t){return!e.isNullish(t)&&o(t,(t=>t.allowReorder))}static usesKey(t){return!e.isNullish(t)&&e.isNotNullish(t.key)}static dump(t){return e.isNullish(t)?"":JSON.stringify(t,((t,e)=>{if("parent"!==t)return e}))}}!function(t){t.NO_ACTIVE_ISOLATE="Not within an active isolate",t.ENCOUNTERED_THE_SAME_KEY_TWICE='Encountered the same test key "{key}" twice. This may lead to tests overriding each other\'s results, or to tests being unexpectedly omitted.'}(l||(l={}));const d=n.createCascade(((t,n)=>{if(n)return null;e.invariant(t.historyRoot);const[r]=t.historyRoot(),i={};return e.assign(i,{historyNode:r,runtimeNode:null,runtimeRoot:null,stateRef:t}),i})),f=d.run,h={Run:f,addNodeToHistory:E,createRef:function(t){return Object.freeze({historyRoot:e.tinyState.createTinyState(null),Bus:e.bus.createBus(),appData:e.optionalFunctionValue(t)})},persist:v,reset:function(){const[,,t]=y();t()},useAvailableRoot:function(){const t=_();if(t)return t;const[e]=y();return e},useCurrentCursor:function(){const t=R();return t?a.cursor(t):0},useXAppData:function(){return p().stateRef.appData}};function v(t){const e=d.useX();return(...n)=>{var r;const i=null!==(r=d.use())&&void 0!==r?r:e;return d.run(i.stateRef,(()=>t(...n)))}}function p(){return d.useX()}function y(){return p().stateRef.historyRoot()}function N(){return p().historyNode}function E(t){const n=R();n?function(t){const n=R();e.invariant(n,l.NO_ACTIVE_ISOLATE),r.addChild(n,t)}(t):function(t){const[,e]=y();e(t)}(t),r.setParent(t,n)}function R(){var t;return null!==(t=p().runtimeNode)&&void 0!==t?t:null}function _(){return p().runtimeRoot}class m{static reconcile(t,n,r){const i=R(),s=N();let u=s;i&&(u=a.at(s,a.cursor(i)));const o=t(n,u);return e.invariant(o),Object.is(o,n)?[n,T(u,n,r)]:[o,o.output]}static removeAllNextNodesInIsolate(){const t=R(),e=N();e&&t&&r.slice(e,a.cursor(t))}static handleIsolateNodeWithKey(t){e.invariant(a.usesKey(t));const n=function(t){var n;if(e.isNullish(t))return null;const r=p().historyNode;return null!==(n=null==r?void 0:r.keys[t])&&void 0!==n?n:null}(t.key);let r=t;return e.isNullish(n)||(r=n),function(t,n){if(!t)return;const r=R();e.invariant(r,l.NO_ACTIVE_ISOLATE),e.isNullish(r.keys[t])?r.keys[t]=n:e.deferThrow(e.text(l.ENCOUNTERED_THE_SAME_KEY_TWICE,{key:t}))}(t.key,t),r}}function T(t,e,n){const r=_(),i=f(Object.assign({historyNode:t,runtimeNode:e},!r&&{runtimeRoot:e}),(()=>n(e)));return e.output=i,i}class I{constructor(t){this.children=[],this.keys={},this.parent=null,this.key=null,this.allowReorder=!1,this.type="Isolate"}static create(t,e){const n=R(),i=r.setParent(new this(e),n),[s,u]=m.reconcile(this.reconciler,i,t);return r.saveOutput(s,u),E(s),s}}function O(){return p().stateRef.Bus}function k(t,n){const r=O().emit;return e.isNullish(t)||r(t,n),v(r)}I.reconciler=function(t,n){return e.isNullish(n),t};var C=Object.freeze({__proto__:null,useBus:O,useEmit:k,usePrepareEmitter:function(t){const e=k();return n=>e(t,n)}});t.Bus=C,t.Isolate=I,t.IsolateInspector=a,t.IsolateMutator=r,t.Reconciler=m,t.VestRuntime=h,t.Walker=c}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vestjs-runtime",
3
- "version": "0.1.0-dev-ae6b14",
3
+ "version": "0.1.0-next-25c20e",
4
4
  "description": "Internal runtime module used by Vest",
5
5
  "license": "MIT",
6
6
  "author": "ealush",
@@ -18,8 +18,8 @@
18
18
  "url": "https://github.com/ealush/vest.git/issues"
19
19
  },
20
20
  "dependencies": {
21
- "context": "3.0.9-dev-ae6b14",
22
- "vest-utils": "1.0.0-dev-ae6b14"
21
+ "context": "3.0.9-next-25c20e",
22
+ "vest-utils": "1.0.0-next-25c20e"
23
23
  },
24
24
  "exports": {
25
25
  ".": {
@@ -44,5 +44,6 @@
44
44
  },
45
45
  "./package.json": "./package.json",
46
46
  "./*": "./*"
47
- }
47
+ },
48
+ "vxAllowResolve": []
48
49
  }
@@ -1,81 +1,61 @@
1
- import { CB, TinyState, BusType } from "vest-utils";
2
- interface IRecociler {
3
- (currentNode: Isolate, historicNode: Isolate | null): Isolate;
1
+ import { CB, Nullable, TinyState, BusType, DynamicValue } from "vest-utils";
2
+ // I would rather not use `any` here, but instead use `Isolate`.
3
+ // The problem is that it breaks the actual implementation of `Isolate` in `IsolateTest`
4
+ // As it is not properly extending `Isolate`.
5
+ interface IRecociler<I = any> {
6
+ (currentNode: I, historicNode: Nullable<Isolate>): I;
4
7
  }
5
8
  declare class Reconciler {
6
- static reconcile<Callback extends CB = CB>(reconciler: IRecociler, node: Isolate, callback: Callback): [
9
+ static reconcile<Callback extends CB = CB>(reconciler: IRecociler<Isolate>, node: Isolate, callback: Callback): [
7
10
  Isolate,
8
11
  ReturnType<Callback>
9
12
  ];
10
13
  static removeAllNextNodesInIsolate(): void;
11
14
  static handleIsolateNodeWithKey(node: Isolate): Isolate;
12
15
  }
13
- type IsolateKey = null | string;
16
+ type IsolateKey = Nullable<string>;
14
17
  declare class Isolate<_D = any> {
15
- children: Isolate[] | null;
18
+ children: Nullable<Isolate[]>;
16
19
  keys: Record<string, Isolate>;
17
- parent: Isolate | null;
18
- output?: any;
20
+ parent: Nullable<Isolate>;
21
+ output: any;
19
22
  key: IsolateKey;
20
23
  allowReorder: boolean;
21
- static reconciler: IRecociler;
24
+ type: string;
22
25
  // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
23
26
  constructor(_data?: _D);
24
- setParent(parent: Isolate | null): this;
25
- saveOutput(output: any): this;
26
- setKey(key: string | null): this;
27
- usesKey(): boolean;
28
- addChild(child: Isolate): void;
29
- removeChild(node: Isolate): void;
30
- slice(at: number): void;
31
- at(at: number): Isolate | null;
32
- cursor(): number;
33
- shouldAllowReorder(): boolean;
34
- get rootNode(): Isolate;
27
+ static reconciler: IRecociler;
35
28
  static create<Callback extends CB = CB>(callback: Callback, data?: any): Isolate;
36
- private static createImplementation;
37
- static setNode(node: Isolate): void;
38
- static is(node: any): boolean;
39
29
  }
40
30
  declare namespace Walker {
41
- interface IRecociler {
42
- (currentNode: Isolate, historicNode: Isolate | null): Isolate;
31
+ // I would rather not use `any` here, but instead use `Isolate`.
32
+ // The problem is that it breaks the actual implementation of `Isolate` in `IsolateTest`
33
+ // As it is not properly extending `Isolate`.
34
+ interface IRecociler<I = any> {
35
+ (currentNode: I, historicNode: Nullable<Isolate>): I;
43
36
  }
44
- function BaseReconciler(currentNode: Isolate, historicNode: Isolate | null): Isolate;
37
+ function BaseReconciler(currentNode: Isolate, historicNode: Nullable<Isolate>): Isolate;
45
38
  class Reconciler {
46
- static reconcile<Callback extends CB = CB>(reconciler: IRecociler, node: Isolate, callback: Callback): [
39
+ static reconcile<Callback extends CB = CB>(reconciler: IRecociler<Isolate>, node: Isolate, callback: Callback): [
47
40
  Isolate,
48
41
  ReturnType<Callback>
49
42
  ];
50
43
  static removeAllNextNodesInIsolate(): void;
51
44
  static handleIsolateNodeWithKey(node: Isolate): Isolate;
52
45
  }
53
- type IsolateKey = null | string;
46
+ type IsolateKey = Nullable<string>;
54
47
  class Isolate<_D = any> {
55
- children: Isolate[] | null;
48
+ children: Nullable<Isolate[]>;
56
49
  keys: Record<string, Isolate>;
57
- parent: Isolate | null;
58
- output?: any;
50
+ parent: Nullable<Isolate>;
51
+ output: any;
59
52
  key: IsolateKey;
60
53
  allowReorder: boolean;
61
- static reconciler: IRecociler;
54
+ type: string;
62
55
  // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
63
56
  constructor(_data?: _D);
64
- setParent(parent: Isolate | null): this;
65
- saveOutput(output: any): this;
66
- setKey(key: string | null): this;
67
- usesKey(): boolean;
68
- addChild(child: Isolate): void;
69
- removeChild(node: Isolate): void;
70
- slice(at: number): void;
71
- at(at: number): Isolate | null;
72
- cursor(): number;
73
- shouldAllowReorder(): boolean;
74
- get rootNode(): Isolate;
57
+ static reconciler: IRecociler;
75
58
  static create<Callback extends CB = CB>(callback: Callback, data?: any): Isolate;
76
- private static createImplementation;
77
- static setNode(node: Isolate): void;
78
- static is(node: any): boolean;
79
59
  }
80
60
  type VisitOnlyPredicate = (isolate: Isolate) => boolean;
81
61
  // eslint-disable-next-line
@@ -88,7 +68,7 @@ declare namespace Walker {
88
68
  function has(startNode: Isolate, match: VisitOnlyPredicate): boolean;
89
69
  // This function returns the first Isolate object in the tree that satisfies the given predicate function.
90
70
  // If visitOnly is provided, only Isolate objects that satisfy the predicate are visited.
91
- function find(startNode: Isolate, predicate: (node: Isolate) => boolean, visitOnly?: VisitOnlyPredicate): Isolate | null;
71
+ function find(startNode: Isolate, predicate: (node: Isolate) => boolean, visitOnly?: VisitOnlyPredicate): Nullable<Isolate>;
92
72
  // This function returns true if the given predicate function returns true for every Isolate object in the tree.
93
73
  // If visitOnly is provided, only Isolate objects that satisfy the predicate are visited.
94
74
  function every(startNode: Isolate, predicate: (node: Isolate) => boolean, visitOnly?: VisitOnlyPredicate): boolean;
@@ -98,46 +78,62 @@ declare namespace Walker {
98
78
  function pluck(startNode: Isolate, predicate: (node: Isolate) => boolean, visitOnly?: VisitOnlyPredicate): void;
99
79
  // Returns the closest ancestor Isolate object of the given
100
80
  //startNode that satisfies the given predicate function.
101
- function closest(startNode: Isolate, predicate: (node: Isolate) => boolean): Isolate | null;
81
+ function closest(startNode: Isolate, predicate: (node: Isolate) => boolean): Nullable<Isolate>;
102
82
  // This function returns true if the closest ancestor Isolates of the
103
83
  // given startNode that satisfies the given predicate function exists.
104
84
  function closestExists(startNode: Isolate, predicate: (node: Isolate) => boolean): boolean;
105
85
  }
106
86
  type CTXType = StateRefType & {
107
- historyNode: Isolate | null;
108
- runtimeNode: Isolate | null;
109
- runtimeRoot: Isolate | null;
87
+ historyNode: Nullable<Isolate>;
88
+ runtimeNode: Nullable<Isolate>;
89
+ runtimeRoot: Nullable<Isolate>;
110
90
  stateRef: StateRefType;
111
91
  };
112
92
  type StateRefType = {
113
- historyRoot: TinyState<Isolate | null>;
93
+ historyRoot: TinyState<Nullable<Isolate>>;
114
94
  Bus: BusType;
115
95
  appData: Record<string, any>;
116
96
  };
117
97
  declare const RuntimeApi: {
118
98
  Run: <R>(value: Partial<CTXType>, fn: () => R) => R;
99
+ addNodeToHistory: typeof addNodeToHistory;
119
100
  createRef: typeof createRef;
120
101
  persist: typeof persist;
121
102
  reset: typeof reset;
122
103
  useAvailableRoot: typeof useAvailableRoot;
123
- useBus: typeof useBus;
124
104
  useCurrentCursor: typeof useCurrentCursor;
125
- useEmit: typeof useEmit;
126
- usePrepareEmitter: typeof usePrepareEmitter;
127
105
  useXAppData: typeof useXAppData;
128
106
  };
129
- declare function useBus(): BusType;
130
- /*
131
- Returns an emitter, but it also has a shortcut for emitting an event immediately
132
- by passing an event name.
133
- */
134
- declare function useEmit(): (event: string, ...args: any[]) => void;
135
- declare function usePrepareEmitter<T = void>(event: string): (arg: T) => void;
136
107
  declare function useXAppData<T = object>(): T;
137
- declare function createRef(setter: Record<string, any> | (() => Record<string, any>)): StateRefType;
138
- declare function persist<T extends CB>(cb: T): T;
108
+ declare function createRef(setter: DynamicValue<Record<string, any>>): StateRefType;
109
+ declare function persist<T extends (...args: any[]) => any>(cb: T): T;
110
+ declare function addNodeToHistory(node: Isolate): void;
139
111
  declare function useCurrentCursor(): number;
140
112
  declare function useAvailableRoot<I extends Isolate = Isolate>(): I;
141
113
  declare function reset(): void;
142
- export { Isolate, IsolateKey, Reconciler, IRecociler, Walker, RuntimeApi as VestRuntime };
114
+ declare class IsolateInspector {
115
+ static at(isolate: Nullable<Isolate>, at: number): Nullable<Isolate>;
116
+ static cursor(isolate: Nullable<Isolate>): number;
117
+ static shouldAllowReorder(isolate: Nullable<Isolate>): boolean;
118
+ static usesKey(isolate: Nullable<Isolate>): boolean;
119
+ static dump(isolate: Isolate): string;
120
+ }
121
+ declare class IsolateMutator {
122
+ static setParent(isolate: Isolate, parent: Nullable<Isolate>): Isolate;
123
+ static saveOutput(isolate: Isolate, output: any): Isolate;
124
+ static setKey(isolate: Isolate, key: Nullable<string>): Isolate;
125
+ static addChild(isolate: Isolate, child: Isolate): void;
126
+ static removeChild(isolate: Isolate, node: Isolate): void;
127
+ static slice(isolate: Isolate, at: number): void;
128
+ }
129
+ declare namespace Bus {
130
+ function useBus(): import("vest-utils").BusType;
131
+ /*
132
+ Returns an emitter, but it also has a shortcut for emitting an event immediately
133
+ by passing an event name.
134
+ */
135
+ function useEmit(event?: string, data?: any): (event: string, data?: any) => void;
136
+ function usePrepareEmitter<T = void>(event: string): (arg: T) => void;
137
+ }
138
+ export { IsolateKey, Isolate, Reconciler, IRecociler, Walker, RuntimeApi as VestRuntime, IsolateInspector, IsolateMutator, Bus };
143
139
  //# sourceMappingURL=vestjs-runtime.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"vestjs-runtime.d.ts","sourceRoot":"","sources":["../src/vestjs-runtime.ts","../src/IsolateWalker.ts","../src/errors/ErrorStrings.ts","../src/VestRuntime.ts","../src/Reconciler.ts","../src/Isolate.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,kFAAuB,CAAgB"}
1
+ {"version":3,"file":"vestjs-runtime.d.ts","sourceRoot":"","sources":["../src/vestjs-runtime.ts","../src/Isolate/IsolateMutator.ts","../src/IsolateWalker.ts","../src/Isolate/IsolateInspector.ts","../src/errors/ErrorStrings.ts","../src/VestRuntime.ts","../src/Reconciler.ts","../src/Isolate/Isolate.ts","../src/Bus.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,yHAAuB,CAAgB"}