@manyducks.co/dolla 2.0.0-alpha.34 → 2.0.0-alpha.36

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.
Files changed (52) hide show
  1. package/README.md +9 -22
  2. package/dist/core/{batch.d.ts → _batch.d.ts} +4 -0
  3. package/dist/core/_signals_new.d.ts +130 -0
  4. package/dist/core/context.d.ts +7 -55
  5. package/dist/core/dolla.d.ts +7 -48
  6. package/dist/core/markup.d.ts +9 -31
  7. package/dist/core/nodes/{observer.d.ts → dynamic.d.ts} +10 -12
  8. package/dist/core/nodes/html.d.ts +11 -17
  9. package/dist/core/nodes/list.d.ts +28 -0
  10. package/dist/core/nodes/outlet.d.ts +7 -7
  11. package/dist/core/nodes/portal.d.ts +2 -2
  12. package/dist/core/nodes/view.d.ts +21 -34
  13. package/dist/core/ref.d.ts +5 -6
  14. package/dist/core/signals.d.ts +128 -0
  15. package/dist/core/store.d.ts +14 -18
  16. package/dist/core/symbols.d.ts +0 -3
  17. package/dist/{views → core/views}/default-crash-view.d.ts +1 -1
  18. package/dist/{views → core/views}/passthrough.d.ts +2 -2
  19. package/dist/{modules/http.d.ts → http/index.d.ts} +1 -2
  20. package/dist/index.d.ts +8 -11
  21. package/dist/index.js +517 -695
  22. package/dist/index.js.map +1 -1
  23. package/dist/jsx-dev-runtime.d.ts +1 -1
  24. package/dist/jsx-dev-runtime.js +2 -2
  25. package/dist/jsx-dev-runtime.js.map +1 -1
  26. package/dist/jsx-runtime.d.ts +1 -1
  27. package/dist/jsx-runtime.js +2 -2
  28. package/dist/jsx-runtime.js.map +1 -1
  29. package/dist/markup-ILMFXzoo.js +1442 -0
  30. package/dist/markup-ILMFXzoo.js.map +1 -0
  31. package/dist/{modules/router.d.ts → router/index.d.ts} +5 -5
  32. package/dist/router/router.utils.test.d.ts +1 -0
  33. package/dist/{modules/i18n.d.ts → translate/index.d.ts} +20 -16
  34. package/dist/typeChecking.d.ts +0 -96
  35. package/dist/types.d.ts +9 -9
  36. package/dist/utils.d.ts +4 -1
  37. package/docs/signals.md +149 -0
  38. package/docs/views.md +1 -87
  39. package/notes/atomic.md +146 -0
  40. package/notes/context-routes.md +56 -0
  41. package/notes/elimination.md +33 -0
  42. package/package.json +9 -7
  43. package/vite.config.js +3 -0
  44. package/dist/core/nodes/repeat.d.ts +0 -36
  45. package/dist/core/state.d.ts +0 -126
  46. package/dist/core/stats.d.ts +0 -31
  47. package/dist/markup-B3FV_fq9.js +0 -1525
  48. package/dist/markup-B3FV_fq9.js.map +0 -1
  49. package/notes/viewstate.md +0 -15
  50. package/tests/state.test.js +0 -135
  51. /package/dist/{modules/router.utils.test.d.ts → core/signals.test.d.ts} +0 -0
  52. /package/dist/{modules → router}/router.utils.d.ts +0 -0
@@ -1,13 +1,12 @@
1
- import { Emitter } from "@manyducks.co/emitter";
2
- import { type ElementContext, type StorableContext, type WildcardListenerMap } from "../context.js";
1
+ import type { ComponentContext, ElementContext, StoreConsumerContext, StoreProviderContext } from "../context.js";
3
2
  import type { Logger } from "../dolla.js";
4
3
  import { type Markup, type MarkupElement } from "../markup.js";
5
- import { type MaybeState, type State, type StateValues, type StopFunction } from "../state.js";
4
+ import { type EffectCallback, type Reactive, type UnsubscribeFunction } from "../signals.js";
6
5
  import { IS_MARKUP_ELEMENT } from "../symbols.js";
7
6
  /**
8
7
  * Any valid value that a View can return.
9
8
  */
10
- export type ViewResult = Node | State<any> | Markup | Markup[] | null;
9
+ export type ViewResult = Node | Reactive<any> | Markup | Markup[] | null;
11
10
  export type ViewFunction<P> = (this: ViewContext, props: P, context: ViewContext) => ViewResult;
12
11
  /**
13
12
  * A view that has been constructed into DOM nodes.
@@ -18,18 +17,11 @@ export interface ViewElement extends MarkupElement {
18
17
  */
19
18
  setChildView(view: ViewFunction<{}>): ViewElement;
20
19
  }
21
- export interface ViewContext extends Logger, StorableContext {
20
+ export interface ViewContext extends Omit<Logger, "setName">, ComponentContext, StoreProviderContext, StoreConsumerContext {
22
21
  /**
23
22
  * An ID unique to this view.
24
23
  */
25
24
  readonly uid: string;
26
- /**
27
- * Returns an object of all variables stored on this context.
28
- */
29
- /**
30
- * Sets the name of the view's built in logger.
31
- */
32
- setName(name: string): ViewContext;
33
25
  /**
34
26
  * True while this view is connected to the DOM.
35
27
  */
@@ -51,36 +43,32 @@ export interface ViewContext extends Logger, StorableContext {
51
43
  */
52
44
  onUnmount(callback: () => void): void;
53
45
  /**
54
- * Watch a set of states. The callback is called when any of the states receive a new value.
55
- * Watchers will be automatically stopped when this view is unmounted.
46
+ * Passes a getter function to `callback` that will track reactive states and return their current values.
47
+ * Callback will be run each time a tracked state gets a new value.
56
48
  */
57
- watch<T extends MaybeState<any>[]>(states: [...T], callback: (...values: StateValues<T>) => void): StopFunction;
49
+ effect(callback: EffectCallback): UnsubscribeFunction;
58
50
  /**
59
51
  * Returns a Markup element that displays this view's children.
60
52
  */
61
53
  outlet(): Markup;
62
54
  }
63
- type ViewEvents = {
64
- beforeMount: [];
65
- mounted: [];
66
- beforeUnmount: [];
67
- unmounted: [];
68
- };
69
55
  export declare class View<P> implements ViewElement {
70
56
  [IS_MARKUP_ELEMENT]: boolean;
71
57
  uniqueId: string;
72
- _elementContext: ElementContext;
73
- _logger: Logger;
74
- _view: ViewFunction<P>;
75
- _props: P;
76
- _element?: MarkupElement;
77
- _childMarkup: Markup[];
78
- _$children: State<MarkupElement[]>;
79
- _setChildren: import("../state.js").Setter<MarkupElement[], MarkupElement[]>;
80
- _watcher: import("../state.js").StateWatcher;
81
- _emitter: Emitter<ViewEvents>;
82
- _wildcardListeners: WildcardListenerMap;
83
- constructor(elementContext: ElementContext, view: ViewFunction<P>, props: P, children?: Markup[]);
58
+ elementContext: ElementContext;
59
+ logger: Logger;
60
+ props: P;
61
+ fn: ViewFunction<P>;
62
+ element?: MarkupElement;
63
+ childMarkup: Markup[];
64
+ children: import("../signals.js").Atom<MarkupElement[]>;
65
+ lifecycleListeners: {
66
+ beforeMount: (() => any)[];
67
+ mount: (() => any)[];
68
+ beforeUnmount: (() => any)[];
69
+ unmount: (() => any)[];
70
+ };
71
+ constructor(elementContext: ElementContext, fn: ViewFunction<P>, props: P, children?: Markup[]);
84
72
  get node(): Node;
85
73
  isMounted: boolean;
86
74
  mount(parent: Node, after?: Node): void;
@@ -88,4 +76,3 @@ export declare class View<P> implements ViewElement {
88
76
  setChildView(fn: ViewFunction<{}>): View<{}>;
89
77
  private _initialize;
90
78
  }
91
- export {};
@@ -20,10 +20,9 @@ export interface Ref<T> {
20
20
  * @param value - An (optional) initial value to store.
21
21
  *
22
22
  * @example
23
- * const ref = createRef(5);
24
- * ref(); // 5
25
- * ref(500);
26
- * ref(); // 500
23
+ * const number = ref(5);
24
+ * number(); // 5
25
+ * number(500);
26
+ * number(); // 500
27
27
  */
28
- export declare function createRef<T>(value?: T): Ref<T>;
29
- export declare function isRef<T extends Node>(value: any): value is Ref<T>;
28
+ export declare function ref<T>(value?: T): Ref<T>;
@@ -0,0 +1,128 @@
1
+ import { type Dependency, type Subscriber } from "alien-signals";
2
+ export interface Effect extends Subscriber, Dependency {
3
+ fn(): void;
4
+ }
5
+ export interface Computed<T = any> extends Signal<T | undefined>, Subscriber {
6
+ getter: (cachedValue?: T) => T;
7
+ equals: EqualityFunction<T>;
8
+ }
9
+ export interface Signal<T = any> extends Dependency {
10
+ currentValue: T;
11
+ }
12
+ /**
13
+ * A readable reactive state object.
14
+ */
15
+ export interface Reactive<T> {
16
+ /**
17
+ * The current value.
18
+ */
19
+ readonly value: T;
20
+ }
21
+ export type MaybeReactive<T> = Reactive<T> | T;
22
+ export type UnsubscribeFunction = () => void;
23
+ /**
24
+ * A function to compare the current and next values. Returning `true` means the value has changed.
25
+ */
26
+ export type EqualityFunction<T> = (current: T, next: T) => boolean;
27
+ export interface ReactiveOptions<T> {
28
+ /**
29
+ * A label for debugging purposes.
30
+ */
31
+ name?: string;
32
+ /**
33
+ * A function to compare the current and next values. Returning `true` means the value has changed.
34
+ */
35
+ equals?: EqualityFunction<T>;
36
+ }
37
+ export declare class Atom<T> implements Reactive<T> {
38
+ #private;
39
+ /**
40
+ * A label for debugging purposes.
41
+ */
42
+ name?: string;
43
+ constructor(signal: Signal<T>, options?: ReactiveOptions<T>);
44
+ get value(): T;
45
+ set value(next: T);
46
+ }
47
+ /**
48
+ * Determines if a value is reactive.
49
+ */
50
+ export declare function isReactive<T>(value: any): value is Reactive<T>;
51
+ /**
52
+ * Creates a simple reactive container that stores a value.
53
+ * Atom values can be read and updated with the `value` property
54
+ *
55
+ * @example
56
+ * const count = atom(1);
57
+ * count.value++;
58
+ * count.value; // 2
59
+ */
60
+ export declare function atom<T>(): Atom<T | undefined>;
61
+ /**
62
+ * Creates a simple reactive container that stores a value.
63
+ * Atom values can be read and updated with the `value` property.
64
+ *
65
+ * @example
66
+ * const count = atom(1);
67
+ * count.value++;
68
+ * count.value; // 2
69
+ */
70
+ export declare function atom<T>(value: T, options?: ReactiveOptions<T>): Atom<T>;
71
+ /**
72
+ * Creates a simple reactive container that stores a value.
73
+ * Atom values can be read and updated with the `value` property.
74
+ *
75
+ * @example
76
+ * const count = atom(1);
77
+ * count.value++;
78
+ * count.value; // 2
79
+ */
80
+ export declare function atom<T>(value?: T, options?: ReactiveOptions<T>): Atom<T | undefined>;
81
+ type ComposeCallback<T> = (previousValue?: T) => MaybeReactive<T>;
82
+ /**
83
+ * Creates a reactive container that derives its value from other reactive values that it tracks.
84
+ *
85
+ * @example
86
+ * const count = atom(1);
87
+ * const doubled = compose(() => get(count) * 2);
88
+ */
89
+ export declare function compose<T>(fn: ComposeCallback<T>, options?: ReactiveOptions<T>): Reactive<T>;
90
+ /**
91
+ * Takes a new value to set, or a callback that receives the current value and returns a new value to set.
92
+ */
93
+ type Setter<T> = (next: T | ((current: T) => T)) => void;
94
+ export declare function set<T>(atom: Atom<T>): Setter<T>;
95
+ export declare function set<T>(atom: Atom<T>, next: T | ((current: T) => T)): void;
96
+ /**
97
+ * Returns the current value from a reactive _and track it_ if called in a `compose` or `effect` scope.
98
+ * If a non-reactive value is passed it will just be returned untracked.
99
+ *
100
+ * @example
101
+ * const count = atom(1);
102
+ * const value = get(count); // 1
103
+ *
104
+ * const value = get(5); // 5
105
+ */
106
+ export declare function get<T>(value: MaybeReactive<T>): T;
107
+ /**
108
+ * Returns the current value from a reactive (without tracking).
109
+ * If a non-reactive value is passed it will be returned.
110
+ *
111
+ * @example
112
+ * ctx.effect(() => {
113
+ * const doubled = get(count) * 2; // `count` will be tracked
114
+ *
115
+ * const doubled = peek(count) * 2; // `count` will NOT be tracked
116
+ * });
117
+ */
118
+ export declare function peek<T>(value: MaybeReactive<T>): T;
119
+ export type EffectCallback = () => void;
120
+ /**
121
+ * Creates a tracked scope that re-runs whenever the values of any tracked reactives changes.
122
+ * Reactives are tracked by accessing their `value` within the body of the function.
123
+ *
124
+ * NOTE: You must call the unsubscribe function to stop watching for changes.
125
+ * If you are using an effect inside a View or Store, use `ctx.effect` instead, which cleans up automatically when the component unmounts.
126
+ */
127
+ export declare function effect(fn: EffectCallback): UnsubscribeFunction;
128
+ export {};
@@ -1,10 +1,9 @@
1
- import { Emitter } from "@manyducks.co/emitter";
2
- import { type ComponentContext, type ElementContext, type WildcardListenerMap } from "./context.js";
1
+ import type { StoreConsumerContext, ComponentContext, ElementContext } from "./context.js";
3
2
  import type { Logger } from "./dolla.js";
4
- import { type MaybeState, type StateValues, type StopFunction } from "./state.js";
3
+ import { EffectCallback, UnsubscribeFunction } from "./signals.js";
5
4
  export type StoreFunction<Options, Value> = (this: StoreContext, options: Options, context: StoreContext) => Value;
6
5
  export type StoreFactory<Options, Value> = Options extends undefined ? () => Store<Options, Value> : (options: Options) => Store<Options, Value>;
7
- export interface StoreContext extends Logger, ComponentContext {
6
+ export interface StoreContext extends Omit<Logger, "setName">, ComponentContext, StoreConsumerContext {
8
7
  /**
9
8
  * True while this store is attached to a context that is currently mounted in the view tree.
10
9
  */
@@ -18,15 +17,11 @@ export interface StoreContext extends Logger, ComponentContext {
18
17
  */
19
18
  onUnmount(callback: () => void): void;
20
19
  /**
21
- * Watch a set of states. The callback is called when any of the states receive a new value.
22
- * Watchers will be automatically stopped when this store is unmounted.
20
+ * Passes a getter function to `callback` that will track reactive states and return their current values.
21
+ * Callback will be run each time a tracked state gets a new value.
23
22
  */
24
- watch<T extends MaybeState<any>[]>(states: [...T], callback: (...values: StateValues<T>) => void): StopFunction;
23
+ effect(callback: EffectCallback): UnsubscribeFunction;
25
24
  }
26
- type StoreEvents = {
27
- mounted: [];
28
- unmounted: [];
29
- };
30
25
  export declare class Store<Options, Value> {
31
26
  readonly fn: StoreFunction<Options, Value>;
32
27
  private _options;
@@ -35,12 +30,14 @@ export declare class Store<Options, Value> {
35
30
  */
36
31
  value: Value;
37
32
  isMounted: boolean;
38
- _elementContext: ElementContext;
39
- _emitter: Emitter<StoreEvents>;
40
- _wildcardListeners: WildcardListenerMap;
41
- _logger: Logger;
42
- _watcher: import("./state.js").StateWatcher;
43
- get name(): string;
33
+ elementContext: ElementContext;
34
+ lifecycleListeners: {
35
+ mount: (() => any)[];
36
+ unmount: (() => any)[];
37
+ };
38
+ logger: Logger;
39
+ id: string;
40
+ name: string;
44
41
  constructor(fn: StoreFunction<Options, Value>, options: Options);
45
42
  /**
46
43
  * Attaches this Store to the elementContext.
@@ -53,4 +50,3 @@ export declare class Store<Options, Value> {
53
50
  export declare function isStore<Options, Value>(value: any): value is Store<Options, Value>;
54
51
  export declare class StoreError extends Error {
55
52
  }
56
- export {};
@@ -1,7 +1,4 @@
1
- export declare const IS_STATE: unique symbol;
2
- export declare const IS_REF: unique symbol;
3
1
  export declare const IS_MARKUP: unique symbol;
4
2
  export declare const IS_MARKUP_ELEMENT: unique symbol;
5
3
  export declare const IS_STORE: unique symbol;
6
- export declare const IS_STORE_FACTORY: unique symbol;
7
4
  export declare const IS_ROUTER: unique symbol;
@@ -15,4 +15,4 @@ export type CrashViewProps = {
15
15
  */
16
16
  uid?: string;
17
17
  };
18
- export declare function DefaultCrashView(props: CrashViewProps): import("../core/markup.js").Markup | import("../core/markup.js").Markup[];
18
+ export declare function DefaultCrashView(props: CrashViewProps): import("../markup.js").Markup | import("../markup.js").Markup[];
@@ -1,5 +1,5 @@
1
- import { type ViewContext } from "../core/nodes/view.js";
1
+ import { type ViewContext } from "../nodes/view.js";
2
2
  /**
3
3
  * A utility view that simply displays its children.
4
4
  */
5
- export declare function Passthrough(_: {}, ctx: ViewContext): import("../index.js").Markup;
5
+ export declare function Passthrough(_: {}, ctx: ViewContext): import("../markup.js").Markup;
@@ -1,11 +1,10 @@
1
- import type { Dolla } from "../core/dolla.js";
2
1
  /**
3
2
  * A simple HTTP client with middleware support. Middleware applies to all requests made through this store,
4
3
  * so it's the perfect way to handle things like auth headers and permission checks for API calls.
5
4
  */
6
5
  export declare class HTTP {
7
6
  #private;
8
- constructor(dolla: Dolla);
7
+ constructor();
9
8
  /**
10
9
  * Adds a new middleware that will apply to subsequent requests.
11
10
  * Returns a function to remove this middleware.
package/dist/index.d.ts CHANGED
@@ -1,22 +1,19 @@
1
- export { createState, derive, isState, toState, toValue } from "./core/state.js";
2
- export type { MaybeState, Setter, State, StopFunction } from "./core/state.js";
3
- export { createRef, isRef, type Ref } from "./core/ref.js";
4
- export { type StoreFunction, type StoreContext } from "./core/store.js";
5
- export { createRouter, type Router, type RouterOptions } from "./modules/router.js";
1
+ export { atom, compose, effect, get, set, peek } from "./core/signals.js";
2
+ export type { Reactive, MaybeReactive, Atom } from "./core/signals.js";
6
3
  export { deepEqual, shallowEqual, strictEqual } from "./utils.js";
7
- export { cond, createMarkup, html, portal, repeat } from "./core/markup.js";
4
+ export { ref, type Ref } from "./core/ref.js";
5
+ export { type StoreFunction, type StoreContext } from "./core/store.js";
6
+ export { createRouter, type Router, type RouterOptions } from "./router/index.js";
7
+ export { cond, createMarkup, html, portal, list } from "./core/markup.js";
8
8
  export type { Markup, MarkupElement } from "./core/markup.js";
9
9
  import { Dolla } from "./core/dolla.js";
10
10
  declare const dolla: Dolla;
11
11
  export default dolla;
12
- export declare const t: (selector: string, options?: import("./modules/i18n.js").TOptions) => import("./core/state.js").State<string>;
13
- export declare function setDevDebug(value: boolean): void;
14
- export declare function getDevDebug(): boolean;
12
+ export declare const t: (selector: string, options?: import("./translate/index.js").TOptions) => import("./core/signals.js").Reactive<string>;
15
13
  export type { Dolla, Environment, Logger, LoggerErrorContext, LoggerOptions, Loggles } from "./core/dolla.js";
16
14
  export type { ViewContext, ViewElement, ViewFunction } from "./core/nodes/view.js";
17
- export type { HTTPRequest, HTTPResponse } from "./modules/http.js";
18
15
  export type { InputType, Renderable } from "./types.js";
19
- export type { CrashViewProps } from "./views/default-crash-view.js";
16
+ export type { CrashViewProps } from "./core/views/default-crash-view.js";
20
17
  import type { IntrinsicElements as Elements } from "./types";
21
18
  declare global {
22
19
  namespace JSX {