Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 18 additions & 10 deletions packages/util/src/osm.js → packages/util/src/osm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ export function utilCleanTags(tags) {
// service: [ 'driveway', 'parking_aisle' ],
// width: [ '3', undefined ]
// }
export function utilCombinedTags(entityIDs, graph) {
export function utilCombinedTags(entityIDs: [string], graph) {
let tags = {};
let tagCounts = {};
let allKeys = new Set();
let allKeys: Set<string> = new Set();

const entities = entityIDs.map((entityID) => graph.hasEntity(entityID)).filter(Boolean);

Expand Down Expand Up @@ -100,14 +100,14 @@ export function utilCombinedTags(entityIDs, graph) {
});
});

for (let key in tags) {
if (!Array.isArray(tags[key])) continue;
for (let k in tags) {
if (!Array.isArray(tags[k])) continue;

// sort values by frequency then alphabetically
tags[key] = tags[key].sort((val1, val2) => {
let key = key; // capture
let count2 = tagCounts[key + '=' + val2];
let count1 = tagCounts[key + '=' + val1];
tags[k] = tags[k].sort((val1, val2) => {
const key = k;
const count2 = tagCounts[key + '=' + val2];
const count1 = tagCounts[key + '=' + val1];
if (count2 !== count1) return count2 - count1;
if (val2 && val1) return val1.localeCompare(val2);
return val1 ? 1 : -1;
Expand Down Expand Up @@ -233,9 +233,17 @@ export function utilGetAllNodes(ids, graph) {
//
//
//
type TagDiff = {
type: string,
key: string,
oldVal: any,
newVal: any,
display: string
};

export function utilTagDiff(oldTags, newTags) {
let tagDiff = [];
const keys = utilArrayUnion(Object.keys(oldTags), Object.keys(newTags)).sort();
let tagDiff: TagDiff[] = [];
const keys = utilArrayUnion(Object.keys(oldTags), Object.keys(newTags)).sort() as [string];
keys.forEach((k) => {
const oldVal = oldTags[k];
const newVal = newTags[k];
Expand Down
38 changes: 0 additions & 38 deletions packages/util/src/session_mutex.js

This file was deleted.

38 changes: 38 additions & 0 deletions packages/util/src/session_mutex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// A per-domain session mutex backed by a cookie and dead man's
// switch. If the session crashes, the mutex will auto-release
// after 5 seconds.

// This accepts a string and returns an object that complies with utilSessionMutexType
export function utilSessionMutex(name) {
// let _mutex = {};
let _intervalID;

function renew() {
let expires = new Date();
expires.setSeconds(expires.getSeconds() + 5);
document.cookie = name + '=1; expires=' + expires.toUTCString() + '; sameSite=strict';
}

let _mutex = {
lock: () => {
if (_intervalID) return true;
let cookie = document.cookie.replace(
new RegExp('(?:(?:^|.*;)\\s*' + name + '\\s*\\=\\s*([^;]*).*$)|^.*$'),
'$1'
);
if (cookie) return false;
renew();
_intervalID = window.setInterval(renew, 4000);
return true;
},
unlock: () => {
if (!_intervalID) return;
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; sameSite=strict';
clearInterval(_intervalID);
_intervalID = null;
},
locked: () => { return !!_intervalID; }
};

return _mutex;
}
11 changes: 5 additions & 6 deletions packages/util/src/string.js → packages/util/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ export function utilEditDistance(a, b) {
if (a.length === 0) return b.length;
if (b.length === 0) return a.length;

let matrix = [];
let i, j;
for (i = 0; i <= b.length; i++) {
let matrix: number[][] = [];
for (let i = 0; i <= b.length; i++) {
matrix[i] = [i];
}
for (j = 0; j <= a.length; j++) {
for (let j = 0; j <= a.length; j++) {
matrix[0][j] = j;
}
for (i = 1; i <= b.length; i++) {
for (j = 1; j <= a.length; j++) {
for (let i = 1; i <= b.length; i++) {
for (let j = 1; j <= a.length; j++) {
if (b.charAt(i - 1) === a.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,85 +5,105 @@ import * as util from '../src/index';
describe('utilSessionMutex', () => {
let a, b;

// Hack: Rename methods so esbuild-jest doesn't mistransform them...
function esbuildJestHack(obj) {
obj.lok = obj.lock;
obj.unlok = obj.unlock;
}

afterEach(() => {
if (a) a.unlock();
if (b) b.unlock();
if (a) a.unlok();
if (b) b.unlok();
});

describe('#lock', () => {
it('returns true when it gets a lock', () => {
a = util.utilSessionMutex('name');
expect(a.lock()).toBeTrue();
esbuildJestHack(a);
expect(a.lok()).toBeTrue();
});

it('returns true when already locked', () => {
a = util.utilSessionMutex('name');
a.lock();
expect(a.lock()).toBeTrue();
esbuildJestHack(a);
a.lok();
expect(a.lok()).toBeTrue();
});

it('returns false when the lock is held by another session', () => {
a = util.utilSessionMutex('name');
a.lock();
esbuildJestHack(a);
a.lok();

b = util.utilSessionMutex('name');
expect(b.lock()).toBeFalse();
esbuildJestHack(b);
expect(b.lok()).toBeFalse();
});
});

describe('#locked', () => {
it('returns false by default', () => {
a = util.utilSessionMutex('name');
esbuildJestHack(a);
expect(a.locked()).toBeFalse();
});

it('returns true when locked', () => {
a = util.utilSessionMutex('name');
a.lock();
esbuildJestHack(a);
a.lok();
expect(a.locked()).toBeTrue();
});

it('returns false when unlocked', () => {
a = util.utilSessionMutex('name');
a.lock();
a.unlock();
esbuildJestHack(a);
a.lok();
a.unlok();
expect(a.locked()).toBeFalse();
});
});

describe('#unlock', () => {
it('unlocks the mutex', () => {
a = util.utilSessionMutex('name');
a.lock();
a.unlock();
esbuildJestHack(a);
a.lok();
a.unlok();

b = util.utilSessionMutex('name');
expect(b.lock()).toBeTrue();
esbuildJestHack(b);
expect(b.lok()).toBeTrue();
});

it('does nothing when the lock is held by another session', () => {
a = util.utilSessionMutex('name');
a.lock();
esbuildJestHack(a);
a.lok();

b = util.utilSessionMutex('name');
b.unlock();
esbuildJestHack(b);
b.unlok();

expect(a.locked()).toBeTrue();
});

it('does nothing when not locked', () => {
a = util.utilSessionMutex('name');
a.unlock();
esbuildJestHack(a);
a.unlok();
expect(a.locked()).toBeFalse();
});
});

it('namespaces locks', () => {
a = util.utilSessionMutex('a');
a.lock();
esbuildJestHack(a);
a.lok();

b = util.utilSessionMutex('b');
esbuildJestHack(b);
expect(b.locked()).toBeFalse();
expect(b.lock()).toBeTrue();
expect(b.lok()).toBeTrue();
});
});
File renamed without changes.