@microphi/store 2.2.3 → 2.5.1

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
@@ -63,3 +63,30 @@ class MyStore extends Store<ItemsState, ItemsActions>
63
63
 
64
64
  }
65
65
  ```
66
+
67
+ ### Dispatch and select state
68
+ ```typescript
69
+ // given an instance of the store
70
+ const store = new MyStore();
71
+
72
+ // subscribe to any state change
73
+ store.state$.subscribe((state) => console.log({ state }) );
74
+
75
+ // or better select a projection of the state
76
+ store.select(({users}) => users).subscribe((users) => console.log({ users }));
77
+
78
+ // finally dispatch an action
79
+ store.dispatch('findAll');
80
+
81
+ ```
82
+
83
+ #### Watch loading state
84
+ We assume every action to be an asyncronious task so every action as a loading state associated to it. Loading state can we subscribed such as:
85
+ ```typescript
86
+ store.getLoadingFor('findAll').subscribe((loading) => {
87
+
88
+ console.log({loading});
89
+
90
+ });
91
+
92
+ ```
package/lib/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from './lib/store/store';
2
2
  export * from './lib/effect/effect';
3
3
  export * from './lib/reduce/reduce';
4
- export * from './lib/list/list';
5
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC"}
package/lib/index.js CHANGED
@@ -4,5 +4,3 @@
4
4
  export * from './lib/store/store';
5
5
  export * from './lib/effect/effect';
6
6
  export * from './lib/reduce/reduce';
7
- export * from './lib/list/list';
8
- //# sourceMappingURL=index.js.map
@@ -72,4 +72,3 @@ export function getEffects(instance) {
72
72
  });
73
73
  return effects;
74
74
  }
75
- //# sourceMappingURL=effect.js.map
@@ -23,4 +23,3 @@ export function getReducers(instance) {
23
23
  });
24
24
  return reducers;
25
25
  }
26
- //# sourceMappingURL=reduce.js.map
@@ -99,4 +99,3 @@ export class Store {
99
99
  return EMPTY;
100
100
  }
101
101
  }
102
- //# sourceMappingURL=store.js.map
@@ -4,4 +4,3 @@ export function capitalise(s) {
4
4
  }
5
5
  return s[0].toUpperCase() + s.slice(1);
6
6
  }
7
- //# sourceMappingURL=capitalise.js.map
@@ -16,4 +16,3 @@ export function scanInstance(instance, cb) {
16
16
  }
17
17
  }
18
18
  }
19
- //# sourceMappingURL=scan-instance.js.map
package/package.json CHANGED
@@ -1,5 +1,17 @@
1
1
  {
2
2
  "name": "@microphi/store",
3
+ "version": "2.5.1",
4
+ "description": "RxJS base state manager",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "module": "lib/index.js",
8
+ "directories": {
9
+ "lib": "lib"
10
+ },
11
+ "files": [
12
+ "lib"
13
+ ],
14
+ "licence": "MIT",
3
15
  "keywords": [
4
16
  "redux",
5
17
  "reducer",
@@ -13,18 +25,21 @@
13
25
  "flux",
14
26
  "elm"
15
27
  ],
16
- "main": "lib/index.js",
17
- "typings": "lib/index.d.ts",
18
- "module": "lib/index.js",
19
- "directories": {
20
- "lib": "lib"
28
+ "author": {
29
+ "email": "cavaliere.davide@gmail.com",
30
+ "name": "Davide Cavaliere",
31
+ "url": "www.dcsoftware.io"
32
+ },
33
+ "homepage": "https://github.com/microph1/microphi",
34
+ "scripts": {
35
+ "build": "tsc -b tsconfig.build.json",
36
+ "build:ci": "yarn build",
37
+ "test:ci": "jest --coverage --ci",
38
+ "prepack": "yarn build",
39
+ "lint:ci": "eslint ./src"
21
40
  },
22
- "files": [
23
- "lib"
24
- ],
25
- "version": "2.2.3",
26
41
  "dependencies": {
27
- "@microphi/debug": "^2.2.3",
42
+ "@microphi/debug": "^2.5.1",
28
43
  "reflect-metadata": "^0.1.13",
29
44
  "tslib": "^2.5.0"
30
45
  },
@@ -34,12 +49,5 @@
34
49
  "peerDependencies": {
35
50
  "rxjs": "~7.8.0"
36
51
  },
37
- "scripts": {
38
- "build": "tsc -b tsconfig.build.json",
39
- "build:ci": "yarn build",
40
- "test:ci": "jest --coverage --ci",
41
- "prepack": "yarn build",
42
- "lint:ci": "eslint ./src"
43
- },
44
- "gitHead": "e86cbc52e62e0862db78792d5ff64c6ae1de50f8"
52
+ "gitHead": "85be3ad7bf34a32e5b386d817b71ef3f6703b397"
45
53
  }
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC","sourcesContent":["/*\n * Public API Surface of store\n */\n\nexport * from './lib/store/store';\nexport * from './lib/effect/effect';\nexport * from './lib/reduce/reduce';\nexport * from './lib/list/list';\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"effect.js","sourceRoot":"","sources":["../../../src/lib/effect/effect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAS1D,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,MAAM,UAAU,MAAM,CAAC,WAA2B,WAAW;IAC3D,mDAAmD;IACnD,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,EAAE;QAC7B,wEAAwE;QACxE,OAAO,OAAO,CAAC,cAAc,CAAC,YAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;IAC7E,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,KAAa,EAAE,GAAW;IAC1D,OAAO,OAAO,CAAC,WAAW,CAAC,YAAY,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;AACvD,CAAC;AAED,MAAM,UAAU,UAAU,CAAmB,QAAW;IAEtD,MAAM,OAAO,GAAa,EAAE,CAAC;IAE7B,YAAY,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QAEpC,MAAM,QAAQ,GAAG,iBAAiB,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAC/C,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,CAAC,IAAI,CAAC,EAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,GAAE,CAAC,CAAC;QACzC,CAAC;IAEH,CAAC,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { scanInstance } from '../utilities/scan-instance';\n\nexport type EffectStrategy = 'switchMap' | 'mergeMap' | 'concatMap';\n\nexport interface Effect {\n action: string;\n strategy: EffectStrategy;\n}\n\nexport const EffectSymbol = Symbol.for('@Effect');\n\n/**\n * Tags a method as an effect <br>\n *\n * Strategy is one of the following rxjs pipe-able operators:\n * - `mergeMap`\n * - `concatMap`\n * - `switchMap`\n *\n * As an example consider the following:\n * suppose there's an effect that takes 10 ms to return and then a reducer is called.<br>\n * Suppose we dispatch its event, the following diagram demonstrate what happens<br>\n * <code>\n * `D -> E -> (10) -> R`<br>\n * </code>\n * Event is dispatched `D`, event is executed `E`, takes 10ms and finally reducer is called with result from `E`\n *\n * Now let's suppose we dispatch again the same while `E` is still pending (executing) and suppose we call it with a different argument.\n *\n *\n * `D(a) -> E -> D(b)`\n *\n *\n * what happens next depends on the strategy used.<br>\n *\n *\n * If `switchMap` is used (default)\n *\n * `D(a) -> E(a) -> D(b) -> E(b) -> R(b)`\n *\n * the second dispatch cancels the first and so only `R(b)` is executed\n *\n *\n * If `mergeMap` is used\n *\n * `D(a) -> E(a) -> D(b) -> E(b) -> R(a) -> R(b)\n *\n * effects are executed in parallel and the first to come is the first served\n *\n * i.e.: lets say E(a) takes longer to respond then\n *\n * `D(a) -> D(b) -> E(b) -> R(b) -> E(a) -> -> R(a)`\n *\n *\n * If `concatMap` is used then the order of calls is respected.\n *\n * in the case above we'll have whatever the timings are on the effects\n *\n * `D(a) -> D(b) -> E(a) -> R(a) -> E(b) -> -> R(b)`\n *\n * @param strategy\n * @constructor\n */\nexport function Effect(strategy: EffectStrategy = 'switchMap'): MethodDecorator {\n // return Reflect.metadata(EffectSymbol, strategy);\n return (target, propertyKey) => {\n // return Reflect.metadata(EffectSymbol, strategy)(target, propertyKey);\n return Reflect.defineMetadata(EffectSymbol, strategy, target, propertyKey);\n };\n}\n\nexport function getEffectMetadata(klass: object, key: string) {\n return Reflect.getMetadata(EffectSymbol, klass, key);\n}\n\nexport function getEffects<T extends object>(instance: T): Effect[] {\n\n const effects: Effect[] = [];\n\n scanInstance(instance, (proto, key) => {\n\n const strategy = getEffectMetadata(proto, key);\n if (strategy) {\n effects.push({action: key, strategy,});\n }\n\n });\n\n return effects;\n}\n"]}
@@ -1,21 +0,0 @@
1
- import { $Keys } from 'utility-types';
2
- export declare class List<EntityType extends object> implements Iterable<EntityType> {
3
- private IDField;
4
- readonly entities: Map<any, EntityType>;
5
- /**
6
- * maps entities position from the original array to their ids
7
- */
8
- private readonly ids;
9
- get size(): number;
10
- has(id: any): boolean;
11
- get(id: any): EntityType | undefined;
12
- private getId;
13
- constructor(IDField: $Keys<EntityType>, entities?: EntityType[]);
14
- [Symbol.iterator](): IterableIterator<EntityType>;
15
- upsert(...entities: EntityType[]): void;
16
- prepend(...entities: EntityType[]): void;
17
- clear(): void;
18
- delete(...entities: EntityType[]): void;
19
- append(...entities: EntityType[]): void;
20
- }
21
- //# sourceMappingURL=list.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../../src/lib/list/list.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC,qBAAa,IAAI,CAAC,UAAU,SAAS,MAAM,CAAE,YAAW,QAAQ,CAAC,UAAU,CAAC;IA8B9D,OAAO,CAAC,OAAO;IA3B3B,SAAgB,QAAQ,uBAA8B;IAEtD;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAa;IAEjC,IAAW,IAAI,WAOd;IAEM,GAAG,CAAC,EAAE,EAAE,GAAG;IAIX,GAAG,CAAC,EAAE,EAAE,GAAG;IAGlB,OAAO,CAAC,KAAK;gBAIO,OAAO,EAAE,KAAK,CAAC,UAAU,CAAC,EAAE,QAAQ,GAAE,UAAU,EAAO;IAW1E,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAgB,CAAC,UAAU,CAAC;IAQ3C,MAAM,CAAC,GAAG,QAAQ,EAAE,UAAU,EAAE;IAYhC,OAAO,CAAC,GAAG,QAAQ,EAAE,UAAU,EAAE;IAejC,KAAK;IAOL,MAAM,CAAC,GAAG,QAAQ,EAAE,UAAU,EAAE;IAchC,MAAM,CAAC,GAAG,QAAQ,EAAE,UAAU,EAAE;CAYxC"}
@@ -1,82 +0,0 @@
1
- export class List {
2
- get size() {
3
- if (this.ids.length !== this.entities.size) {
4
- throw new Error(`Size Mismatch ${this.ids.length} !== ${this.entities.size}`);
5
- }
6
- return this.entities.size;
7
- }
8
- has(id) {
9
- return this.entities.has(id);
10
- }
11
- get(id) {
12
- return this.entities.get(id);
13
- }
14
- getId(entity) {
15
- return entity[this.IDField];
16
- }
17
- constructor(IDField, entities = []) {
18
- this.IDField = IDField;
19
- this.entities = new Map();
20
- /**
21
- * maps entities position from the original array to their ids
22
- */
23
- this.ids = [];
24
- this.ids = entities.map((e) => this.getId(e));
25
- entities
26
- .forEach((item) => {
27
- const id = item[IDField];
28
- this.entities.set(id, item);
29
- });
30
- }
31
- *[Symbol.iterator]() {
32
- for (const idx in this.ids) {
33
- const id = this.ids[idx];
34
- // @ts-ignore
35
- yield this.entities.get(id);
36
- }
37
- }
38
- upsert(...entities) {
39
- entities.forEach((e) => {
40
- const id = this.getId(e);
41
- if (!this.entities.has(id)) {
42
- this.ids.push(id);
43
- }
44
- this.entities.set(id, e);
45
- });
46
- }
47
- prepend(...entities) {
48
- entities.reverse().forEach((e) => {
49
- const id = this.getId(e);
50
- if (this.entities.has(id)) {
51
- this.delete(e);
52
- }
53
- this.ids.unshift(id);
54
- this.entities.set(id, e);
55
- });
56
- }
57
- clear() {
58
- this.ids.length = 0;
59
- this.entities.clear();
60
- }
61
- delete(...entities) {
62
- entities.forEach((e) => {
63
- const id = this.getId(e);
64
- const indexOfId = this.ids.findIndex((_id) => _id === id);
65
- if (indexOfId >= 0) {
66
- this.ids.splice(indexOfId, 1);
67
- this.entities.delete(id);
68
- }
69
- });
70
- }
71
- append(...entities) {
72
- entities.forEach((e) => {
73
- const id = this.getId(e);
74
- if (this.entities.has(id)) {
75
- this.delete(e);
76
- }
77
- this.ids.push(id);
78
- this.entities.set(id, e);
79
- });
80
- }
81
- }
82
- //# sourceMappingURL=list.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"list.js","sourceRoot":"","sources":["../../../src/lib/list/list.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,IAAI;IAUf,IAAW,IAAI;QACb,IAAI,IAAI,CAAC,GAAG,CAAC,MAAM,KAAK,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;YAE3C,MAAM,IAAI,KAAK,CAAC,iBAAiB,IAAI,CAAC,GAAG,CAAC,MAAM,QAAQ,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;QAChF,CAAC;QAED,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;IAC5B,CAAC;IAEM,GAAG,CAAC,EAAO;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC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AAC,CAAC;YAC3B,CAAC;QACH,CAAC,CAAC,CAAC;IAEL,CAAC;IAEM,MAAM,CAAC,GAAG,QAAsB;QACrC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;YACrB,MAAM,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAEzB,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;gBAC1B,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;YACjB,CAAC;YAED,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAClB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { $Keys } from 'utility-types';\n\nexport class List<EntityType extends object> implements Iterable<EntityType> {\n\n\n public readonly entities = new Map<any, EntityType>();\n\n /**\n * maps entities position from the original array to their ids\n */\n private readonly ids: any[] = [];\n\n public get size() {\n if (this.ids.length !== this.entities.size) {\n\n throw new Error(`Size Mismatch ${this.ids.length} !== ${this.entities.size}`);\n }\n\n return this.entities.size;\n }\n\n public has(id: any) {\n return this.entities.has(id);\n }\n\n public get(id: any) {\n return this.entities.get(id);\n }\n private getId(entity: EntityType) {\n return entity[this.IDField];\n }\n\n constructor(private IDField: $Keys<EntityType>, entities: EntityType[] = []) {\n\n this.ids = entities.map((e) => this.getId(e));\n\n entities\n .forEach((item) => {\n const id = item[IDField];\n this.entities.set(id, item);\n });\n }\n\n *[Symbol.iterator](): IterableIterator<EntityType> {\n for (const idx in this.ids) {\n const id = this.ids[idx];\n // @ts-ignore\n yield this.entities.get(id);\n }\n }\n\n public upsert(...entities: EntityType[]) {\n entities.forEach((e) => {\n const id = this.getId(e);\n\n if (!this.entities.has(id)) {\n this.ids.push(id);\n }\n\n this.entities.set(id, e);\n });\n }\n\n public prepend(...entities: EntityType[]) {\n entities.reverse().forEach((e) => {\n const id = this.getId(e);\n\n if (this.entities.has(id)) {\n this.delete(e);\n }\n\n this.ids.unshift(id);\n\n this.entities.set(id, e);\n\n });\n }\n\n public clear() {\n\n this.ids.length = 0;\n this.entities.clear();\n\n }\n\n public delete(...entities: EntityType[]) {\n\n entities.forEach((e) => {\n const id = this.getId(e);\n const indexOfId = this.ids.findIndex((_id) => _id === id);\n\n if (indexOfId >= 0) {\n this.ids.splice(indexOfId, 1);\n this.entities.delete(id);\n }\n });\n\n }\n\n public append(...entities: EntityType[]) {\n entities.forEach((e) => {\n const id = this.getId(e);\n\n if (this.entities.has(id)) {\n this.delete(e);\n }\n\n this.ids.push(id);\n this.entities.set(id, e);\n });\n }\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"reduce.js","sourceRoot":"","sources":["../../../src/lib/reduce/reduce.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAG1D,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;AAM9C,MAAM,UAAU,MAAM;IACpB,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,EAAE;QACzC,sDAAsD;QACtD,yCAAyC;QACzC,kDAAkD;QAClD,MAAM,MAAM,GAAI,WAAsB,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,GAAI,WAAsB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAEpG,OAAO,OAAO,CAAC,cAAc,CAAC,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;IAC3E,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,KAAiB,EAAE,GAAW;IAC9D,OAAO,OAAO,CAAC,WAAW,CAAC,YAAY,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;AACvD,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,QAAgB;IAE1C,MAAM,QAAQ,GAAwB,EAAE,CAAC;IAEzC,YAAY,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QAEpC,MAAM,QAAQ,GAAG,iBAAiB,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAC/C,IAAI,QAAQ,EAAE,CAAC;YACb,QAAQ,CAAC,IAAI,CAAC,EAAC,MAAM,EAAE,QAAQ,EAAC,CAAC,CAAC;QACpC,CAAC;IAEH,CAAC,CAAC,CAAC;IAEH,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import 'reflect-metadata';\nimport { scanInstance } from '../utilities/scan-instance';\nimport { Class } from 'utility-types';\n\nexport const ReduceSymbol = Symbol('@Reduce');\n\nexport interface Reducer {\n action: string;\n}\n\nexport function Reduce(): MethodDecorator {\n return (target, propertyKey, descriptor) => {\n // by convention a reducer have name such as onFindAll\n // where findAll is the associated action\n // with the following we extract the action's name\n const action = (propertyKey as string).slice(2, 3).toLowerCase() + (propertyKey as string).slice(3);\n\n return Reflect.defineMetadata(ReduceSymbol, action, target, propertyKey);\n };\n}\n\nexport function getReduceMetadata(klass: Class<any>, key: string) {\n return Reflect.getMetadata(ReduceSymbol, klass, key);\n}\n\nexport function getReducers(instance: object): {action: string;}[] {\n\n const reducers: {action: string;}[] = [];\n\n scanInstance(instance, (proto, key) => {\n\n const metadata = getReduceMetadata(proto, key);\n if (metadata) {\n reducers.push({action: metadata});\n }\n\n });\n\n return reducers;\n}\n"]}
@@ -1,55 +0,0 @@
1
- import { BehaviorSubject, Observable, Subject } from 'rxjs';
2
- import { getPayloadFromActionType } from './store';
3
- export type Class = new (...args: any[]) => {};
4
- export type makeEffects<A> = {
5
- [k in keyof A]: A[k] extends (...args: infer I) => infer O ? (...args: I) => Observable<O> : void;
6
- };
7
- export type returnType<T> = T extends (...args: any[]) => infer O ? O : string[];
8
- export type makeReducers<A, S> = {
9
- [k in keyof A]: (state: S, payload: returnType<A[k]>) => S;
10
- };
11
- export interface LoadingState {
12
- status: boolean;
13
- action: string;
14
- payload: any;
15
- }
16
- export declare function StoreFactory<E extends Class, R extends Class, S, IE = InstanceType<E>, IR = InstanceType<R>>(effects: E, reducers: R, state: S): {
17
- new (e: IE, r: IR): {
18
- loading$: Subject<LoadingState>;
19
- actions: Map<keyof (IE & IR), Subject<any>>;
20
- _state$: BehaviorSubject<S>;
21
- e: IE;
22
- r: IR;
23
- /**
24
- * Select a state field using a mapping function
25
- * @param fn mapping function
26
- * @example
27
- * store.select(({items}) => items);
28
- */
29
- select(fn: (s: S) => any): Observable<any>;
30
- /**
31
- * Get the loading$ observable of a given action
32
- * @param a the action
33
- * @example
34
- * store.getLoadingFor('findAll')
35
- */
36
- getLoadingFor<K extends keyof IE>(a: K): Observable<LoadingState>;
37
- /**
38
- * Dispatch an action with the given payload.
39
- * @param action action to dispatch
40
- * @param payload payload to pass to the action
41
- *
42
- * @example
43
- *
44
- * interface IActions {
45
- * findAll: () => string[];
46
- * findOne: (name: string) => string;
47
- * }
48
- *
49
- * store.dispatch('findAll');
50
- * store.dispatch('findOne', 'alice');
51
- */
52
- dispatch<K_1 extends keyof IE>(action: K_1, ...payload: getPayloadFromActionType<IE, K_1>): void;
53
- };
54
- };
55
- //# sourceMappingURL=store-3.experiment.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"store-3.experiment.d.ts","sourceRoot":"","sources":["../../../src/lib/store/store-3.experiment.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,OAAO,EAAkB,MAAM,MAAM,CAAC;AAG5E,OAAO,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC;AAOnD,MAAM,MAAM,KAAK,GAAG,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;AAE/C,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;KAC1B,CAAC,IAAI,M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rxjs';
2
- import { filter, map, withLatestFrom } from 'rxjs/operators';
3
- import { getDebugger } from '@microphi/debug';
4
- const d = getDebugger('store-3');
5
- export function StoreFactory(effects, reducers, state) {
6
- const effectNames = (Object.getOwnPropertyNames(effects.prototype))
7
- .filter((m) => typeof effects.prototype[m] === 'function')
8
- .filter((m) => m !== 'constructor');
9
- d({ effectNames });
10
- const reducerNames = Object.getOwnPropertyNames(reducers.prototype)
11
- .filter((m) => typeof reducers.prototype[m] === 'function')
12
- .filter((m) => m !== 'constructor');
13
- d({ reducerNames });
14
- // this will be common to all instances
15
- const actions = new Set(effectNames.concat(reducerNames));
16
- d({ actions });
17
- return class {
18
- constructor(e, r) {
19
- this.e = e;
20
- this.r = r;
21
- this.loading$ = new Subject();
22
- this.actions = new Map();
23
- this._state$ = new BehaviorSubject(state);
24
- d('initial state is', state);
25
- actions.forEach((action) => {
26
- d('creating Subject for action', action);
27
- const actionSubject = new Subject();
28
- actionSubject.pipe(withLatestFrom(this._state$), tap(([payload]) => {
29
- d('action dispatched with payload', payload);
30
- d('loading will start now!');
31
- this.loading$.next({
32
- action: action,
33
- payload,
34
- status: true
35
- });
36
- }), switchMap(([payload, state]) => {
37
- // eslint-disable-next-line @typescript-eslint/ban-types
38
- return this.e[action].apply(this.e, payload).pipe(map((response) => {
39
- // eslint-disable-next-line @typescript-eslint/ban-types
40
- return this.r[action].apply(this.r, [state, response]);
41
- }),
42
- // this tap needs to be here because we need the `payload`
43
- tap(() => {
44
- d('loading stops now');
45
- this.loading$.next({
46
- action: action,
47
- status: false,
48
- payload
49
- });
50
- }, () => {
51
- d('loading stops now');
52
- this.loading$.next({
53
- action: action,
54
- status: false,
55
- payload
56
- });
57
- }));
58
- })).subscribe((state) => {
59
- d('new state is', state);
60
- // @ts-ignore
61
- this._state$.next(state);
62
- });
63
- this.actions.set(action, actionSubject);
64
- });
65
- }
66
- /**
67
- * Select a state field using a mapping function
68
- * @param fn mapping function
69
- * @example
70
- * store.select(({items}) => items);
71
- */
72
- select(fn) {
73
- return this._state$.pipe(map(fn));
74
- }
75
- /**
76
- * Get the loading$ observable of a given action
77
- * @param a the action
78
- * @example
79
- * store.getLoadingFor('findAll')
80
- */
81
- getLoadingFor(a) {
82
- return this.loading$.pipe(filter(({ action }) => {
83
- return action === a;
84
- }));
85
- }
86
- /**
87
- * Dispatch an action with the given payload.
88
- * @param action action to dispatch
89
- * @param payload payload to pass to the action
90
- *
91
- * @example
92
- *
93
- * interface IActions {
94
- * findAll: () => string[];
95
- * findOne: (name: string) => string;
96
- * }
97
- *
98
- * store.dispatch('findAll');
99
- * store.dispatch('findOne', 'alice');
100
- */
101
- dispatch(action, ...payload) {
102
- d('dispatching action', action, 'with payload', payload);
103
- this.actions.get(action)?.next(payload);
104
- }
105
- };
106
- }
107
- //# sourceMappingURL=store-3.experiment.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"store-3.experiment.js","sourceRoot":"","sources":["../../../src/lib/store/store-3.experiment.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAc,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAC5E,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAG9C,MAAM,CAAC,GAAG,WAAW,CAAC,SAAS,CAAC,CAAC;AAuBjC,MAAM,UAAU,YAAY,CAK1B,OAAU,EAAE,QAAW,EAAE,KAAQ;IAEjC,MAAM,WAAW,GAAG,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;SAChE,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC;SACzD,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,aAAa,CAAyB,CAAC;IAC9D,CA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AAC,CAAC;YAC1C,CAAC,CAAC,CAAC;QAEL,CAAC;QAGD;;;;;WAKG;QACH,MAAM,CAAC,EAAiB;YACtB,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CACtB,GAAG,CAAC,EAAE,CAAC,CACR,CAAC;QACJ,CAAC;QAED;;;;;WAKG;QACH,aAAa,CAAqB,CAAI;YACpC,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,CACvB,MAAM,CAAC,CAAC,EAAC,MAAM,EAAC,EAAE,EAAE;gBAClB,OAAO,MAAM,KAAK,CAAC,CAAC;YACtB,CAAC,CAAC,CACH,CAAC;QACJ,CAAC;QAED;;;;;;;;;;;;;;WAcG;QACH,QAAQ,CAAqB,MAAS,EAAE,GAAG,OAAwC;YACjF,CAAC,CAAC,oBAAoB,EAAE,MAAM,EAAE,cAAc,EAAE,OAAO,CAAC,CAAC;YACzD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC1C,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { $Keys } from 'utility-types';\nimport { BehaviorSubject, Observable, Subject, switchMap, tap } from 'rxjs';\nimport { filter, map, withLatestFrom } from 'rxjs/operators';\nimport { getDebugger } from '@microphi/debug';\nimport { getPayloadFromActionType } from './store';\n\nconst d = getDebugger('store-3');\n\n\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport type Class = new (...args: any[]) => {};\n\nexport type makeEffects<A> = {\n [k in keyof A]: A[k] extends (...args: infer I) => infer O ? (...args: I) => Observable<O> : void;\n}\n\nexport type returnType<T> = T extends (...args: any[]) => infer O ? O : string[];\n\nexport type makeReducers<A, S> = {\n [k in keyof A]: (state: S, payload: returnType<A[k]>) => S;\n}\n\nexport interface LoadingState {\n status: boolean;\n action: string;\n payload: any;\n}\n\nexport function StoreFactory<E extends Class,\n R extends Class,\n S,\n IE = InstanceType<E>,\n IR = InstanceType<R>,\n>(effects: E, reducers: R, state: S) {\n\n const effectNames = (Object.getOwnPropertyNames(effects.prototype))\n .filter((m) => typeof effects.prototype[m] === 'function')\n .filter((m) => m !== 'constructor') as $Keys<IE & object>[];\n d({effectNames});\n\n const reducerNames = (Object.getOwnPropertyNames(reducers.prototype) as $Keys<IE & object>[])\n .filter((m) => typeof reducers.prototype[m] === 'function')\n .filter((m) => m !== 'constructor');\n d({reducerNames});\n\n\n // this will be common to all instances\n const actions = new Set<keyof (IE&IR)>(\n effectNames.concat(reducerNames)\n );\n\n d({actions});\n\n return class {\n\n loading$: Subject<LoadingState> = new Subject<LoadingState>();\n\n actions: Map<keyof (IE&IR), Subject<any>> = new Map();\n\n _state$: BehaviorSubject<S> = new BehaviorSubject<S>(state);\n\n constructor(public e: IE, public r: IR) {\n d('initial state is', state);\n\n actions.forEach((action) => {\n d('creating Subject for action', action);\n const actionSubject = new Subject<{ action: string; payload: unknown; }>();\n\n actionSubject.pipe(\n withLatestFrom(this._state$),\n tap(([payload]) => {\n d('action dispatched with payload', payload);\n d('loading will start now!');\n this.loading$.next({\n action: action as string,\n payload,\n status: true\n });\n }),\n switchMap(([payload, state]) => {\n // eslint-disable-next-line @typescript-eslint/ban-types\n return (this.e[action as keyof IE] as unknown as Function).apply(this.e, payload).pipe(\n map((response) => {\n // eslint-disable-next-line @typescript-eslint/ban-types\n return (this.r[action as keyof IR] as unknown as Function).apply(this.r, [state, response]);\n }),\n // this tap needs to be here because we need the `payload`\n tap(() => {\n d('loading stops now');\n this.loading$.next({\n action: action as string,\n status: false,\n payload\n });\n }, () => {\n d('loading stops now');\n this.loading$.next({\n action: action as string,\n status: false,\n payload\n });\n })\n );\n })\n ).subscribe((state) => {\n d('new state is', state);\n // @ts-ignore\n this._state$.next(state);\n });\n\n this.actions.set(action, actionSubject);\n });\n\n }\n\n\n /**\n * Select a state field using a mapping function\n * @param fn mapping function\n * @example\n * store.select(({items}) => items);\n */\n select(fn: (s: S) => any) {\n return this._state$.pipe(\n map(fn)\n );\n }\n\n /**\n * Get the loading$ observable of a given action\n * @param a the action\n * @example\n * store.getLoadingFor('findAll')\n */\n getLoadingFor<K extends keyof IE>(a: K) {\n return this.loading$.pipe(\n filter(({action}) => {\n return action === a;\n })\n );\n }\n\n /**\n * Dispatch an action with the given payload.\n * @param action action to dispatch\n * @param payload payload to pass to the action\n *\n * @example\n *\n * interface IActions {\n * findAll: () => string[];\n * findOne: (name: string) => string;\n * }\n *\n * store.dispatch('findAll');\n * store.dispatch('findOne', 'alice');\n */\n dispatch<K extends keyof IE>(action: K, ...payload: getPayloadFromActionType<IE, K>) {\n d('dispatching action', action, 'with payload', payload);\n this.actions.get(action)?.next(payload);\n }\n };\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"store.js","sourceRoot":"","sources":["../../../src/lib/store/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,KAAK,EAAc,EAAE,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACvE,OAAO,EAAE,WAAW,EAAW,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC9G,OAAO,EAA0B,UAAU,EAAE,MAAM,kBAAkB,CAAC;AA2CtE,MAAM,OAAgB,KAAK;IAWzB,IAAW,QAAQ;QACjB,OAAO,IAAI,CAAC,SAAS,CAAC,YAAY,EAAE,CAAC;IACvC,CAAC;IAOD,IAAW,MAAM;QACf,OAAO,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC;IACrC,CAAC;IAED,YAAoB,YAAmB;QAAnB,iBAAY,GAAZ,YAAY,CAAO;QAtBtB,YAAO,GAA0D,IAAI,GAAG,EAAE,CAAC;QAOpF,cAAS,GAAG,IAAI,OAAO,EAAmB,CAAC;QAMhC,YAAO,GAA2B,IAAI,eAAe;QACtE,oEAAoE;QACpE,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,CAAC;QAEH,OAAO,KAAK,CAAC;IACf,CAAC;CAEF","sourcesContent":["import { BehaviorSubject, EMPTY, Observable, of, Subject } from 'rxjs';\nimport { getReducers, Reducer } from '../reduce/reduce';\nimport { catchError, concatMap, filter, map, mergeMap, switchMap, tap, withLatestFrom } from 'rxjs/operators';\nimport { Effect, EffectStrategy, getEffects } from '../effect/effect';\nimport { Brand, Primitive } from 'utility-types';\n\nexport type getPayloadFromActionType<A, C extends keyof A> =\n// eslint-disable-next-line @typescript-eslint/ban-types\n A[C] extends Function ? A[C] extends () => any\n ? never[] : A[C] extends (...args: infer T) => any\n ? T : never[] : A[C][];\n\nexport type PureReducer<T extends Primitive> = Brand<T, 'reducer'>;\n\nexport type makeEffects<Actions> = {\n // Effects\n [k in keyof Actions]: Actions[k] extends PureReducer<Primitive>\n ? never\n : Actions[k]\n}\n\nexport type makeStore<State, Actions> =\n {\n [k in keyof makeEffects<Actions>]: makeEffects<Actions>[k]\n }\n // &\n // Actions\n &\n {\n // Reducers\n [k in keyof Actions as `on${Capitalize<string & k>}`]: Actions[k] extends (...args: any) => Observable<infer O>\n ? (state: State, payload: O) => State\n : Actions[k] extends PureReducer<infer O>\n ? (state?: State, payload?: O) => State\n : (state?: State) => State\n\n };\n\nexport interface LoadingState<A> {\n code: keyof A;\n payload?: any;\n response?: any;\n error?: any;\n status: boolean;\n}\n\nexport abstract class Store<State, A> {\n\n private readonly actions: Map<string, Subject<{ name: string, payload?: any }>> = new Map();\n\n readonly effects: Effect[];\n\n readonly reducers: Reducer[];\n\n\n private _loading$ = new Subject<LoadingState<A>>();\n\n public get loading$(): Observable<LoadingState<A>> {\n return this._loading$.asObservable();\n }\n\n protected readonly _store$: BehaviorSubject<State> = new BehaviorSubject<State>(\n // we need this in order to decouple initial state between instances\n JSON.parse(JSON.stringify(this.initialState))\n );\n\n public get state$(): Observable<State> {\n return this._store$.asObservable();\n }\n\n constructor(private initialState: State) {\n\n this.effects = getEffects(this);\n this.reducers = getReducers(this);\n\n // @ts-ignore\n const actions = [].concat(this.effects, this.reducers);\n\n const actionsSet = new Set<string>();\n\n actions.forEach(({action}) => actionsSet.add(action));\n\n actionsSet.forEach((key) => {\n\n this.actions.set(key, new Subject());\n\n const effect = this.effects.find((e) => e.action === key);\n\n const operator = Store.getOperator(effect!.strategy);\n\n // @ts-ignore\n this.actions.get(key).pipe(\n withLatestFrom(this._store$),\n tap(([{payload}]) => {\n this._loading$.next({\n payload: payload,\n status: true,\n code: key as keyof A,\n });\n }),\n operator(([{name, payload}]) => {\n\n // @ts-ignore\n return this[name](...payload).pipe(\n tap((response) => {\n this._loading$.next({\n payload,\n response,\n status: false,\n // here the action is done. The action is the only async call\n // we can assume the loading is done as the effect is not async\n code: key as keyof A,\n });\n }),\n // the above is\n map((response) => {\n // by convention reducer name must be\n // on + Action\n const reducerName = `on${(name[0]).toUpperCase()}${(name as string).slice(1)}`;\n\n // @ts-ignore\n return this[reducerName](this._store$.getValue(), response);\n }),\n // error need to be swallowed here too otherwise the observable chain will be broken\n // meaning that once an action goes into an error it cannot be dispatched again\n catchError((error) => this.swallowError(key, error, payload)),\n );\n }),\n // @ts-ignore\n ).subscribe(this._store$);\n });\n }\n\n select<R>(projection: (s: State) => R) {\n return this.state$.pipe(\n map(projection)\n );\n }\n\n getLoadingFor<C extends keyof A>(action: C): Observable<boolean> {\n return this.loading$.pipe(\n filter(({code}) => code === action),\n map(({status}) => status),\n );\n }\n\n dispatch<C extends keyof A>(\n action: C,\n ...payload: getPayloadFromActionType<A, C>\n ) {\n\n if (!this.actions.has(action as string)) {\n throw new Error(`Cannot find action ${String(action)}`);\n }\n\n const name: string = (typeof (this as unknown as A)[action] === 'function' ? action : 'noopEffect') as string;\n\n\n this.actions.get(action as string)?.next({name, payload});\n }\n\n // eslint-disable-next-line @typescript-eslint/member-ordering\n private static noopEffect(payload: any) {\n return of(payload);\n }\n\n // eslint-disable-next-line @typescript-eslint/member-ordering\n private static getOperator(strategyFn: EffectStrategy) {\n if (strategyFn === 'mergeMap') {\n return mergeMap;\n } else if (strategyFn === 'concatMap') {\n return concatMap;\n } else {\n return switchMap;\n }\n }\n\n private swallowError(key: string, error: Error, payload: any) {\n console.log(`Error handling action \"${key}\"`, error);\n\n this._loading$.next({\n payload,\n error: error,\n status: false,\n code: key as keyof A,\n });\n\n return EMPTY;\n }\n\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"capitalise.js","sourceRoot":"","sources":["../../../src/lib/utilities/capitalise.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,UAAU,CAAC,CAAS;IAClC,IAAI,CAAC,CAAC,EAAE,CAAC;QAAC,OAAO,EAAE,CAAC;IAAC,CAAC;IAEtB,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACzC,CAAC","sourcesContent":["export function capitalise(s: string) {\n if (!s) { return ''; }\n\n return s[0].toUpperCase() + s.slice(1);\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"scan-instance.js","sourceRoot":"","sources":["../../../src/lib/utilities/scan-instance.ts"],"names":[],"mappings":"AAEA,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,yBAAyB,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAEhG,MAAM,UAAU,YAAY,CAAmB,QAAW,EAAE,EAA0C;IAEpG,IAAI,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;IAE5C,OAAO,KAAK,EAAE,CAAC;QAEb,MAAM,WAAW,GAAG,MAAM,CAAC,yBAAyB,CAAC,KAAK,CAAC,CAAC;QAE5D,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC;aACrB,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE;YACZ,OAAO,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QACrC,CAAC,CAAC;aACD,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC;QAEpC,IAAI,CAAC;YACH,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QACvC,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,KAAK,GAAG,IAAI,CAAC;QACf,CAAC;IACH,CAAC;AAEH,CAAC","sourcesContent":["import { Class } from 'utility-types';\n\nconst builtInMethods = Object.keys(Object.getOwnPropertyDescriptors(Object.getPrototypeOf({})));\n\nexport function scanInstance<T extends object>(instance: T, cb: (proto: Class<T>, key: string) => void) {\n\n let proto = Object.getPrototypeOf(instance);\n\n while (proto) {\n\n const descriptors = Object.getOwnPropertyDescriptors(proto);\n\n Object.keys(descriptors)\n .filter((k) => {\n return !builtInMethods.includes(k);\n })\n .forEach((key) => cb(proto, key));\n\n try {\n proto = Object.getPrototypeOf(proto);\n } catch (e) {\n proto = null;\n }\n }\n\n}\n"]}