solid-ctrl-flow 1.0.60 → 1.0.62
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 +0 -103
- package/dist/index.mjs +24 -135
- package/dist/index.umd.js +23 -134
- package/package.json +1 -1
- package/readMe.md +7 -57
package/dist/index.d.ts
CHANGED
|
@@ -4,56 +4,6 @@ import { JSX } from 'solid-js';
|
|
|
4
4
|
import { MemoOptions } from 'solid-js';
|
|
5
5
|
import { Owner } from 'solid-js';
|
|
6
6
|
import { ParentProps } from 'solid-js';
|
|
7
|
-
import { Signal } from 'solid-js';
|
|
8
|
-
|
|
9
|
-
/** Reactive atomic value without the inconveniences of {@link Signal} */
|
|
10
|
-
export declare class Atom<T> {
|
|
11
|
-
get: Accessor<T>;
|
|
12
|
-
set: (x: T) => void;
|
|
13
|
-
get value(): T;
|
|
14
|
-
set value(v: T);
|
|
15
|
-
constructor(get: Accessor<T>, set: (x: T) => void);
|
|
16
|
-
/**
|
|
17
|
-
* Allows you to set the current {@link Atom} based on its current value.
|
|
18
|
-
* The current value gets read through {@link untrack} to mimic the {@link Setter} behaviour
|
|
19
|
-
* @param f Function that creates a new value based on the current one
|
|
20
|
-
*/
|
|
21
|
-
update<V extends T>(f: (prev: T) => V): V;
|
|
22
|
-
/**
|
|
23
|
-
* Creates a new {@link Atom} that applies a conversion to the current one
|
|
24
|
-
* @param to Conversion function from {@link S} to {@link D}
|
|
25
|
-
* @param from Conversion function from {@link D} to {@link S}
|
|
26
|
-
*/
|
|
27
|
-
convert<R>(to: (x: T) => R, from: (x: R) => T): Atom<R>;
|
|
28
|
-
/**
|
|
29
|
-
* Creates an {@link Atom} that forwards an {@link Accessor} to another {@link Atom}
|
|
30
|
-
* @param f The reactive {@link Accessor} to the {@link Atom} to forward
|
|
31
|
-
*/
|
|
32
|
-
static unwrap<T>(f: Accessor<Atom<T>>): Atom<T>;
|
|
33
|
-
/**
|
|
34
|
-
* Creates an {@link Atom} based on a {@link Signal}
|
|
35
|
-
* @param param0 The {@link Signal} to forward
|
|
36
|
-
*/
|
|
37
|
-
static from<T>([get, set]: Signal<T>): Atom<T>;
|
|
38
|
-
/**
|
|
39
|
-
* Creates an {@link Atom} based on an object property.
|
|
40
|
-
* The result of {@link k} will be memoized, while {@link obj}'s one won't
|
|
41
|
-
* @param obj The object containing the property
|
|
42
|
-
* @param k A function that returns the key of the property and will be passed to {@link nameOf}
|
|
43
|
-
*/
|
|
44
|
-
static prop<T, K extends keyof T>(obj: Accessor<T>, k: (x: NamesOf<T>) => K): Atom<T[K]>;
|
|
45
|
-
/**
|
|
46
|
-
* Creates a bindable data source.
|
|
47
|
-
* If {@link bind} returns an {@link Atom} it gets wrapped, otherwise it creates a {@link Signal} using {@link f} and uses it to store the value until {@link bind}'s value changes
|
|
48
|
-
* @param bind The bound {@link Atom}
|
|
49
|
-
* @param f The function that will create the actual {@link Signal} that will store the {@link Atom}'s data in case that {@link bind} doesn't return anything
|
|
50
|
-
*/
|
|
51
|
-
static source<T>(bind: Accessor<Atom<T> | undefined>): Atom<T | undefined>;
|
|
52
|
-
static source<T>(bind: Accessor<Atom<T> | undefined>, f: Accessor<Signal<T>>): Atom<T>;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Type hack for making {@link ReactiveContext} implement {@link Accessor} */
|
|
56
|
-
declare const BASE_CTOR: new <T>() => Accessor<T>;
|
|
57
7
|
|
|
58
8
|
/** Like a {@link Match} but gets the value from the closest {@link On} ancestor */
|
|
59
9
|
export declare function Case(props: ParentProps<{
|
|
@@ -120,9 +70,6 @@ export declare function createExtractor(name?: string): {
|
|
|
120
70
|
readonly extracting: () => number | null;
|
|
121
71
|
};
|
|
122
72
|
|
|
123
|
-
/** Built-in {@link ReactiveContext} which allows you to display different things in debug mode */
|
|
124
|
-
export declare const debug: ReactiveContext<boolean>;
|
|
125
|
-
|
|
126
73
|
/**
|
|
127
74
|
* Eventually wraps its content into a template if a certain condition is met.
|
|
128
75
|
* If you set {@link Standard.recycled} to `true`, the content will be memoized in the beginning and will be recycled even if the wrapper changes.
|
|
@@ -156,19 +103,6 @@ export declare function memoProps<T>(obj: T, keys?: undefined, equals?: Equals):
|
|
|
156
103
|
|
|
157
104
|
export declare function memoProps<T, K extends readonly (keyof T)[]>(obj: T, keys: K, equals?: Equals): Pick<T, K[number]>;
|
|
158
105
|
|
|
159
|
-
/**
|
|
160
|
-
* Function that returns the name of the field accessed inside of {@link f}.
|
|
161
|
-
* This function is not meant to be used on its own because it has very poor type inference
|
|
162
|
-
* @param f A function that returns a value based on {@link NamesOf}
|
|
163
|
-
* @returns The same thing that {@link f} returned
|
|
164
|
-
*/
|
|
165
|
-
export declare const nameOf: <T, const R>(f: (x: NamesOf<T>) => R) => R;
|
|
166
|
-
|
|
167
|
-
/** Type that maps each key of {@link T} to itself */
|
|
168
|
-
export declare type NamesOf<T> = {
|
|
169
|
-
[k in keyof T]: k;
|
|
170
|
-
};
|
|
171
|
-
|
|
172
106
|
/**
|
|
173
107
|
* Allows the use of {@link Case}s inside of {@link Switch}es.
|
|
174
108
|
* Example:
|
|
@@ -203,43 +137,6 @@ export declare const On: (props: ParentProps<{
|
|
|
203
137
|
value: any;
|
|
204
138
|
}>) => JSX.Element;
|
|
205
139
|
|
|
206
|
-
/**
|
|
207
|
-
* Reactive {@link Context} with some additional built-in functionalities.
|
|
208
|
-
* The call signature returns the value in the current {@link Owner} (It's like calling {@link get})
|
|
209
|
-
*/
|
|
210
|
-
export declare abstract class ReactiveContext<T> extends BASE_CTOR<T> {
|
|
211
|
-
/** The actual {@link Context} that contains the reactive {@link Accessor} to the value */
|
|
212
|
-
abstract ctx: Context<Accessor<T>>;
|
|
213
|
-
/**
|
|
214
|
-
* Returns the {@link Accessor} for the value in the current {@link Owner}.
|
|
215
|
-
* It's like calling {@link useContext}
|
|
216
|
-
*/
|
|
217
|
-
get get(): Accessor<T>;
|
|
218
|
-
/**
|
|
219
|
-
* Component that sets the value of the current {@link Context} for its children.
|
|
220
|
-
* If no value is provided then it will use the default value for {@link ctx}.
|
|
221
|
-
* The function is bound for the way solid compiles components inside other objects.
|
|
222
|
-
* (The value is memoized)
|
|
223
|
-
*/
|
|
224
|
-
get Provider(): (props: ParentProps<{
|
|
225
|
-
value?: T;
|
|
226
|
-
}>) => JSX.Element;
|
|
227
|
-
/**
|
|
228
|
-
* Executes {@link runWithContext} on the current context
|
|
229
|
-
* @param value The value for the context
|
|
230
|
-
* @param f The function to run
|
|
231
|
-
* @returns The same thing {@link f} returned
|
|
232
|
-
*/
|
|
233
|
-
runWith<V extends T, R>(value: V | undefined, f: (x: V) => R): R;
|
|
234
|
-
/**
|
|
235
|
-
* Creates a {@link ReactiveContext}
|
|
236
|
-
* @param defaultValue Initial value for the context
|
|
237
|
-
* @param name Name to give to the context
|
|
238
|
-
*/
|
|
239
|
-
static create<T>(defaultValue?: undefined, name?: string): ReactiveContext<T | undefined>;
|
|
240
|
-
static create<T>(defaultValue: T, name?: string): ReactiveContext<T>;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
140
|
/**
|
|
244
141
|
* Executes {@link f} with the provided value for the specified {@link Context}.
|
|
245
142
|
* You can pass `undefined` to {@link value} in order to get back the default value for {@link ctx}.
|
package/dist/index.mjs
CHANGED
|
@@ -1,74 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createMemo, splitProps, untrack, createRoot, getOwner, createComponent, Show, createContext, useContext, onCleanup, For, Match, mergeProps } from "solid-js";
|
|
2
2
|
import { createMutable } from "solid-js/store";
|
|
3
|
-
const BASE_CTOR = Function;
|
|
4
|
-
class ReactiveContext extends BASE_CTOR {
|
|
5
|
-
/** The actual {@link Context} that contains the reactive {@link Accessor} to the value */
|
|
6
|
-
/**
|
|
7
|
-
* Returns the {@link Accessor} for the value in the current {@link Owner}.
|
|
8
|
-
* It's like calling {@link useContext}
|
|
9
|
-
*/
|
|
10
|
-
get get() {
|
|
11
|
-
return useContext(this.ctx);
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Component that sets the value of the current {@link Context} for its children.
|
|
15
|
-
* If no value is provided then it will use the default value for {@link ctx}.
|
|
16
|
-
* The function is bound for the way solid compiles components inside other objects.
|
|
17
|
-
* (The value is memoized)
|
|
18
|
-
*/
|
|
19
|
-
get Provider() {
|
|
20
|
-
const {
|
|
21
|
-
ctx: ctx2
|
|
22
|
-
} = this;
|
|
23
|
-
return function(props) {
|
|
24
|
-
const memo = createMemo(() => props.value);
|
|
25
|
-
return createComponent(ctx2.Provider, {
|
|
26
|
-
value: memo,
|
|
27
|
-
get children() {
|
|
28
|
-
return props.children;
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Executes {@link runWithContext} on the current context
|
|
35
|
-
* @param value The value for the context
|
|
36
|
-
* @param f The function to run
|
|
37
|
-
* @returns The same thing {@link f} returned
|
|
38
|
-
*/
|
|
39
|
-
runWith(value, f) {
|
|
40
|
-
return runWithContext(this.ctx, value === void 0 ? void 0 : () => value, (x) => f(x()));
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Creates a {@link ReactiveContext}
|
|
44
|
-
* @param defaultValue Initial value for the context
|
|
45
|
-
* @param name Name to give to the context
|
|
46
|
-
*/
|
|
47
|
-
static create(defaultValue, name) {
|
|
48
|
-
const ctx2 = createContext(() => defaultValue, {
|
|
49
|
-
name
|
|
50
|
-
});
|
|
51
|
-
const f = () => out.get();
|
|
52
|
-
f.ctx = ctx2;
|
|
53
|
-
const out = Object.setPrototypeOf(f, ReactiveContext.prototype);
|
|
54
|
-
return out;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
function runWithContext(ctx2, value, f) {
|
|
58
|
-
return createRoot((d) => {
|
|
59
|
-
try {
|
|
60
|
-
(getOwner().context ??= {})[ctx2.id] = value;
|
|
61
|
-
return f(value === void 0 ? ctx2.defaultValue : value);
|
|
62
|
-
} finally {
|
|
63
|
-
d();
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
function untrackCall(f, ...args) {
|
|
68
|
-
return untrack(() => f.apply(this, args));
|
|
69
|
-
}
|
|
70
|
-
const PROXY_KEYOF = new Proxy({}, { get: (_, k) => k });
|
|
71
|
-
const nameOf = (f) => f(PROXY_KEYOF);
|
|
72
3
|
function memoProps(obj, keys = Object.keys(obj), equals = false) {
|
|
73
4
|
const out = {};
|
|
74
5
|
for (const elm of keys)
|
|
@@ -80,6 +11,19 @@ function splitAndMemoProps(obj, keys, equals) {
|
|
|
80
11
|
out[0] = memoProps(out[0], keys, equals);
|
|
81
12
|
return out;
|
|
82
13
|
}
|
|
14
|
+
function untrackCall(f, ...args) {
|
|
15
|
+
return untrack(() => f.apply(this, args));
|
|
16
|
+
}
|
|
17
|
+
function runWithContext(ctx2, value, f) {
|
|
18
|
+
return createRoot((d) => {
|
|
19
|
+
try {
|
|
20
|
+
(getOwner().context ??= {})[ctx2.id] = value;
|
|
21
|
+
return f(value === void 0 ? ctx2.defaultValue : value);
|
|
22
|
+
} finally {
|
|
23
|
+
d();
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
83
27
|
const unwrap = (f) => new Proxy(f, HANDLER);
|
|
84
28
|
const HANDLER = new Proxy({}, {
|
|
85
29
|
get(cache, k) {
|
|
@@ -212,13 +156,18 @@ function createInfo(props) {
|
|
|
212
156
|
}
|
|
213
157
|
};
|
|
214
158
|
}
|
|
215
|
-
const ctx =
|
|
216
|
-
|
|
159
|
+
const ctx = createContext(void 0, {
|
|
160
|
+
name: "on-case"
|
|
161
|
+
});
|
|
162
|
+
const On = (props) => createComponent(ctx.Provider, {
|
|
163
|
+
value: () => props.value,
|
|
164
|
+
get children() {
|
|
165
|
+
return props.children;
|
|
166
|
+
}
|
|
167
|
+
});
|
|
217
168
|
function Case(props) {
|
|
218
169
|
const [mine, other] = splitAndMemoProps(props, ["value", "pred"]);
|
|
219
|
-
const
|
|
220
|
-
get
|
|
221
|
-
} = ctx;
|
|
170
|
+
const get = useContext(ctx);
|
|
222
171
|
const pred = (x) => mine.pred ? mine.pred(x) : x === mine.value;
|
|
223
172
|
return createComponent(Match, mergeProps({
|
|
224
173
|
get when() {
|
|
@@ -226,62 +175,6 @@ function Case(props) {
|
|
|
226
175
|
}
|
|
227
176
|
}, other));
|
|
228
177
|
}
|
|
229
|
-
class Atom {
|
|
230
|
-
constructor(get, set) {
|
|
231
|
-
this.get = get;
|
|
232
|
-
this.set = set;
|
|
233
|
-
}
|
|
234
|
-
get value() {
|
|
235
|
-
return this.get();
|
|
236
|
-
}
|
|
237
|
-
set value(v) {
|
|
238
|
-
this.set(v);
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Allows you to set the current {@link Atom} based on its current value.
|
|
242
|
-
* The current value gets read through {@link untrack} to mimic the {@link Setter} behaviour
|
|
243
|
-
* @param f Function that creates a new value based on the current one
|
|
244
|
-
*/
|
|
245
|
-
update(f) {
|
|
246
|
-
return this.value = f(untrack(this.get));
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Creates a new {@link Atom} that applies a conversion to the current one
|
|
250
|
-
* @param to Conversion function from {@link S} to {@link D}
|
|
251
|
-
* @param from Conversion function from {@link D} to {@link S}
|
|
252
|
-
*/
|
|
253
|
-
convert(to, from) {
|
|
254
|
-
return new Atom(() => to(this.value), (v) => this.value = from(v));
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Creates an {@link Atom} that forwards an {@link Accessor} to another {@link Atom}
|
|
258
|
-
* @param f The reactive {@link Accessor} to the {@link Atom} to forward
|
|
259
|
-
*/
|
|
260
|
-
static unwrap(f) {
|
|
261
|
-
return new this(() => f().value, (v) => f().value = v);
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Creates an {@link Atom} based on a {@link Signal}
|
|
265
|
-
* @param param0 The {@link Signal} to forward
|
|
266
|
-
*/
|
|
267
|
-
static from([get, set]) {
|
|
268
|
-
return new this(get, (v) => set(() => v));
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Creates an {@link Atom} based on an object property.
|
|
272
|
-
* The result of {@link k} will be memoized, while {@link obj}'s one won't
|
|
273
|
-
* @param obj The object containing the property
|
|
274
|
-
* @param k A function that returns the key of the property and will be passed to {@link nameOf}
|
|
275
|
-
*/
|
|
276
|
-
static prop(obj, k) {
|
|
277
|
-
const temp = createMemo(() => nameOf(k));
|
|
278
|
-
return new this(() => obj()[temp()], (v) => obj()[temp()] = v);
|
|
279
|
-
}
|
|
280
|
-
static source(bind, f = createSignal) {
|
|
281
|
-
return this.unwrap(createMemo(on(bind, (x) => x ?? Atom.from(f()))));
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
const debug = ReactiveContext.create(false, "debug-scope");
|
|
285
178
|
function SameContext(props) {
|
|
286
179
|
return createMemo(() => {
|
|
287
180
|
getOwner().context = props.owner.context;
|
|
@@ -289,17 +182,13 @@ function SameContext(props) {
|
|
|
289
182
|
});
|
|
290
183
|
}
|
|
291
184
|
export {
|
|
292
|
-
Atom,
|
|
293
185
|
Case,
|
|
294
186
|
Enfold,
|
|
295
187
|
On,
|
|
296
|
-
ReactiveContext,
|
|
297
188
|
SameContext,
|
|
298
189
|
Slot,
|
|
299
190
|
createExtractor,
|
|
300
|
-
debug,
|
|
301
191
|
memoProps,
|
|
302
|
-
nameOf,
|
|
303
192
|
runWithContext,
|
|
304
193
|
splitAndMemoProps,
|
|
305
194
|
untrackCall,
|
package/dist/index.umd.js
CHANGED
|
@@ -2,75 +2,6 @@
|
|
|
2
2
|
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("solid-js"), require("solid-js/store")) : typeof define === "function" && define.amd ? define(["exports", "solid-js", "solid-js/store"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.solidCtrlFlow = {}, global.solidJs, global.solidJsStore));
|
|
3
3
|
})(this, function(exports2, solidJs, store) {
|
|
4
4
|
"use strict";
|
|
5
|
-
const BASE_CTOR = Function;
|
|
6
|
-
class ReactiveContext extends BASE_CTOR {
|
|
7
|
-
/** The actual {@link Context} that contains the reactive {@link Accessor} to the value */
|
|
8
|
-
/**
|
|
9
|
-
* Returns the {@link Accessor} for the value in the current {@link Owner}.
|
|
10
|
-
* It's like calling {@link useContext}
|
|
11
|
-
*/
|
|
12
|
-
get get() {
|
|
13
|
-
return solidJs.useContext(this.ctx);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Component that sets the value of the current {@link Context} for its children.
|
|
17
|
-
* If no value is provided then it will use the default value for {@link ctx}.
|
|
18
|
-
* The function is bound for the way solid compiles components inside other objects.
|
|
19
|
-
* (The value is memoized)
|
|
20
|
-
*/
|
|
21
|
-
get Provider() {
|
|
22
|
-
const {
|
|
23
|
-
ctx: ctx2
|
|
24
|
-
} = this;
|
|
25
|
-
return function(props) {
|
|
26
|
-
const memo = solidJs.createMemo(() => props.value);
|
|
27
|
-
return solidJs.createComponent(ctx2.Provider, {
|
|
28
|
-
value: memo,
|
|
29
|
-
get children() {
|
|
30
|
-
return props.children;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Executes {@link runWithContext} on the current context
|
|
37
|
-
* @param value The value for the context
|
|
38
|
-
* @param f The function to run
|
|
39
|
-
* @returns The same thing {@link f} returned
|
|
40
|
-
*/
|
|
41
|
-
runWith(value, f) {
|
|
42
|
-
return runWithContext(this.ctx, value === void 0 ? void 0 : () => value, (x) => f(x()));
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Creates a {@link ReactiveContext}
|
|
46
|
-
* @param defaultValue Initial value for the context
|
|
47
|
-
* @param name Name to give to the context
|
|
48
|
-
*/
|
|
49
|
-
static create(defaultValue, name) {
|
|
50
|
-
const ctx2 = solidJs.createContext(() => defaultValue, {
|
|
51
|
-
name
|
|
52
|
-
});
|
|
53
|
-
const f = () => out.get();
|
|
54
|
-
f.ctx = ctx2;
|
|
55
|
-
const out = Object.setPrototypeOf(f, ReactiveContext.prototype);
|
|
56
|
-
return out;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
function runWithContext(ctx2, value, f) {
|
|
60
|
-
return solidJs.createRoot((d) => {
|
|
61
|
-
try {
|
|
62
|
-
(solidJs.getOwner().context ??= {})[ctx2.id] = value;
|
|
63
|
-
return f(value === void 0 ? ctx2.defaultValue : value);
|
|
64
|
-
} finally {
|
|
65
|
-
d();
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
function untrackCall(f, ...args) {
|
|
70
|
-
return solidJs.untrack(() => f.apply(this, args));
|
|
71
|
-
}
|
|
72
|
-
const PROXY_KEYOF = new Proxy({}, { get: (_, k) => k });
|
|
73
|
-
const nameOf = (f) => f(PROXY_KEYOF);
|
|
74
5
|
function memoProps(obj, keys = Object.keys(obj), equals = false) {
|
|
75
6
|
const out = {};
|
|
76
7
|
for (const elm of keys)
|
|
@@ -82,6 +13,19 @@
|
|
|
82
13
|
out[0] = memoProps(out[0], keys, equals);
|
|
83
14
|
return out;
|
|
84
15
|
}
|
|
16
|
+
function untrackCall(f, ...args) {
|
|
17
|
+
return solidJs.untrack(() => f.apply(this, args));
|
|
18
|
+
}
|
|
19
|
+
function runWithContext(ctx2, value, f) {
|
|
20
|
+
return solidJs.createRoot((d) => {
|
|
21
|
+
try {
|
|
22
|
+
(solidJs.getOwner().context ??= {})[ctx2.id] = value;
|
|
23
|
+
return f(value === void 0 ? ctx2.defaultValue : value);
|
|
24
|
+
} finally {
|
|
25
|
+
d();
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
85
29
|
const unwrap = (f) => new Proxy(f, HANDLER);
|
|
86
30
|
const HANDLER = new Proxy({}, {
|
|
87
31
|
get(cache, k) {
|
|
@@ -214,13 +158,18 @@
|
|
|
214
158
|
}
|
|
215
159
|
};
|
|
216
160
|
}
|
|
217
|
-
const ctx =
|
|
218
|
-
|
|
161
|
+
const ctx = solidJs.createContext(void 0, {
|
|
162
|
+
name: "on-case"
|
|
163
|
+
});
|
|
164
|
+
const On = (props) => solidJs.createComponent(ctx.Provider, {
|
|
165
|
+
value: () => props.value,
|
|
166
|
+
get children() {
|
|
167
|
+
return props.children;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
219
170
|
function Case(props) {
|
|
220
171
|
const [mine, other] = splitAndMemoProps(props, ["value", "pred"]);
|
|
221
|
-
const
|
|
222
|
-
get
|
|
223
|
-
} = ctx;
|
|
172
|
+
const get = solidJs.useContext(ctx);
|
|
224
173
|
const pred = (x) => mine.pred ? mine.pred(x) : x === mine.value;
|
|
225
174
|
return solidJs.createComponent(solidJs.Match, solidJs.mergeProps({
|
|
226
175
|
get when() {
|
|
@@ -228,78 +177,18 @@
|
|
|
228
177
|
}
|
|
229
178
|
}, other));
|
|
230
179
|
}
|
|
231
|
-
class Atom {
|
|
232
|
-
constructor(get, set) {
|
|
233
|
-
this.get = get;
|
|
234
|
-
this.set = set;
|
|
235
|
-
}
|
|
236
|
-
get value() {
|
|
237
|
-
return this.get();
|
|
238
|
-
}
|
|
239
|
-
set value(v) {
|
|
240
|
-
this.set(v);
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Allows you to set the current {@link Atom} based on its current value.
|
|
244
|
-
* The current value gets read through {@link untrack} to mimic the {@link Setter} behaviour
|
|
245
|
-
* @param f Function that creates a new value based on the current one
|
|
246
|
-
*/
|
|
247
|
-
update(f) {
|
|
248
|
-
return this.value = f(solidJs.untrack(this.get));
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Creates a new {@link Atom} that applies a conversion to the current one
|
|
252
|
-
* @param to Conversion function from {@link S} to {@link D}
|
|
253
|
-
* @param from Conversion function from {@link D} to {@link S}
|
|
254
|
-
*/
|
|
255
|
-
convert(to, from) {
|
|
256
|
-
return new Atom(() => to(this.value), (v) => this.value = from(v));
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Creates an {@link Atom} that forwards an {@link Accessor} to another {@link Atom}
|
|
260
|
-
* @param f The reactive {@link Accessor} to the {@link Atom} to forward
|
|
261
|
-
*/
|
|
262
|
-
static unwrap(f) {
|
|
263
|
-
return new this(() => f().value, (v) => f().value = v);
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Creates an {@link Atom} based on a {@link Signal}
|
|
267
|
-
* @param param0 The {@link Signal} to forward
|
|
268
|
-
*/
|
|
269
|
-
static from([get, set]) {
|
|
270
|
-
return new this(get, (v) => set(() => v));
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Creates an {@link Atom} based on an object property.
|
|
274
|
-
* The result of {@link k} will be memoized, while {@link obj}'s one won't
|
|
275
|
-
* @param obj The object containing the property
|
|
276
|
-
* @param k A function that returns the key of the property and will be passed to {@link nameOf}
|
|
277
|
-
*/
|
|
278
|
-
static prop(obj, k) {
|
|
279
|
-
const temp = solidJs.createMemo(() => nameOf(k));
|
|
280
|
-
return new this(() => obj()[temp()], (v) => obj()[temp()] = v);
|
|
281
|
-
}
|
|
282
|
-
static source(bind, f = solidJs.createSignal) {
|
|
283
|
-
return this.unwrap(solidJs.createMemo(solidJs.on(bind, (x) => x ?? Atom.from(f()))));
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
const debug = ReactiveContext.create(false, "debug-scope");
|
|
287
180
|
function SameContext(props) {
|
|
288
181
|
return solidJs.createMemo(() => {
|
|
289
182
|
solidJs.getOwner().context = props.owner.context;
|
|
290
183
|
return props.children;
|
|
291
184
|
});
|
|
292
185
|
}
|
|
293
|
-
exports2.Atom = Atom;
|
|
294
186
|
exports2.Case = Case;
|
|
295
187
|
exports2.Enfold = Enfold;
|
|
296
188
|
exports2.On = On;
|
|
297
|
-
exports2.ReactiveContext = ReactiveContext;
|
|
298
189
|
exports2.SameContext = SameContext;
|
|
299
190
|
exports2.createExtractor = createExtractor;
|
|
300
|
-
exports2.debug = debug;
|
|
301
191
|
exports2.memoProps = memoProps;
|
|
302
|
-
exports2.nameOf = nameOf;
|
|
303
192
|
exports2.runWithContext = runWithContext;
|
|
304
193
|
exports2.splitAndMemoProps = splitAndMemoProps;
|
|
305
194
|
exports2.untrackCall = untrackCall;
|
package/package.json
CHANGED
package/readMe.md
CHANGED
|
@@ -162,67 +162,17 @@ return <>
|
|
|
162
162
|
|
|
163
163
|
## Utility
|
|
164
164
|
|
|
165
|
-
### `Atom`s
|
|
166
|
-
Customizable and simplified wrappers for reactive states
|
|
167
|
-
|
|
168
|
-
> #### `new Atom()`
|
|
169
|
-
> Creates an `Atom` with custom getter and setter
|
|
170
|
-
>
|
|
171
|
-
> #### `Atom.update()`
|
|
172
|
-
> Like the `Setter` overload of a `Signal` that takes a function with the previous value
|
|
173
|
-
>
|
|
174
|
-
> #### `Atom.convert()`
|
|
175
|
-
> Creates a new `Atom` that applies a conversion to the current one
|
|
176
|
-
>
|
|
177
|
-
> #### `Atom.unwrap()`
|
|
178
|
-
> An `Atom`-specific (optimized) version of `unwrap()` that allows the destructuring of its result
|
|
179
|
-
>
|
|
180
|
-
> #### `Atom.from()`
|
|
181
|
-
> Creates an `Atom` based on a `Signal`
|
|
182
|
-
>
|
|
183
|
-
> #### `Atom.prop()`
|
|
184
|
-
> Creates an `Atom` based on an object property
|
|
185
|
-
>
|
|
186
|
-
> #### `Atom.source()`
|
|
187
|
-
> Similiar to `Atom.unwrap()`, but if the `Accessor` doesn't return anything it automatically creates an internal `Signal` in which to store the value
|
|
188
|
-
|
|
189
|
-
### `ReactiveContext.create()`
|
|
190
|
-
Method that creates a reactive version of a solid `Context` with some additional built-in functionalities
|
|
191
|
-
```ts
|
|
192
|
-
const ctx = ReactiveContext.create("SOMETHING");
|
|
193
|
-
const underlyingNormalContext = ctx.ctx;
|
|
194
|
-
const valueAccessorForTheCurrentOwner = ctx.get;
|
|
195
|
-
const value = ctx();
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
### `runWithContext()`
|
|
199
|
-
Creates a context scope that persists for the duration of a function
|
|
200
|
-
|
|
201
165
|
### `unwrap()`
|
|
202
166
|
Calls an `Accessor` maintaining its reactivity at 1 level
|
|
203
167
|
|
|
204
|
-
### `debug()`
|
|
205
|
-
Allows you to define ui section that are only visible in debug or NOT in debug
|
|
206
|
-
```tsx
|
|
207
|
-
const value = false;
|
|
208
|
-
return <>
|
|
209
|
-
<debug.Provider value={value}>
|
|
210
|
-
{/* (A LOT OF NESTING...) */}
|
|
211
|
-
<Show when={debug()}>
|
|
212
|
-
THIS WOULD BE VISIBLE IF "value" HAD BEEN {true}
|
|
213
|
-
</Show>
|
|
214
|
-
</debug.Provider>
|
|
215
|
-
</>
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
### `untrackCall()`
|
|
219
|
-
Calls a function untracking what happens inside of it but not what gets passed as its argument
|
|
220
|
-
|
|
221
|
-
### `nameOf()`
|
|
222
|
-
Utility function that powers `Atom.prop()`
|
|
223
|
-
|
|
224
168
|
### `memoProps()`
|
|
225
169
|
Creates a partial version of an object with memoized remaining properties
|
|
226
170
|
|
|
227
171
|
### `splitAndMemoProps()`
|
|
228
|
-
Like `splitProps()` but memoizes the whole local part
|
|
172
|
+
Like `splitProps()` but memoizes the whole local part
|
|
173
|
+
|
|
174
|
+
### `untrackCall()`
|
|
175
|
+
Calls a function untracking what happens inside of it but not what gets passed as its argument
|
|
176
|
+
|
|
177
|
+
### `runWithContext()`
|
|
178
|
+
Creates a context scope that persists for the duration of a function
|