onecore 0.0.38 → 0.0.39

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/lib/index.js CHANGED
@@ -105,3 +105,43 @@ exports.DefaultGenericService = GenericManager;
105
105
  exports.GenericSearchManager = Manager;
106
106
  exports.DefaultService = GenericManager;
107
107
  exports.DefaultGenericSearchService = GenericManager;
108
+ var SavedService = (function () {
109
+ function SavedService(repository, query, max) {
110
+ this.repository = repository;
111
+ this.query = query;
112
+ this.max = max;
113
+ this.load = this.load.bind(this);
114
+ this.save = this.save.bind(this);
115
+ }
116
+ SavedService.prototype.load = function (id) {
117
+ var _this = this;
118
+ return this.repository.load(id).then(function (items) {
119
+ if (!items || items.length === 0) {
120
+ return [];
121
+ }
122
+ return _this.query(items);
123
+ });
124
+ };
125
+ SavedService.prototype.save = function (id, itemId) {
126
+ var _this = this;
127
+ return this.repository.load(id).then(function (items) {
128
+ if (items == null) {
129
+ return _this.repository.insert(id, [itemId]);
130
+ }
131
+ else {
132
+ if (items.includes(itemId)) {
133
+ return Promise.resolve(0);
134
+ }
135
+ else {
136
+ items.push(itemId);
137
+ if (items.length > _this.max) {
138
+ items.shift();
139
+ }
140
+ return _this.repository.update(id, items);
141
+ }
142
+ }
143
+ });
144
+ };
145
+ return SavedService;
146
+ }());
147
+ exports.SavedService = SavedService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onecore",
3
- "version": "0.0.38",
3
+ "version": "0.0.39",
4
4
  "description": "onecore",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -140,3 +140,40 @@ export const DefaultGenericService = GenericManager;
140
140
  export const GenericSearchManager = Manager;
141
141
  export const DefaultService = GenericManager;
142
142
  export const DefaultGenericSearchService = GenericManager;
143
+ export interface SavedRepository<ID> {
144
+ load(id: ID): Promise<string[]|null>;
145
+ insert(id: ID, arr: string[]): Promise<number>;
146
+ update(id: ID, arr: string[]): Promise<number>;
147
+ }
148
+ // tslint:disable-next-line:max-classes-per-file
149
+ export class SavedService<ID, T> {
150
+ constructor(protected repository: SavedRepository<ID>, protected query: (ids: string[]) => Promise<T[]>, public max: number) {
151
+ this.load = this.load.bind(this);
152
+ this.save = this.save.bind(this);
153
+ }
154
+ load(id: ID): Promise<T[]> {
155
+ return this.repository.load(id).then(items => {
156
+ if (!items || items.length === 0) {
157
+ return [];
158
+ }
159
+ return this.query(items);
160
+ });
161
+ }
162
+ save(id: ID, itemId: string): Promise<number> {
163
+ return this.repository.load(id).then(items => {
164
+ if (items == null) {
165
+ return this.repository.insert(id, [itemId]);
166
+ } else {
167
+ if (items.includes(itemId)) {
168
+ return Promise.resolve(0);
169
+ } else {
170
+ items.push(itemId);
171
+ if (items.length > this.max) {
172
+ items.shift();
173
+ }
174
+ return this.repository.update(id, items);
175
+ }
176
+ }
177
+ });
178
+ }
179
+ }
package/tsconfig.json CHANGED
@@ -14,7 +14,8 @@
14
14
  "removeComments": true,
15
15
  "lib": [
16
16
  "es5",
17
- "es6"
17
+ "es6",
18
+ "es7"
18
19
  ]
19
20
  },
20
21
  "include": [