1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const BB = require("botbuilder");
|
9 | const CLDebug_1 = require("./CLDebug");
|
10 | const BotMemory_1 = require("./Memory/BotMemory");
|
11 | const BotState_1 = require("./Memory/BotState");
|
12 | class CLMemory {
|
13 | constructor(userkey) {
|
14 | this.memCache = {};
|
15 | this.userkey = userkey;
|
16 | }
|
17 | static Init(memoryStorage) {
|
18 | CLMemory.memoryStorage = memoryStorage;
|
19 |
|
20 | if (!memoryStorage) {
|
21 | CLDebug_1.CLDebug.Log('Storage not defined. Defaulting to in-memory storage.');
|
22 | CLMemory.memoryStorage = new BB.MemoryStorage();
|
23 | }
|
24 | }
|
25 | static GetMemory(key) {
|
26 | return new CLMemory(key);
|
27 | }
|
28 |
|
29 | static InitMemory(turnContext) {
|
30 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
31 | const user = turnContext.activity.from;
|
32 | const conversationReference = BB.TurnContext.getConversationReference(turnContext.activity);
|
33 | if (!user) {
|
34 | throw new Error(`Attempted to initialize memory, but cannot get memory key because current request did not have 'from'/user specified`);
|
35 | }
|
36 | if (!user.id) {
|
37 | throw new Error(`Attempted to initialize memory, but user.id was not provided which is required for use as memory key.`);
|
38 | }
|
39 | let memory = new CLMemory(user.id);
|
40 | memory.turnContext = turnContext;
|
41 | yield memory.BotState.SetConversationReference(conversationReference);
|
42 | return memory;
|
43 | });
|
44 | }
|
45 | Key(datakey) {
|
46 | return `${this.userkey}_${datakey}`;
|
47 | }
|
48 | GetAsync(datakey) {
|
49 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
50 | if (!CLMemory.memoryStorage) {
|
51 | throw new Error('Memory storage not found');
|
52 | }
|
53 | let key = this.Key(datakey);
|
54 | let cacheData = this.memCache[key];
|
55 | if (cacheData) {
|
56 | CLDebug_1.CLDebug.Log(`-< ${key} : ${cacheData}`, CLDebug_1.DebugType.MemVerbose);
|
57 | return cacheData;
|
58 | }
|
59 | else {
|
60 | try {
|
61 | let data = yield CLMemory.memoryStorage.read([key]);
|
62 | if (data[key]) {
|
63 | this.memCache[key] = data[key].value;
|
64 | }
|
65 | else {
|
66 | this.memCache[key] = null;
|
67 | }
|
68 | CLDebug_1.CLDebug.Log(`R< ${key} : ${this.memCache[key]}`, CLDebug_1.DebugType.Memory);
|
69 | return this.memCache[key];
|
70 | }
|
71 | catch (err) {
|
72 | CLDebug_1.CLDebug.Error(err);
|
73 | return null;
|
74 | }
|
75 | }
|
76 | });
|
77 | }
|
78 | SetAsync(datakey, jsonString) {
|
79 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
80 | if (!CLMemory.memoryStorage) {
|
81 | throw new Error('Memory storage not found');
|
82 | }
|
83 | if (jsonString == "null") {
|
84 | yield this.DeleteAsync(datakey);
|
85 | return;
|
86 | }
|
87 | let key = this.Key(datakey);
|
88 | try {
|
89 |
|
90 | let cacheData = this.memCache[key];
|
91 | if (cacheData == jsonString) {
|
92 | CLDebug_1.CLDebug.Log(`-> ${key} : ${jsonString}`, CLDebug_1.DebugType.MemVerbose);
|
93 | }
|
94 | else {
|
95 |
|
96 | yield CLMemory.memoryStorage.write({ [key]: { value: jsonString, eTag: '*' } });
|
97 | this.memCache[key] = jsonString;
|
98 | CLDebug_1.CLDebug.Log(`W> ${key} : ${jsonString}`, CLDebug_1.DebugType.Memory);
|
99 | }
|
100 | }
|
101 | catch (err) {
|
102 | CLDebug_1.CLDebug.Error(err);
|
103 | }
|
104 | });
|
105 | }
|
106 | DeleteAsync(datakey) {
|
107 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
108 | let key = this.Key(datakey);
|
109 | try {
|
110 |
|
111 | if (!CLMemory.memoryStorage) {
|
112 | CLDebug_1.CLDebug.Error(`You attempted to delete key: ${key} before memoryStorage was defined`);
|
113 | }
|
114 | else {
|
115 | CLMemory.memoryStorage.delete([key]);
|
116 | this.memCache[key] = null;
|
117 | CLDebug_1.CLDebug.Log(`D> ${key} : -----`, CLDebug_1.DebugType.Memory);
|
118 | }
|
119 | }
|
120 | catch (err) {
|
121 | CLDebug_1.CLDebug.Error(err);
|
122 | }
|
123 | });
|
124 | }
|
125 | SetAppAsync(app) {
|
126 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
127 | const curApp = yield this.BotState.GetApp();
|
128 | yield this.BotState._SetAppAsync(app);
|
129 | if (!app || !curApp || curApp.appId !== app.appId) {
|
130 | yield this.BotMemory.ClearAsync();
|
131 | }
|
132 | });
|
133 | }
|
134 | get BotMemory() {
|
135 | return BotMemory_1.BotMemory.Get(this);
|
136 | }
|
137 | get BotState() {
|
138 | return BotState_1.BotState.Get(this);
|
139 | }
|
140 | get TurnContext() {
|
141 | return this.turnContext;
|
142 | }
|
143 | }
|
144 | CLMemory.memoryStorage = null;
|
145 | exports.CLMemory = CLMemory;
|
146 |
|
\ | No newline at end of file |