Comparing version 1.0.153 to 1.0.154
@@ -32,1 +32,9 @@ import { Delta } from "ag-utilities/AgDiff"; | ||
}; | ||
export type TmPhrase = { | ||
_id: number; | ||
value: any; | ||
index: number; | ||
start: Date; | ||
end: Date; | ||
deltas: Omit<TmDelta, 'snapshot'>[]; | ||
}; |
import type { Db } from "mongodb"; | ||
import { AgTmObject } from "./AgTmObject"; | ||
import { TmDelta, TmFrequence } from "./AgDataTypes"; | ||
import { TmDelta, TmFrequence, TmPhrase } from "./AgDataTypes"; | ||
export type ClientTmApi<T> = Omit<AgTmObject<T>, 'bind' | 'unbind' | 'loadLatest'>; | ||
@@ -14,3 +14,3 @@ export type TimeMachine<T> = { | ||
id: string; | ||
deltas?: TmDelta[]; | ||
phrases?: TmPhrase[]; | ||
frequence?: TmFrequence; | ||
@@ -17,0 +17,0 @@ lifetime?: [Date, Date]; |
@@ -1,2 +0,2 @@ | ||
import { TmFrequence, TmDelta, Ided } from "./AgDataTypes"; | ||
import { TmFrequence, TmDelta, Ided, TmPhrase } from "./AgDataTypes"; | ||
import { Collection, Db } from "mongodb"; | ||
@@ -12,3 +12,3 @@ import { Delta } from "ag-utilities/AgDiff"; | ||
hotData(): { | ||
deltas: TmDelta[]; | ||
phrases: TmPhrase[]; | ||
frequence: TmFrequence; | ||
@@ -18,3 +18,3 @@ lifetime: [Date, Date]; | ||
current(): any; | ||
recent_deltas(): TmDelta[]; | ||
recent_deltas(): TmPhrase[]; | ||
recent_frequence(): TmFrequence; | ||
@@ -26,3 +26,3 @@ lifetime(): [Date, Date]; | ||
constructor(db: Promise<Db>, id: string); | ||
private _recent_deltas; | ||
private _recent_phrases; | ||
private _recent_frequence; | ||
@@ -29,0 +29,0 @@ private _lifetime; |
@@ -38,2 +38,13 @@ "use strict"; | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -57,3 +68,3 @@ exports.AgTmObject = void 0; | ||
this._current = (0, AgVueWrap_1.ref)(undefined); | ||
this._recent_deltas = (0, AgVueWrap_1.ref)([]); | ||
this._recent_phrases = (0, AgVueWrap_1.ref)([]); | ||
this._recent_frequence = (0, AgVueWrap_1.ref)({ | ||
@@ -112,3 +123,3 @@ time: new Date(), | ||
return { | ||
deltas: this._recent_deltas.value, | ||
phrases: this._recent_phrases.value, | ||
frequence: this._recent_frequence.value, | ||
@@ -122,3 +133,3 @@ lifetime: this._lifetime.value | ||
AgTmObject.prototype.recent_deltas = function () { | ||
return this._recent_deltas.value; | ||
return this._recent_phrases.value; | ||
}; | ||
@@ -155,2 +166,6 @@ AgTmObject.prototype.recent_frequence = function () { | ||
record = { _id: AgHelper_1.AgHelper.uuid(), time: time, delta: delta }; | ||
if (!this.lastSnapshotTime || (time.getTime() - this.lastSnapshotTime.getTime()) > snapshot_interval * 1000) { | ||
record.snapshot = { value: (0, AgLodashWrap_1.cloneDeep)(this._current.value) }; | ||
this.lastSnapshotTime = time; | ||
} | ||
this.updateDeltas(record); | ||
@@ -160,6 +175,2 @@ time = record.time; | ||
this.updateFrequence(time); | ||
if (!this.lastSnapshotTime || (time.getTime() - this.lastSnapshotTime.getTime()) > snapshot_interval * 1000) { | ||
record.snapshot = { value: (0, AgLodashWrap_1.cloneDeep)(this._current.value) }; | ||
this.lastSnapshotTime = time; | ||
} | ||
return [4 /*yield*/, this.sync]; | ||
@@ -184,36 +195,48 @@ case 2: | ||
return __awaiter(this, void 0, void 0, function () { | ||
var table, _a, _b, _c, index, i, snapshot, diff; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
var table, _a, deltas, phrases, _b, lastPhrase, value_1; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, this.table]; | ||
case 1: | ||
table = _d.sent(); | ||
table = _c.sent(); | ||
_a = this._lifetime; | ||
return [4 /*yield*/, table.get_lifetime()]; | ||
case 2: | ||
_a.value = _d.sent(); | ||
_b = this._recent_deltas; | ||
_a.value = _c.sent(); | ||
return [4 /*yield*/, table.get_recent_deltas(buffer_span_sec)]; | ||
case 3: | ||
_b.value = _d.sent(); | ||
_c = this._recent_frequence; | ||
deltas = _c.sent(); | ||
deltas.sort(function (a, b) { return a.time.getTime() - b.time.getTime(); }); | ||
phrases = this._recent_phrases.value; | ||
deltas.forEach(function (delta) { | ||
if (delta.snapshot) { | ||
if (phrases.length > 0) | ||
phrases[phrases.length - 1].end = delta.time; | ||
var snapshot = delta.snapshot, rest = __rest(delta, ["snapshot"]); | ||
phrases.push({ | ||
_id: delta.time.getTime(), | ||
value: snapshot.value, | ||
index: 0, | ||
start: delta.time, | ||
end: delta.time, | ||
deltas: [rest] | ||
}); | ||
} | ||
else if (phrases.length > 0) { | ||
phrases[phrases.length - 1].end = delta.time; | ||
phrases[phrases.length - 1].deltas.push(delta); | ||
} | ||
}); | ||
_b = this._recent_frequence; | ||
return [4 /*yield*/, table.get_today_frequence()]; | ||
case 4: | ||
_c.value = _d.sent(); | ||
index = -1; | ||
for (i = this._recent_deltas.value.length - 1; i >= 0; i--) { | ||
if (this._recent_deltas.value[i].snapshot) { | ||
index = i; | ||
break; | ||
} | ||
_b.value = _c.sent(); | ||
lastPhrase = phrases[phrases.length - 1]; | ||
if (lastPhrase) { | ||
value_1 = AgDiff_1.AgDiff.clone(lastPhrase.value); | ||
lastPhrase.deltas.forEach(function (delta) { | ||
value_1 = AgDiff_1.AgDiff.patch(value_1, delta.delta); | ||
}); | ||
this._current.value = value_1; | ||
} | ||
if (index >= 0) { | ||
snapshot = this._recent_deltas.value[index].snapshot; | ||
diff = (0, AgVueWrap_1.diffRef)((0, AgLodashWrap_1.cloneDeep)(snapshot.value)); | ||
index++; | ||
for (; index < this._recent_deltas.value.length; index++) { | ||
diff.patch(this._recent_deltas.value[index].delta); | ||
} | ||
this._current.value = diff.value; | ||
} | ||
return [2 /*return*/]; | ||
@@ -233,13 +256,31 @@ } | ||
AgTmObject.prototype.updateDeltas = function (delta) { | ||
var deltas = this._recent_deltas.value; | ||
if (deltas.length > 0) { | ||
var last = deltas[deltas.length - 1]; | ||
if (last.time.getTime() == delta.time.getTime()) { | ||
var phrases = this._recent_phrases.value; | ||
if (phrases.length > 0) { | ||
var deltas = phrases[phrases.length - 1].deltas; | ||
if (deltas[deltas.length - 1].time.getTime() == delta.time.getTime()) { | ||
delta.time = AgHelper_1.AgHelper.offsetTime(delta.time, 1); | ||
} | ||
} | ||
deltas.push(delta); | ||
var min = AgHelper_1.AgHelper.offsetTime(new Date(), -buffer_span_sec * 1000).getTime(); | ||
while (deltas.length > 100 && deltas[0].time.getTime() < min) | ||
deltas.shift(); | ||
if (delta.snapshot) { | ||
if (phrases.length > 0) | ||
phrases[phrases.length - 1].end = delta.time; | ||
var snapshot = delta.snapshot, rest = __rest(delta, ["snapshot"]); | ||
phrases.push({ | ||
_id: delta.time.getTime(), | ||
value: snapshot.value, | ||
index: 0, | ||
start: delta.time, | ||
end: delta.time, | ||
deltas: [rest] | ||
}); | ||
} | ||
else { | ||
if (phrases.length > 0) { | ||
phrases[phrases.length - 1].end = delta.time; | ||
phrases[phrases.length - 1].deltas.push(delta); | ||
} | ||
} | ||
var min = AgHelper_1.AgHelper.offsetTime(new Date(), -60 * 1000).getTime(); | ||
while (phrases[0].end.getTime() < min) | ||
phrases.shift(); | ||
return delta.time; | ||
@@ -246,0 +287,0 @@ }; |
@@ -65,7 +65,6 @@ "use strict"; | ||
var hot = api.hotData.args(function () { return index.type; }, function () { return index.id; }).sync({ type: '', id: '' }); | ||
var watcher = (0, AgVueWrap_1.watch)(function () { var _a; return (_a = hot.value.deltas) !== null && _a !== void 0 ? _a : []; }, makeSnapshot, { immediate: true, deep: true }); | ||
// const watcher = watch(() => hot.value.deltas ?? [], makeSnapshot, { immediate: true, deep: true }) | ||
var dirty = (0, AgVueWrap_1.computed)(function () { return !index.type || !index.id || index.type !== hot.value.type || index.id !== hot.value.id; }); | ||
var lifetime = (0, AgVueWrap_1.computed)(function () { return dirty.value ? undefined : hot.value.lifetime; }); | ||
var value = (0, AgVueWrap_1.computed)(function () { | ||
var _a, _b; | ||
// AgCounter.inc('value') | ||
@@ -78,14 +77,34 @@ if (dirty.value) | ||
return undefined; | ||
var deltas = []; | ||
if (hot.value.deltas && hot.value.deltas.length > 0 && cursor.value >= hot.value.deltas[0].time) { | ||
deltas = hot.value.deltas; | ||
var phrase; | ||
if (hot.value.phrases && hot.value.phrases.length > 0 && cursor.value >= hot.value.phrases[0].start) { | ||
phrase = hot.value.phrases.find(function (v) { return cursor.value >= v.start && cursor.value < v.end; }); | ||
if (!phrase) | ||
phrase = hot.value.phrases[hot.value.phrases.length - 1]; | ||
} | ||
else { | ||
deltas = deltas_cache.get(index.type, index.id, cursor.value); | ||
} | ||
// if(!phrase){ | ||
// phrase = deltas_cache.get(index.type, index.id, cursor.value).find(v => cursor.value >= v.time) | ||
// } | ||
// else { | ||
// deltas = deltas_cache.get(index.type, index.id, cursor.value) | ||
// } | ||
if (!phrase) | ||
return undefined; | ||
var deltas = phrase.deltas; | ||
var i = deltas.findIndex(function (v) { return v.time.getTime() > cursor.value.getTime(); }); | ||
// won't happen | ||
if (i === 0) | ||
return undefined; | ||
if (i < 0) | ||
return (_a = deltas[deltas.length - 1].snapshot) === null || _a === void 0 ? void 0 : _a.value; | ||
else if (i > 0) | ||
return (_b = deltas[i - 1].snapshot) === null || _b === void 0 ? void 0 : _b.value; | ||
i = deltas.length - 1; | ||
else | ||
i--; | ||
while (phrase.index < i) { | ||
phrase.index++; | ||
phrase.value = AgDiff_1.AgDiff.patch(phrase.value, deltas[phrase.index].delta); | ||
} | ||
while (phrase.index > i) { | ||
phrase.value = AgDiff_1.AgDiff.unpatch(phrase.value, deltas[phrase.index].delta); | ||
phrase.index--; | ||
} | ||
return phrase.value; | ||
// else return deltas[i].snapshot?.value | ||
@@ -118,3 +137,3 @@ }); | ||
cursor.stop(); | ||
watcher.stop(); | ||
// watcher.stop() | ||
}; | ||
@@ -121,0 +140,0 @@ return { |
{ | ||
"name": "ag-repo", | ||
"version": "1.0.153", | ||
"version": "1.0.154", | ||
"description": "", | ||
@@ -13,3 +13,3 @@ "author": "", | ||
"dependencies": { | ||
"ag-utilities": "^1.1.193", | ||
"ag-utilities": "^1.1.199", | ||
"mongodb": "^5.4.0" | ||
@@ -16,0 +16,0 @@ }, |
157737
21
3549
Updatedag-utilities@^1.1.199