-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny-ng-store.umd.js
170 lines (170 loc) · 7.15 KB
/
tiny-ng-store.umd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "@angular/core", "rxjs/Observable", "rxjs/Subject", "rxjs/BehaviorSubject", "rxjs/add/operator/scan", "rxjs/add/operator/map", "rxjs/add/operator/take", "rxjs/add/operator/distinctUntilChanged"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var Observable_1 = require("rxjs/Observable");
var Subject_1 = require("rxjs/Subject");
var BehaviorSubject_1 = require("rxjs/BehaviorSubject");
require("rxjs/add/operator/scan");
require("rxjs/add/operator/map");
require("rxjs/add/operator/take");
require("rxjs/add/operator/distinctUntilChanged");
/**
* A type of Observable<T> representing the store Observable
* @export
* @class TnsState
* @extends {Observable<T>}
* @template T
*/
var TnsState = (function (_super) {
__extends(TnsState, _super);
function TnsState() {
return _super !== null && _super.apply(this, arguments) || this;
}
return TnsState;
}(Observable_1.Observable));
exports.TnsState = TnsState;
/**
* This class represents and data added into the store
* Name your store item, and provide data
* @export
* @class StoreItem
* @property {string} name - This is the name of the data stored
* @property {any} data - This is the data stored under the name provided
*/
var StoreItem = (function () {
function StoreItem() {
}
return StoreItem;
}());
exports.StoreItem = StoreItem;
var AddItem = (function () {
function AddItem(storeItem) {
this.storeItem = storeItem;
}
return AddItem;
}());
var RemoveItem = (function () {
function RemoveItem(storeItem) {
this.storeItem = storeItem;
}
return RemoveItem;
}());
var UpdateItem = (function () {
function UpdateItem(storeItem) {
this.storeItem = storeItem;
}
return UpdateItem;
}());
/**
* Class representing the data store
* Creates an instance of TinyNgStore.
* @export
* @class TinyNgStore
*/
var TinyNgStore = (function () {
function TinyNgStore() {
this.dispatcher = new Subject_1.Subject();
this.state = this.storeInit([], this.dispatcher);
}
/**
* Insert an item into the data store
* This will return TnsState<StoreItem> representing that object inserted
* @param {StoreItem} storeItem - StoreItem class representing your data
* @returns {TnsState<StoreItem>}
* @memberOf TinyNgStore
*/
TinyNgStore.prototype.InsertItem = function (storeItem) {
this.dispatcher.next(new AddItem(storeItem));
return this.GetItem(storeItem.name);
};
/**
* Delete an item from the data store
* @param {string} name - This is the name of the data stored
* @memberOf TinyNgStore
*/
TinyNgStore.prototype.DeleteItem = function (name) {
this.dispatcher.next(new RemoveItem({ name: name }));
};
/**
* Update and item within the data store
* If the item does not exist, nothing will be created
* @param {StoreItem} storeItem - StoreItem class representing your data
* @memberOf TinyNgStore
*/
TinyNgStore.prototype.UpdateItem = function (storeItem) {
this.dispatcher.next(new UpdateItem(storeItem));
};
/**
* Retrieve an item from the data store
* This will return TnsState<StoreItem> representing that object
* @param {string} name - This is the name of the data stored
* @returns {TnsState<StoreItem>}
* @memberOf TinyNgStore
*/
TinyNgStore.prototype.GetItem = function (name) {
return this.state.map(function (s) { return s.filter(function (si) { return si.name === name; })[0]; }).distinctUntilChanged();
};
TinyNgStore.prototype.storeInit = function (initState, actions) {
var behavior = new BehaviorSubject_1.BehaviorSubject(initState);
this.store(initState, actions).subscribe(function (s) { return behavior.next(s); });
return behavior;
};
TinyNgStore.prototype.store = function (initState, actions) {
var _this = this;
return actions.scan(function (state, action) {
switch (action.constructor) {
case AddItem:
var filteredStore = state.filter(function (s) { return action.storeItem.name !== s.name; });
return filteredStore.concat([action.storeItem]);
case RemoveItem:
return state.filter(function (s) { return s.name !== action.storeItem.name; });
case UpdateItem:
return state.map(function (s) { return s.name !== action.storeItem.name ? s : _this.updateItem(action.storeItem); });
default:
return state;
}
/* tslint:disable */
}, initState);
/* tslint:enable */
};
TinyNgStore.prototype.updateItem = function (item) {
var updatedItem = {};
Object.keys(item).map(function (key) { return updatedItem[key] = item[key]; });
return updatedItem;
};
return TinyNgStore;
}());
TinyNgStore = __decorate([
core_1.Injectable(),
__metadata("design:paramtypes", [])
], TinyNgStore);
exports.TinyNgStore = TinyNgStore;
});