elbe-ui 2.0.15 → 2.0.17

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.
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { jsx as _jsx } from "react/jsx-runtime";
11
- import { useEffect, useState } from "react";
11
+ import { useEffect, useRef, useState } from "react";
12
12
  import { Column, ErrorView, Spinner, } from "..";
13
13
  import { _isFn } from "./_bit_types";
14
14
  function _LoadView({}) {
@@ -16,45 +16,60 @@ function _LoadView({}) {
16
16
  }
17
17
  export function _makeBitProvider(context, bitP) {
18
18
  function _BitProvider(p) {
19
- const [streamCancel, setStreamCancel] = useState(null);
19
+ const streamCancelRef = useRef(null);
20
+ const stateRef = useRef({
21
+ v: { type: "loading" },
22
+ history: [],
23
+ });
20
24
  const [state, setState] = useState({
21
25
  v: { type: "loading" },
22
26
  history: [],
23
27
  });
28
+ stateRef.current = state;
24
29
  // ========== DEFINE BASIC CTRLS ==========
25
30
  function _make() {
26
31
  const _partCtrl = {
27
32
  setData: (d) => {
28
- // if it's the same data, don't update
29
- if (state.v.type === "data" && state.v.value === d)
30
- return;
31
- setState({
32
- history: bitP.useHistory ? [...state.history, d] : [],
33
- v: { type: "data", value: d },
33
+ setState((prev) => {
34
+ // if it's the same data, don't update
35
+ if (prev.v.type === "data" && prev.v.value === d)
36
+ return prev;
37
+ return {
38
+ history: bitP.useHistory ? [...prev.history, d] : [],
39
+ v: { type: "data", value: d },
40
+ };
34
41
  });
35
42
  },
36
- setLoading: () => setState(Object.assign(Object.assign({}, state), { v: { type: "loading" } })),
37
- setError: (e) => setState(Object.assign(Object.assign({}, state), { v: { type: "error", value: e } })),
38
- canGoBack: !!(bitP.useHistory && state.history.length > 1),
43
+ setLoading: () => setState((prev) => (Object.assign(Object.assign({}, prev), { v: { type: "loading" } }))),
44
+ setError: (e) => setState((prev) => (Object.assign(Object.assign({}, prev), { v: { type: "error", value: e } }))),
45
+ canGoBack: !!(bitP.useHistory && stateRef.current.history.length > 1),
39
46
  back: () => {
40
47
  if (!bitP.useHistory)
41
48
  return false;
42
- if (state.history.length < 2)
43
- return false;
44
- const newHistory = state.history.slice(0, -1);
45
- const newData = newHistory[newHistory.length - 1];
46
- setState({
47
- history: newHistory,
48
- v: { type: "data", value: newData },
49
+ let didGoBack = false;
50
+ setState((prev) => {
51
+ if (prev.history.length < 2)
52
+ return prev;
53
+ didGoBack = true;
54
+ const newHistory = prev.history.slice(0, -1);
55
+ const newData = newHistory[newHistory.length - 1];
56
+ return {
57
+ history: newHistory,
58
+ v: { type: "data", value: newData },
59
+ };
49
60
  });
50
- return true;
61
+ return didGoBack;
51
62
  },
52
- state: state.v.type,
53
- isData: state.v.type === "data",
54
- isLoading: state.v.type === "loading",
55
- isError: state.v.type === "error",
56
- data: state.v.type === "data" ? state.v.value : undefined,
57
- error: state.v.type === "error" ? state.v.value : undefined,
63
+ state: stateRef.current.v.type,
64
+ isData: stateRef.current.v.type === "data",
65
+ isLoading: stateRef.current.v.type === "loading",
66
+ isError: stateRef.current.v.type === "error",
67
+ data: stateRef.current.v.type === "data"
68
+ ? stateRef.current.v.value
69
+ : undefined,
70
+ error: stateRef.current.v.type === "error"
71
+ ? stateRef.current.v.value
72
+ : undefined,
58
73
  };
59
74
  // ========== DEFINE QoL FUNCTIONS ==========
60
75
  function _worker(fn, silent, reset) {
@@ -81,10 +96,14 @@ export function _makeBitProvider(context, bitP) {
81
96
  try {
82
97
  if (!silent)
83
98
  _partCtrl.setLoading();
84
- if (streamCancel)
85
- streamCancel();
99
+ const prevCancel = streamCancelRef.current;
100
+ streamCancelRef.current = null;
101
+ if (prevCancel)
102
+ prevCancel();
86
103
  const cancel = _bit.stream(p, _partCtrl);
87
- setStreamCancel(() => cancel());
104
+ streamCancelRef.current = () => {
105
+ cancel();
106
+ };
88
107
  return;
89
108
  }
90
109
  catch (e) {
@@ -96,7 +115,9 @@ export function _makeBitProvider(context, bitP) {
96
115
  };
97
116
  function act(fn, silent) {
98
117
  return __awaiter(this, void 0, void 0, function* () {
99
- const data = _partCtrl.data;
118
+ const data = stateRef.current.v.type === "data"
119
+ ? stateRef.current.v.value
120
+ : undefined;
100
121
  if (data === undefined)
101
122
  return;
102
123
  _worker(() => fn(data), silent);
@@ -104,14 +125,15 @@ export function _makeBitProvider(context, bitP) {
104
125
  }
105
126
  function map(onData, onError, onLoading) {
106
127
  var _a, _b, _c;
107
- if (state.v.type === "data") {
128
+ const currentState = stateRef.current.v;
129
+ if (currentState.type === "data") {
108
130
  return _isFn(onData)
109
- ? onData(state.v.value)
131
+ ? onData(currentState.value)
110
132
  : ((_a = onData) !== null && _a !== void 0 ? _a : null);
111
133
  }
112
- if (state.v.type === "error") {
134
+ if (currentState.type === "error") {
113
135
  return _isFn(onError)
114
- ? onError(state.v.value)
136
+ ? onError(currentState.value)
115
137
  : ((_b = onError) !== null && _b !== void 0 ? _b : null);
116
138
  }
117
139
  return _isFn(onLoading) ? onLoading() : ((_c = onLoading) !== null && _c !== void 0 ? _c : null);
@@ -126,14 +148,51 @@ export function _makeBitProvider(context, bitP) {
126
148
  return children();
127
149
  }
128
150
  const userCtrl = bitP.control(Object.assign(Object.assign({}, baseCtrl), { parameters: p, reload: _reload, consider: consider }));
129
- return Object.assign(Object.assign(Object.assign({}, baseCtrl), userCtrl), { reload: _reload, parameters: p, consider: consider });
151
+ const ctrl = Object.assign(Object.assign(Object.assign({}, baseCtrl), userCtrl), { reload: _reload, parameters: p, consider: consider });
152
+ Object.defineProperties(ctrl, {
153
+ state: {
154
+ enumerable: true,
155
+ get: () => stateRef.current.v.type,
156
+ },
157
+ isData: {
158
+ enumerable: true,
159
+ get: () => stateRef.current.v.type === "data",
160
+ },
161
+ isLoading: {
162
+ enumerable: true,
163
+ get: () => stateRef.current.v.type === "loading",
164
+ },
165
+ isError: {
166
+ enumerable: true,
167
+ get: () => stateRef.current.v.type === "error",
168
+ },
169
+ data: {
170
+ enumerable: true,
171
+ get: () => stateRef.current.v.type === "data"
172
+ ? stateRef.current.v.value
173
+ : undefined,
174
+ },
175
+ error: {
176
+ enumerable: true,
177
+ get: () => stateRef.current.v.type === "error"
178
+ ? stateRef.current.v.value
179
+ : undefined,
180
+ },
181
+ canGoBack: {
182
+ enumerable: true,
183
+ get: () => !!(bitP.useHistory && stateRef.current.history.length > 1),
184
+ },
185
+ });
186
+ return ctrl;
130
187
  }
131
188
  const ctrl = _make();
132
189
  useEffect(() => {
133
190
  ctrl.reload(true);
134
191
  return () => {
135
- if (streamCancel)
136
- streamCancel();
192
+ const cancel = streamCancelRef.current;
193
+ streamCancelRef.current = null;
194
+ if (cancel)
195
+ cancel();
137
196
  // call dispose if exists
138
197
  const d = ctrl.dispose;
139
198
  if (typeof d !== "function")
@@ -147,7 +206,7 @@ export function _makeBitProvider(context, bitP) {
147
206
  };
148
207
  }, []);
149
208
  // ========== DEFINE THE JSX ELEMENT ==========
150
- return (_jsx(context.Provider, { value: Object.assign({}, ctrl), children: p.children }));
209
+ return _jsx(context.Provider, { value: ctrl, children: p.children });
151
210
  }
152
211
  return _BitProvider;
153
212
  }
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import * as Lucide from "lucide-react";
3
+ //@ts-ignore
3
4
  import "./elbe.css";
4
5
  export * as Wouter from "wouter";
5
6
  export * as wouter_hash from "wouter/use-hash-location";
@@ -1,4 +1,4 @@
1
- import { ColorSelection, ElbeChildren } from "../../..";
1
+ import { ColorSelection, Dict, ElbeChildren } from "../../..";
2
2
  declare const _dialogs: {
3
3
  showConfirmDialog: DialogsConfig<{
4
4
  message: string;
@@ -44,7 +44,6 @@ export function WithTooltip(p) {
44
44
  var _a;
45
45
  timeoutRef.current = window.setTimeout(() => setVisible(true), (_a = p.delay) !== null && _a !== void 0 ? _a : 1000);
46
46
  }, onMouseLeave: () => {
47
- console.log("leave");
48
47
  if (timeoutRef.current) {
49
48
  clearTimeout(timeoutRef.current);
50
49
  timeoutRef.current = null;
@@ -1,3 +1,4 @@
1
+ import { Dict } from "../../util/util";
1
2
  import { ElbeSubThemeData } from "../theme";
2
3
  export type TypeStyle<T extends Dict<any> = {}> = T & {
3
4
  bold?: boolean;
@@ -1,3 +1,4 @@
1
+ import { Dict } from "../../../../..";
1
2
  export type ElbeColor<T extends string, Data extends Dict<any>, Computed extends Dict<any>> = {
2
3
  type: [T, ...string[]];
3
4
  } & Data & Computed;
@@ -1,4 +1,4 @@
1
- import "../../elbe.css";
1
+ import { Dict } from "../..";
2
2
  export type ElbeSubThemeData<Computed extends Dict<any>, Seed extends Dict<any> = Computed, Config extends Dict<any> = Computed> = {
3
3
  seed: Seed;
4
4
  _configType: Config;
@@ -1,4 +1,5 @@
1
- import "../../elbe.css";
1
+ //@ts-ignore
2
+ //import "./elbe.css";
2
3
  import { geometryThemeData } from "./subthemes/_theme_geometry";
3
4
  import { menuThemeData } from "./subthemes/_theme_menu";
4
5
  import { motionThemeData } from "./subthemes/_theme_motion";
@@ -1 +1,2 @@
1
+ import { Dict } from "../..";
1
2
  export declare function deepMerge<T extends Dict<any>>(original: T, toMerge: Partial<T>, depth?: number): T;
@@ -1,6 +1,9 @@
1
1
  import { int } from "../..";
2
2
  export type Maybe<T> = T | null | undefined;
3
3
  export type PromiseOr<T> = Promise<T> | T;
4
+ export type Dict<T> = {
5
+ [key: string]: T;
6
+ };
4
7
  export declare function clamp(value: number, min: number, max: number): number;
5
8
  export declare function classString(classes: (string | false | null | undefined)[]): string;
6
9
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elbe-ui",
3
- "version": "2.0.15",
3
+ "version": "2.0.17",
4
4
  "author": "Robin Naumann",
5
5
  "license": "MIT",
6
6
  "repository": {