context.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var context_exports = {};
  19. __export(context_exports, {
  20. createContext: () => createContext,
  21. globalContexts: () => globalContexts,
  22. useContext: () => useContext
  23. });
  24. module.exports = __toCommonJS(context_exports);
  25. var import_html = require("../helper/html");
  26. var import_base = require("./base");
  27. var import_constants = require("./constants");
  28. var import_context = require("./dom/context");
  29. const globalContexts = [];
  30. const createContext = (defaultValue) => {
  31. const values = [defaultValue];
  32. const context = ((props) => {
  33. values.push(props.value);
  34. let string;
  35. try {
  36. string = props.children ? (Array.isArray(props.children) ? new import_base.JSXFragmentNode("", {}, props.children) : props.children).toString() : "";
  37. } catch (e) {
  38. values.pop();
  39. throw e;
  40. }
  41. if (string instanceof Promise) {
  42. return string.finally(() => values.pop()).then((resString) => (0, import_html.raw)(resString, resString.callbacks));
  43. } else {
  44. values.pop();
  45. return (0, import_html.raw)(string);
  46. }
  47. });
  48. context.values = values;
  49. context.Provider = context;
  50. context[import_constants.DOM_RENDERER] = (0, import_context.createContextProviderFunction)(values);
  51. globalContexts.push(context);
  52. return context;
  53. };
  54. const useContext = (context) => {
  55. return context.values.at(-1);
  56. };
  57. // Annotate the CommonJS export names for ESM import in node:
  58. 0 && (module.exports = {
  59. createContext,
  60. globalContexts,
  61. useContext
  62. });