@manyducks.co/dolla 2.0.0-alpha.23 → 2.0.0-alpha.25

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 CHANGED
@@ -20,8 +20,8 @@ Let's first get into some examples.
20
20
 
21
21
  ### Basic State API
22
22
 
23
- ```jsx
24
- import { createState, toState, valueOf, derive, createWatcher } from "@manyducks.co/dolla";
23
+ ```js
24
+ import { createState, derive } from "@manyducks.co/dolla";
25
25
 
26
26
  const [$count, setCount] = createState(72);
27
27
 
@@ -35,26 +35,24 @@ $count.get(); // 300
35
35
  // You can also pass a function that takes the current value and returns a new one
36
36
  setCount((current) => current + 1);
37
37
  $count.get(); // 301
38
+ ```
38
39
 
39
- // Derive a new state from one or more other states. Whenever $count changes, $doubled will follow.
40
+ Now that you have a state you can derive more states from that one. Derived states automatically stay in sync with the values of their dependencies.
41
+
42
+ ```js
43
+ // Pass and array of one or more states followed by a function that computes a new value.
40
44
  const $doubled = derive([$count], (count) => count * 2);
41
- const $sum = derive([$count, $doubled], (count, doubled) => count + doubled);
42
45
 
43
- // Returns the value of a state. If the value is not a state it is returned as is.
44
- const count = valueOf($count);
45
- const bool = valueOf(true);
46
+ $doubled.get(); // 602
46
47
 
47
- // Creates a state from a value. If the value is already a state it is returned as is.
48
- const $bool = toState(true);
49
- const $anotherCount = toState($count);
48
+ setCount(500);
50
49
 
51
- const watcher = createWatcher();
50
+ $doubled.get(); // 1000
51
+ ```
52
+
53
+ ### In Views
52
54
 
53
- // Watch for changes to the value
54
- const stop = watcher.watch([$count], (value) => [
55
- // This function is called immediately with the current value, then again each time the value changes.
56
- ]);
57
- stop(); // Stop watching for changes
55
+ ```jsx
58
56
 
59
57
  ```
60
58
 
@@ -105,12 +103,12 @@ $doubled(); // 100
105
103
  $value(); // 50
106
104
  ```
107
105
 
108
- ## Views [id="section-views"]
106
+ <h2 id="section-views">Views</h2>
109
107
 
110
108
  A basic view:
111
109
 
112
110
  ```js
113
- import Dolla, { createState, html } from "@manyducks.co/dolla";
111
+ import Dolla, { createState } from "@manyducks.co/dolla";
114
112
 
115
113
  function Counter(props, ctx) {
116
114
  const [$count, setCount] = createState(0);
@@ -119,12 +117,24 @@ function Counter(props, ctx) {
119
117
  setCount((count) => count + 1);
120
118
  }
121
119
 
122
- return html`
120
+ function decrement() {
121
+ setCount((count) => count - 1);
122
+ }
123
+
124
+ function reset() {
125
+ setCount(0);
126
+ }
127
+
128
+ return (
123
129
  <div>
124
- <p>Clicks: ${$count}</p>
125
- <button onclick=${increment}>+1</button>
130
+ <p>Clicks: {$count}</p>
131
+ <div>
132
+ <button onClick={decrement}>-1</button>
133
+ <button onClick={reset}>0</button>
134
+ <button onClick={increment}>-1</button>
135
+ </div>
126
136
  </div>
127
- `;
137
+ );
128
138
  }
129
139
 
130
140
  Dolla.mount(document.body, Counter);
@@ -2,7 +2,7 @@ import { type CrashViewProps } from "../views/default-crash-view.js";
2
2
  import { Batch } from "./batch.js";
3
3
  import { type Markup, type MarkupElement } from "./markup.js";
4
4
  import { type ViewElement, type ViewFunction } from "./nodes/view.js";
5
- import { createRef, createSettableState, createState, createWatcher, derive, isRef, toSettableState, toState, valueOf } from "./state.js";
5
+ import { createRef, createState, createWatcher, derive, isRef, isState, toState, toValue } from "./state.js";
6
6
  import { HTTP } from "../modules/http.js";
7
7
  import { I18n } from "../modules/i18n.js";
8
8
  import { Router } from "../modules/router.js";
@@ -52,10 +52,9 @@ export declare class Dolla {
52
52
  constructor();
53
53
  watch: <I extends import("./state.js").MaybeState<any>[]>(states: [...I], fn: (...currentValues: import("./state.js").StateValues<I>) => void) => import("./state.js").StopFunction;
54
54
  createState: typeof createState;
55
- createSettableState: typeof createSettableState;
56
- toSettableState: typeof toSettableState;
57
55
  toState: typeof toState;
58
- valueOf: typeof valueOf;
56
+ toValue: typeof toValue;
57
+ isState: typeof isState;
59
58
  derive: typeof derive;
60
59
  createWatcher: typeof createWatcher;
61
60
  createRef: typeof createRef;
@@ -15,6 +15,7 @@ export declare class HTML implements MarkupElement {
15
15
  stopCallbacks: StopFunction[];
16
16
  elementContext: ElementContext;
17
17
  uniqueId: string;
18
+ _batchWrite: (callback: () => void, key?: string) => void;
18
19
  ref?: Ref<any>;
19
20
  canClickAway: boolean;
20
21
  get isMounted(): boolean;
@@ -22,6 +23,7 @@ export declare class HTML implements MarkupElement {
22
23
  mount(parent: Node, after?: Node): void;
23
24
  unmount(parentIsUnmounting?: boolean): void;
24
25
  getUpdateKey(type: string, value: string | number): string;
26
+ _mutate(callback: () => any, updateKey?: string): void;
25
27
  attachProp<T>(value: State<T> | T, callback: (value: T) => void, updateKey: string): void;
26
28
  applyProps(element: HTMLElement | SVGElement, props: Record<string, unknown>): void;
27
29
  applyStyles(element: HTMLElement | SVGElement, styles: unknown, stopCallbacks: StopFunction[]): () => void;
@@ -15,19 +15,16 @@ export declare class Observer implements MarkupElement {
15
15
  [TYPE_MARKUP_ELEMENT]: boolean;
16
16
  node: Node;
17
17
  endNode: Node;
18
- connectedViews: MarkupElement[];
18
+ children: MarkupElement[];
19
19
  renderFn: (...values: any) => Renderable;
20
20
  elementContext: ElementContext;
21
- observerControls: {
22
- start: () => void;
23
- stop: () => void;
24
- };
25
21
  watcher: import("../state.js").StateWatcher;
22
+ sources: State<any>[];
26
23
  get isMounted(): boolean;
27
24
  constructor({ states, renderFn, elementContext }: ObserverOptions);
28
25
  mount(parent: Node, after?: Node): void;
29
26
  unmount(parentIsUnmounting?: boolean): void;
30
27
  cleanup(parentIsUnmounting: boolean): void;
31
- update(...children: Renderable[]): void;
28
+ update(children: Renderable[]): void;
32
29
  }
33
30
  export {};
@@ -8,11 +8,12 @@ export declare class Outlet implements MarkupElement {
8
8
  [TYPE_MARKUP_ELEMENT]: boolean;
9
9
  node: Text;
10
10
  isMounted: boolean;
11
- elements: MaybeState<MarkupElement[]>;
12
- children: MarkupElement[];
11
+ source: MaybeState<MarkupElement[]>;
12
+ elements: MarkupElement[];
13
13
  stopCallback?: StopFunction;
14
- constructor(elements: MaybeState<MarkupElement[]>);
14
+ constructor(source: MaybeState<MarkupElement[]>);
15
15
  mount(parent: Node, after?: Node | undefined): void;
16
16
  unmount(parentIsUnmounting?: boolean): void;
17
- update(newChildren: MarkupElement[]): void;
17
+ cleanup(parentIsUnmounting: boolean): void;
18
+ update(newElements: MarkupElement[]): void;
18
19
  }
@@ -71,6 +71,7 @@ export declare class View<P> implements ViewElement {
71
71
  _view: ViewFunction<P>;
72
72
  _props: P;
73
73
  _element?: MarkupElement;
74
+ _childMarkup: Markup[];
74
75
  _$children: State<MarkupElement[]>;
75
76
  _setChildren: import("../state.js").Setter<MarkupElement[], MarkupElement[]>;
76
77
  _watcher: import("../state.js").StateWatcher;
@@ -1,5 +1,5 @@
1
1
  import { strictEqual } from "../utils";
2
- import { TYPE_SETTABLE_STATE, TYPE_STATE } from "./symbols";
2
+ import { TYPE_STATE } from "./symbols";
3
3
  /**
4
4
  * Stops the observer that created it when called.
5
5
  */
@@ -62,16 +62,25 @@ export interface SettableState<I, O = I> extends State<I> {
62
62
  */
63
63
  set(callback: (current: I) => O): void;
64
64
  }
65
- export interface Ref<T extends Node> extends State<T | undefined> {
66
- node: T | undefined;
65
+ /**
66
+ *
67
+ */
68
+ export interface Ref<T> {
69
+ /**
70
+ * Get: returns the current value stored in the ref (or undefined).
71
+ */
72
+ (): T | undefined;
73
+ /**
74
+ * Set: stores a new `value` in the ref.
75
+ */
76
+ <T>(value: T | undefined): void;
67
77
  }
68
78
  export declare function isState<T>(value: any): value is State<T>;
69
- export declare function isSettableState<T>(value: any): value is SettableState<T>;
70
79
  export declare function isRef<T extends Node>(value: any): value is Ref<T>;
71
80
  /**
72
81
  * Retrieves a plain value from a variable that may be a state.
73
82
  */
74
- export declare function valueOf<T>(value: MaybeState<T>): T;
83
+ export declare function toValue<T>(source: MaybeState<T>): T;
75
84
  /**
76
85
  * Ensures a variable that may be a state or plain value is a state.
77
86
  */
@@ -95,44 +104,44 @@ export declare class Signal<T> implements State<T> {
95
104
  /**
96
105
  * Creates a state and setter.
97
106
  */
98
- export declare function createState<T>(initialValue: T, options?: CreateStateOptions<T>): [State<T>, Setter<T>];
107
+ export declare function createState<T>(value: T, options?: CreateStateOptions<T>): [State<T>, Setter<T>];
99
108
  /**
100
109
  * Creates a state and setter.
101
110
  */
102
- export declare function createState<T>(initialValue?: T, options?: CreateStateOptions<T | undefined>): [State<T | undefined>, Setter<T | undefined>];
103
- export declare class SettableSignal<T> implements State<T>, SettableState<T> {
104
- [TYPE_STATE]: boolean;
105
- [TYPE_SETTABLE_STATE]: boolean;
106
- __value: ValueHolder<T>;
107
- constructor(value: ValueHolder<T>);
108
- get(): T;
109
- set(action: T | ((value: T) => T)): void;
110
- watch(callback: (value: T) => void, options?: WatchOptions<T>): () => void;
111
- }
112
- /**
113
- * Creates a SettableState.
114
- */
115
- export declare function createSettableState<T>(initialValue: T, options?: CreateStateOptions<T>): SettableState<T>;
116
- /**
117
- * Creates a SettableState.
118
- */
119
- export declare function createSettableState<T>(initialValue?: T, options?: CreateStateOptions<T | undefined>): SettableState<T | undefined>;
120
- /**
121
- * Join a state and its setter into a single SettableState object.
122
- */
123
- export declare function toSettableState<I, O = I>($state: State<I>, setter: Setter<I, O>): SettableState<I, O>;
124
- /**
125
- * Creates a Setter with custom logic provided by `callback`.
126
- */
127
- export declare function createSetter<I, O = I>($state: State<I>, callback: (next: O, current: I) => void): Setter<I, O>;
111
+ export declare function createState<T>(value?: T, options?: CreateStateOptions<T | undefined>): [State<T | undefined>, Setter<T | undefined>];
128
112
  export interface DeriveOptions {
129
113
  equals?: (next: unknown, current: unknown) => boolean;
130
114
  }
131
- export declare function derive<Inputs extends MaybeState<any>[], T>(states: [...Inputs], fn: (...currentValues: StateValues<Inputs>) => T | State<T>, options?: DeriveOptions): State<T>;
132
115
  /**
133
- * A special kind of State exclusively for storing references to DOM nodes.
116
+ * Derives a new `State` from one or more existing states.
117
+ *
118
+ * @param sources - Array of source states to track.
119
+ * @param fn - A function called to recompute the value when any tracked source states receive a new value.
120
+ *
121
+ * @example
122
+ * // With one source...
123
+ * const [$count, setCount] = createState(5);
124
+ * const $doubled = derive([$count], count => count * 2);
125
+ * // ... or many:
126
+ * const [$greeting, setGreeting] = createState("Hello");
127
+ * const [$name, setName] = createState("World");
128
+ * const $hello = derive([$greeting, name], (greeting, name) => `${greeting}, ${name}!`);
129
+ */
130
+ export declare function derive<Sources extends MaybeState<any>[], T>(sources: [...Sources], fn: (...values: StateValues<Sources>) => T | State<T>, options?: DeriveOptions): State<T>;
131
+ /**
132
+ * A Ref is a function that returns the last argument it was called with.
133
+ * Calling it with no arguments will simply return the latest value.
134
+ * Calling it with an argument will store that value and immediately return it.
135
+ *
136
+ * @param value - An (optional) initial value to store.
137
+ *
138
+ * @example
139
+ * const ref = createRef(5);
140
+ * ref(); // 5
141
+ * ref(500);
142
+ * ref(); // 500
134
143
  */
135
- export declare function createRef<T extends Node>(): Ref<T>;
144
+ export declare function createRef<T>(value?: T): Ref<T>;
136
145
  export interface StateWatcher {
137
146
  /**
138
147
  * Watch one or more states, calling the provided `fn` each time one of their values changes.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export { createRef, createSettableState, createSetter, createState, derive, isRef, toSettableState, toState, valueOf, } from "./core/state.js";
2
- export type { MaybeState, Ref, SettableState, State, StopFunction } from "./core/state.js";
3
- export { strictEqual, shallowEqual, deepEqual } from "./utils.js";
1
+ export { createRef, createState, derive, isRef, isState, toState, toValue } from "./core/state.js";
2
+ export type { MaybeState, Ref, Setter, State, StopFunction } from "./core/state.js";
3
+ export { deepEqual, shallowEqual, strictEqual } from "./utils.js";
4
4
  export { cond, createMarkup, html, portal, repeat } from "./core/markup.js";
5
5
  export type { Markup, MarkupElement } from "./core/markup.js";
6
6
  import { Dolla } from "./core/dolla.js";
@@ -10,9 +10,9 @@ export declare const t: (selector: string, options?: import("./modules/i18n.js")
10
10
  export declare function setDevDebug(value: boolean): void;
11
11
  export declare function getDevDebug(): boolean;
12
12
  export type { Dolla, Environment, Logger, LoggerErrorContext, LoggerOptions, Loggles } from "./core/dolla.js";
13
+ export type { ViewContext, ViewElement, ViewFunction } from "./core/nodes/view.js";
13
14
  export type { HTTPRequest, HTTPResponse } from "./modules/http.js";
14
15
  export type { InputType, Renderable } from "./types.js";
15
- export type { ViewContext, ViewFunction, ViewElement as ViewNode } from "./core/nodes/view.js";
16
16
  export type { CrashViewProps } from "./views/default-crash-view.js";
17
17
  import type { IntrinsicElements as Elements } from "./types";
18
18
  declare global {