Skip to content

Commit

Permalink
Refactor _global and getObjectDiff to own modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dfahlander committed Jun 10, 2021
1 parent 3148a56 commit 50b1ab1
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/classes/dexie/dexie-dom-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _global } from '../../functions/utils';
import { _global } from '../../globals/global';
import { DexieDOMDependencies } from '../../public/types/dexie-dom-dependencies';

export let domDeps: DexieDOMDependencies
Expand Down
4 changes: 3 additions & 1 deletion src/classes/dexie/dexie-static-props.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Dexie as _Dexie } from './dexie';
import { props, derive, extend, override, getByKeyPath, setByKeyPath, delByKeyPath, shallowClone, deepClone, getObjectDiff, asap, _global } from '../../functions/utils';
import { _global } from '../../globals/global';
import { props, derive, extend, override, getByKeyPath, setByKeyPath, delByKeyPath, shallowClone, deepClone, asap } from '../../functions/utils';
import { getObjectDiff } from "../../functions/get-object-diff";
import { fullNameExceptions } from '../../errors';
import { DexieConstructor } from '../../public/types/dexie-constructor';
import { getDatabaseNames } from '../../helpers/database-enumerator';
Expand Down
3 changes: 2 additions & 1 deletion src/classes/version/schema-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Dexie } from '../dexie';
import { DbSchema } from '../../public/types/db-schema';
import { setProp, keys, slice, _global, isArray, shallowClone, isAsyncFunction, defineProperty, getPropertyDescriptor } from '../../functions/utils';
import { _global } from "../../globals/global";
import { setProp, keys, slice, isArray, shallowClone, isAsyncFunction, defineProperty, getPropertyDescriptor } from '../../functions/utils';
import { Transaction } from '../transaction';
import { Version } from './version';
import Promise, { PSD, newScope, NativePromise, decrementExpectedAwaits, incrementExpectedAwaits } from '../../helpers/promise';
Expand Down
51 changes: 51 additions & 0 deletions src/functions/get-object-diff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { keys, hasOwn, toStringTag, intrinsicTypeNameSet, isArray } from "./utils";

export const getValueOf = (val:any, type: string) =>
type === "Array" ? ''+val.map(v => getValueOf(v, toStringTag(v))) :
type === "ArrayBuffer" ? ''+new Uint8Array(val) :
type === "Date" ? val.getTime() :
ArrayBuffer.isView(val) ? ''+new Uint8Array(val.buffer) :
val;

export function getObjectDiff(a, b, rv?, prfx?) {
// Compares objects a and b and produces a diff object.
rv = rv || {};
prfx = prfx || '';
keys(a).forEach(prop => {
if (!hasOwn(b, prop))
rv[prfx+prop] = undefined; // Property removed
else {
var ap = a[prop],
bp = b[prop];
if (typeof ap === 'object' && typeof bp === 'object' && ap && bp)
{
const apTypeName = toStringTag(ap);
const bpTypeName = toStringTag(bp);

if (apTypeName === bpTypeName) {
if (intrinsicTypeNameSet[apTypeName] || isArray(ap)) {
// This is an intrinsic type. Don't go deep diffing it.
// Instead compare its value in best-effort:
// (Can compare real values of Date, ArrayBuffers and views)
if (getValueOf(ap, apTypeName) !== getValueOf(bp, bpTypeName)) {
rv[prfx + prop] = b[prop]; // Date / ArrayBuffer etc is of different value
}
} else {
// This is not an intrinsic object. Compare the it deeply:
getObjectDiff(ap, bp, rv, prfx + prop + ".");
}
} else {
rv[prfx + prop] = b[prop];// Property changed to other type
}
} else if (ap !== bp)
rv[prfx + prop] = b[prop];// Primitive value changed
}
});
keys(b).forEach(prop => {
if (!hasOwn(a, prop)) {
rv[prfx+prop] = b[prop]; // Property added
}
});
return rv;
}

57 changes: 2 additions & 55 deletions src/functions/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
declare var global;
import { _global } from "../globals/global";
export const keys = Object.keys;
export const isArray = Array.isArray;
const _global =
typeof self !== 'undefined' ? self :
typeof window !== 'undefined' ? window :
global;
if (typeof Promise !== 'undefined' && !_global.Promise){
// In jsdom, this it can be the case that Promise is not put on the global object.
// If so, we need to patch the global object for the rest of the code to work as expected.
Expand Down Expand Up @@ -196,7 +192,7 @@ const intrinsicTypeNames =
flatten([8,16,32,64].map(num=>["Int","Uint","Float"].map(t=>t+num+"Array")))
).filter(t=>_global[t]);
const intrinsicTypes = intrinsicTypeNames.map(t=>_global[t]);
const intrinsicTypeNameSet = arrayToObject(intrinsicTypeNames, x=>[x,true]);
export const intrinsicTypeNameSet = arrayToObject(intrinsicTypeNames, x=>[x,true]);

let circularRefs: null | WeakMap<any,any> = null;
export function deepClone<T>(any: T): T {
Expand Down Expand Up @@ -236,55 +232,6 @@ export function toStringTag(o: Object) {
return toString.call(o).slice(8, -1);
}

export const getValueOf = (val:any, type: string) =>
type === "Array" ? ''+val.map(v => getValueOf(v, toStringTag(v))) :
type === "ArrayBuffer" ? ''+new Uint8Array(val) :
type === "Date" ? val.getTime() :
ArrayBuffer.isView(val) ? ''+new Uint8Array(val.buffer) :
val;

export function getObjectDiff(a, b, rv?, prfx?) {
// Compares objects a and b and produces a diff object.
rv = rv || {};
prfx = prfx || '';
keys(a).forEach(prop => {
if (!hasOwn(b, prop))
rv[prfx+prop] = undefined; // Property removed
else {
var ap = a[prop],
bp = b[prop];
if (typeof ap === 'object' && typeof bp === 'object' && ap && bp)
{
const apTypeName = toStringTag(ap);
const bpTypeName = toStringTag(bp);

if (apTypeName === bpTypeName) {
if (intrinsicTypeNameSet[apTypeName] || isArray(ap)) {
// This is an intrinsic type. Don't go deep diffing it.
// Instead compare its value in best-effort:
// (Can compare real values of Date, ArrayBuffers and views)
if (getValueOf(ap, apTypeName) !== getValueOf(bp, bpTypeName)) {
rv[prfx + prop] = b[prop]; // Date / ArrayBuffer etc is of different value
}
} else {
// This is not an intrinsic object. Compare the it deeply:
getObjectDiff(ap, bp, rv, prfx + prop + ".");
}
} else {
rv[prfx + prop] = b[prop];// Property changed to other type
}
} else if (ap !== bp)
rv[prfx + prop] = b[prop];// Primitive value changed
}
});
keys(b).forEach(prop => {
if (!hasOwn(a, prop)) {
rv[prfx+prop] = b[prop]; // Property added
}
});
return rv;
}

// If first argument is iterable or array-like, return it as an array
export const iteratorSymbol = typeof Symbol !== 'undefined' ?
Symbol.iterator :
Expand Down
6 changes: 6 additions & 0 deletions src/globals/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare var global;
export const _global: any =
typeof globalThis !== 'undefined' ? globalThis :
typeof self !== 'undefined' ? self :
typeof window !== 'undefined' ? window :
global;
1 change: 1 addition & 0 deletions src/helpers/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2014-2017 David Fahlander
* Apache License Version 2.0, January 2004, http://www.apache.org/licenses/LICENSE-2.0
*/
import { _global } from '../globals/global';
import {tryCatch, props, setProp, _global,
getPropertyDescriptor, getArrayOf, extend, getProto} from '../functions/utils';
import {nop, callBoth, mirror} from '../functions/chaining-functions';
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/hooks-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
DBCoreKeyRange
} from "../public/types/dbcore";
import { nop } from '../functions/chaining-functions';
import { getObjectDiff, hasOwn, setByKeyPath } from '../functions/utils';
import { hasOwn, setByKeyPath } from '../functions/utils';
import { getObjectDiff } from "../functions/get-object-diff";
import { PSD } from '../helpers/promise';
//import { LockableTableMiddleware } from '../dbcore/lockable-table-middleware';
import { getEffectiveKeys } from '../dbcore/get-effective-keys';
Expand Down

0 comments on commit 50b1ab1

Please sign in to comment.