larapi 0.0.7 → 0.0.8
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/Core.d.ts +7 -5
- package/lib/Core.js +35 -14
- package/lib/Http.d.ts +2 -1
- package/lib/Http.js +6 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +2 -2
- package/lib/types.d.ts +6 -1
- package/package.json +1 -1
- package/src/Core.ts +39 -15
- package/src/Http.ts +8 -1
- package/src/index.ts +1 -1
- package/src/types.ts +7 -1
package/lib/Core.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Events, Event } from 'easy-event-emitter';
|
|
2
|
-
import { TData, TGroupsData, TListenerEvents } from './types';
|
|
2
|
+
import { TCacheBody, TData, TGroupsData, TListenerEvents } from './types';
|
|
3
3
|
declare abstract class Core {
|
|
4
4
|
private cache;
|
|
5
5
|
initData: TGroupsData;
|
|
@@ -8,11 +8,13 @@ declare abstract class Core {
|
|
|
8
8
|
token: string;
|
|
9
9
|
constructor(context?: Core);
|
|
10
10
|
addListener(event: TListenerEvents, callback: (data: any) => void): Event;
|
|
11
|
-
protected getCache(
|
|
11
|
+
protected getCache(key: string): TData | undefined;
|
|
12
12
|
setHost(host: string): void;
|
|
13
|
-
protected setCache(
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
protected setCache(key: string, data: TData, group?: string): void;
|
|
14
|
+
clearCacheGroup(group: string): void;
|
|
15
|
+
updateCacheGroup(group: string, fieldKey: string, data: TData): void;
|
|
16
|
+
protected getCacheByIndex(index: string): TCacheBody;
|
|
17
|
+
protected deleteCache(key: string): void;
|
|
16
18
|
setInitData(data: TGroupsData): void;
|
|
17
19
|
}
|
|
18
20
|
export default Core;
|
package/lib/Core.js
CHANGED
|
@@ -3,8 +3,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
//@ts-ignore
|
|
7
|
-
const md5_1 = __importDefault(require("md5"));
|
|
8
6
|
const easy_event_emitter_1 = __importDefault(require("easy-event-emitter"));
|
|
9
7
|
class Core {
|
|
10
8
|
constructor(context) {
|
|
@@ -46,14 +44,13 @@ class Core {
|
|
|
46
44
|
this.setHost = this.setHost.bind(this);
|
|
47
45
|
this.addListener = this.addListener.bind(this);
|
|
48
46
|
this.setInitData = this.setInitData.bind(this);
|
|
49
|
-
this.
|
|
47
|
+
this.clearCacheGroup = this.clearCacheGroup.bind(this);
|
|
50
48
|
Object.assign(this, context);
|
|
51
49
|
}
|
|
52
50
|
addListener(event, callback) {
|
|
53
51
|
return this.events.addListener(event, callback);
|
|
54
52
|
}
|
|
55
|
-
getCache(
|
|
56
|
-
const key = (0, md5_1.default)(request);
|
|
53
|
+
getCache(key) {
|
|
57
54
|
if (this.cache[key]) {
|
|
58
55
|
if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
|
|
59
56
|
return this.cache[key].data;
|
|
@@ -66,29 +63,53 @@ class Core {
|
|
|
66
63
|
setHost(host) {
|
|
67
64
|
this.host = host;
|
|
68
65
|
}
|
|
69
|
-
setCache(
|
|
70
|
-
this.cache[
|
|
66
|
+
setCache(key, data, group) {
|
|
67
|
+
this.cache[key] = {
|
|
71
68
|
time: (new Date()).getTime(),
|
|
72
69
|
data: data,
|
|
73
70
|
group: group
|
|
74
71
|
};
|
|
75
72
|
}
|
|
76
|
-
|
|
73
|
+
clearCacheGroup(group) {
|
|
77
74
|
for (const key in this.cache) {
|
|
78
75
|
if (this.cache[key].group === group) {
|
|
79
76
|
delete this.cache[key];
|
|
80
77
|
}
|
|
81
78
|
}
|
|
82
79
|
}
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
80
|
+
updateCacheGroup(group, fieldKey, data) {
|
|
81
|
+
const update = (cacheData) => {
|
|
82
|
+
if (cacheData[fieldKey] === data[fieldKey]) {
|
|
83
|
+
for (const fieldKey in cacheData) {
|
|
84
|
+
if (fieldKey in data) {
|
|
85
|
+
cacheData[fieldKey] = data[fieldKey];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return cacheData;
|
|
90
|
+
};
|
|
91
|
+
for (const key in this.cache) {
|
|
92
|
+
if (this.cache[key].group === group) {
|
|
93
|
+
if (Array.isArray(this.cache[key].data)) {
|
|
94
|
+
this.cache[key].data = Object.values(this.cache[key].data).map(update);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
this.cache[key].data = update(this.cache[key].data);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
getCacheByIndex(index) {
|
|
103
|
+
return this.cache[index];
|
|
104
|
+
}
|
|
105
|
+
deleteCache(key) {
|
|
106
|
+
if (!this.cache[key])
|
|
86
107
|
return;
|
|
87
|
-
const group = this.cache[
|
|
108
|
+
const group = this.cache[key].group;
|
|
88
109
|
if (group) {
|
|
89
|
-
this.
|
|
110
|
+
this.clearCacheGroup(group);
|
|
90
111
|
}
|
|
91
|
-
delete this.cache[
|
|
112
|
+
delete this.cache[key];
|
|
92
113
|
}
|
|
93
114
|
setInitData(data) {
|
|
94
115
|
this.initData = data;
|
package/lib/Http.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Core from "./Core";
|
|
2
|
-
import { TMethod, TParams } from "./types";
|
|
2
|
+
import { TMethod, TParams, TData } from "./types";
|
|
3
3
|
declare class Http extends Core {
|
|
4
4
|
private cacheIndex;
|
|
5
5
|
method: TMethod;
|
|
@@ -12,6 +12,7 @@ declare class Http extends Core {
|
|
|
12
12
|
private encodeUrlParams;
|
|
13
13
|
private cuteUndifinedParams;
|
|
14
14
|
request(): this;
|
|
15
|
+
updateCache(fieldKey: string, data: TData): void;
|
|
15
16
|
clearCache(): void;
|
|
16
17
|
}
|
|
17
18
|
export default Http;
|
package/lib/Http.js
CHANGED
|
@@ -151,6 +151,12 @@ class Http extends Core_1.default {
|
|
|
151
151
|
}
|
|
152
152
|
return this;
|
|
153
153
|
}
|
|
154
|
+
updateCache(fieldKey, data) {
|
|
155
|
+
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
156
|
+
if (cache && cache.group) {
|
|
157
|
+
this.updateCacheGroup(cache.group, fieldKey, data);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
154
160
|
clearCache() {
|
|
155
161
|
this.deleteCache(this.cacheIndex);
|
|
156
162
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -34,7 +34,7 @@ declare class API extends Core {
|
|
|
34
34
|
static setToken: (token: string) => void;
|
|
35
35
|
static addListener: (event: import("./types").TListenerEvents, callback: (data: any) => void) => import("easy-event-emitter").Event;
|
|
36
36
|
static setInitData: (data: import("./types").TGroupsData) => void;
|
|
37
|
-
static
|
|
37
|
+
static clearCacheGroup: (group: string) => void;
|
|
38
38
|
static getToken: () => string;
|
|
39
39
|
static getUid: () => number;
|
|
40
40
|
static getUser: () => TUser;
|
package/lib/index.js
CHANGED
|
@@ -162,11 +162,11 @@ Object.defineProperty(API, "setInitData", {
|
|
|
162
162
|
writable: true,
|
|
163
163
|
value: _a.instance.setInitData
|
|
164
164
|
});
|
|
165
|
-
Object.defineProperty(API, "
|
|
165
|
+
Object.defineProperty(API, "clearCacheGroup", {
|
|
166
166
|
enumerable: true,
|
|
167
167
|
configurable: true,
|
|
168
168
|
writable: true,
|
|
169
|
-
value: _a.instance.
|
|
169
|
+
value: _a.instance.clearCacheGroup
|
|
170
170
|
});
|
|
171
171
|
Object.defineProperty(API, "getToken", {
|
|
172
172
|
enumerable: true,
|
package/lib/types.d.ts
CHANGED
package/package.json
CHANGED
package/src/Core.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
//@ts-ignore
|
|
2
|
-
import md5 from 'md5';
|
|
3
1
|
import EventEmitter,{
|
|
4
2
|
Events,
|
|
5
3
|
Event
|
|
6
4
|
} from 'easy-event-emitter';
|
|
7
|
-
// import ApiRequest from './ApiRequest';
|
|
8
5
|
import {
|
|
9
6
|
TCache,
|
|
7
|
+
TCacheBody,
|
|
10
8
|
TData,
|
|
11
9
|
TGroupsData,
|
|
12
10
|
TListenerEvents
|
|
@@ -29,7 +27,7 @@ abstract class Core {
|
|
|
29
27
|
this.setHost = this.setHost.bind(this);
|
|
30
28
|
this.addListener = this.addListener.bind(this);
|
|
31
29
|
this.setInitData = this.setInitData.bind(this);
|
|
32
|
-
this.
|
|
30
|
+
this.clearCacheGroup = this.clearCacheGroup.bind(this);
|
|
33
31
|
Object.assign(this, context);
|
|
34
32
|
}
|
|
35
33
|
|
|
@@ -37,8 +35,7 @@ abstract class Core {
|
|
|
37
35
|
return this.events.addListener(event, callback);
|
|
38
36
|
}
|
|
39
37
|
|
|
40
|
-
protected getCache(
|
|
41
|
-
const key = md5(request);
|
|
38
|
+
protected getCache(key: string): TData | undefined {
|
|
42
39
|
if (this.cache[key]) {
|
|
43
40
|
if ((new Date()).getTime() <= (this.cache[key].time + 3600000000)) {
|
|
44
41
|
return this.cache[key].data;
|
|
@@ -52,15 +49,15 @@ abstract class Core {
|
|
|
52
49
|
this.host = host;
|
|
53
50
|
}
|
|
54
51
|
|
|
55
|
-
protected setCache(
|
|
56
|
-
this.cache[
|
|
52
|
+
protected setCache(key: string, data: TData, group?: string): void {
|
|
53
|
+
this.cache[key] = {
|
|
57
54
|
time: (new Date()).getTime(),
|
|
58
55
|
data: data,
|
|
59
56
|
group: group
|
|
60
57
|
};
|
|
61
58
|
}
|
|
62
59
|
|
|
63
|
-
public
|
|
60
|
+
public clearCacheGroup(group: string): void {
|
|
64
61
|
for(const key in this.cache) {
|
|
65
62
|
if (this.cache[key].group === group) {
|
|
66
63
|
delete this.cache[key];
|
|
@@ -68,16 +65,43 @@ abstract class Core {
|
|
|
68
65
|
}
|
|
69
66
|
}
|
|
70
67
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
68
|
+
public updateCacheGroup(group: string, fieldKey: string, data: TData): void {
|
|
69
|
+
|
|
70
|
+
const update = (cacheData: any) => {
|
|
71
|
+
if (cacheData[fieldKey] === data[fieldKey]) {
|
|
72
|
+
for(const fieldKey in cacheData) {
|
|
73
|
+
if (fieldKey in data) {
|
|
74
|
+
cacheData[fieldKey] = data[fieldKey];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return cacheData;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
for(const key in this.cache) {
|
|
82
|
+
if (this.cache[key].group === group) {
|
|
83
|
+
if (Array.isArray(this.cache[key].data)) {
|
|
84
|
+
this.cache[key].data = Object.values(this.cache[key].data).map(update);
|
|
85
|
+
}else{
|
|
86
|
+
this.cache[key].data = update(this.cache[key].data);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
protected getCacheByIndex(index: string): TCacheBody {
|
|
93
|
+
return this.cache[index];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
protected deleteCache(key: string): void {
|
|
97
|
+
if (!this.cache[key])
|
|
74
98
|
return;
|
|
75
99
|
|
|
76
|
-
const group = this.cache[
|
|
100
|
+
const group = this.cache[key].group;
|
|
77
101
|
if (group) {
|
|
78
|
-
this.
|
|
102
|
+
this.clearCacheGroup(group);
|
|
79
103
|
}
|
|
80
|
-
delete this.cache[
|
|
104
|
+
delete this.cache[key];
|
|
81
105
|
}
|
|
82
106
|
|
|
83
107
|
public setInitData(data: TGroupsData): void {
|
package/src/Http.ts
CHANGED
|
@@ -160,7 +160,14 @@ class Http extends Core {
|
|
|
160
160
|
return this
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
public
|
|
163
|
+
public updateCache(fieldKey: string, data: TData): void {
|
|
164
|
+
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
165
|
+
if (cache && cache.group) {
|
|
166
|
+
this.updateCacheGroup(cache.group, fieldKey, data);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
public clearCache(): void {
|
|
164
171
|
this.deleteCache(this.cacheIndex);
|
|
165
172
|
}
|
|
166
173
|
}
|
package/src/index.ts
CHANGED
|
@@ -116,7 +116,7 @@ class API extends Core {
|
|
|
116
116
|
public static setToken = this.instance.setToken;
|
|
117
117
|
public static addListener = this.instance.addListener;
|
|
118
118
|
public static setInitData = this.instance.setInitData;
|
|
119
|
-
public static
|
|
119
|
+
public static clearCacheGroup = this.instance.clearCacheGroup;
|
|
120
120
|
public static getToken = this.instance.getToken;
|
|
121
121
|
public static getUid = this.instance.getUid;
|
|
122
122
|
public static getUser = this.instance.getUser;
|