@vorplex/core 0.0.46 → 0.0.49

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/dist/index.d.ts CHANGED
@@ -56,6 +56,7 @@ export * from './modules/reflection/types/type.type';
56
56
  export * from './modules/reflection/utils/decorator.util';
57
57
  export * from './modules/reflection/utils/reflection.util';
58
58
  export * from './modules/router/router.util';
59
+ export * from './modules/state/reducer.type';
59
60
  export * from './modules/state/state.model';
60
61
  export * from './modules/state/update.type';
61
62
  export * from './modules/state/adaptors/array/array-adaptor.util';
package/dist/index.js CHANGED
@@ -57,6 +57,7 @@ export * from './modules/reflection/types/type.type';
57
57
  export * from './modules/reflection/utils/decorator.util';
58
58
  export * from './modules/reflection/utils/reflection.util';
59
59
  export * from './modules/router/router.util';
60
+ export * from './modules/state/reducer.type';
60
61
  export * from './modules/state/state.model';
61
62
  export * from './modules/state/update.type';
62
63
  export * from './modules/state/adaptors/array/array-adaptor.util';
@@ -0,0 +1,30 @@
1
+ import { ArrayAdaptor } from './adaptors/array/array-adaptor.util';
2
+ import { EntityAdaptor } from './adaptors/entity/entity-adaptor.util';
3
+ import { EntityMap } from './adaptors/entity/entity-map.type';
4
+ import { IEntity } from './adaptors/entity/entity.interface';
5
+ import { Update } from './update.type';
6
+ type EntityItem<V> = V extends EntityMap<infer U> ? U : never;
7
+ type ArrayItem<V> = V extends (infer U)[] ? U : never;
8
+ type FieldOf<TClass, K> = K extends keyof TClass ? TClass[K] : {};
9
+ type ReducerFieldAdaptor<TState, TReducer extends Reducer, K extends keyof TState> = TState[K] extends EntityMap<IEntity> ? {
10
+ entity: EntityAdaptor<TState, EntityItem<TState[K]>, K & any>;
11
+ } & FieldOf<TReducer, K> : TState[K] extends any[] ? {
12
+ array: ArrayAdaptor<TState, ArrayItem<TState[K]>, K & any>;
13
+ } & FieldOf<TReducer, K> : {
14
+ value: {
15
+ set: (value: TState[K]) => Update<TState>;
16
+ };
17
+ } & FieldOf<TReducer, K>;
18
+ type StateFields<TState, TReducer extends Reducer> = {
19
+ [K in keyof TState]: ReducerFieldAdaptor<TState, TReducer, K>;
20
+ };
21
+ export type ReducerFields<TState> = {
22
+ update: {
23
+ <V>(path: (state: TState) => V, fn: (value: V) => Partial<V>): Update<TState>;
24
+ <V>(path: (state: TState) => V, value: Partial<V>): Update<TState>;
25
+ };
26
+ };
27
+ export type EmptyReducer = Record<never, never>;
28
+ export type Reducer = Record<string, any>;
29
+ export type StateReducer<TState, TReducer extends Reducer> = ReducerFields<TState> & StateFields<TState, TReducer>;
30
+ export {};
File without changes
@@ -1,17 +1,13 @@
1
- import type { Constructor } from '../reflection/types/constructor.type';
2
1
  import { Subscribable } from '../subscribable/subscribable.model';
3
2
  import type { Subscription } from '../subscribable/subscription.interface';
4
- import { ArrayAdaptor } from './adaptors/array/array-adaptor.util';
5
- import { EntityAdaptor } from './adaptors/entity/entity-adaptor.util';
6
- import type { EntityMap } from './adaptors/entity/entity-map.type';
7
- import type { IEntity } from './adaptors/entity/entity.interface';
3
+ import { EmptyReducer, Reducer, StateReducer } from './reducer.type';
8
4
  import type { Update } from './update.type';
9
5
  export interface StateChange<T> {
10
6
  previousValue: T;
11
7
  value: T;
12
8
  }
13
9
  export type StateEffect<T> = (value: T) => T;
14
- export declare class State<T = any, TReducer extends Constructor = Constructor> extends Subscribable<StateChange<T>> {
10
+ export declare class State<T = any, TReducer extends Reducer = EmptyReducer> extends Subscribable<StateChange<T>> {
15
11
  private reducer?;
16
12
  private _value;
17
13
  get value(): T;
@@ -19,22 +15,7 @@ export declare class State<T = any, TReducer extends Constructor = Constructor>
19
15
  static combineLatest<T extends any[]>(states: {
20
16
  [K in keyof T]: State<T[K]>;
21
17
  }, callback: (values: T) => void): Subscription;
22
- reduce(update: (reducer: {
23
- update: {
24
- <V>(path: (state: T) => V, update: (value: V) => Partial<V>): Update<T>;
25
- <V>(path: (state: T) => V, value: Partial<V>): Update<T>;
26
- };
27
- } & {
28
- [K in keyof T]: T[K] extends EntityMap<IEntity> ? {
29
- entity: EntityAdaptor<T, T[K] extends EntityMap<infer U> ? U : never, K & any>;
30
- } & (K extends keyof InstanceType<TReducer> ? InstanceType<TReducer>[K & keyof InstanceType<TReducer>] : {}) : T[K] extends any[] ? {
31
- array: ArrayAdaptor<T, T[K] extends (infer U)[] ? U : never, K & any>;
32
- } & (K extends keyof InstanceType<TReducer> ? InstanceType<TReducer>[K & keyof InstanceType<TReducer>] : {}) : {
33
- value: {
34
- set: (value: T[K]) => Update<T> & (K extends keyof InstanceType<TReducer> ? InstanceType<TReducer>[K & keyof InstanceType<TReducer>] : {});
35
- } & (K extends keyof InstanceType<TReducer> ? InstanceType<TReducer>[K & keyof InstanceType<TReducer>] : {});
36
- };
37
- }) => Update<T>[]): void;
18
+ reduce(update: (reducer: StateReducer<T, TReducer>) => Update<T>[]): void;
38
19
  subscribe(emit: (event: StateChange<T>, subscription: Subscription) => void): Subscription;
39
20
  static update<TState>(state: TState, ...updates: Update<TState>[]): TState;
40
21
  update(...updates: Update<T>[]): void;
@@ -42,11 +42,12 @@ export class State extends Subscribable {
42
42
  };
43
43
  }
44
44
  reduce(update) {
45
- const reducer = new Proxy({
45
+ const reducerFields = {
46
46
  update: (path, update) => {
47
47
  return (state) => $Value.update(state, path, update);
48
- },
49
- }, {
48
+ }
49
+ };
50
+ const reducer = new Proxy(reducerFields, {
50
51
  get: (target, property) => {
51
52
  if (property === 'update')
52
53
  return target.update;
@@ -59,7 +60,7 @@ export class State extends Subscribable {
59
60
  }),
60
61
  },
61
62
  };
62
- Object.assign(reducer, (this.reducer && new this.reducer()[property]) || {});
63
+ Object.assign(reducer, this.reducer?.[property] ?? {});
63
64
  return reducer;
64
65
  },
65
66
  });
@@ -63,16 +63,16 @@ export class TsonArray extends TsonSchemaBase {
63
63
  throw new TsonError(`Array min length of ${this.definition.min} expected`, value, this);
64
64
  if (this.definition.max != null && value.length > this.definition.max)
65
65
  throw new TsonError(`Array max length of ${this.definition.max} expected`, value, this);
66
- if (this.definition.itemDefinition) {
67
- for (const [index, item] of value.entries()) {
68
- try {
69
- result[index] = $Tson.parse(this.definition.itemDefinition).parse(item);
70
- }
71
- catch (error) {
72
- if (!(error instanceof TsonError))
73
- throw error;
74
- throw new TsonError(`Invalid item in array. ${error.message}`, item, this, `[${index}]${error.path}`);
75
- }
66
+ if (!this.definition.itemDefinition)
67
+ return [...value];
68
+ for (const [index, item] of value.entries()) {
69
+ try {
70
+ result[index] = $Tson.parse(this.definition.itemDefinition).parse(item);
71
+ }
72
+ catch (error) {
73
+ if (!(error instanceof TsonError))
74
+ throw error;
75
+ throw new TsonError(`Invalid item in array. ${error.message}`, item, this, `[${index}]${error.path}`);
76
76
  }
77
77
  }
78
78
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vorplex/core",
3
- "version": "0.0.46",
3
+ "version": "0.0.49",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "files": [