static.cjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const equalFn = (a, b) => a === b;
  4. const ERROR = Symbol("error");
  5. const UNOWNED = {
  6. context: null,
  7. owner: null
  8. };
  9. let Owner = null;
  10. function createRoot(fn, detachedOwner) {
  11. detachedOwner && (Owner = detachedOwner);
  12. const owner = Owner,
  13. root = fn.length === 0 ? UNOWNED : {
  14. context: null,
  15. owner
  16. };
  17. Owner = root;
  18. let result;
  19. try {
  20. result = fn(() => {});
  21. } catch (err) {
  22. const fns = lookup(Owner, ERROR);
  23. if (!fns) throw err;
  24. fns.forEach(f => f(err));
  25. } finally {
  26. Owner = owner;
  27. }
  28. return result;
  29. }
  30. function createSignal(value) {
  31. return [() => value, v => value = v];
  32. }
  33. function createComputed(fn, value) {
  34. Owner = {
  35. owner: Owner,
  36. context: null
  37. };
  38. fn(value);
  39. Owner = Owner.owner;
  40. }
  41. const createRenderEffect = createComputed;
  42. function createEffect(fn, value) {}
  43. function createMemo(fn, value) {
  44. Owner = {
  45. owner: Owner,
  46. context: null
  47. };
  48. const v = fn(value);
  49. Owner = Owner.owner;
  50. return () => v;
  51. }
  52. function createDeferred(source) {
  53. return source;
  54. }
  55. function createSelector(source, fn) {
  56. return k => fn(k, source());
  57. }
  58. function batch(fn) {
  59. return fn();
  60. }
  61. const untrack = batch;
  62. function on(deps, fn, options = {}) {
  63. let isArray = Array.isArray(deps);
  64. let defer = options.defer;
  65. return () => {
  66. if (defer) return undefined;
  67. let value;
  68. if (isArray) {
  69. value = [];
  70. for (let i = 0; i < deps.length; i++) value.push(deps[i]());
  71. } else value = deps();
  72. return fn(value, undefined);
  73. };
  74. }
  75. function onMount(fn) {}
  76. function onCleanup(fn) {}
  77. function onError(fn) {
  78. if (Owner === null) console.warn("error handlers created outside a `createRoot` or `render` will never be run");else if (Owner.context === null) Owner.context = {
  79. [ERROR]: [fn]
  80. };else if (!Owner.context[ERROR]) Owner.context[ERROR] = [fn];else Owner.context[ERROR].push(fn);
  81. }
  82. function getListener() {
  83. return null;
  84. }
  85. function createContext(defaultValue) {
  86. const id = Symbol("context");
  87. return {
  88. id,
  89. Provider: createProvider(id),
  90. defaultValue
  91. };
  92. }
  93. function useContext(context) {
  94. return lookup(Owner, context.id) || context.defaultValue;
  95. }
  96. function getOwner() {
  97. return Owner;
  98. }
  99. function children(fn) {
  100. return createMemo(() => resolveChildren(fn()));
  101. }
  102. function runWithOwner(o, fn) {
  103. const prev = Owner;
  104. Owner = o;
  105. try {
  106. return fn();
  107. } finally {
  108. Owner = prev;
  109. }
  110. }
  111. function lookup(owner, key) {
  112. return owner && (owner.context && owner.context[key] || owner.owner && lookup(owner.owner, key));
  113. }
  114. function resolveChildren(children) {
  115. if (typeof children === "function" && !children.length) return resolveChildren(children());
  116. if (Array.isArray(children)) {
  117. const results = [];
  118. for (let i = 0; i < children.length; i++) {
  119. let result = resolveChildren(children[i]);
  120. Array.isArray(result) ? results.push.apply(results, result) : results.push(result);
  121. }
  122. return results;
  123. }
  124. return children;
  125. }
  126. function createProvider(id) {
  127. return function provider(props) {
  128. return createMemo(() => {
  129. Owner.context = {
  130. [id]: props.value
  131. };
  132. return children(() => props.children);
  133. });
  134. };
  135. }
  136. function requestCallback(fn, options) {
  137. return {
  138. id: 0,
  139. fn: () => {},
  140. startTime: 0,
  141. expirationTime: 0
  142. };
  143. }
  144. const $RAW = Symbol("state-raw");
  145. function isWrappable(obj) {
  146. return obj != null && typeof obj === "object" && (obj.__proto__ === Object.prototype || Array.isArray(obj));
  147. }
  148. function unwrap(item) {
  149. return item;
  150. }
  151. function setProperty(state, property, value, force) {
  152. if (!force && state[property] === value) return;
  153. if (value === undefined) {
  154. delete state[property];
  155. } else state[property] = value;
  156. }
  157. function mergeState(state, value, force) {
  158. const keys = Object.keys(value);
  159. for (let i = 0; i < keys.length; i += 1) {
  160. const key = keys[i];
  161. setProperty(state, key, value[key], force);
  162. }
  163. }
  164. function updatePath(current, path, traversed = []) {
  165. let part,
  166. next = current;
  167. if (path.length > 1) {
  168. part = path.shift();
  169. const partType = typeof part,
  170. isArray = Array.isArray(current);
  171. if (Array.isArray(part)) {
  172. for (let i = 0; i < part.length; i++) {
  173. updatePath(current, [part[i]].concat(path), [part[i]].concat(traversed));
  174. }
  175. return;
  176. } else if (isArray && partType === "function") {
  177. for (let i = 0; i < current.length; i++) {
  178. if (part(current[i], i)) updatePath(current, [i].concat(path), [i].concat(traversed));
  179. }
  180. return;
  181. } else if (isArray && partType === "object") {
  182. const {
  183. from = 0,
  184. to = current.length - 1,
  185. by = 1
  186. } = part;
  187. for (let i = from; i <= to; i += by) {
  188. updatePath(current, [i].concat(path), [i].concat(traversed));
  189. }
  190. return;
  191. } else if (path.length > 1) {
  192. updatePath(current[part], path, [part].concat(traversed));
  193. return;
  194. }
  195. next = current[part];
  196. traversed = [part].concat(traversed);
  197. }
  198. let value = path[0];
  199. if (typeof value === "function") {
  200. value = value(next, traversed);
  201. if (value === next) return;
  202. }
  203. if (part === undefined && value == undefined) return;
  204. if (part === undefined || isWrappable(next) && isWrappable(value) && !Array.isArray(value)) {
  205. mergeState(next, value);
  206. } else setProperty(current, part, value);
  207. }
  208. function createState(state) {
  209. function setState(...args) {
  210. updatePath(state, args);
  211. }
  212. return [state, setState];
  213. }
  214. function reconcile(value, options = {}) {
  215. return state => {
  216. if (!isWrappable(state)) return value;
  217. const targetKeys = Object.keys(value);
  218. for (let i = 0, len = targetKeys.length; i < len; i++) {
  219. const key = targetKeys[i];
  220. setProperty(state, key, value[key]);
  221. }
  222. const previousKeys = Object.keys(state);
  223. for (let i = 0, len = previousKeys.length; i < len; i++) {
  224. if (value[previousKeys[i]] === undefined) setProperty(state, previousKeys[i], undefined);
  225. }
  226. };
  227. }
  228. function produce(fn) {
  229. return state => {
  230. if (isWrappable(state)) fn(state);
  231. return state;
  232. };
  233. }
  234. function mapArray(list, mapFn, options = {}) {
  235. const items = list();
  236. let s = [];
  237. if (items.length) {
  238. for (let i = 0, len = items.length; i < len; i++) s.push(mapFn(items[i], () => i));
  239. } else if (options.fallback) s = [options.fallback()];
  240. return () => s;
  241. }
  242. function resolveSSRNode(node) {
  243. const t = typeof node;
  244. if (t === "string") return node;
  245. if (node == null || t === "boolean") return "";
  246. if (Array.isArray(node)) return node.map(resolveSSRNode).join("");
  247. if (t === "object") return resolveSSRNode(node.t);
  248. if (t === "function") return resolveSSRNode(node());
  249. return String(node);
  250. }
  251. const sharedConfig = {};
  252. function setHydrateContext(context) {
  253. sharedConfig.context = context;
  254. }
  255. function nextHydrateContext() {
  256. return sharedConfig.context ? { ...sharedConfig.context,
  257. id: `${sharedConfig.context.id}${sharedConfig.context.count++}.`,
  258. count: 0
  259. } : undefined;
  260. }
  261. function createComponent(Comp, props) {
  262. if (sharedConfig.context) {
  263. const c = sharedConfig.context;
  264. setHydrateContext(nextHydrateContext());
  265. const r = Comp(props);
  266. setHydrateContext(c);
  267. return r;
  268. }
  269. return Comp(props);
  270. }
  271. function mergeProps(...sources) {
  272. const target = {};
  273. for (let i = 0; i < sources.length; i++) {
  274. const descriptors = Object.getOwnPropertyDescriptors(sources[i]);
  275. Object.defineProperties(target, descriptors);
  276. }
  277. return target;
  278. }
  279. function splitProps(props, ...keys) {
  280. const descriptors = Object.getOwnPropertyDescriptors(props),
  281. split = k => {
  282. const clone = {};
  283. for (let i = 0; i < k.length; i++) {
  284. const key = k[i];
  285. if (descriptors[key]) {
  286. Object.defineProperty(clone, key, descriptors[key]);
  287. delete descriptors[key];
  288. }
  289. }
  290. return clone;
  291. };
  292. return keys.map(split).concat(split(Object.keys(descriptors)));
  293. }
  294. function simpleMap(props, wrap) {
  295. const list = props.each || [],
  296. len = list.length,
  297. fn = props.children;
  298. if (len) {
  299. let mapped = "";
  300. for (let i = 0; i < len; i++) mapped += resolveSSRNode(wrap(fn, list[i], i));
  301. return {
  302. t: mapped
  303. };
  304. }
  305. return props.fallback || "";
  306. }
  307. function For(props) {
  308. return simpleMap(props, (fn, item, i) => fn(item, () => i));
  309. }
  310. function Index(props) {
  311. return simpleMap(props, (fn, item, i) => fn(() => item, i));
  312. }
  313. function Show(props) {
  314. let c;
  315. return props.when ? typeof (c = props.children) === "function" ? c(props.when) : c : props.fallback || "";
  316. }
  317. function Switch(props) {
  318. let conditions = props.children;
  319. Array.isArray(conditions) || (conditions = [conditions]);
  320. for (let i = 0; i < conditions.length; i++) {
  321. const w = conditions[i].when;
  322. if (w) {
  323. const c = conditions[i].children;
  324. return typeof c === "function" ? c(w) : c;
  325. }
  326. }
  327. return props.fallback || "";
  328. }
  329. function Match(props) {
  330. return props;
  331. }
  332. function ErrorBoundary(props) {
  333. return props.children;
  334. }
  335. const SuspenseContext = createContext();
  336. let resourceContext = null;
  337. function createResource(fn, fetcher, options = {}) {
  338. if (arguments.length === 2) {
  339. if (typeof fetcher === "object") {
  340. options = fetcher;
  341. fetcher = fn;
  342. fn = true;
  343. }
  344. } else if (arguments.length === 1) {
  345. fetcher = fn;
  346. fn = true;
  347. }
  348. const contexts = new Set();
  349. const id = sharedConfig.context.id + sharedConfig.context.count++;
  350. let resource = {};
  351. let value = options.initialValue;
  352. let p;
  353. if (sharedConfig.context.async) {
  354. resource = sharedConfig.context.resources[id] || (sharedConfig.context.resources[id] = {});
  355. if (resource.ref) {
  356. if (!resource.data && !resource.ref[0].loading) resource.ref[1].refetch();
  357. return resource.ref;
  358. }
  359. }
  360. const read = () => {
  361. if (resourceContext && p) resourceContext.push(p);
  362. const resolved = sharedConfig.context.async && sharedConfig.context.resources[id].data;
  363. if (sharedConfig.context.async && !resolved) {
  364. const ctx = useContext(SuspenseContext);
  365. if (ctx) {
  366. ctx.resources.set(id, read);
  367. contexts.add(ctx);
  368. }
  369. }
  370. return resolved ? sharedConfig.context.resources[id].data : value;
  371. };
  372. read.loading = false;
  373. function load() {
  374. const ctx = sharedConfig.context;
  375. if (!ctx.async && !ctx.streaming) return;
  376. if (ctx.resources && id in ctx.resources && ctx.resources[id].data) {
  377. value = ctx.resources[id].data;
  378. return;
  379. }
  380. resourceContext = [];
  381. const lookup = typeof fn === "function" ? fn() : fn;
  382. if (resourceContext.length) {
  383. p = Promise.all(resourceContext).then(() => fetcher(fn(), () => value));
  384. }
  385. resourceContext = null;
  386. if (!p) {
  387. if (lookup == null || lookup === false) return;
  388. p = fetcher(lookup, () => value);
  389. }
  390. read.loading = true;
  391. if ("then" in p) {
  392. if (ctx.writeResource) {
  393. ctx.writeResource(id, p);
  394. p.then(v => {
  395. value = v;
  396. read.loading = false;
  397. p = null;
  398. });
  399. return;
  400. }
  401. p.then(res => {
  402. read.loading = false;
  403. ctx.resources[id].data = res;
  404. p = null;
  405. notifySuspense(contexts);
  406. return res;
  407. });
  408. return;
  409. }
  410. ctx.resources[id].data = p;
  411. p = null;
  412. }
  413. load();
  414. return resource.ref = [read, {
  415. refetch: load,
  416. mutate: v => value = v
  417. }];
  418. }
  419. function lazy(fn) {
  420. let resolved;
  421. const p = fn();
  422. const contexts = new Set();
  423. p.then(mod => resolved = mod.default);
  424. const wrap = props => {
  425. const id = sharedConfig.context.id + sharedConfig.context.count++;
  426. if (resolved) return resolved(props);
  427. const ctx = useContext(SuspenseContext);
  428. const track = {
  429. loading: true
  430. };
  431. if (ctx) {
  432. ctx.resources.set(id, track);
  433. contexts.add(ctx);
  434. }
  435. p.then(() => {
  436. track.loading = false;
  437. notifySuspense(contexts);
  438. });
  439. return "";
  440. };
  441. wrap.preload = () => {};
  442. return wrap;
  443. }
  444. function suspenseComplete(c) {
  445. for (let r of c.resources.values()) {
  446. if (r.loading) return false;
  447. }
  448. return true;
  449. }
  450. function notifySuspense(contexts) {
  451. for (const c of contexts) {
  452. if (suspenseComplete(c)) c.completed();
  453. }
  454. contexts.clear();
  455. }
  456. function useTransition() {
  457. return [() => false, fn => {
  458. fn();
  459. }];
  460. }
  461. function SuspenseList(props) {
  462. return props.children;
  463. }
  464. const SUSPENSE_GLOBAL = Symbol("suspense-global");
  465. function Suspense(props) {
  466. const ctx = sharedConfig.context;
  467. if (ctx.streaming) createComponent(() => props.children, {});
  468. if (!ctx.async) return props.fallback;
  469. const id = ctx.id + ctx.count;
  470. const done = ctx.async ? lookup(Owner, SUSPENSE_GLOBAL)(id) : () => {};
  471. const o = Owner;
  472. const value = ctx.suspense[id] || (ctx.suspense[id] = {
  473. resources: new Map(),
  474. completed: () => {
  475. const res = runSuspense();
  476. if (suspenseComplete(value)) {
  477. done(resolveSSRNode(res));
  478. }
  479. }
  480. });
  481. function runSuspense() {
  482. setHydrateContext({ ...ctx,
  483. count: 0
  484. });
  485. return runWithOwner(o, () => {
  486. return createComponent(SuspenseContext.Provider, {
  487. value,
  488. get children() {
  489. return props.children;
  490. }
  491. });
  492. });
  493. }
  494. const res = runSuspense();
  495. if (suspenseComplete(value)) {
  496. done();
  497. return res;
  498. }
  499. return sharedConfig.context.async ? {
  500. t: `<#${id}#>`
  501. } : props.fallback;
  502. }
  503. const SUSPENSE_REPLACE = /<#([0-9\.]+)\#>/;
  504. function awaitSuspense(fn) {
  505. return new Promise(resolve => {
  506. const registry = new Set();
  507. const cache = Object.create(null);
  508. const res = createMemo(() => {
  509. Owner.context = {
  510. [SUSPENSE_GLOBAL]: getCallback
  511. };
  512. return fn();
  513. });
  514. if (!registry.size) resolve(res());
  515. function getCallback(key) {
  516. registry.add(key);
  517. return value => {
  518. if (value) cache[key] = value;
  519. registry.delete(key);
  520. if (!registry.size) Promise.resolve().then(() => {
  521. let source = resolveSSRNode(res());
  522. let final = "";
  523. let match;
  524. while (match = source.match(SUSPENSE_REPLACE)) {
  525. final += source.substring(0, match.index);
  526. source = cache[match[1]] + source.substring(match.index + match[0].length);
  527. }
  528. resolve(final + source);
  529. });
  530. };
  531. }
  532. });
  533. }
  534. exports.$RAW = $RAW;
  535. exports.ErrorBoundary = ErrorBoundary;
  536. exports.For = For;
  537. exports.Index = Index;
  538. exports.Match = Match;
  539. exports.Show = Show;
  540. exports.Suspense = Suspense;
  541. exports.SuspenseList = SuspenseList;
  542. exports.Switch = Switch;
  543. exports.awaitSuspense = awaitSuspense;
  544. exports.batch = batch;
  545. exports.createComponent = createComponent;
  546. exports.createComputed = createComputed;
  547. exports.createContext = createContext;
  548. exports.createDeferred = createDeferred;
  549. exports.createEffect = createEffect;
  550. exports.createMemo = createMemo;
  551. exports.createRenderEffect = createRenderEffect;
  552. exports.createResource = createResource;
  553. exports.createRoot = createRoot;
  554. exports.createSelector = createSelector;
  555. exports.createSignal = createSignal;
  556. exports.createState = createState;
  557. exports.equalFn = equalFn;
  558. exports.getListener = getListener;
  559. exports.getOwner = getOwner;
  560. exports.lazy = lazy;
  561. exports.mapArray = mapArray;
  562. exports.mergeProps = mergeProps;
  563. exports.on = on;
  564. exports.onCleanup = onCleanup;
  565. exports.onError = onError;
  566. exports.onMount = onMount;
  567. exports.produce = produce;
  568. exports.reconcile = reconcile;
  569. exports.requestCallback = requestCallback;
  570. exports.runWithOwner = runWithOwner;
  571. exports.sharedConfig = sharedConfig;
  572. exports.splitProps = splitProps;
  573. exports.untrack = untrack;
  574. exports.unwrap = unwrap;
  575. exports.useContext = useContext;
  576. exports.useTransition = useTransition;