@manyducks.co/dolla 2.0.0-alpha.61 → 2.0.0-alpha.63
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/README.md +20 -26
- package/dist/core/context.d.ts +23 -4
- package/dist/core/index.d.ts +5 -3
- package/dist/core/markup.d.ts +6 -11
- package/dist/core/nodes/dynamic.d.ts +3 -3
- package/dist/core/nodes/element.d.ts +2 -0
- package/dist/core/nodes/repeat.d.ts +1 -1
- package/dist/core/nodes/view.d.ts +4 -4
- package/dist/core/scheduler.d.ts +17 -0
- package/dist/core/signals.d.ts +48 -14
- package/dist/core/views/default-crash-view.d.ts +1 -1
- package/dist/core/views/for.d.ts +23 -0
- package/dist/core/views/show.d.ts +26 -0
- package/dist/hooks/index.d.ts +5 -8
- package/dist/hooks.js +26 -31
- package/dist/hooks.js.map +1 -1
- package/dist/i18n.js +23 -23
- package/dist/i18n.js.map +1 -1
- package/dist/index.js +69 -53
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.js +1 -1
- package/dist/jsx-runtime.js +1 -1
- package/dist/logger-B7RBYtzP.js +82 -0
- package/dist/logger-B7RBYtzP.js.map +1 -0
- package/dist/{markup-CX27GJ1M.js → markup-CW_lWDfq.js} +410 -325
- package/dist/markup-CW_lWDfq.js.map +1 -0
- package/dist/router/router.d.ts +10 -5
- package/dist/{router-CjCkk4dA.js → router-CBGsADQ4.js} +100 -100
- package/dist/router-CBGsADQ4.js.map +1 -0
- package/dist/router.js +1 -1
- package/dist/{signals-gCwiIe5X.js → signals-DbDmN2gr.js} +190 -161
- package/dist/signals-DbDmN2gr.js.map +1 -0
- package/docs/signals.md +16 -14
- package/notes/scratch.md +23 -1
- package/package.json +3 -2
- package/dist/logger-Bl496yfY.js +0 -91
- package/dist/logger-Bl496yfY.js.map +0 -1
- package/dist/markup-CX27GJ1M.js.map +0 -1
- package/dist/router-CjCkk4dA.js.map +0 -1
- package/dist/signals-gCwiIe5X.js.map +0 -1
package/README.md
CHANGED
|
@@ -35,44 +35,38 @@ Dolla's goals include:
|
|
|
35
35
|
A basic view. Note that the view function is called exactly once when the view is first mounted. All changes to DOM nodes thereafter happen as a result of `$state` values changing.
|
|
36
36
|
|
|
37
37
|
```jsx
|
|
38
|
-
import { $, when, mount } from "@manyducks.co/dolla";
|
|
38
|
+
import { $, effect, when, mount } from "@manyducks.co/dolla";
|
|
39
39
|
|
|
40
40
|
function Counter(props, ctx) {
|
|
41
41
|
const $count = $(0);
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
effect(() => {
|
|
44
|
+
console.log("Count is: " + $count());
|
|
45
|
+
// An effect will re-run whenever any signal accessed inside it receives a new value.
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
$count((x) => x + 1);
|
|
48
|
+
function reset() {
|
|
49
|
+
$count.set(0);
|
|
51
50
|
}
|
|
52
51
|
|
|
53
|
-
function
|
|
54
|
-
$count((
|
|
52
|
+
function increment() {
|
|
53
|
+
$count.set((current) => current + 1);
|
|
55
54
|
}
|
|
56
55
|
|
|
57
|
-
function
|
|
58
|
-
|
|
59
|
-
$count(0);
|
|
56
|
+
function decrement() {
|
|
57
|
+
$count.set((current) => current - 1);
|
|
60
58
|
}
|
|
61
59
|
|
|
62
60
|
return (
|
|
63
61
|
<div>
|
|
64
|
-
{/* Signals can be slotted into the DOM to render them */}
|
|
65
62
|
<p>Counter: {$count}</p>
|
|
63
|
+
{/* Signals can be slotted into the DOM to render them */}
|
|
64
|
+
|
|
66
65
|
<div>
|
|
67
|
-
<button
|
|
68
|
-
<button
|
|
66
|
+
<button onClick={increment}>+1</button>
|
|
67
|
+
<button onClick={decrement}>-1</button>
|
|
68
|
+
<button onClick={reset}>Reset</button>
|
|
69
69
|
</div>
|
|
70
|
-
|
|
71
|
-
{/* We can derive a new signal on the fly and conditionally render something based on that condition */}
|
|
72
|
-
{when(
|
|
73
|
-
$(() => $count() > 10),
|
|
74
|
-
<span>That's a lot of clicks!</span>,
|
|
75
|
-
)}
|
|
76
70
|
</div>
|
|
77
71
|
);
|
|
78
72
|
}
|
|
@@ -95,8 +89,8 @@ function MessageStore(options, ctx) {
|
|
|
95
89
|
// Context objects contain methods for controlling the component, logging and attaching lifecycle hooks.
|
|
96
90
|
|
|
97
91
|
return {
|
|
98
|
-
message: $(() => message()),
|
|
99
|
-
setMessage: (value: string) => message(value),
|
|
92
|
+
$message: $(() => message()),
|
|
93
|
+
setMessage: (value: string) => message.set(value.toUpperCase()),
|
|
100
94
|
};
|
|
101
95
|
}
|
|
102
96
|
|
|
@@ -104,7 +98,7 @@ function App(props, ctx) {
|
|
|
104
98
|
// Provide a store for this and all child views.
|
|
105
99
|
ctx.addStore(MessageStore);
|
|
106
100
|
|
|
107
|
-
const { message, setMessage } = ctx.getStore(MessageStore);
|
|
101
|
+
const { $message, setMessage } = ctx.getStore(MessageStore);
|
|
108
102
|
// Provides a MessageStore on this context and any child contexts.
|
|
109
103
|
// When a store is provided its value is returned right away.
|
|
110
104
|
|
|
@@ -116,8 +110,8 @@ function App(props, ctx) {
|
|
|
116
110
|
|
|
117
111
|
<input
|
|
118
112
|
type="text"
|
|
119
|
-
value={message}
|
|
120
|
-
|
|
113
|
+
value={$message}
|
|
114
|
+
onInput={(e) => {
|
|
121
115
|
setMessage(e.currentTarget.value);
|
|
122
116
|
}}
|
|
123
117
|
/>
|
package/dist/core/context.d.ts
CHANGED
|
@@ -52,6 +52,22 @@ declare class ContextLifecycle {
|
|
|
52
52
|
*/
|
|
53
53
|
private notify;
|
|
54
54
|
}
|
|
55
|
+
export interface ContextGetStateOptions<T> {
|
|
56
|
+
fallback?: T;
|
|
57
|
+
/**
|
|
58
|
+
* Only check this context; skip parent contexts.
|
|
59
|
+
*/
|
|
60
|
+
immediate?: boolean;
|
|
61
|
+
}
|
|
62
|
+
export interface ContextGetStateOptionsWithFallbackValue<T> extends ContextGetStateOptions<T> {
|
|
63
|
+
fallback: T;
|
|
64
|
+
}
|
|
65
|
+
export interface ContextGetStateMapOptions {
|
|
66
|
+
/**
|
|
67
|
+
* Only include state from this context; skip parent contexts.
|
|
68
|
+
*/
|
|
69
|
+
immediate?: boolean;
|
|
70
|
+
}
|
|
55
71
|
export interface ContextOptions {
|
|
56
72
|
logger?: LoggerOptions;
|
|
57
73
|
}
|
|
@@ -118,17 +134,20 @@ export declare class Context implements Logger {
|
|
|
118
134
|
onUnmount(listener: LifecycleListener): () => void;
|
|
119
135
|
effect(callback: EffectFn): UnsubscribeFn;
|
|
120
136
|
/**
|
|
121
|
-
* Gets the value stored at `key`, or returns
|
|
137
|
+
* Gets the value stored at `key`, or returns `options.fallback` if none is set.
|
|
122
138
|
*/
|
|
123
|
-
getState<T>(key: any,
|
|
139
|
+
getState<T>(key: any, options: ContextGetStateOptionsWithFallbackValue<T>): T;
|
|
124
140
|
/**
|
|
125
141
|
* Gets the value stored at `key`, or throws an error if none is set.
|
|
126
142
|
*/
|
|
127
|
-
getState<T>(key: any): T;
|
|
143
|
+
getState<T>(key: any, options?: ContextGetStateOptions<T>): T;
|
|
128
144
|
/**
|
|
129
145
|
* Returns a Map containing all state values available to this context.
|
|
146
|
+
*
|
|
147
|
+
* Pass `options.immediate` to only include state stored on this context.
|
|
148
|
+
* By default all state stored on parent contexts is also included.
|
|
130
149
|
*/
|
|
131
|
-
|
|
150
|
+
getStateMap(options?: ContextGetStateMapOptions): Map<any, any>;
|
|
132
151
|
/**
|
|
133
152
|
* Stores `value` at `key` in this context's state.
|
|
134
153
|
*/
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
export { $, batch, effect, get, untracked } from "./signals.js";
|
|
2
|
-
export type { MaybeSignal,
|
|
1
|
+
export { $, batch, effect, get, memo, writable, untracked } from "./signals.js";
|
|
2
|
+
export type { MaybeSignal, Writable, Signal } from "./signals.js";
|
|
3
3
|
export { createContext } from "./context.js";
|
|
4
4
|
export type { Context } from "./context.js";
|
|
5
|
-
export { m, MarkupNode, portal, render, repeat, unless, when } from "./markup.js";
|
|
5
|
+
export { m, Markup, MarkupNode, portal, render, repeat, unless, when } from "./markup.js";
|
|
6
6
|
export { ref, type Ref } from "./ref.js";
|
|
7
|
+
export { For, type ForProps } from "./views/for.js";
|
|
8
|
+
export { Show, type ShowProps } from "./views/show.js";
|
|
7
9
|
export { mount, type UnmountFn } from "./mount.js";
|
|
8
10
|
export { deepEqual, shallowEqual, strictEqual } from "../utils.js";
|
|
9
11
|
export { getEnv, setEnv } from "./env.js";
|
package/dist/core/markup.d.ts
CHANGED
|
@@ -2,30 +2,25 @@ import type { IntrinsicElements, Renderable, View } from "../types.js";
|
|
|
2
2
|
import { Context } from "./context.js";
|
|
3
3
|
import { MarkupNode } from "./nodes/_markup.js";
|
|
4
4
|
import { KeyFn, RenderFn } from "./nodes/repeat.js";
|
|
5
|
-
import { type
|
|
5
|
+
import { type Signal, type MaybeSignal } from "./signals.js";
|
|
6
6
|
export { MarkupNode };
|
|
7
|
+
type PropsOf<V extends string | View<any>> = V extends View<infer U> ? U : any;
|
|
7
8
|
/**
|
|
8
9
|
* `Markup` is a set of metadata that will be constructed into a `MarkupNode`.
|
|
9
10
|
*/
|
|
10
|
-
export declare class Markup<
|
|
11
|
+
export declare class Markup<Type extends string | View<any> = string | View<any>> {
|
|
11
12
|
/**
|
|
12
13
|
* In the case of a view, type will be the View function itself. It can also hold an identifier for special nodes like "$cond", "$repeat", etc.
|
|
13
14
|
* DOM nodes can be created by name, such as HTML elements like "div", "ul" or "span", SVG elements like ""
|
|
14
15
|
*/
|
|
15
|
-
type:
|
|
16
|
+
type: Type;
|
|
16
17
|
/**
|
|
17
18
|
* Data that will be passed to a new MarkupNode instance when it is constructed.
|
|
18
19
|
* Includes a `children` prop if children were passed.
|
|
19
20
|
*/
|
|
20
|
-
props:
|
|
21
|
-
constructor(type:
|
|
21
|
+
props: PropsOf<Type>;
|
|
22
|
+
constructor(type: Type, props: PropsOf<Type>);
|
|
22
23
|
}
|
|
23
|
-
/**
|
|
24
|
-
* A node that can be mounted by the Markup layout engine.
|
|
25
|
-
* Implemented by the built in nodes, but can of course also be implemented to create your own custom nodes.
|
|
26
|
-
*
|
|
27
|
-
* A `MarkupNode` instance can be passed anywhere a `Renderable` is required.
|
|
28
|
-
*/
|
|
29
24
|
export declare enum MarkupType {
|
|
30
25
|
DOM = "$dom",
|
|
31
26
|
Dynamic = "$dynamic",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Context } from "../context.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Signal } from "../signals.js";
|
|
3
3
|
import { MarkupNode } from "./_markup.js";
|
|
4
4
|
/**
|
|
5
5
|
* Renders any kind of content; markup, signals, DOM nodes, etc.
|
|
@@ -9,9 +9,9 @@ export declare class DynamicNode extends MarkupNode {
|
|
|
9
9
|
private root;
|
|
10
10
|
private children;
|
|
11
11
|
private context;
|
|
12
|
-
private
|
|
12
|
+
private slot;
|
|
13
13
|
private unsubscribe?;
|
|
14
|
-
constructor(context: Context,
|
|
14
|
+
constructor(context: Context, slot: Signal<any>);
|
|
15
15
|
getRoot(): Text;
|
|
16
16
|
isMounted(): boolean;
|
|
17
17
|
mount(parent: Node, after?: Node): void;
|
|
@@ -5,6 +5,7 @@ import { MarkupNode } from "./_markup.js";
|
|
|
5
5
|
*/
|
|
6
6
|
export declare class ElementNode extends MarkupNode {
|
|
7
7
|
private root;
|
|
8
|
+
private id;
|
|
8
9
|
readonly tag: string;
|
|
9
10
|
readonly props: Record<string, any>;
|
|
10
11
|
private context;
|
|
@@ -19,6 +20,7 @@ export declare class ElementNode extends MarkupNode {
|
|
|
19
20
|
unmount(skipDOM?: boolean): void;
|
|
20
21
|
move(parent: Element, after?: Node): void;
|
|
21
22
|
private attachProp;
|
|
23
|
+
private getKey;
|
|
22
24
|
private applyProps;
|
|
23
25
|
private applyStyles;
|
|
24
26
|
private applyClasses;
|
|
@@ -2,7 +2,7 @@ import type { Renderable } from "../../types.js";
|
|
|
2
2
|
import type { Context } from "../context.js";
|
|
3
3
|
import { type Signal } from "../signals.js";
|
|
4
4
|
import { MarkupNode } from "./_markup.js";
|
|
5
|
-
export type Key =
|
|
5
|
+
export type Key = any;
|
|
6
6
|
export type KeyFn<T> = (item: T, index: number) => Key;
|
|
7
7
|
export type RenderFn<T> = (item: Signal<T>, index: Signal<number>, ctx: Context) => Renderable;
|
|
8
8
|
/**
|
|
@@ -6,10 +6,10 @@ export declare const VIEW: unique symbol;
|
|
|
6
6
|
* Renders a View.
|
|
7
7
|
*/
|
|
8
8
|
export declare class ViewNode<P> extends MarkupNode {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
view: View<P>;
|
|
9
|
+
readonly id: string;
|
|
10
|
+
readonly props: P;
|
|
11
|
+
readonly context: Context;
|
|
12
|
+
readonly view: View<P>;
|
|
13
13
|
node?: MarkupNode;
|
|
14
14
|
/**
|
|
15
15
|
* @param context - Parent contenxt to link to.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type Callback = () => void;
|
|
2
|
+
/**
|
|
3
|
+
* Centralized scheduling for DOM updates.
|
|
4
|
+
*/
|
|
5
|
+
declare class Scheduler {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Schedules an update on the next tick. If `key` is supplied, this update will replace any scheduled update with the same key.
|
|
9
|
+
*/
|
|
10
|
+
scheduleUpdate(update: Callback, key?: string): void;
|
|
11
|
+
/**
|
|
12
|
+
* Registers a callback to run after all processing has finished in the next tick.
|
|
13
|
+
*/
|
|
14
|
+
nextTick(callback: Callback): void;
|
|
15
|
+
}
|
|
16
|
+
declare const _default: Scheduler;
|
|
17
|
+
export default _default;
|
package/dist/core/signals.d.ts
CHANGED
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
import { Context } from "./context";
|
|
2
|
-
interface ComputedGetterState<T> {
|
|
3
|
-
value?: T;
|
|
4
|
-
}
|
|
5
2
|
export declare function getCurrentContext(): Context | undefined;
|
|
6
3
|
export declare function setCurrentContext(context: Context | undefined): Context | undefined;
|
|
7
4
|
/**
|
|
8
|
-
* A
|
|
9
|
-
*
|
|
5
|
+
* A function that returns the current value of a signal.
|
|
6
|
+
* Automatically tracked as a dependency when called within a tracking scope (such as `memo` or `effect` functions).
|
|
10
7
|
*/
|
|
11
8
|
export interface Signal<T> {
|
|
12
9
|
(): T;
|
|
13
10
|
}
|
|
14
11
|
/**
|
|
15
|
-
*
|
|
12
|
+
* A function that sets the value of the signal.
|
|
16
13
|
*/
|
|
17
|
-
export interface
|
|
14
|
+
export interface Setter<T> {
|
|
18
15
|
(value: T): void;
|
|
19
|
-
(
|
|
16
|
+
(update: (current: T) => T): void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A getter and setter in a single object. Callable like a getter, but includes a `set` method for updating the signal's value.
|
|
20
|
+
*/
|
|
21
|
+
export interface Writable<T> extends Signal<T> {
|
|
22
|
+
set: Setter<T>;
|
|
20
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Utility type for a value that may be a getter or a plain value.
|
|
26
|
+
* This value can be unwrapped to a plain value with `get` or `untracked` (depending on whether you're in a tracking context and need to track it).
|
|
27
|
+
*/
|
|
21
28
|
export type MaybeSignal<T> = Signal<T> | T;
|
|
22
29
|
export type EqualityFn<T> = (current: T, next: T) => boolean;
|
|
23
30
|
export interface SignalOptions<T> {
|
|
@@ -26,6 +33,26 @@ export interface SignalOptions<T> {
|
|
|
26
33
|
*/
|
|
27
34
|
equals?: EqualityFn<T>;
|
|
28
35
|
}
|
|
36
|
+
export declare function $<T>(compute: (previousValue?: T) => MaybeSignal<T>, options?: MemoOptions<T>): Signal<T>;
|
|
37
|
+
export declare function $<T>(): Writable<T | undefined>;
|
|
38
|
+
export declare function $<T>(value: undefined, options: SignalOptions<T | undefined>): Writable<T | undefined>;
|
|
39
|
+
export declare function $<T>(value: T, options?: SignalOptions<T>): Writable<T>;
|
|
40
|
+
export declare function writable<T>(): Writable<T | undefined>;
|
|
41
|
+
export declare function writable<T>(value: undefined, options: SignalOptions<T | undefined>): Writable<T | undefined>;
|
|
42
|
+
export declare function writable<T>(value: T, options?: SignalOptions<T>): Writable<T>;
|
|
43
|
+
export interface MemoOptions<T> extends SignalOptions<T> {
|
|
44
|
+
/**
|
|
45
|
+
* An array of signals this `memo` depends on. If this is passed, calls to signals within `fn` will NOT be tracked.
|
|
46
|
+
* Instead the `deps` array will be tracked and `fn` will re-run when any value in `deps` changes.
|
|
47
|
+
*/
|
|
48
|
+
deps?: Signal<any>[];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Creates a derived Signal that recomputes its value only when its dependencies change.
|
|
52
|
+
* Dependencies are tracked when called inside `fn` by default,
|
|
53
|
+
* but can be overridden by passing a `deps` array in the options object.
|
|
54
|
+
*/
|
|
55
|
+
export declare function memo<T>(fn: (previousValue?: T) => MaybeSignal<T>, options?: MemoOptions<T>): Signal<T>;
|
|
29
56
|
/**
|
|
30
57
|
* Suspends effects during `fn`. Effects for all updated Signal values are called at the end of the batch.
|
|
31
58
|
*/
|
|
@@ -43,6 +70,18 @@ export declare function get<T>(value: MaybeSignal<T>): T;
|
|
|
43
70
|
*/
|
|
44
71
|
export type EffectFn = () => void | (() => void);
|
|
45
72
|
export type UnsubscribeFn = () => void;
|
|
73
|
+
export declare const INTERNAL_EFFECT: unique symbol;
|
|
74
|
+
export interface EffectOptions {
|
|
75
|
+
/**
|
|
76
|
+
* An array of signals this effect depends on. If this is passed, calls to signals within `fn` will NOT be tracked.
|
|
77
|
+
* Instead the `deps` array will be tracked and `fn` will re-run when any value in `deps` changes.
|
|
78
|
+
*/
|
|
79
|
+
deps?: Signal<any>[];
|
|
80
|
+
/**
|
|
81
|
+
* For internal use.
|
|
82
|
+
*/
|
|
83
|
+
_type?: symbol;
|
|
84
|
+
}
|
|
46
85
|
/**
|
|
47
86
|
* Creates a tracked scope that re-runs whenever the values of any tracked reactives changes.
|
|
48
87
|
* Reactives are tracked by accessing their `value` within the body of the function.
|
|
@@ -50,9 +89,4 @@ export type UnsubscribeFn = () => void;
|
|
|
50
89
|
* NOTE: You must call the unsubscribe function to stop watching for changes.
|
|
51
90
|
* If you are using an effect inside a View or Store, use `ctx.effect` instead, which cleans up automatically when the component unmounts.
|
|
52
91
|
*/
|
|
53
|
-
export declare function effect(fn: EffectFn): UnsubscribeFn;
|
|
54
|
-
export declare function $<T>(compute: (this: ComputedGetterState<T>) => MaybeSignal<T>, options?: SignalOptions<T>): Signal<T>;
|
|
55
|
-
export declare function $<T>(value: T, options?: SignalOptions<T>): Source<T>;
|
|
56
|
-
export declare function $<T>(value: undefined, options?: SignalOptions<T>): Source<T | undefined>;
|
|
57
|
-
export declare function $<T>(): Source<T | undefined>;
|
|
58
|
-
export {};
|
|
92
|
+
export declare function effect(fn: EffectFn, options?: EffectOptions): UnsubscribeFn;
|
|
@@ -22,4 +22,4 @@ export interface CrashViewProps {
|
|
|
22
22
|
/**
|
|
23
23
|
* The crash view displayed unless you specify your own.
|
|
24
24
|
*/
|
|
25
|
-
export declare function DefaultCrashView(props: CrashViewProps): import("../markup.js").Markup<any
|
|
25
|
+
export declare function DefaultCrashView(props: CrashViewProps): import("../markup.js").Markup<string | import("../index.js").View<any>>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Renderable } from "../../types";
|
|
2
|
+
import type { Context } from "../context";
|
|
3
|
+
import { type Key, RepeatNode } from "../nodes/repeat";
|
|
4
|
+
import { type Signal } from "../signals";
|
|
5
|
+
export interface ForProps<T> {
|
|
6
|
+
/**
|
|
7
|
+
* An array of items to render.
|
|
8
|
+
*/
|
|
9
|
+
each: Signal<T[]>;
|
|
10
|
+
/**
|
|
11
|
+
* A function to extract a unique key that identifies each item.
|
|
12
|
+
* If no `key` function is passed, object identity (===) will be used.
|
|
13
|
+
*/
|
|
14
|
+
key?: (item: T, index: number) => Key;
|
|
15
|
+
/**
|
|
16
|
+
* A render function. Takes the item and its index in signal form and returns something to display for each item.
|
|
17
|
+
*/
|
|
18
|
+
children: (item: Signal<T>, index: Signal<number>, ctx: Context) => Renderable;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
export declare function For<T>(props: ForProps<T>, context: Context): RepeatNode<T>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Renderable } from "../../types";
|
|
2
|
+
import type { Context } from "../context";
|
|
3
|
+
import { DynamicNode } from "../nodes/dynamic";
|
|
4
|
+
import { type Signal } from "../signals";
|
|
5
|
+
export interface ShowProps {
|
|
6
|
+
/**
|
|
7
|
+
* If present, children will be rendered only when this signal holds a truthy value.
|
|
8
|
+
*/
|
|
9
|
+
when?: Signal<any>;
|
|
10
|
+
/**
|
|
11
|
+
* If present, children will be rendered only when this signal holds a falsy value.
|
|
12
|
+
*/
|
|
13
|
+
unless?: Signal<any>;
|
|
14
|
+
/**
|
|
15
|
+
* Content to render if conditions permit.
|
|
16
|
+
*/
|
|
17
|
+
children: Renderable;
|
|
18
|
+
/**
|
|
19
|
+
* Content to render when conditions don't permit `children` to render.
|
|
20
|
+
*/
|
|
21
|
+
fallback?: Renderable;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Conditionally display children.
|
|
25
|
+
*/
|
|
26
|
+
export declare function Show(props: ShowProps, context: Context): DynamicNode;
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Context, Logger, type Ref, type Store } from "../core";
|
|
2
|
-
import { type EffectFn, MaybeSignal,
|
|
2
|
+
import { type EffectFn, Signal, MaybeSignal, Setter, SignalOptions } from "../core/signals";
|
|
3
3
|
/**
|
|
4
4
|
* Returns the Context object of the View, Store or Mixin this hook is called in.
|
|
5
5
|
*/
|
|
@@ -10,21 +10,18 @@ export declare function useContext(): Context;
|
|
|
10
10
|
*/
|
|
11
11
|
export declare function useLogger(name?: MaybeSignal<string>): Logger;
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* Creates a new read-only Getter and a bound Setter function.
|
|
14
14
|
*/
|
|
15
|
-
export
|
|
16
|
-
(value: T): void;
|
|
17
|
-
(fn: (current: T) => T): void;
|
|
18
|
-
}
|
|
15
|
+
export declare function useState<T>(value: T, options?: SignalOptions<T>): [Signal<T>, Setter<T>];
|
|
19
16
|
/**
|
|
20
17
|
* Creates a new read-only Signal and a bound Setter function.
|
|
21
18
|
*/
|
|
22
|
-
export declare function useState<T>(value: T): [Signal<T>, Setter<T>];
|
|
19
|
+
export declare function useState<T>(value: undefined, options: SignalOptions<T>): [Signal<T | undefined>, Setter<T | undefined>];
|
|
23
20
|
/**
|
|
24
21
|
* Creates a new read-only Signal and a bound Setter function.
|
|
25
22
|
*/
|
|
26
23
|
export declare function useState<T>(): [Signal<T | undefined>, Setter<T | undefined>];
|
|
27
|
-
export declare function useMemo<T>(compute: (current?: T) => T
|
|
24
|
+
export declare function useMemo<T>(compute: (current?: T) => MaybeSignal<T>, deps?: Signal<any>[], options?: SignalOptions<T>): Signal<T>;
|
|
28
25
|
export declare function useEffect(fn: EffectFn, deps?: Signal<any>[]): void;
|
|
29
26
|
/**
|
|
30
27
|
* Takes the current state and a dispatched action. Returns a new state based on the action.
|
package/dist/hooks.js
CHANGED
|
@@ -1,69 +1,64 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { r as
|
|
1
|
+
import { w as s, m as f, g as i, u as a, h as x } from "./signals-DbDmN2gr.js";
|
|
2
|
+
import { r as m } from "./ref-BD79iqlg.js";
|
|
3
3
|
function o() {
|
|
4
|
-
const t =
|
|
4
|
+
const t = x();
|
|
5
5
|
if (!t)
|
|
6
6
|
throw new Error("No context found; hooks can only be called in the body of a View, Store or Mixin.");
|
|
7
7
|
return t;
|
|
8
8
|
}
|
|
9
|
-
function
|
|
9
|
+
function p(t) {
|
|
10
10
|
const e = o();
|
|
11
11
|
return t && e.setName(t), e;
|
|
12
12
|
}
|
|
13
|
-
function
|
|
13
|
+
function d(t, e) {
|
|
14
14
|
o();
|
|
15
|
-
const
|
|
16
|
-
return [() =>
|
|
15
|
+
const n = s(t, e);
|
|
16
|
+
return [() => n(), n.set];
|
|
17
17
|
}
|
|
18
|
-
function
|
|
19
|
-
return o(), e
|
|
20
|
-
for (const n of e) c(n);
|
|
21
|
-
return s(() => t(this.value));
|
|
22
|
-
}) : u(function() {
|
|
23
|
-
return t(this.value);
|
|
24
|
-
});
|
|
18
|
+
function w(t, e, n) {
|
|
19
|
+
return o(), f(t, { ...n, deps: e });
|
|
25
20
|
}
|
|
26
21
|
function S(t, e) {
|
|
27
22
|
const n = o();
|
|
28
23
|
e ? n.effect(() => {
|
|
29
|
-
for (const
|
|
30
|
-
|
|
24
|
+
for (const c of e) i(c);
|
|
25
|
+
a(t);
|
|
31
26
|
}) : n.effect(t);
|
|
32
27
|
}
|
|
33
|
-
function
|
|
34
|
-
const [n,
|
|
35
|
-
return [n, (
|
|
36
|
-
|
|
28
|
+
function b(t, e) {
|
|
29
|
+
const [n, c] = d(e);
|
|
30
|
+
return [n, (r) => {
|
|
31
|
+
c((u) => t(u, r));
|
|
37
32
|
}];
|
|
38
33
|
}
|
|
39
34
|
function M(t) {
|
|
40
35
|
return o().getStore(t);
|
|
41
36
|
}
|
|
42
|
-
function
|
|
37
|
+
function y(...t) {
|
|
43
38
|
o();
|
|
44
|
-
const e =
|
|
39
|
+
const e = m(...t);
|
|
45
40
|
return Object.defineProperty(e, "current", { get: e, set: e }), e;
|
|
46
41
|
}
|
|
47
|
-
function
|
|
42
|
+
function C(t) {
|
|
48
43
|
const e = o();
|
|
49
44
|
e.onMount(() => {
|
|
50
45
|
const n = t();
|
|
51
46
|
n && e.onUnmount(n);
|
|
52
47
|
});
|
|
53
48
|
}
|
|
54
|
-
function
|
|
49
|
+
function R(t) {
|
|
55
50
|
o().onUnmount(t);
|
|
56
51
|
}
|
|
57
52
|
export {
|
|
58
53
|
o as useContext,
|
|
59
54
|
S as useEffect,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
55
|
+
p as useLogger,
|
|
56
|
+
w as useMemo,
|
|
57
|
+
C as useMount,
|
|
58
|
+
b as useReducer,
|
|
59
|
+
y as useRef,
|
|
60
|
+
d as useState,
|
|
66
61
|
M as useStore,
|
|
67
|
-
|
|
62
|
+
R as useUnmount
|
|
68
63
|
};
|
|
69
64
|
//# sourceMappingURL=hooks.js.map
|
package/dist/hooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.js","sources":["../src/hooks/index.ts"],"sourcesContent":["import { Context, Logger, ref, type Ref, type Store } from \"../core\";\nimport {
|
|
1
|
+
{"version":3,"file":"hooks.js","sources":["../src/hooks/index.ts"],"sourcesContent":["import { Context, Logger, ref, type Ref, type Store } from \"../core\";\nimport {\n type EffectFn,\n get,\n getCurrentContext,\n Signal,\n MaybeSignal,\n memo,\n Setter,\n writable,\n SignalOptions,\n untracked,\n} from \"../core/signals\";\n\n/**\n * Returns the Context object of the View, Store or Mixin this hook is called in.\n */\nexport function useContext(): Context {\n const context = getCurrentContext();\n if (!context) {\n throw new Error(`No context found; hooks can only be called in the body of a View, Store or Mixin.`);\n }\n return context;\n}\n\n/**\n * Returns a logger. If a name is passed it will be used as a prefix for all console messages.\n * Otherwise the default name of the context will be used.\n */\nexport function useLogger(name?: MaybeSignal<string>): Logger {\n const context = useContext();\n if (name) context.setName(name);\n return context;\n}\n\n/**\n * Creates a new read-only Getter and a bound Setter function.\n */\nexport function useState<T>(value: T, options?: SignalOptions<T>): [Signal<T>, Setter<T>];\n\n/**\n * Creates a new read-only Signal and a bound Setter function.\n */\nexport function useState<T>(\n value: undefined,\n options: SignalOptions<T>,\n): [Signal<T | undefined>, Setter<T | undefined>];\n\n/**\n * Creates a new read-only Signal and a bound Setter function.\n */\nexport function useState<T>(): [Signal<T | undefined>, Setter<T | undefined>];\n\nexport function useState<T>(value?: T, options?: SignalOptions<T>): [Signal<T>, Setter<T>] {\n useContext(); // assert that we're in a valid context\n const state = writable(value as T, options);\n return [() => state(), state.set];\n}\n\nexport function useMemo<T>(\n compute: (current?: T) => MaybeSignal<T>,\n deps?: Signal<any>[],\n options?: SignalOptions<T>,\n): Signal<T> {\n useContext(); // assert that we're in a valid context\n return memo(compute, { ...options, deps });\n}\n\nexport function useEffect(fn: EffectFn, deps?: Signal<any>[]): void {\n const context = useContext();\n if (deps) {\n context.effect(() => {\n // Track deps and run `fn` untracked.\n for (const dep of deps) get(dep);\n untracked(fn);\n });\n } else {\n context.effect(fn);\n }\n}\n\n/**\n * Takes the current state and a dispatched action. Returns a new state based on the action.\n * Typically the body of this function will be a large switch statement.\n */\nexport type ReducerFn<State, Action> = (state: State, action: Action) => State;\n\n/**\n * Dispatches an action to this reducer, causing the state to update.\n */\nexport type DispatchFn<Action> = (action: Action) => void;\n\n/**\n *\n */\nexport function useReducer<State, Action>(\n reducer: ReducerFn<State, Action>,\n initialState: State,\n): [Signal<State>, DispatchFn<Action>] {\n const [state, setState] = useState(initialState);\n const dispatch = (action: Action) => {\n setState((current) => reducer(current, action));\n };\n return [state, dispatch];\n}\n\n/**\n * Uses a previously added Store. Takes the Store function itself and returns the nearest instance.\n */\nexport function useStore<T>(store: Store<any, T>): T {\n const context = useContext();\n return context.getStore(store);\n}\n\n/**\n * A hybrid Ref which is both a function ref and a React-style object ref with a `current` property.\n * Both the `current` property and the function syntax access the same value.\n */\nexport interface HybridRef<T> extends Ref<T> {\n current: T;\n}\n\n/**\n * Creates a Ref. Useful for getting references to DOM nodes.\n */\nexport function useRef<T>(initialValue?: T): HybridRef<T>;\n\nexport function useRef<T>(...value: [T]): HybridRef<T> {\n useContext(); // assert that we're in a valid context\n const valueRef = ref(...value);\n Object.defineProperty(valueRef, \"current\", { get: valueRef, set: valueRef });\n return valueRef as HybridRef<T>;\n}\n\n/**\n * Calls `callback` when the context is mounted. If `callback` returns a function, that function is called when the context is unmounted.\n */\nexport function useMount(callback: () => void | (() => void)): void {\n const context = useContext();\n context.onMount(() => {\n const result = callback();\n if (result) context.onUnmount(result);\n });\n}\n\n/**\n * Calls `callback` when the context is unmounted.\n */\nexport function useUnmount(callback: () => void): void {\n const context = useContext();\n context.onUnmount(callback);\n}\n"],"names":["useContext","context","getCurrentContext","useLogger","name","useState","value","options","state","writable","useMemo","compute","deps","memo","useEffect","fn","dep","get","untracked","useReducer","reducer","initialState","setState","action","current","useStore","store","useRef","valueRef","ref","useMount","callback","result","useUnmount"],"mappings":";;AAiBO,SAASA,IAAsB;AACpC,QAAMC,IAAUC,EAAkB;AAClC,MAAI,CAACD;AACG,UAAA,IAAI,MAAM,mFAAmF;AAE9F,SAAAA;AACT;AAMO,SAASE,EAAUC,GAAoC;AAC5D,QAAMH,IAAUD,EAAW;AACvB,SAAAI,KAAcH,EAAA,QAAQG,CAAI,GACvBH;AACT;AAoBgB,SAAAI,EAAYC,GAAWC,GAAoD;AAC9E,EAAAP,EAAA;AACL,QAAAQ,IAAQC,EAASH,GAAYC,CAAO;AAC1C,SAAO,CAAC,MAAMC,KAASA,EAAM,GAAG;AAClC;AAEgB,SAAAE,EACdC,GACAC,GACAL,GACW;AACA,SAAAP,EAAA,GACJa,EAAKF,GAAS,EAAE,GAAGJ,GAAS,MAAAK,GAAM;AAC3C;AAEgB,SAAAE,EAAUC,GAAcH,GAA4B;AAClE,QAAMX,IAAUD,EAAW;AAC3B,EAAIY,IACFX,EAAQ,OAAO,MAAM;AAER,eAAAe,KAAOJ,EAAM,CAAAK,EAAID,CAAG;AAC/B,IAAAE,EAAUH,CAAE;AAAA,EAAA,CACb,IAEDd,EAAQ,OAAOc,CAAE;AAErB;AAgBgB,SAAAI,EACdC,GACAC,GACqC;AACrC,QAAM,CAACb,GAAOc,CAAQ,IAAIjB,EAASgB,CAAY;AAIxC,SAAA,CAACb,GAHS,CAACe,MAAmB;AACnC,IAAAD,EAAS,CAACE,MAAYJ,EAAQI,GAASD,CAAM,CAAC;AAAA,EAChD,CACuB;AACzB;AAKO,SAASE,EAAYC,GAAyB;AAE5C,SADS1B,EAAW,EACZ,SAAS0B,CAAK;AAC/B;AAeO,SAASC,KAAarB,GAA0B;AAC1C,EAAAN,EAAA;AACL,QAAA4B,IAAWC,EAAI,GAAGvB,CAAK;AACtB,gBAAA,eAAesB,GAAU,WAAW,EAAE,KAAKA,GAAU,KAAKA,GAAU,GACpEA;AACT;AAKO,SAASE,EAASC,GAA2C;AAClE,QAAM9B,IAAUD,EAAW;AAC3B,EAAAC,EAAQ,QAAQ,MAAM;AACpB,UAAM+B,IAASD,EAAS;AACpB,IAAAC,KAAgB/B,EAAA,UAAU+B,CAAM;AAAA,EAAA,CACrC;AACH;AAKO,SAASC,EAAWF,GAA4B;AAErD,EADgB/B,EAAW,EACnB,UAAU+B,CAAQ;AAC5B;"}
|