@reactables/examples 0.4.0-alpha.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 ADDED
@@ -0,0 +1,7 @@
1
+ # Reactables Examples
2
+
3
+ ## Description
4
+
5
+ Three examples using [Reactables Core](https://github.com/reactables/reactables/tree/main/packages/core)
6
+
7
+ See [Reactables Examples](https://github.com/reactables/reactables/tree/main/packages/core#examples)
@@ -0,0 +1,2 @@
1
+ import { Observable } from 'rxjs';
2
+ export type ObservableOrPromise<T> = Observable<T> | Promise<T>;
@@ -0,0 +1,10 @@
1
+ import { Reactable } from '@reactables/core';
2
+ interface CounterState {
3
+ count: number;
4
+ }
5
+ type CounterActions = {
6
+ increment: () => void;
7
+ reset: () => void;
8
+ };
9
+ export declare const RxCounter: () => Reactable<CounterState, CounterActions>;
10
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export { RxCounter } from './RxCounter';
@@ -0,0 +1,9 @@
1
+ export declare enum EventTypes {
2
+ ChiliCookOff = "Chili Cook Off",
3
+ GrammarRodeo = "Grammar Rodeo",
4
+ ItchyAndScratchyMovie = "Itchy And Scratchy Movie"
5
+ }
6
+ export interface FetchPricePayload {
7
+ event: EventTypes;
8
+ qty: number;
9
+ }
@@ -0,0 +1,19 @@
1
+ import { Reactable } from '@reactables/core';
2
+ import { EventTypes, FetchPricePayload } from './Models/EventTypes';
3
+ import { ObservableOrPromise } from '../Models/ObservableOrPromise';
4
+ export interface ControlState {
5
+ selectedEvent: EventTypes;
6
+ qty: number;
7
+ }
8
+ export interface EventTicketsState {
9
+ controls: ControlState;
10
+ calculating: boolean;
11
+ price: number | null;
12
+ }
13
+ export declare const initialState: EventTicketsState;
14
+ type EventTicketsActions = {
15
+ selectEvent: (event: EventTypes) => void;
16
+ setQty: (qty: number) => void;
17
+ };
18
+ export declare const RxEventTickets: (getPriceApi: (payload: FetchPricePayload) => ObservableOrPromise<number>) => Reactable<EventTicketsState, EventTicketsActions>;
19
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export { RxEventTickets, EventTicketsState } from './RxEventTickets';
2
+ export { EventTypes, FetchPricePayload } from './Models/EventTypes';
@@ -0,0 +1,12 @@
1
+ export type TodoStatus = 'done' | 'incomplete' | 'in progress';
2
+ export interface Todo {
3
+ id: number;
4
+ description: string;
5
+ status: TodoStatus;
6
+ updating: boolean;
7
+ }
8
+ export type UpdateTodoPayload = {
9
+ todoId: number;
10
+ status: TodoStatus;
11
+ };
12
+ export type UpdateTodoPayloadSuccess = UpdateTodoPayload;
@@ -0,0 +1,12 @@
1
+ import { Reactable } from '@reactables/core';
2
+ import { UpdateTodoPayload, UpdateTodoPayloadSuccess, Todo } from './Models/Todos';
3
+ import { ObservableOrPromise } from '../Models/ObservableOrPromise';
4
+ interface TodoUpdatesState {
5
+ todos: Todo[];
6
+ }
7
+ export declare const initialState: TodoUpdatesState;
8
+ type TodoUpdatesActions = {
9
+ sendTodoStatusUpdate: (payload: UpdateTodoPayload) => void;
10
+ };
11
+ export declare const RxTodoUpdates: (updateTodoApi: (payload: UpdateTodoPayload) => ObservableOrPromise<UpdateTodoPayloadSuccess>) => Reactable<TodoUpdatesState, TodoUpdatesActions>;
12
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export { RxTodoUpdates } from './RxTodoUpdates';
2
+ export { TodoStatus, Todo, UpdateTodoPayload } from './Models/Todos';
@@ -0,0 +1,3 @@
1
+ export * from './RxCounter';
2
+ export * from './RxTodoUpdates';
3
+ export * from './RxEventTickets';
package/dist/index.js ADDED
@@ -0,0 +1,175 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var core = require('@reactables/core');
6
+ var operators = require('rxjs/operators');
7
+
8
+ var RxCounter = function () {
9
+ return core.RxBuilder({
10
+ initialState: { count: 0 },
11
+ reducers: {
12
+ increment: function (state) { return ({ count: state.count + 1 }); },
13
+ reset: function () { return ({ count: 0 }); }
14
+ }
15
+ });
16
+ };
17
+
18
+ /******************************************************************************
19
+ Copyright (c) Microsoft Corporation.
20
+
21
+ Permission to use, copy, modify, and/or distribute this software for any
22
+ purpose with or without fee is hereby granted.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
25
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
26
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
27
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
28
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
29
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
30
+ PERFORMANCE OF THIS SOFTWARE.
31
+ ***************************************************************************** */
32
+
33
+ var __assign = function() {
34
+ __assign = Object.assign || function __assign(t) {
35
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
36
+ s = arguments[i];
37
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
38
+ }
39
+ return t;
40
+ };
41
+ return __assign.apply(this, arguments);
42
+ };
43
+
44
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
45
+ var e = new Error(message);
46
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
47
+ };
48
+
49
+ var initialState$1 = {
50
+ todos: [
51
+ {
52
+ id: 1,
53
+ description: 'Pick Up Bart',
54
+ status: 'incomplete',
55
+ updating: false
56
+ },
57
+ {
58
+ id: 2,
59
+ description: 'Moe the lawn',
60
+ status: 'incomplete',
61
+ updating: false
62
+ },
63
+ ]
64
+ };
65
+ var RxTodoUpdates = function (updateTodoApi) {
66
+ return core.RxBuilder({
67
+ initialState: initialState$1,
68
+ reducers: {
69
+ sendTodoStatusUpdate: {
70
+ reducer: function (state, _a) {
71
+ var todoId = _a.payload.todoId;
72
+ return ({
73
+ todos: state.todos.reduce(function (acc, todo) { return acc.concat(todo.id === todoId ? __assign(__assign({}, todo), { updating: true }) : todo); }, [])
74
+ });
75
+ },
76
+ effects: function (payload) { return ({
77
+ key: payload.todoId,
78
+ effects: [
79
+ function (actions$) {
80
+ return actions$.pipe(
81
+ // Call todo API Service - switchMap operator cancels previous pending call if a new one is initiated
82
+ operators.switchMap(function (_a) {
83
+ var payload = _a.payload;
84
+ return updateTodoApi(payload);
85
+ }),
86
+ // Map success response to appropriate action
87
+ operators.map(function (payload) { return ({ type: 'todoStatusUpdateSuccess', payload: payload }); }));
88
+ },
89
+ ]
90
+ }); }
91
+ },
92
+ todoStatusUpdateSuccess: function (state, _a) {
93
+ var _b = _a.payload, todoId = _b.todoId, status = _b.status;
94
+ return ({
95
+ todos: state.todos.reduce(function (acc, todo) {
96
+ return acc.concat(todo.id === todoId ? __assign(__assign({}, todo), { status: status, updating: false }) : todo);
97
+ }, [])
98
+ });
99
+ }
100
+ }
101
+ });
102
+ };
103
+
104
+ exports.EventTypes = void 0;
105
+ (function (EventTypes) {
106
+ EventTypes["ChiliCookOff"] = "Chili Cook Off";
107
+ EventTypes["GrammarRodeo"] = "Grammar Rodeo";
108
+ EventTypes["ItchyAndScratchyMovie"] = "Itchy And Scratchy Movie";
109
+ })(exports.EventTypes || (exports.EventTypes = {}));
110
+
111
+ var initialState = {
112
+ controls: null,
113
+ calculating: false,
114
+ price: null
115
+ };
116
+ var RxEventTickets = function (getPriceApi) {
117
+ // Create a reactable for the controls
118
+ var rxControls = core.RxBuilder({
119
+ initialState: {
120
+ selectedEvent: exports.EventTypes.ChiliCookOff,
121
+ qty: 0
122
+ },
123
+ reducers: {
124
+ selectEvent: function (state, _a) {
125
+ var payload = _a.payload;
126
+ return (__assign(__assign({}, state), { selectedEvent: payload }));
127
+ },
128
+ setQty: function (state, _a) {
129
+ var payload = _a.payload;
130
+ return (__assign(__assign({}, state), { qty: payload }));
131
+ }
132
+ }
133
+ });
134
+ // Create reactable for combining controls and price info.
135
+ var state$ = core.RxBuilder({
136
+ // Add control changes as a source for second reactable
137
+ sources: [
138
+ rxControls.state$.pipe(operators.map(function (payload) { return ({ type: 'controlChange', payload: payload }); })),
139
+ ],
140
+ initialState: initialState,
141
+ reducers: {
142
+ controlChange: {
143
+ reducer: function (state, _a) {
144
+ var payload = _a.payload;
145
+ return (__assign(__assign({}, state), { controls: payload, calculating: true }));
146
+ },
147
+ effects: function () { return ({
148
+ // Add effect for fetching price on controlChange
149
+ effects: [
150
+ function (actions$) {
151
+ return actions$.pipe(operators.switchMap(function (_a) {
152
+ var _b = _a.payload, event = _b.selectedEvent, qty = _b.qty;
153
+ return getPriceApi({ event: event, qty: qty });
154
+ }),
155
+ // Map success response to success action
156
+ operators.map(function (price) { return ({ type: 'fetchPriceSuccess', payload: price }); }));
157
+ },
158
+ ]
159
+ }); }
160
+ },
161
+ fetchPriceSuccess: function (state, _a) {
162
+ var payload = _a.payload;
163
+ return (__assign(__assign({}, state), { calculating: false, price: payload }));
164
+ }
165
+ }
166
+ }).state$;
167
+ return {
168
+ state$: state$,
169
+ actions: rxControls.actions
170
+ };
171
+ };
172
+
173
+ exports.RxCounter = RxCounter;
174
+ exports.RxEventTickets = RxEventTickets;
175
+ exports.RxTodoUpdates = RxTodoUpdates;
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@reactables/examples",
3
+ "description": "Examples with reactables",
4
+ "main": "dist/index.js",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "scripts": {
9
+ "test": "jest",
10
+ "build": "rimraf dist && rollup --config",
11
+ "lint": "eslint --max-warnings 0 \"src/**/*.ts*\" && prettier --check src/",
12
+ "fix": "eslint --fix \"src/**/*.ts*\" && prettier --write \"src/**/*.(ts*|scss)\""
13
+ },
14
+ "author": "David Lai",
15
+ "license": "ISC",
16
+ "dependencies": {
17
+ "@reactables/core": "^0.4.0-alpha.1"
18
+ },
19
+ "peerDependencies": {
20
+ "rxjs": "^6.0.0 || ^7.0.0"
21
+ },
22
+ "version": "0.4.0-alpha.1"
23
+ }