reactivedb
Advanced tools
Comparing version 0.10.4-alpha.7-diff to 0.10.4-alpha.8-diff
{ | ||
"name": "reactivedb", | ||
"version": "0.10.4-alpha.7-diff", | ||
"version": "0.10.4-alpha.8-diff", | ||
"description": "Reactive ORM for Lovefield", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -15,3 +15,3 @@ import { Observable } from 'rxjs/Observable' | ||
import { TokenConsumed } from '../../exception/token' | ||
import diff, { Ops } from '../../utils/diff' | ||
import { diff, Ops } from '../../utils/diff' | ||
@@ -30,7 +30,7 @@ export interface TraceResult<T> { | ||
selector$: Observable<SelectorMeta<T>> | ||
lastEmit: T[] = [] | ||
private consumed = false | ||
private lastEmit: T[] = [] | ||
constructor(selector$: Observable<SelectorMeta<T>>, lasEmit?: T[]) { | ||
constructor(selector$: Observable<SelectorMeta<T>>, lastEmit?: T[]) { | ||
this.selector$ = selector$.pipe( | ||
@@ -40,3 +40,3 @@ publishReplay(1), | ||
) | ||
this.lastEmit = lasEmit || [] | ||
this.lastEmit = lastEmit || [] | ||
} | ||
@@ -75,10 +75,9 @@ | ||
traces(pk?: string): Observable<TraceResult<T>> { | ||
return this.changes() | ||
.pipe( | ||
map((result: T[]) => { | ||
const ops = diff(this.lastEmit, result, pk) | ||
return { result, ops } | ||
}), | ||
tap(({ result }) => this.lastEmit = result), | ||
) | ||
return this.changes().pipe( | ||
map((result: T[]) => { | ||
const ops = diff(this.lastEmit, result, pk) | ||
return { result, ops } | ||
}), | ||
tap(({ result }) => (this.lastEmit = result)), | ||
) | ||
} | ||
@@ -85,0 +84,0 @@ |
@@ -1,13 +0,24 @@ | ||
export type Op = { | ||
export enum OpType { | ||
// 0 = reuse | ||
// 1 = use new item | ||
type: 0 | 1 | ||
Reuse, | ||
New, | ||
} | ||
export type Op = { | ||
type: OpType | ||
index: number | ||
} | ||
export type Ops = { | ||
export enum OpsType { | ||
// 0 = error | ||
// 1 = success | ||
// 2 = success but should skip | ||
type: 0 | 1 | 2 | ||
Error, | ||
Success, | ||
SuccessAndSkip, | ||
} | ||
export type Ops = { | ||
type: OpsType | ||
ops: Op[] | ||
@@ -26,3 +37,3 @@ message?: string | ||
if (op.type === 0) { | ||
if (op.type === OpType.Reuse) { | ||
return oldList[op.index] | ||
@@ -35,107 +46,103 @@ } | ||
export default function diff<T>(oldList: T[], newList: T[], pk = '_id'): Ops { | ||
const prev = oldList | ||
const curr = newList | ||
export const getPatchResult = <T>(oldList: T[], newList: T[], ops: Ops): T[] => { | ||
switch (ops.type) { | ||
case OpsType.Error: | ||
return newList | ||
case OpsType.SuccessAndSkip: | ||
return oldList | ||
case OpsType.Success: | ||
default: | ||
return patch(ops.ops, oldList, newList) | ||
} | ||
} | ||
if (!Array.isArray(prev) || !Array.isArray(curr)) { | ||
return { | ||
type: 0, | ||
ops: [], | ||
message: `cannot compare non-list object`, | ||
} | ||
function fastEqual(left: object, right: object) { | ||
if (left === right) { | ||
return true | ||
} | ||
const currIds = [] | ||
const prevIds = [] | ||
const index = {} | ||
if (left && right && typeof left == 'object' && typeof right == 'object') { | ||
const isLeftArray = Array.isArray(left) | ||
const isRightArray = Array.isArray(right) | ||
for (let i = 0; i < prev.length; i++) { | ||
const value = prev[i][pk] | ||
if (value === undefined) { | ||
return { | ||
type: 0, | ||
ops: [], | ||
message: `cannot find pk: ${pk} at prev.${i}`, | ||
if (isLeftArray && isRightArray) { | ||
const length = (left as any[]).length | ||
if (length != (right as any[]).length) { | ||
return false | ||
} | ||
} | ||
prevIds.push(value) | ||
index[value] = i | ||
} | ||
for (let j = 0; j < curr.length; j++) { | ||
const value = curr[j][pk] | ||
if (value === undefined) { | ||
return { | ||
type: 0, | ||
ops: [], | ||
message: `cannot find pk: ${pk} at curr.${j}`, | ||
for (let i = length; i-- !== 0; ) { | ||
if (!fastEqual(left[i], right[i])) { | ||
return false | ||
} | ||
} | ||
} | ||
currIds.push(value) | ||
} | ||
const fastEqual = (left: object, right: object) => { | ||
if (left === right) { | ||
return true | ||
} | ||
if (left && right && typeof left == 'object' && typeof right == 'object') { | ||
const isLeftArray = Array.isArray(left) | ||
const isRightArray = Array.isArray(right) | ||
if (isLeftArray !== isRightArray) { | ||
return false | ||
} | ||
if (isLeftArray && isRightArray) { | ||
const length = (left as any[]).length | ||
const isLeftDate = left instanceof Date | ||
const isRightDate = right instanceof Date | ||
if (length != (right as any[]).length) { | ||
return false | ||
} | ||
if (isLeftDate != isRightDate) { | ||
return false | ||
} | ||
for (let i = length; i-- !== 0; ) { | ||
if (!fastEqual(left[i], right[i])) { | ||
return false | ||
} | ||
} | ||
if (isLeftDate && isRightDate) { | ||
return (left as Date).getTime() == (right as Date).getTime() | ||
} | ||
return true | ||
} | ||
const keys = Object.keys(left) | ||
const LeftLen = keys.length | ||
if (isLeftArray !== isRightArray) { | ||
if (LeftLen !== Object.keys(right).length) { | ||
return false | ||
} | ||
for (let k = LeftLen; k-- !== 0; ) { | ||
if (!right.hasOwnProperty(keys[k])) { | ||
return false | ||
} | ||
} | ||
const isLeftDate = left instanceof Date | ||
const isRightDate = right instanceof Date | ||
if (isLeftDate != isRightDate) { | ||
for (let j = LeftLen; j-- !== 0; ) { | ||
const key = keys[j] | ||
if (!fastEqual(left[key], right[key])) { | ||
return false | ||
} | ||
} | ||
if (isLeftDate && isRightDate) { | ||
return (left as Date).getTime() == (right as Date).getTime() | ||
} | ||
return true | ||
} | ||
const keys = Object.keys(left) | ||
const LeftLen = keys.length | ||
return left !== left && right !== right | ||
} | ||
if (LeftLen !== Object.keys(right).length) { | ||
return false | ||
} | ||
export function diff<T>(oldList: T[], newList: T[], pk = '_id'): Ops { | ||
const prev = oldList | ||
const curr = newList | ||
for (let k = LeftLen; k-- !== 0; ) { | ||
if (!right.hasOwnProperty(keys[k])) { | ||
return false | ||
} | ||
} | ||
if (!Array.isArray(prev) || !Array.isArray(curr)) { | ||
return { | ||
type: OpsType.Error, | ||
ops: [], | ||
message: `cannot compare non-list object`, | ||
} | ||
} | ||
for (let j = LeftLen; j-- !== 0; ) { | ||
const key = keys[j] | ||
if (!fastEqual(left[key], right[key])) { | ||
return false | ||
} | ||
const index = {} | ||
for (let i = 0; i < prev.length; i++) { | ||
const value = prev[i][pk] | ||
if (value === undefined) { | ||
return { | ||
type: OpsType.Error, | ||
ops: [], | ||
message: `cannot find pk: ${pk} at prev.${i}`, | ||
} | ||
return true | ||
} | ||
return left !== left && right !== right | ||
index[value] = i | ||
} | ||
@@ -146,4 +153,12 @@ | ||
for (let k = 0; k < currIds.length; k++) { | ||
const key = currIds[k] | ||
for (let k = 0; k < curr.length; k++) { | ||
const key = curr[k][pk] | ||
if (key === undefined) { | ||
return { | ||
type: OpsType.Error, | ||
ops: [], | ||
message: `cannot find pk: ${pk} at curr.${k}`, | ||
} | ||
} | ||
const prevIndex = index[key] | ||
@@ -154,3 +169,3 @@ | ||
// if equal then reuse the previous data otherwise use the new data | ||
const op: Op = isEqual ? { type: 0, index: prevIndex } : { type: 1, index: k } | ||
const op: Op = isEqual ? { type: OpType.Reuse, index: prevIndex } : { type: OpType.New, index: k } | ||
@@ -162,3 +177,3 @@ if (prevIndex === k && isEqual) { | ||
} else { | ||
ret.push({ type: 1, index: k }) | ||
ret.push({ type: OpType.New, index: k }) | ||
} | ||
@@ -169,5 +184,5 @@ } | ||
return { | ||
type: arrayIsSame ? 2 : 1, | ||
type: arrayIsSame ? OpsType.SuccessAndSkip : OpsType.Success, | ||
ops: ret, | ||
} | ||
} |
@@ -1,1 +0,1 @@ | ||
export default '0.10.4-alpha.7-diff-alpha0.10.4-alpha.7-diff-lazyassert' | ||
export default '0.10.4-alpha.8-diff' |
@@ -13,5 +13,5 @@ import { Observable } from 'rxjs/Observable'; | ||
selector$: Observable<SelectorMeta<T>>; | ||
lastEmit: T[]; | ||
private consumed; | ||
constructor(selector$: Observable<SelectorMeta<T>>, lasEmit?: T[]); | ||
private lastEmit; | ||
constructor(selector$: Observable<SelectorMeta<T>>, lastEmit?: T[]); | ||
setLastEmit(data: T[]): void; | ||
@@ -18,0 +18,0 @@ map<K>(fn: OperatorFunction<T[], K[]>): QueryToken<K>; |
@@ -18,7 +18,7 @@ "use strict"; | ||
var QueryToken = /** @class */ (function () { | ||
function QueryToken(selector$, lasEmit) { | ||
function QueryToken(selector$, lastEmit) { | ||
this.consumed = false; | ||
this.lastEmit = []; | ||
this.consumed = false; | ||
this.selector$ = selector$.pipe(publishReplay_1.publishReplay(1), refCount_1.refCount()); | ||
this.lastEmit = lasEmit || []; | ||
this.lastEmit = lastEmit || []; | ||
} | ||
@@ -44,9 +44,8 @@ QueryToken.prototype.setLastEmit = function (data) { | ||
var _this = this; | ||
return this.changes() | ||
.pipe(map_1.map(function (result) { | ||
var ops = diff_1.default(_this.lastEmit, result, pk); | ||
return this.changes().pipe(map_1.map(function (result) { | ||
var ops = diff_1.diff(_this.lastEmit, result, pk); | ||
return { result: result, ops: ops }; | ||
}), tap_1.tap(function (_a) { | ||
var result = _a.result; | ||
return _this.lastEmit = result; | ||
return (_this.lastEmit = result); | ||
})); | ||
@@ -53,0 +52,0 @@ }; |
@@ -0,7 +1,16 @@ | ||
export declare enum OpType { | ||
Reuse = 0, | ||
New = 1, | ||
} | ||
export declare type Op = { | ||
type: 0 | 1; | ||
type: OpType; | ||
index: number; | ||
}; | ||
export declare enum OpsType { | ||
Error = 0, | ||
Success = 1, | ||
SuccessAndSkip = 2, | ||
} | ||
export declare type Ops = { | ||
type: 0 | 1 | 2; | ||
type: OpsType; | ||
ops: Op[]; | ||
@@ -11,2 +20,3 @@ message?: string; | ||
export declare const patch: <T>(ops: Op[], oldList: T[], newList: T[]) => T[]; | ||
export default function diff<T>(oldList: T[], newList: T[], pk?: string): Ops; | ||
export declare const getPatchResult: <T>(oldList: T[], newList: T[], ops: Ops) => T[]; | ||
export declare function diff<T>(oldList: T[], newList: T[], pk?: string): Ops; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var OpType; | ||
(function (OpType) { | ||
// 0 = reuse | ||
// 1 = use new item | ||
OpType[OpType["Reuse"] = 0] = "Reuse"; | ||
OpType[OpType["New"] = 1] = "New"; | ||
})(OpType = exports.OpType || (exports.OpType = {})); | ||
var OpsType; | ||
(function (OpsType) { | ||
// 0 = error | ||
// 1 = success | ||
// 2 = success but should skip | ||
OpsType[OpsType["Error"] = 0] = "Error"; | ||
OpsType[OpsType["Success"] = 1] = "Success"; | ||
OpsType[OpsType["SuccessAndSkip"] = 2] = "SuccessAndSkip"; | ||
})(OpsType = exports.OpsType || (exports.OpsType = {})); | ||
// as an example, use diff to patch data | ||
@@ -10,3 +26,3 @@ exports.patch = function (ops, oldList, newList) { | ||
var op = ops[i]; | ||
if (op.type === 0) { | ||
if (op.type === OpType.Reuse) { | ||
return oldList[op.index]; | ||
@@ -17,2 +33,63 @@ } | ||
}; | ||
exports.getPatchResult = function (oldList, newList, ops) { | ||
switch (ops.type) { | ||
case OpsType.Error: | ||
return newList; | ||
case OpsType.SuccessAndSkip: | ||
return oldList; | ||
case OpsType.Success: | ||
default: | ||
return exports.patch(ops.ops, oldList, newList); | ||
} | ||
}; | ||
function fastEqual(left, right) { | ||
if (left === right) { | ||
return true; | ||
} | ||
if (left && right && typeof left == 'object' && typeof right == 'object') { | ||
var isLeftArray = Array.isArray(left); | ||
var isRightArray = Array.isArray(right); | ||
if (isLeftArray && isRightArray) { | ||
var length_1 = left.length; | ||
if (length_1 != right.length) { | ||
return false; | ||
} | ||
for (var i = length_1; i-- !== 0;) { | ||
if (!fastEqual(left[i], right[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (isLeftArray !== isRightArray) { | ||
return false; | ||
} | ||
var isLeftDate = left instanceof Date; | ||
var isRightDate = right instanceof Date; | ||
if (isLeftDate != isRightDate) { | ||
return false; | ||
} | ||
if (isLeftDate && isRightDate) { | ||
return left.getTime() == right.getTime(); | ||
} | ||
var keys = Object.keys(left); | ||
var LeftLen = keys.length; | ||
if (LeftLen !== Object.keys(right).length) { | ||
return false; | ||
} | ||
for (var k = LeftLen; k-- !== 0;) { | ||
if (!right.hasOwnProperty(keys[k])) { | ||
return false; | ||
} | ||
} | ||
for (var j = LeftLen; j-- !== 0;) { | ||
var key = keys[j]; | ||
if (!fastEqual(left[key], right[key])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return left !== left && right !== right; | ||
} | ||
function diff(oldList, newList, pk) { | ||
@@ -24,3 +101,3 @@ if (pk === void 0) { pk = '_id'; } | ||
return { | ||
type: 0, | ||
type: OpsType.Error, | ||
ops: [], | ||
@@ -30,4 +107,2 @@ message: "cannot compare non-list object", | ||
} | ||
var currIds = []; | ||
var prevIds = []; | ||
var index = {}; | ||
@@ -38,3 +113,3 @@ for (var i = 0; i < prev.length; i++) { | ||
return { | ||
type: 0, | ||
type: OpsType.Error, | ||
ops: [], | ||
@@ -44,70 +119,15 @@ message: "cannot find pk: " + pk + " at prev." + i, | ||
} | ||
prevIds.push(value); | ||
index[value] = i; | ||
} | ||
for (var j = 0; j < curr.length; j++) { | ||
var value = curr[j][pk]; | ||
if (value === undefined) { | ||
var ret = []; | ||
var reused = 0; | ||
for (var k = 0; k < curr.length; k++) { | ||
var key = curr[k][pk]; | ||
if (key === undefined) { | ||
return { | ||
type: 0, | ||
type: OpsType.Error, | ||
ops: [], | ||
message: "cannot find pk: " + pk + " at curr." + j, | ||
message: "cannot find pk: " + pk + " at curr." + k, | ||
}; | ||
} | ||
currIds.push(value); | ||
} | ||
var fastEqual = function (left, right) { | ||
if (left === right) { | ||
return true; | ||
} | ||
if (left && right && typeof left == 'object' && typeof right == 'object') { | ||
var isLeftArray = Array.isArray(left); | ||
var isRightArray = Array.isArray(right); | ||
if (isLeftArray && isRightArray) { | ||
var length_1 = left.length; | ||
if (length_1 != right.length) { | ||
return false; | ||
} | ||
for (var i = length_1; i-- !== 0;) { | ||
if (!fastEqual(left[i], right[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (isLeftArray !== isRightArray) { | ||
return false; | ||
} | ||
var isLeftDate = left instanceof Date; | ||
var isRightDate = right instanceof Date; | ||
if (isLeftDate != isRightDate) { | ||
return false; | ||
} | ||
if (isLeftDate && isRightDate) { | ||
return left.getTime() == right.getTime(); | ||
} | ||
var keys = Object.keys(left); | ||
var LeftLen = keys.length; | ||
if (LeftLen !== Object.keys(right).length) { | ||
return false; | ||
} | ||
for (var k = LeftLen; k-- !== 0;) { | ||
if (!right.hasOwnProperty(keys[k])) { | ||
return false; | ||
} | ||
} | ||
for (var j = LeftLen; j-- !== 0;) { | ||
var key = keys[j]; | ||
if (!fastEqual(left[key], right[key])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return left !== left && right !== right; | ||
}; | ||
var ret = []; | ||
var reused = 0; | ||
for (var k = 0; k < currIds.length; k++) { | ||
var key = currIds[k]; | ||
var prevIndex = index[key]; | ||
@@ -117,3 +137,3 @@ if (prevIndex !== undefined) { | ||
// if equal then reuse the previous data otherwise use the new data | ||
var op = isEqual ? { type: 0, index: prevIndex } : { type: 1, index: k }; | ||
var op = isEqual ? { type: OpType.Reuse, index: prevIndex } : { type: OpType.New, index: k }; | ||
if (prevIndex === k && isEqual) { | ||
@@ -125,3 +145,3 @@ reused++; | ||
else { | ||
ret.push({ type: 1, index: k }); | ||
ret.push({ type: OpType.New, index: k }); | ||
} | ||
@@ -131,7 +151,7 @@ } | ||
return { | ||
type: arrayIsSame ? 2 : 1, | ||
type: arrayIsSame ? OpsType.SuccessAndSkip : OpsType.Success, | ||
ops: ret, | ||
}; | ||
} | ||
exports.default = diff; | ||
exports.diff = diff; | ||
//# sourceMappingURL=diff.js.map |
@@ -1,2 +0,2 @@ | ||
declare const _default: "0.10.4-alpha.7-diff-alpha0.10.4-alpha.7-diff-lazyassert"; | ||
declare const _default: "0.10.4-alpha.8-diff"; | ||
export default _default; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = '0.10.4-alpha.7-diff-alpha0.10.4-alpha.7-diff-lazyassert'; | ||
exports.default = '0.10.4-alpha.8-diff'; | ||
//# sourceMappingURL=version.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
294724
5289