Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported to TypeScript (NEEDS TESTS) #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/queue-that-ts.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ bootstrap:
@npm install

test:
@$(BIN)/standard
@./node_modules/karma/bin/karma start --single-run=true

watch:
Expand Down
23 changes: 23 additions & 0 deletions dist/global-variable-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createGlobalVariableAdapter = void 0;
const local_storage_adapter_1 = require("./local-storage-adapter");
function createGlobalVariableAdapter(queueName) {
window.__queueThat__ = window.__queueThat__ || {};
const localStorageAdapter = (0, local_storage_adapter_1.createLocalStorageAdapter)(queueName);
localStorageAdapter.save = save;
localStorageAdapter.load = load;
localStorageAdapter.remove = remove;
localStorageAdapter.type = 'globalVariable';
return localStorageAdapter;
function save(key, data) {
window.__queueThat__[key] = String(data);
}
function load(key) {
return window.__queueThat__[key];
}
function remove(key) {
delete window.__queueThat__[key];
}
}
exports.createGlobalVariableAdapter = createGlobalVariableAdapter;
125 changes: 125 additions & 0 deletions dist/local-storage-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLocalStorageAdapter = void 0;
const QUEUE_KEY = '* - Queue';
const ACTIVE_QUEUE_KEY = '* - Active Queue';
const BACKOFF_TIME_KEY = '* - Backoff Time';
const ERROR_COUNT_KEY = '* - Error Count';
const QUEUE_PROCESSING_KEY = '* - Queue Processing';
function createLocalStorageAdapter(queueName) {
const queueKey = QUEUE_KEY.replace('*', queueName);
const activeQueueKey = ACTIVE_QUEUE_KEY.replace('*', queueName);
const backoffTimeKey = BACKOFF_TIME_KEY.replace('*', queueName);
const errorCountKey = ERROR_COUNT_KEY.replace('*', queueName);
const queueProcessingKey = QUEUE_PROCESSING_KEY.replace('*', queueName);
let dirtyCache = true;
let setPending = false;
let queueCache = [];
const adapter = {
getQueue: getQueue,
setQueue: setQueue,
getErrorCount: getErrorCount,
getBackoffTime: getBackoffTime,
setErrorCount: setErrorCount,
setBackoffTime: setBackoffTime,
getActiveQueue: getActiveQueue,
setActiveQueue: setActiveQueue,
clearActiveQueue: clearActiveQueue,
getQueueProcessing: getQueueProcessing,
setQueueProcessing: setQueueProcessing,
save: save,
load: load,
works: works,
reset: reset,
remove: remove,
type: 'localStorage',
flush: flush
};
return adapter;
function flush() {
dirtyCache = true;
if (setPending) {
adapter.save(queueKey, JSON.stringify(queueCache));
setPending = false;
}
}
function getQueue() {
if (dirtyCache) {
queueCache = JSON.parse(adapter.load(queueKey) || '[]');
dirtyCache = false;
setTimeout(flush, 0);
}
return queueCache;
}
function setQueue(queue) {
queueCache = queue;
dirtyCache = false;
setPending = true;
setTimeout(flush, 0);
}
function getErrorCount() {
const count = adapter.load(errorCountKey);
return count === undefined ? 0 : Number(count);
}
function getBackoffTime() {
const time = adapter.load(backoffTimeKey);
return time === undefined ? 0 : Number(time);
}
function setErrorCount(n) {
adapter.save(errorCountKey, n);
}
function setBackoffTime(n) {
adapter.save(backoffTimeKey, n);
}
function getActiveQueue() {
if (adapter.load(activeQueueKey) === undefined) {
return;
}
return JSON.parse(adapter.load(activeQueueKey));
}
function setActiveQueue(id) {
adapter.save(activeQueueKey, JSON.stringify({
id: id,
ts: now()
}));
}
function clearActiveQueue() {
adapter.remove(activeQueueKey);
}
function getQueueProcessing() {
return Boolean(Number(adapter.load(queueProcessingKey)));
}
function setQueueProcessing(isProcessing) {
adapter.save(queueProcessingKey, Number(isProcessing));
}
function works() {
let works = false;
try {
adapter.save('queue-that-works', 'anything');
works = adapter.load('queue-that-works') === 'anything';
adapter.remove('queue-that-works');
}
catch (e) { /* empty */ }
return works;
}
function reset() {
adapter.remove(activeQueueKey);
adapter.remove(backoffTimeKey);
adapter.remove(errorCountKey);
adapter.remove(queueKey);
adapter.remove(queueProcessingKey);
}
}
exports.createLocalStorageAdapter = createLocalStorageAdapter;
function save(key, data) {
window.localStorage[key] = String(data);
}
function load(key) {
return window.localStorage[key];
}
function remove(key) {
window.localStorage.removeItem(key);
}
function now() {
return (new Date()).getTime();
}
Loading