solid-ctrl-flow 1.0.8 → 1.0.10

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/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Accessor } from 'solid-js';
2
+ import { Context } from 'solid-js';
2
3
  import { JSX } from 'solid-js';
3
4
 
4
5
  /**
@@ -61,9 +62,7 @@ export declare function createExtractor(name?: string): {
61
62
  /** Bound {@link Dest} */
62
63
  Dest: () => JSX.Element;
63
64
  /** Bound {@link Source} */
64
- Source: (props: {
65
- children: JSX.Element;
66
- }) => JSX.Element;
65
+ Source: (props: Info) => JSX.Element;
67
66
  /** Bound {@link Joint} */
68
67
  Joint: (props: {
69
68
  children: JSX.Element;
@@ -87,6 +86,13 @@ export declare namespace Debug {
87
86
  const isDebug: Accessor<boolean>;
88
87
  }
89
88
 
89
+ /** Informations about a {@link Source} component */
90
+ declare type Info = {
91
+ /** Order of the current source if there are many */
92
+ order?: number;
93
+ children: JSX.Element;
94
+ };
95
+
90
96
  /**
91
97
  * Memoizes some of the properties of {@link obj}
92
98
  * If {@link keys} is not provided, memoizes everything that get returned by {@link Object.keys} when provided with {@link obj}
@@ -95,6 +101,15 @@ export declare namespace Debug {
95
101
  */
96
102
  export declare function memoProps<T, K extends readonly (keyof T)[] = (keyof T)[]>(obj: T, keys?: K): Pick<T, K[number]>;
97
103
 
104
+ /**
105
+ * Executes {@link f} with the provided {@link Context}
106
+ * @param ctx The context to which to set the value
107
+ * @param value The value for the context
108
+ * @param f The function to run
109
+ * @returns The same thing {@link f} returned
110
+ */
111
+ export declare function runWithContext<T, R>(ctx: Context<T>, value: T, f: (x: T) => R): Promise<R>;
112
+
98
113
  /**
99
114
  * Esecutes {@link splitProps} and memoizes the first of the two returned objects
100
115
  * @param obj Object to split and partially memoize
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createContext, createMemo, createComponent, useContext, Match, mergeProps, onCleanup, Show, splitProps } from "solid-js";
1
+ import { createContext, createMemo, createComponent, useContext, Match, mergeProps, onCleanup, For, Show, splitProps, createRoot, getOwner } from "solid-js";
2
2
  import { createMutable } from "solid-js/store";
3
3
  const ctx$1 = createContext(() => void 0, {
4
4
  name: "case-when"
@@ -65,22 +65,34 @@ function Dest() {
65
65
  return;
66
66
  obj.attached++;
67
67
  onCleanup(() => obj.attached--);
68
- return createMemo(() => obj.source);
68
+ return createComponent(For, {
69
+ get each() {
70
+ return obj.source.sort((a, b) => a.order - b.order);
71
+ },
72
+ children: (x) => createMemo(() => x.children)
73
+ });
69
74
  }
70
75
  function Source(props) {
71
76
  const obj = useContext(this);
72
77
  if (!obj)
73
78
  return props.children;
74
- if (obj.source)
75
- throw new Error("An extractor can't have more than one source");
76
- obj.source = () => props.children;
77
- onCleanup(() => obj.source = void 0);
79
+ const order = createMemo(() => props.order || 0);
80
+ const info = {
81
+ get order() {
82
+ return order();
83
+ },
84
+ get children() {
85
+ return props.children;
86
+ }
87
+ };
88
+ obj.source.push(info);
89
+ onCleanup(() => obj.source.splice(obj.source.indexOf(info), 1));
78
90
  return createComponent(Show, {
79
91
  get when() {
80
92
  return !obj.attached;
81
93
  },
82
94
  get children() {
83
- return obj.source();
95
+ return info.children;
84
96
  }
85
97
  });
86
98
  }
@@ -89,7 +101,8 @@ function Joint(props) {
89
101
  Provider
90
102
  } = this;
91
103
  const obj = createMutable({
92
- attached: 0
104
+ attached: 0,
105
+ source: []
93
106
  });
94
107
  return createComponent(Provider, {
95
108
  value: obj,
@@ -101,20 +114,32 @@ function Joint(props) {
101
114
  function memoProps(obj, keys = Object.keys(obj)) {
102
115
  const out = {};
103
116
  for (const elm of keys)
104
- Object.defineProperty(out, elm, {
105
- get: createMemo(() => obj[elm])
106
- });
117
+ Object.defineProperty(out, elm, { enumerable: true, get: createMemo(() => obj[elm]) });
107
118
  return out;
108
119
  }
109
120
  function splitAndMemoProps(obj, keys) {
110
121
  const [mine, other] = splitProps(obj, keys);
111
122
  return [memoProps(mine, keys), other];
112
123
  }
124
+ function runWithContext(ctx2, value, f) {
125
+ return createRoot(async (d) => {
126
+ try {
127
+ getOwner().context = { [ctx2.id]: value };
128
+ const out = f(value);
129
+ if (out instanceof Promise)
130
+ return out.finally(d);
131
+ return d(), out;
132
+ } catch (ex) {
133
+ throw d(), ex;
134
+ }
135
+ });
136
+ }
113
137
  export {
114
138
  Case,
115
139
  Debug,
116
140
  When,
117
141
  createExtractor,
118
142
  memoProps,
143
+ runWithContext,
119
144
  splitAndMemoProps
120
145
  };
package/dist/index.umd.js CHANGED
@@ -67,22 +67,34 @@
67
67
  return;
68
68
  obj.attached++;
69
69
  solidJs.onCleanup(() => obj.attached--);
70
- return solidJs.createMemo(() => obj.source);
70
+ return solidJs.createComponent(solidJs.For, {
71
+ get each() {
72
+ return obj.source.sort((a, b) => a.order - b.order);
73
+ },
74
+ children: (x) => solidJs.createMemo(() => x.children)
75
+ });
71
76
  }
72
77
  function Source(props) {
73
78
  const obj = solidJs.useContext(this);
74
79
  if (!obj)
75
80
  return props.children;
76
- if (obj.source)
77
- throw new Error("An extractor can't have more than one source");
78
- obj.source = () => props.children;
79
- solidJs.onCleanup(() => obj.source = void 0);
81
+ const order = solidJs.createMemo(() => props.order || 0);
82
+ const info = {
83
+ get order() {
84
+ return order();
85
+ },
86
+ get children() {
87
+ return props.children;
88
+ }
89
+ };
90
+ obj.source.push(info);
91
+ solidJs.onCleanup(() => obj.source.splice(obj.source.indexOf(info), 1));
80
92
  return solidJs.createComponent(solidJs.Show, {
81
93
  get when() {
82
94
  return !obj.attached;
83
95
  },
84
96
  get children() {
85
- return obj.source();
97
+ return info.children;
86
98
  }
87
99
  });
88
100
  }
@@ -91,7 +103,8 @@
91
103
  Provider
92
104
  } = this;
93
105
  const obj = store.createMutable({
94
- attached: 0
106
+ attached: 0,
107
+ source: []
95
108
  });
96
109
  return solidJs.createComponent(Provider, {
97
110
  value: obj,
@@ -103,20 +116,32 @@
103
116
  function memoProps(obj, keys = Object.keys(obj)) {
104
117
  const out = {};
105
118
  for (const elm of keys)
106
- Object.defineProperty(out, elm, {
107
- get: solidJs.createMemo(() => obj[elm])
108
- });
119
+ Object.defineProperty(out, elm, { enumerable: true, get: solidJs.createMemo(() => obj[elm]) });
109
120
  return out;
110
121
  }
111
122
  function splitAndMemoProps(obj, keys) {
112
123
  const [mine, other] = solidJs.splitProps(obj, keys);
113
124
  return [memoProps(mine, keys), other];
114
125
  }
126
+ function runWithContext(ctx2, value, f) {
127
+ return solidJs.createRoot(async (d) => {
128
+ try {
129
+ solidJs.getOwner().context = { [ctx2.id]: value };
130
+ const out = f(value);
131
+ if (out instanceof Promise)
132
+ return out.finally(d);
133
+ return d(), out;
134
+ } catch (ex) {
135
+ throw d(), ex;
136
+ }
137
+ });
138
+ }
115
139
  exports2.Case = Case;
116
140
  exports2.Debug = Debug;
117
141
  exports2.When = When;
118
142
  exports2.createExtractor = createExtractor;
119
143
  exports2.memoProps = memoProps;
144
+ exports2.runWithContext = runWithContext;
120
145
  exports2.splitAndMemoProps = splitAndMemoProps;
121
146
  Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
122
147
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-ctrl-flow",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "Control flow components for solid-js",
5
5
  "author": "AFatNiBBa",
6
6
  "license": "ISC",
@@ -120,4 +120,7 @@ Utility functions needed for the components above
120
120
  Creates a partial version of an object with memoized remaining properties
121
121
 
122
122
  ### `splitAndMemoProps()`
123
- Like `splitProps()` but memoizes the whole local part
123
+ Like `splitProps()` but memoizes the whole local part
124
+
125
+ ### `runWithContext()`
126
+ Creates a context scope that persists for the duration of a function