@rotorjs/dashboard 0.3.1 → 0.4.0

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.
@@ -14,6 +14,9 @@ export type NavigateDashboardAction = {
14
14
  href: string;
15
15
  replace?: boolean;
16
16
  };
17
+ export type SyncDashboardAction = {
18
+ type: 'sync';
19
+ };
17
20
  export type DashboardAction = {
18
21
  type: string;
19
22
  [key: string]: unknown;
@@ -10,12 +10,17 @@ import { DashboardVar } from './DashboardVar';
10
10
  export type DashboardStateReducerMap<Engine extends DashboardEngine = DashboardEngine> = {
11
11
  [type: string]: DashboardStateReducerConfig<Engine>;
12
12
  };
13
- export declare class DashboardEngine extends StateEngine<DashboardStateDescriptor, DashboardState, DashboardAction> implements DashboardEventTarget {
13
+ export type DashboardEngineInit = {
14
+ vars?: {
15
+ [name: string]: DashboardVar;
16
+ };
17
+ facts?: {
18
+ [name: string]: DashboardFact;
19
+ };
20
+ };
21
+ export declare class DashboardEngine extends StateEngine<DashboardStateDescriptor, DashboardState, DashboardAction, DashboardEventTarget> {
14
22
  #private;
15
- constructor(reducerInit: DashboardStateReducerMap);
16
- protected onAction(action: DashboardAction): void;
17
- dispatchVar(name: string, value: unknown, exposed?: boolean): void;
18
- dispatchFact(name: string, value: unknown): void;
23
+ constructor(target: DashboardEventTarget, reducerInit: DashboardStateReducerMap, init?: DashboardEngineInit);
19
24
  hasVar(name: string): boolean;
20
25
  getVar(name: string): DashboardVar | undefined;
21
26
  hasFact(name: string): boolean;
@@ -0,0 +1,28 @@
1
+ import { DashboardEventTarget } from './DashboardEventTarget';
2
+ import { DashboardFact } from './DashboardFact';
3
+ import { DashboardVar } from './DashboardVar';
4
+ export type DashboardEnvironmentInit = {
5
+ vars?: {
6
+ [name: string]: DashboardVar;
7
+ };
8
+ facts?: {
9
+ [name: string]: DashboardFact;
10
+ };
11
+ onVar?: (name: string, value: DashboardVar | undefined) => void;
12
+ onFact?: (name: string, value: DashboardFact | undefined) => void;
13
+ signal?: AbortSignal;
14
+ };
15
+ export declare class DashboardEnvironment {
16
+ #private;
17
+ constructor(target: DashboardEventTarget, init?: DashboardEnvironmentInit);
18
+ get target(): DashboardEventTarget;
19
+ get id(): string;
20
+ get signal(): AbortSignal;
21
+ hasVar(name: string): boolean;
22
+ getVar(name: string): DashboardVar | undefined;
23
+ getVars(): [name: string, value: DashboardVar][];
24
+ hasFact(name: string): boolean;
25
+ getFact(name: string): DashboardFact | undefined;
26
+ getFacts(): [name: string, value: DashboardFact][];
27
+ stop(): void;
28
+ }
@@ -5,4 +5,5 @@ import { DashboardStateDescriptor } from './DashboardStateDescriptor';
5
5
  export declare class DashboardEventTarget extends StateEventTarget<DashboardStateDescriptor, DashboardState, DashboardAction> {
6
6
  dispatchVar(name: string, value: unknown, exposed?: boolean): void;
7
7
  dispatchFact(name: string, value: unknown): void;
8
+ sync(): void;
8
9
  }
@@ -1,7 +1,8 @@
1
- import { StateConsumer } from '@rotorjs/state';
1
+ import { StateConsumer, StateCallback, StateEventTarget } from '@rotorjs/state';
2
2
  import { DashboardAction } from './DashboardAction';
3
3
  import { DashboardState } from './DashboardState';
4
4
  import { DashboardStateDescriptor } from './DashboardStateDescriptor';
5
5
  export declare class DashboardStateConsumer extends StateConsumer<DashboardStateDescriptor, DashboardState, DashboardAction> {
6
+ constructor(target: StateEventTarget<DashboardStateDescriptor, DashboardState, DashboardAction>, descriptor: DashboardStateDescriptor, callback: StateCallback<DashboardState>);
6
7
  protected compareStates(nextState: DashboardState, prevState: DashboardState): boolean;
7
8
  }
package/dist/main.js CHANGED
@@ -1,63 +1,121 @@
1
- import { StateConsumer as e, StateEngine as t, StateEventTarget as n, StateReducer as r } from "@rotorjs/state";
2
- import i from "fast-deep-equal";
3
- import { v7 as a } from "uuid";
1
+ import { ActionEvent as e, StateConsumer as t, StateEngine as n, StateEventTarget as r, StateReducer as i } from "@rotorjs/state";
2
+ import a from "fast-deep-equal";
3
+ import { v7 as o } from "uuid";
4
+ //#region lib/DashboardEnvironment.ts
5
+ var s = class {
6
+ #e;
7
+ #t = o();
8
+ #n;
9
+ #r;
10
+ #i;
11
+ #a = new AbortController();
12
+ constructor(t, n) {
13
+ this.#e = t, this.#n = Object.fromEntries(Object.entries(n?.vars ?? {}).map(([e, t]) => [e, Object.freeze(t)])), this.#r = Object.fromEntries(Object.entries(n?.facts ?? {}).map(([e, t]) => [e, Object.freeze(t)])), this.#i = n?.signal;
14
+ let r = this.signal;
15
+ this.target.addEventListener("action", (t) => {
16
+ if (t.emitter === this.#t) return;
17
+ let r = t.action;
18
+ switch (r.type) {
19
+ case "var": {
20
+ let e = this.#n[r.name], t = Object.freeze({
21
+ value: r.value,
22
+ exposed: r.exposed ?? !1
23
+ });
24
+ a(e, t) || (this.#n[r.name] = t, n?.onVar?.(r.name, t));
25
+ return;
26
+ }
27
+ case "fact": {
28
+ let e = this.#r[r.name], t = Object.freeze({ value: r.value });
29
+ a(e, t) || (this.#r[r.name] = t, n?.onFact?.(r.name, t));
30
+ return;
31
+ }
32
+ case "sync": Object.entries(this.#n).forEach(([t, { value: n, exposed: r }]) => {
33
+ let i = new e({
34
+ type: "var",
35
+ name: t,
36
+ value: n,
37
+ exposed: r
38
+ });
39
+ i.emitter = this.#t, this.target.dispatchEvent(i);
40
+ }), Object.entries(this.#r).forEach(([t, { value: n }]) => {
41
+ let r = new e({
42
+ type: "fact",
43
+ name: t,
44
+ value: n
45
+ });
46
+ r.emitter = this.#t, this.target.dispatchEvent(r);
47
+ });
48
+ }
49
+ }, { signal: r }), this.target.sync();
50
+ }
51
+ get target() {
52
+ return this.#e;
53
+ }
54
+ get id() {
55
+ return this.#t;
56
+ }
57
+ get signal() {
58
+ return this.#i ? AbortSignal.any([this.#a.signal, this.#i]) : this.#a.signal;
59
+ }
60
+ hasVar(e) {
61
+ return Object.hasOwn(this.#n, e);
62
+ }
63
+ getVar(e) {
64
+ return this.#n[e];
65
+ }
66
+ getVars() {
67
+ return Object.entries(this.#n);
68
+ }
69
+ hasFact(e) {
70
+ return Object.hasOwn(this.#r, e);
71
+ }
72
+ getFact(e) {
73
+ return this.#r[e];
74
+ }
75
+ getFacts() {
76
+ return Object.entries(this.#r);
77
+ }
78
+ stop() {
79
+ this.#a.abort();
80
+ }
81
+ };
82
+ //#endregion
4
83
  //#region lib/interests.ts
5
- function o(e) {
84
+ function c(e) {
6
85
  return `dashboard://var/${encodeURIComponent(e)}`;
7
86
  }
8
- function s(e) {
87
+ function l(e) {
9
88
  return `dashboard://fact/${encodeURIComponent(e)}`;
10
89
  }
11
90
  //#endregion
12
91
  //#region lib/DashboardEngine.ts
13
- var c = class extends t {
92
+ var u = class extends n {
14
93
  #e;
15
- #t = {};
16
- #n = {};
17
- constructor(e) {
18
- super(), this.#e = e;
19
- }
20
- onAction(e) {
21
- super.onAction(e);
22
- let t = e;
23
- switch (t.type) {
24
- case "var":
25
- this.#t[t.name] = {
26
- value: t.value,
27
- exposed: t.exposed ?? !1
28
- }, this.dispatchInterest(o(t.name));
29
- return;
30
- case "fact":
31
- this.#n[t.name] = { value: t.value }, this.dispatchInterest(s(t.name));
32
- return;
33
- }
34
- }
35
- dispatchVar(e, t, n) {
36
- this.dispatchAction({
37
- type: "var",
38
- name: e,
39
- value: t,
40
- exposed: n
41
- });
42
- }
43
- dispatchFact(e, t) {
44
- this.dispatchAction({
45
- type: "fact",
46
- name: e,
47
- value: t
94
+ #t;
95
+ constructor(e, t, n) {
96
+ super(e), this.#e = t, this.#t = new s(this.target, {
97
+ vars: n?.vars,
98
+ facts: n?.facts,
99
+ onVar: (e) => {
100
+ this.target.dispatchInterest(c(e));
101
+ },
102
+ onFact: (e) => {
103
+ this.target.dispatchInterest(l(e));
104
+ },
105
+ signal: this.signal
48
106
  });
49
107
  }
50
108
  hasVar(e) {
51
- return Object.hasOwn(this.#t, e);
109
+ return this.#t.hasVar(e);
52
110
  }
53
111
  getVar(e) {
54
- return this.#t[e];
112
+ return this.#t.getVar(e);
55
113
  }
56
114
  hasFact(e) {
57
- return Object.hasOwn(this.#n, e);
115
+ return this.#t.hasFact(e);
58
116
  }
59
117
  getFact(e) {
60
- return this.#n[e];
118
+ return this.#t.getFact(e);
61
119
  }
62
120
  getReducerConfig(e) {
63
121
  if (!Object.hasOwn(this.#e, e.type)) throw Error(`Unknown reducer type "${e.type}"`);
@@ -69,7 +127,7 @@ var c = class extends t {
69
127
  createReducer(e) {
70
128
  return this.getReducerConfig(e).createReducer(this, e);
71
129
  }
72
- }, l = class extends n {
130
+ }, d = class extends r {
73
131
  dispatchVar(e, t, n) {
74
132
  this.dispatchAction({
75
133
  type: "var",
@@ -85,22 +143,34 @@ var c = class extends t {
85
143
  value: t
86
144
  });
87
145
  }
88
- }, u = class extends e {
146
+ sync() {
147
+ this.dispatchAction({ type: "sync" });
148
+ }
149
+ }, f = class extends t {
150
+ constructor(e, t, n) {
151
+ super(e, t, n), this.target.addEventListener("action", (e) => {
152
+ switch (e.action.type) {
153
+ case "sync":
154
+ this.target.subscribeState(this.id, this.descriptor);
155
+ return;
156
+ }
157
+ }, { signal: this.signal });
158
+ }
89
159
  compareStates(e, t) {
90
- return i(e, t);
160
+ return a(e, t);
91
161
  }
92
- }, d = class extends r {
162
+ }, p = class extends i {
93
163
  recover(e, t) {
94
164
  return [{
95
165
  type: "error",
96
- id: a(),
166
+ id: o(),
97
167
  error: t
98
168
  }];
99
169
  }
100
170
  compareStates(e, t) {
101
- return i(e, t);
171
+ return a(e, t);
102
172
  }
103
- }, f = "locale", p = class {
173
+ }, m = "locale", h = class {
104
174
  #e;
105
175
  constructor(e) {
106
176
  this.#e = e ?? {};
@@ -108,19 +178,19 @@ var c = class extends t {
108
178
  getLocale(e) {
109
179
  if (e?.locale) return e.locale;
110
180
  if (this.#e?.locale) return this.#e.locale;
111
- let t = this.#e?.reducer?.engine.getFact(f)?.value;
112
- return this.#e?.reducer?.addInterest(s(f)), typeof t == "string" ? t : void 0;
181
+ let t = this.#e?.reducer?.engine.getFact(m)?.value;
182
+ return this.#e?.reducer?.addInterest(l(m)), typeof t == "string" ? t : void 0;
113
183
  }
114
184
  getFormat(e) {
115
185
  return new Intl.NumberFormat(this.getLocale(e));
116
186
  }
117
187
  format(e, t) {
118
188
  switch (t?.type) {
119
- default: return m(this.getFormat(t).format(e), t);
189
+ default: return g(this.getFormat(t).format(e), t);
120
190
  }
121
191
  }
122
192
  };
123
- function m(e, t) {
193
+ function g(e, t) {
124
194
  let n;
125
195
  if (n = typeof t?.unit == "string" ? t.unit : t?.unit?.value, !n) return e;
126
196
  let r;
@@ -138,4 +208,4 @@ function m(e, t) {
138
208
  }
139
209
  }
140
210
  //#endregion
141
- export { c as DashboardEngine, l as DashboardEventTarget, u as DashboardStateConsumer, d as DashboardStateReducer, p as NumberFormatter, s as dashboardFactInterest, f as dashboardLocaleFact, o as dashboardVarInterest };
211
+ export { u as DashboardEngine, d as DashboardEventTarget, f as DashboardStateConsumer, p as DashboardStateReducer, h as NumberFormatter, l as dashboardFactInterest, m as dashboardLocaleFact, c as dashboardVarInterest };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rotorjs/dashboard",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "Rotor",
5
5
  "author": {
6
6
  "name": "Aaron Burmeister"
@@ -63,7 +63,7 @@
63
63
  "vite": "^8.0.16"
64
64
  },
65
65
  "dependencies": {
66
- "@rotorjs/state": "^0.6.1",
66
+ "@rotorjs/state": "^0.7.0",
67
67
  "fast-deep-equal": "^3.1.3",
68
68
  "uuid": "^14.0.0"
69
69
  }