@valbuild/shared 0.57.0 → 0.59.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.
@@ -5,6 +5,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var core = require('@valbuild/core');
6
6
  var marked = require('marked');
7
7
  var zod = require('zod');
8
+ var fp = require('@valbuild/core/fp');
9
+ var patch = require('@valbuild/core/patch');
8
10
 
9
11
  function _interopNamespace(e) {
10
12
  if (e && e.__esModule) return e;
@@ -1170,12 +1172,10 @@ function parseTokens(tokens, sourceNodes, cursor) {
1170
1172
  if (i === lines.length - 1) return [line];
1171
1173
  if (i === lines.length - 1 && line === "") return [];
1172
1174
  if (line === "") return {
1173
- tag: "br",
1174
- children: []
1175
+ tag: "br"
1175
1176
  };
1176
1177
  return [line, {
1177
- tag: "br",
1178
- children: []
1178
+ tag: "br"
1179
1179
  }];
1180
1180
  });
1181
1181
  children.push.apply(children, _toConsumableArray(tags));
@@ -1234,8 +1234,7 @@ function parseTokens(tokens, sourceNodes, cursor) {
1234
1234
  src: core.Internal.convertFileSource(sourceNode).url,
1235
1235
  width: (_sourceNode$metadata = sourceNode.metadata) === null || _sourceNode$metadata === void 0 ? void 0 : _sourceNode$metadata.width,
1236
1236
  height: (_sourceNode$metadata2 = sourceNode.metadata) === null || _sourceNode$metadata2 === void 0 ? void 0 : _sourceNode$metadata2.height,
1237
- mimeType: (_sourceNode$metadata3 = sourceNode.metadata) === null || _sourceNode$metadata3 === void 0 ? void 0 : _sourceNode$metadata3.mimeType,
1238
- children: []
1237
+ mimeType: (_sourceNode$metadata3 = sourceNode.metadata) === null || _sourceNode$metadata3 === void 0 ? void 0 : _sourceNode$metadata3.mimeType
1239
1238
  });
1240
1239
  }
1241
1240
  cursor = subCursor;
@@ -1244,8 +1243,7 @@ function parseTokens(tokens, sourceNodes, cursor) {
1244
1243
  if (token.text.trim().match(br_html_regex)) {
1245
1244
  var _tokens;
1246
1245
  children.push({
1247
- tag: "br",
1248
- children: []
1246
+ tag: "br"
1249
1247
  });
1250
1248
  if (((_tokens = tokens[cursor + 1]) === null || _tokens === void 0 ? void 0 : _tokens.raw.trim()) === "") {
1251
1249
  // if next token is a new line or white-spaces, skip it
@@ -1266,8 +1264,7 @@ function parseTokens(tokens, sourceNodes, cursor) {
1266
1264
  }
1267
1265
  } else if (token.type === "br") {
1268
1266
  children.push({
1269
- tag: "br",
1270
- children: []
1267
+ tag: "br"
1271
1268
  });
1272
1269
  } else {
1273
1270
  console.error("Could not parse markdown: unsupported token type: ".concat(token.type, ". Found: ").concat(token.raw));
@@ -1645,6 +1642,725 @@ var VAL_SESSION_COOKIE = core.Internal.VAL_SESSION_COOKIE;
1645
1642
  var VAL_STATE_COOKIE = core.Internal.VAL_STATE_COOKIE;
1646
1643
  var VAL_ENABLE_COOKIE_NAME = core.Internal.VAL_ENABLE_COOKIE_NAME;
1647
1644
 
1645
+ function _regeneratorRuntime() {
1646
+ _regeneratorRuntime = function () {
1647
+ return e;
1648
+ };
1649
+ var t,
1650
+ e = {},
1651
+ r = Object.prototype,
1652
+ n = r.hasOwnProperty,
1653
+ o = Object.defineProperty || function (t, e, r) {
1654
+ t[e] = r.value;
1655
+ },
1656
+ i = "function" == typeof Symbol ? Symbol : {},
1657
+ a = i.iterator || "@@iterator",
1658
+ c = i.asyncIterator || "@@asyncIterator",
1659
+ u = i.toStringTag || "@@toStringTag";
1660
+ function define(t, e, r) {
1661
+ return Object.defineProperty(t, e, {
1662
+ value: r,
1663
+ enumerable: !0,
1664
+ configurable: !0,
1665
+ writable: !0
1666
+ }), t[e];
1667
+ }
1668
+ try {
1669
+ define({}, "");
1670
+ } catch (t) {
1671
+ define = function (t, e, r) {
1672
+ return t[e] = r;
1673
+ };
1674
+ }
1675
+ function wrap(t, e, r, n) {
1676
+ var i = e && e.prototype instanceof Generator ? e : Generator,
1677
+ a = Object.create(i.prototype),
1678
+ c = new Context(n || []);
1679
+ return o(a, "_invoke", {
1680
+ value: makeInvokeMethod(t, r, c)
1681
+ }), a;
1682
+ }
1683
+ function tryCatch(t, e, r) {
1684
+ try {
1685
+ return {
1686
+ type: "normal",
1687
+ arg: t.call(e, r)
1688
+ };
1689
+ } catch (t) {
1690
+ return {
1691
+ type: "throw",
1692
+ arg: t
1693
+ };
1694
+ }
1695
+ }
1696
+ e.wrap = wrap;
1697
+ var h = "suspendedStart",
1698
+ l = "suspendedYield",
1699
+ f = "executing",
1700
+ s = "completed",
1701
+ y = {};
1702
+ function Generator() {}
1703
+ function GeneratorFunction() {}
1704
+ function GeneratorFunctionPrototype() {}
1705
+ var p = {};
1706
+ define(p, a, function () {
1707
+ return this;
1708
+ });
1709
+ var d = Object.getPrototypeOf,
1710
+ v = d && d(d(values([])));
1711
+ v && v !== r && n.call(v, a) && (p = v);
1712
+ var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
1713
+ function defineIteratorMethods(t) {
1714
+ ["next", "throw", "return"].forEach(function (e) {
1715
+ define(t, e, function (t) {
1716
+ return this._invoke(e, t);
1717
+ });
1718
+ });
1719
+ }
1720
+ function AsyncIterator(t, e) {
1721
+ function invoke(r, o, i, a) {
1722
+ var c = tryCatch(t[r], t, o);
1723
+ if ("throw" !== c.type) {
1724
+ var u = c.arg,
1725
+ h = u.value;
1726
+ return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
1727
+ invoke("next", t, i, a);
1728
+ }, function (t) {
1729
+ invoke("throw", t, i, a);
1730
+ }) : e.resolve(h).then(function (t) {
1731
+ u.value = t, i(u);
1732
+ }, function (t) {
1733
+ return invoke("throw", t, i, a);
1734
+ });
1735
+ }
1736
+ a(c.arg);
1737
+ }
1738
+ var r;
1739
+ o(this, "_invoke", {
1740
+ value: function (t, n) {
1741
+ function callInvokeWithMethodAndArg() {
1742
+ return new e(function (e, r) {
1743
+ invoke(t, n, e, r);
1744
+ });
1745
+ }
1746
+ return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
1747
+ }
1748
+ });
1749
+ }
1750
+ function makeInvokeMethod(e, r, n) {
1751
+ var o = h;
1752
+ return function (i, a) {
1753
+ if (o === f) throw new Error("Generator is already running");
1754
+ if (o === s) {
1755
+ if ("throw" === i) throw a;
1756
+ return {
1757
+ value: t,
1758
+ done: !0
1759
+ };
1760
+ }
1761
+ for (n.method = i, n.arg = a;;) {
1762
+ var c = n.delegate;
1763
+ if (c) {
1764
+ var u = maybeInvokeDelegate(c, n);
1765
+ if (u) {
1766
+ if (u === y) continue;
1767
+ return u;
1768
+ }
1769
+ }
1770
+ if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
1771
+ if (o === h) throw o = s, n.arg;
1772
+ n.dispatchException(n.arg);
1773
+ } else "return" === n.method && n.abrupt("return", n.arg);
1774
+ o = f;
1775
+ var p = tryCatch(e, r, n);
1776
+ if ("normal" === p.type) {
1777
+ if (o = n.done ? s : l, p.arg === y) continue;
1778
+ return {
1779
+ value: p.arg,
1780
+ done: n.done
1781
+ };
1782
+ }
1783
+ "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
1784
+ }
1785
+ };
1786
+ }
1787
+ function maybeInvokeDelegate(e, r) {
1788
+ var n = r.method,
1789
+ o = e.iterator[n];
1790
+ if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
1791
+ var i = tryCatch(o, e.iterator, r.arg);
1792
+ if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
1793
+ var a = i.arg;
1794
+ return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
1795
+ }
1796
+ function pushTryEntry(t) {
1797
+ var e = {
1798
+ tryLoc: t[0]
1799
+ };
1800
+ 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
1801
+ }
1802
+ function resetTryEntry(t) {
1803
+ var e = t.completion || {};
1804
+ e.type = "normal", delete e.arg, t.completion = e;
1805
+ }
1806
+ function Context(t) {
1807
+ this.tryEntries = [{
1808
+ tryLoc: "root"
1809
+ }], t.forEach(pushTryEntry, this), this.reset(!0);
1810
+ }
1811
+ function values(e) {
1812
+ if (e || "" === e) {
1813
+ var r = e[a];
1814
+ if (r) return r.call(e);
1815
+ if ("function" == typeof e.next) return e;
1816
+ if (!isNaN(e.length)) {
1817
+ var o = -1,
1818
+ i = function next() {
1819
+ for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
1820
+ return next.value = t, next.done = !0, next;
1821
+ };
1822
+ return i.next = i;
1823
+ }
1824
+ }
1825
+ throw new TypeError(typeof e + " is not iterable");
1826
+ }
1827
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
1828
+ value: GeneratorFunctionPrototype,
1829
+ configurable: !0
1830
+ }), o(GeneratorFunctionPrototype, "constructor", {
1831
+ value: GeneratorFunction,
1832
+ configurable: !0
1833
+ }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
1834
+ var e = "function" == typeof t && t.constructor;
1835
+ return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
1836
+ }, e.mark = function (t) {
1837
+ return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
1838
+ }, e.awrap = function (t) {
1839
+ return {
1840
+ __await: t
1841
+ };
1842
+ }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
1843
+ return this;
1844
+ }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
1845
+ void 0 === i && (i = Promise);
1846
+ var a = new AsyncIterator(wrap(t, r, n, o), i);
1847
+ return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
1848
+ return t.done ? t.value : a.next();
1849
+ });
1850
+ }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
1851
+ return this;
1852
+ }), define(g, "toString", function () {
1853
+ return "[object Generator]";
1854
+ }), e.keys = function (t) {
1855
+ var e = Object(t),
1856
+ r = [];
1857
+ for (var n in e) r.push(n);
1858
+ return r.reverse(), function next() {
1859
+ for (; r.length;) {
1860
+ var t = r.pop();
1861
+ if (t in e) return next.value = t, next.done = !1, next;
1862
+ }
1863
+ return next.done = !0, next;
1864
+ };
1865
+ }, e.values = values, Context.prototype = {
1866
+ constructor: Context,
1867
+ reset: function (e) {
1868
+ if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
1869
+ },
1870
+ stop: function () {
1871
+ this.done = !0;
1872
+ var t = this.tryEntries[0].completion;
1873
+ if ("throw" === t.type) throw t.arg;
1874
+ return this.rval;
1875
+ },
1876
+ dispatchException: function (e) {
1877
+ if (this.done) throw e;
1878
+ var r = this;
1879
+ function handle(n, o) {
1880
+ return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
1881
+ }
1882
+ for (var o = this.tryEntries.length - 1; o >= 0; --o) {
1883
+ var i = this.tryEntries[o],
1884
+ a = i.completion;
1885
+ if ("root" === i.tryLoc) return handle("end");
1886
+ if (i.tryLoc <= this.prev) {
1887
+ var c = n.call(i, "catchLoc"),
1888
+ u = n.call(i, "finallyLoc");
1889
+ if (c && u) {
1890
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
1891
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
1892
+ } else if (c) {
1893
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
1894
+ } else {
1895
+ if (!u) throw new Error("try statement without catch or finally");
1896
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
1897
+ }
1898
+ }
1899
+ }
1900
+ },
1901
+ abrupt: function (t, e) {
1902
+ for (var r = this.tryEntries.length - 1; r >= 0; --r) {
1903
+ var o = this.tryEntries[r];
1904
+ if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
1905
+ var i = o;
1906
+ break;
1907
+ }
1908
+ }
1909
+ i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
1910
+ var a = i ? i.completion : {};
1911
+ return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
1912
+ },
1913
+ complete: function (t, e) {
1914
+ if ("throw" === t.type) throw t.arg;
1915
+ return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
1916
+ },
1917
+ finish: function (t) {
1918
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
1919
+ var r = this.tryEntries[e];
1920
+ if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
1921
+ }
1922
+ },
1923
+ catch: function (t) {
1924
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
1925
+ var r = this.tryEntries[e];
1926
+ if (r.tryLoc === t) {
1927
+ var n = r.completion;
1928
+ if ("throw" === n.type) {
1929
+ var o = n.arg;
1930
+ resetTryEntry(r);
1931
+ }
1932
+ return o;
1933
+ }
1934
+ }
1935
+ throw new Error("illegal catch attempt");
1936
+ },
1937
+ delegateYield: function (e, r, n) {
1938
+ return this.delegate = {
1939
+ iterator: values(e),
1940
+ resultName: r,
1941
+ nextLoc: n
1942
+ }, "next" === this.method && (this.arg = t), y;
1943
+ }
1944
+ }, e;
1945
+ }
1946
+
1947
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
1948
+ try {
1949
+ var info = gen[key](arg);
1950
+ var value = info.value;
1951
+ } catch (error) {
1952
+ reject(error);
1953
+ return;
1954
+ }
1955
+ if (info.done) {
1956
+ resolve(value);
1957
+ } else {
1958
+ Promise.resolve(value).then(_next, _throw);
1959
+ }
1960
+ }
1961
+ function _asyncToGenerator(fn) {
1962
+ return function () {
1963
+ var self = this,
1964
+ args = arguments;
1965
+ return new Promise(function (resolve, reject) {
1966
+ var gen = fn.apply(self, args);
1967
+ function _next(value) {
1968
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
1969
+ }
1970
+ function _throw(err) {
1971
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
1972
+ }
1973
+ _next(undefined);
1974
+ });
1975
+ };
1976
+ }
1977
+
1978
+ function _classCallCheck(instance, Constructor) {
1979
+ if (!(instance instanceof Constructor)) {
1980
+ throw new TypeError("Cannot call a class as a function");
1981
+ }
1982
+ }
1983
+
1984
+ function _defineProperties(target, props) {
1985
+ for (var i = 0; i < props.length; i++) {
1986
+ var descriptor = props[i];
1987
+ descriptor.enumerable = descriptor.enumerable || false;
1988
+ descriptor.configurable = true;
1989
+ if ("value" in descriptor) descriptor.writable = true;
1990
+ Object.defineProperty(target, toPropertyKey(descriptor.key), descriptor);
1991
+ }
1992
+ }
1993
+ function _createClass(Constructor, protoProps, staticProps) {
1994
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
1995
+ if (staticProps) _defineProperties(Constructor, staticProps);
1996
+ Object.defineProperty(Constructor, "prototype", {
1997
+ writable: false
1998
+ });
1999
+ return Constructor;
2000
+ }
2001
+
2002
+ var ops = new patch.JSONOps();
2003
+ var ValStore = /*#__PURE__*/function () {
2004
+ // uncertain whether this is the optimal way of returning
2005
+
2006
+ function ValStore(api) {
2007
+ var _this = this;
2008
+ _classCallCheck(this, ValStore);
2009
+ _defineProperty(this, "subscribe", function (moduleIds) {
2010
+ return function (listener) {
2011
+ var subscriberId = createSubscriberId(moduleIds);
2012
+ if (!_this.listeners[subscriberId]) {
2013
+ _this.listeners[subscriberId] = [];
2014
+ _this.subscribers.set(subscriberId, {});
2015
+ }
2016
+ _this.listeners[subscriberId].push(listener);
2017
+ return function () {
2018
+ _this.listeners[subscriberId].splice(_this.listeners[subscriberId].indexOf(listener), 1);
2019
+ };
2020
+ };
2021
+ });
2022
+ _defineProperty(this, "getSnapshot", function (moduleIds) {
2023
+ return function () {
2024
+ return _this.get(moduleIds);
2025
+ };
2026
+ });
2027
+ _defineProperty(this, "getServerSnapshot", function (moduleIds) {
2028
+ return function () {
2029
+ return _this.get(moduleIds);
2030
+ };
2031
+ });
2032
+ _defineProperty(this, "get", function (moduleIds) {
2033
+ var subscriberId = createSubscriberId(moduleIds);
2034
+ return _this.subscribers.get(subscriberId);
2035
+ });
2036
+ this.api = api;
2037
+ this.subscribers = new Map();
2038
+ this.listeners = {};
2039
+ this.drafts = {};
2040
+ this.schema = {};
2041
+ }
2042
+ _createClass(ValStore, [{
2043
+ key: "getModule",
2044
+ value: function () {
2045
+ var _getModule = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(moduleId) {
2046
+ var refetch,
2047
+ data,
2048
+ fetchedSource,
2049
+ fetchedSchema,
2050
+ _args = arguments;
2051
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2052
+ while (1) switch (_context.prev = _context.next) {
2053
+ case 0:
2054
+ refetch = _args.length > 1 && _args[1] !== undefined ? _args[1] : false;
2055
+ if (!(!refetch && this.drafts[moduleId] && this.schema[moduleId])) {
2056
+ _context.next = 3;
2057
+ break;
2058
+ }
2059
+ return _context.abrupt("return", fp.result.ok({
2060
+ source: this.drafts[moduleId],
2061
+ schema: this.schema[moduleId]
2062
+ }));
2063
+ case 3:
2064
+ _context.next = 5;
2065
+ return this.api.getTree({
2066
+ patch: true,
2067
+ treePath: moduleId,
2068
+ includeSource: true
2069
+ });
2070
+ case 5:
2071
+ data = _context.sent;
2072
+ if (!fp.result.isOk(data)) {
2073
+ _context.next = 19;
2074
+ break;
2075
+ }
2076
+ fetchedSource = data.value.modules[moduleId].source;
2077
+ fetchedSchema = data.value.modules[moduleId].schema;
2078
+ if (!(fetchedSource !== undefined && fetchedSchema !== undefined)) {
2079
+ _context.next = 15;
2080
+ break;
2081
+ }
2082
+ this.drafts[moduleId] = fetchedSource;
2083
+ this.schema[moduleId] = fetchedSchema;
2084
+ return _context.abrupt("return", fp.result.ok({
2085
+ source: fetchedSource,
2086
+ schema: fetchedSchema
2087
+ }));
2088
+ case 15:
2089
+ console.error("Val: could not find the module source");
2090
+ return _context.abrupt("return", fp.result.err({
2091
+ message: "Could not fetch data. Verify that the module exists."
2092
+ }));
2093
+ case 17:
2094
+ _context.next = 21;
2095
+ break;
2096
+ case 19:
2097
+ console.error("Val: failed to get module", data.error);
2098
+ return _context.abrupt("return", fp.result.err({
2099
+ message: "Could not fetch data. Verify that Val is correctly configured."
2100
+ }));
2101
+ case 21:
2102
+ case "end":
2103
+ return _context.stop();
2104
+ }
2105
+ }, _callee, this);
2106
+ }));
2107
+ function getModule(_x) {
2108
+ return _getModule.apply(this, arguments);
2109
+ }
2110
+ return getModule;
2111
+ }()
2112
+ }, {
2113
+ key: "applyPatch",
2114
+ value: function () {
2115
+ var _applyPatch2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(moduleId, patch$1) {
2116
+ var currentSource, currentSchema, data, fetchedSource, fetchedSchema, res, patchRes, _i, _Array$from, _Array$from$_i, subscriberId, subscriberModules;
2117
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
2118
+ while (1) switch (_context2.prev = _context2.next) {
2119
+ case 0:
2120
+ currentSource = this.drafts[moduleId];
2121
+ currentSchema = this.schema[moduleId];
2122
+ if (!(!currentSource || !currentSchema)) {
2123
+ _context2.next = 21;
2124
+ break;
2125
+ }
2126
+ _context2.next = 5;
2127
+ return this.api.getTree({
2128
+ patch: true,
2129
+ treePath: moduleId,
2130
+ includeSource: true
2131
+ });
2132
+ case 5:
2133
+ data = _context2.sent;
2134
+ if (!fp.result.isOk(data)) {
2135
+ _context2.next = 19;
2136
+ break;
2137
+ }
2138
+ fetchedSource = data.value.modules[moduleId].source;
2139
+ fetchedSchema = data.value.modules[moduleId].schema;
2140
+ if (!(fetchedSource !== undefined && fetchedSchema !== undefined)) {
2141
+ _context2.next = 15;
2142
+ break;
2143
+ }
2144
+ currentSource = fetchedSource;
2145
+ this.drafts[moduleId] = fetchedSource;
2146
+ this.schema[moduleId] = fetchedSchema;
2147
+ _context2.next = 17;
2148
+ break;
2149
+ case 15:
2150
+ console.error("Val: could not find the module source");
2151
+ return _context2.abrupt("return", fp.result.err({
2152
+ message: "Val: could not fetch data. Verify that the module exists."
2153
+ }));
2154
+ case 17:
2155
+ _context2.next = 21;
2156
+ break;
2157
+ case 19:
2158
+ console.error("Val: failed to get module", data.error);
2159
+ return _context2.abrupt("return", fp.result.err({
2160
+ message: "Val: could not fetch data. Verify that Val is correctly configured."
2161
+ }));
2162
+ case 21:
2163
+ _context2.next = 23;
2164
+ return this.api.postPatches(moduleId, patch$1);
2165
+ case 23:
2166
+ res = _context2.sent;
2167
+ if (!fp.result.isErr(res)) {
2168
+ _context2.next = 27;
2169
+ break;
2170
+ }
2171
+ console.error("Val: failed to post patch", res.error);
2172
+ return _context2.abrupt("return", res);
2173
+ case 27:
2174
+ patchRes = patch.applyPatch(currentSource, ops, patch$1.filter(core.Internal.notFileOp) // we cannot apply file ops here
2175
+ );
2176
+ if (!fp.result.isOk(patchRes)) {
2177
+ _context2.next = 34;
2178
+ break;
2179
+ }
2180
+ this.drafts[moduleId] = patchRes.value;
2181
+ for (_i = 0, _Array$from = Array.from(this.subscribers.entries()); _i < _Array$from.length; _i++) {
2182
+ _Array$from$_i = _slicedToArray(_Array$from[_i], 2), subscriberId = _Array$from$_i[0], subscriberModules = _Array$from$_i[1];
2183
+ if (subscriberModules[moduleId]) {
2184
+ this.subscribers.set(subscriberId, _objectSpread2(_objectSpread2({}, subscriberModules), {}, _defineProperty({}, moduleId, this.drafts[moduleId])));
2185
+ this.emitChange(subscriberId);
2186
+ }
2187
+ }
2188
+ return _context2.abrupt("return", res);
2189
+ case 34:
2190
+ console.error("Val: failed to apply patch", patchRes.error);
2191
+ return _context2.abrupt("return", patchRes);
2192
+ case 36:
2193
+ case "end":
2194
+ return _context2.stop();
2195
+ }
2196
+ }, _callee2, this);
2197
+ }));
2198
+ function applyPatch(_x2, _x3) {
2199
+ return _applyPatch2.apply(this, arguments);
2200
+ }
2201
+ return applyPatch;
2202
+ }()
2203
+ }, {
2204
+ key: "update",
2205
+ value: function () {
2206
+ var _update = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(moduleIds) {
2207
+ var _this2 = this;
2208
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
2209
+ while (1) switch (_context3.prev = _context3.next) {
2210
+ case 0:
2211
+ _context3.next = 2;
2212
+ return Promise.all(moduleIds.map(function (moduleId) {
2213
+ return _this2.updateTree(moduleId);
2214
+ }));
2215
+ case 2:
2216
+ case "end":
2217
+ return _context3.stop();
2218
+ }
2219
+ }, _callee3);
2220
+ }));
2221
+ function update(_x4) {
2222
+ return _update.apply(this, arguments);
2223
+ }
2224
+ return update;
2225
+ }()
2226
+ }, {
2227
+ key: "reset",
2228
+ value: function () {
2229
+ var _reset = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
2230
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
2231
+ while (1) switch (_context4.prev = _context4.next) {
2232
+ case 0:
2233
+ return _context4.abrupt("return", this.updateTree());
2234
+ case 1:
2235
+ case "end":
2236
+ return _context4.stop();
2237
+ }
2238
+ }, _callee4, this);
2239
+ }));
2240
+ function reset() {
2241
+ return _reset.apply(this, arguments);
2242
+ }
2243
+ return reset;
2244
+ }()
2245
+ }, {
2246
+ key: "updateTree",
2247
+ value: function () {
2248
+ var _updateTree = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(treePath) {
2249
+ var data, moduleIds, updatedSubscriberIds, subscriberIds, _loop, _i2, _arr, _i3, _Array$from2, _Array$from2$_i, updatedSubscriberId, _moduleIds, subscriberModules, msg;
2250
+ return _regeneratorRuntime().wrap(function _callee5$(_context6) {
2251
+ while (1) switch (_context6.prev = _context6.next) {
2252
+ case 0:
2253
+ _context6.next = 2;
2254
+ return this.api.getTree({
2255
+ patch: true,
2256
+ treePath: treePath,
2257
+ includeSource: true
2258
+ });
2259
+ case 2:
2260
+ data = _context6.sent;
2261
+ moduleIds = [];
2262
+ if (!fp.result.isOk(data)) {
2263
+ _context6.next = 18;
2264
+ break;
2265
+ }
2266
+ updatedSubscriberIds = new Map();
2267
+ subscriberIds = Array.from(this.subscribers.keys()); // Figure out which modules have been updated and map to updated subscribed id
2268
+ _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
2269
+ var moduleId, source, updatedSubscriberId;
2270
+ return _regeneratorRuntime().wrap(function _loop$(_context5) {
2271
+ while (1) switch (_context5.prev = _context5.next) {
2272
+ case 0:
2273
+ moduleId = _arr[_i2];
2274
+ moduleIds.push(moduleId);
2275
+ source = data.value.modules[moduleId].source;
2276
+ if (typeof source !== "undefined") {
2277
+ updatedSubscriberId = subscriberIds.find(function (subscriberId) {
2278
+ return subscriberId.includes(moduleId);
2279
+ } // NOTE: dependent on
2280
+ );
2281
+ if (updatedSubscriberId) {
2282
+ updatedSubscriberIds.set(updatedSubscriberId, (updatedSubscriberIds.get(updatedSubscriberId) || []).concat(moduleId));
2283
+ }
2284
+ }
2285
+ case 4:
2286
+ case "end":
2287
+ return _context5.stop();
2288
+ }
2289
+ }, _loop);
2290
+ });
2291
+ _i2 = 0, _arr = Object.keys(data.value.modules);
2292
+ case 9:
2293
+ if (!(_i2 < _arr.length)) {
2294
+ _context6.next = 14;
2295
+ break;
2296
+ }
2297
+ return _context6.delegateYield(_loop(), "t0", 11);
2298
+ case 11:
2299
+ _i2++;
2300
+ _context6.next = 9;
2301
+ break;
2302
+ case 14:
2303
+ // For all updated subscribers: set new module data and emit change
2304
+ for (_i3 = 0, _Array$from2 = Array.from(updatedSubscriberIds.entries()); _i3 < _Array$from2.length; _i3++) {
2305
+ _Array$from2$_i = _slicedToArray(_Array$from2[_i3], 2), updatedSubscriberId = _Array$from2$_i[0], _moduleIds = _Array$from2$_i[1];
2306
+ subscriberModules = Object.fromEntries(_moduleIds.flatMap(function (moduleId) {
2307
+ var source = data.value.modules[moduleId].source;
2308
+ if (!source) {
2309
+ return [];
2310
+ }
2311
+ return [[moduleId, source]];
2312
+ }));
2313
+ this.subscribers.set(updatedSubscriberId, subscriberModules);
2314
+ this.emitChange(updatedSubscriberId);
2315
+ }
2316
+ return _context6.abrupt("return", fp.result.ok(moduleIds));
2317
+ case 18:
2318
+ msg = "Failed to fetch content. ";
2319
+ if (data.error.statusCode === 401) {
2320
+ msg += "Authorization failed - check that you are logged in.";
2321
+ } else {
2322
+ msg += "Get a developer to verify that Val is correctly setup.";
2323
+ }
2324
+ return _context6.abrupt("return", fp.result.err({
2325
+ message: msg,
2326
+ details: {
2327
+ fetchError: data.error
2328
+ }
2329
+ }));
2330
+ case 21:
2331
+ case "end":
2332
+ return _context6.stop();
2333
+ }
2334
+ }, _callee5, this);
2335
+ }));
2336
+ function updateTree(_x5) {
2337
+ return _updateTree.apply(this, arguments);
2338
+ }
2339
+ return updateTree;
2340
+ }()
2341
+ }, {
2342
+ key: "emitChange",
2343
+ value: function emitChange(subscriberId) {
2344
+ var _iterator = _createForOfIteratorHelper(this.listeners[subscriberId]),
2345
+ _step;
2346
+ try {
2347
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
2348
+ var listener = _step.value;
2349
+ listener();
2350
+ }
2351
+ } catch (err) {
2352
+ _iterator.e(err);
2353
+ } finally {
2354
+ _iterator.f();
2355
+ }
2356
+ }
2357
+ }]);
2358
+ return ValStore;
2359
+ }();
2360
+ function createSubscriberId(moduleIds) {
2361
+ return moduleIds.slice().sort().join("&");
2362
+ }
2363
+
1648
2364
  exports.EXT_TO_MIME_TYPES = EXT_TO_MIME_TYPES;
1649
2365
  exports.MIME_TYPES_TO_EXT = MIME_TYPES_TO_EXT;
1650
2366
  exports.RemirrorBr = RemirrorBr;
@@ -1661,6 +2377,7 @@ exports.RemirrorTextMark = RemirrorTextMark;
1661
2377
  exports.VAL_ENABLE_COOKIE_NAME = VAL_ENABLE_COOKIE_NAME;
1662
2378
  exports.VAL_SESSION_COOKIE = VAL_SESSION_COOKIE;
1663
2379
  exports.VAL_STATE_COOKIE = VAL_STATE_COOKIE;
2380
+ exports.ValStore = ValStore;
1664
2381
  exports.filenameToMimeType = filenameToMimeType;
1665
2382
  exports.getMimeType = getMimeType;
1666
2383
  exports.mimeTypeToFileExt = mimeTypeToFileExt;