context.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // src/jsx/dom/context.ts
  2. import { DOM_ERROR_HANDLER } from "../constants.js";
  3. import { globalContexts } from "../context.js";
  4. import { setInternalTagFlag } from "./utils.js";
  5. var createContextProviderFunction = (values) => ({ value, children }) => {
  6. if (!children) {
  7. return void 0;
  8. }
  9. const props = {
  10. children: [
  11. {
  12. tag: setInternalTagFlag(() => {
  13. values.push(value);
  14. }),
  15. props: {}
  16. }
  17. ]
  18. };
  19. if (Array.isArray(children)) {
  20. props.children.push(...children.flat());
  21. } else {
  22. props.children.push(children);
  23. }
  24. props.children.push({
  25. tag: setInternalTagFlag(() => {
  26. values.pop();
  27. }),
  28. props: {}
  29. });
  30. const res = { tag: "", props, type: "" };
  31. res[DOM_ERROR_HANDLER] = (err) => {
  32. values.pop();
  33. throw err;
  34. };
  35. return res;
  36. };
  37. var createContext = (defaultValue) => {
  38. const values = [defaultValue];
  39. const context = createContextProviderFunction(values);
  40. context.values = values;
  41. context.Provider = context;
  42. globalContexts.push(context);
  43. return context;
  44. };
  45. export {
  46. createContext,
  47. createContextProviderFunction
  48. };