Skip to content

Commit

Permalink
use only standard JS
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Jun 24, 2020
1 parent 8efc8ec commit 2dca219
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 70 deletions.
37 changes: 18 additions & 19 deletions packages/outbox/src/outbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class Outbox {
this.maxRetries = 10;
this.csrftoken = null;
this.csrftokenField = 'csrfmiddlewaretoken';
this._memoryItems = {};
this._waiting = {};
this._lastOutbox = [];
}

init(opts) {
Expand Down Expand Up @@ -458,7 +461,7 @@ class Outbox {
if (!options.storage) {
data = effect.data;
} else if (options.storage === 'temporary') {
data = this.#_memoryItems[options.id];
data = this._memoryItems[options.id];
} else {
throw new Error(
'Binary submissions not currently supported in batch mode.'
Expand Down Expand Up @@ -750,20 +753,17 @@ class Outbox {
this.store.dispatch(busy(false));
}

#_waiting = {};
#_lastOutbox = [];

waitForAll() {
return this.waitForItem('ALL');
}

waitForItem(id) {
var resolve;
const promise = new Promise(res => (resolve = res));
if (!this.#_waiting[id]) {
this.#_waiting[id] = [];
if (!this._waiting[id]) {
this._waiting[id] = [];
}
this.#_waiting[id].push(resolve);
this._waiting[id].push(resolve);
return promise;
}

Expand All @@ -772,26 +772,26 @@ class Outbox {
{ offline } = state,
{ outbox } = offline;

if (outbox === this.#_lastOutbox) {
if (outbox === this._lastOutbox) {
return;
}

const lastIds = {};
this.#_lastOutbox.forEach(action => {
this._lastOutbox.forEach(action => {
lastIds[action.meta.offline.effect.options.id] = true;
});
this.#_lastOutbox = outbox;
this._lastOutbox = outbox;

const pending = await this._allPendingItems();
if (!pending.length && this.#_waiting['ALL']) {
if (!pending.length && this._waiting['ALL']) {
this._resolveWaiting('ALL');
}

const pendingById = {};
pending.forEach(item => (pendingById[item.id] = true));

const checkIds = Object.keys(lastIds).concat(
Object.keys(this.#_waiting)
Object.keys(this._waiting)
);
checkIds.forEach(id => {
if (!pendingById[id] && id != 'ALL') {
Expand All @@ -801,7 +801,7 @@ class Outbox {
}

async _resolveWaiting(id) {
const waiting = this.#_waiting[id];
const waiting = this._waiting[id];
if (!waiting && !(this.app && this.app.hasPlugin('onsync'))) {
return;
}
Expand All @@ -811,7 +811,7 @@ class Outbox {
}
if (waiting) {
waiting.forEach(fn => fn(item));
delete this.#_waiting[id];
delete this._waiting[id];
}
}

Expand Down Expand Up @@ -972,12 +972,11 @@ class Outbox {
return this._parseJsonForm(item);
}

#_memoryItems = {};
async _loadItemData(item) {
if (!item || !item.options || !item.options.storage) {
return item;
} else if (item.options.storage == 'temporary') {
return setData(item, this.#_memoryItems[item.id]);
return setData(item, this._memoryItems[item.id]);
} else {
return this.store.lf
.getItem('outbox_' + item.id)
Expand Down Expand Up @@ -1025,7 +1024,7 @@ class Outbox {
return item;
}
if (item.options.storage == 'temporary') {
this.#_memoryItems[item.id] = item.data;
this._memoryItems[item.id] = item.data;
return this._withoutData(item);
} else {
return this.store.lf.setItem('outbox_' + item.id, item.data).then(
Expand Down Expand Up @@ -1064,9 +1063,9 @@ class Outbox {
validItems.forEach(item => {
validId[item.id] = true;
});
Object.keys(this.#_memoryItems).forEach(itemId => {
Object.keys(this._memoryItems).forEach(itemId => {
if (!validId[itemId]) {
delete this.#_memoryItems[itemId];
delete this._memoryItems[itemId];
}
});
const keys = await this.store.lf.keys();
Expand Down
104 changes: 53 additions & 51 deletions packages/store/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,40 @@ var _verbosity = {
};

class Store {
name;
debug = false;
// Base URL of web service
service;
// Default parameters (e.g f=json)
defaults = {};

ready = {
then: function() {
throw new Error('Call init first!');
}
};

// Registered redux functions
#reducers = {};
#enhanceReducers = [];
#persistKeys = [];
#transforms = [];
#middleware = [];
#enhancers = [];
#subscribers = [];
#deferActions = [];
#thunkHandler = null;

#_promises = {}; // Save promises to prevent redundant fetches

constructor(name) {
if (_stores[name]) {
throw name + ' store already exists!';
}
this.name = name;
_stores[name] = this;

this.debug = false;

// Base URL of web service
this.service = undefined;

// Default parameters (e.g f=json)
this.defaults = {};

this.ready = {
then: function() {
throw new Error('Call init first!');
}
};

// Registered redux functions
this._reducers = {};
this._enhanceReducers = [];
this._persistKeys = [];
this._transforms = [];
this._middleware = [];
this._enhancers = [];
this._subscribers = [];
this._deferActions = [];
this._thunkHandler = null;

this._promises = {}; // Save promises to prevent redundant fetches

this.addReducer(
'kvp',
(state, action) => this.kvpReducer(state, action),
Expand Down Expand Up @@ -92,13 +94,13 @@ class Store {
var storeReady;
self.ready = new Promise(resolve => (storeReady = resolve));

var reducer = combineReducers(this.#reducers);
this.#enhanceReducers.forEach(enhanceReducer => {
var reducer = combineReducers(this._reducers);
this._enhanceReducers.forEach(enhanceReducer => {
reducer = enhanceReducer(reducer);
});
const enhancers = compose(
...this.#enhancers,
applyMiddleware(...this.#middleware)
...this._enhancers,
applyMiddleware(...this._middleware)
);

this.lf = createStorage(this.name);
Expand All @@ -109,8 +111,8 @@ class Store {
stateReconciler: autoMergeLevel2,
serialize,
deserialize,
transforms: this.#transforms,
whitelist: this.#persistKeys,
transforms: this._transforms,
whitelist: this._persistKeys,
writeFailHandler: error => this.storageFail(error)
};
const persistedReducer = persistReducer(persistConfig, reducer);
Expand All @@ -122,20 +124,20 @@ class Store {
storeReady();
}
});
this.#subscribers.forEach(fn => this._store.subscribe(fn));
this.#deferActions.forEach(this._store.dispatch);
this._subscribers.forEach(fn => this._store.subscribe(fn));
this._deferActions.forEach(this._store.dispatch);
}

dispatch(action) {
if (this._store) {
return this._store.dispatch(action);
} else {
this.#deferActions.push(action);
this._deferActions.push(action);
}
}

subscribe(fn) {
this.#subscribers.push(fn);
this._subscribers.push(fn);
if (this._store) {
this._store.subscribe(fn);
}
Expand All @@ -146,49 +148,49 @@ class Store {
}

addReducer(name, reducer, persist, deserialize) {
this.#reducers[name] = reducer;
this._reducers[name] = reducer;
if (persist) {
this.persistKey(name, persist, deserialize);
}
}

addEnhanceReducer(name, enhanceReducer, persist, deserialize) {
this.#enhanceReducers.push(enhanceReducer);
this._enhanceReducers.push(enhanceReducer);
if (persist) {
this.persistKey(name, persist, deserialize);
}
}

persistKey(name, serialize, deserialize) {
this.#persistKeys.push(name);
this._persistKeys.push(name);
if (serialize && deserialize) {
this.#transforms.push(
this._transforms.push(
createTransform(serialize, deserialize, { whitelist: [name] })
);
}
}

addMiddleware(middleware) {
this.#middleware.push(middleware);
this._middleware.push(middleware);
}

addEnhancer(enhancer) {
this.#enhancers.push(enhancer);
this._enhancers.push(enhancer);
}

bindActionCreators(actions) {
return bindActionCreators(actions, this.dispatch.bind(this));
}

addThunk(name, thunk) {
if (!this.#thunkHandler) {
if (!this._thunkHandler) {
throw new Error('@wq/router is required to handle thunks');
}
this.#thunkHandler(name, thunk);
this._thunkHandler(name, thunk);
}

setThunkHandler(handler) {
this.#thunkHandler = handler;
this._thunkHandler = handler;
}

kvpReducer(state = {}, action) {
Expand Down Expand Up @@ -329,18 +331,18 @@ class Store {
delete data.format;
}

if (this.#_promises[key]) {
return this.#_promises[key];
if (this._promises[key]) {
return this._promises[key];
}

if (self.debugNetwork) {
console.log('fetching ' + key);
}

var promise = self.ajax(url, data, 'GET');
this.#_promises[key] = promise.then(
this._promises[key] = promise.then(
async data => {
delete this.#_promises[key];
delete this._promises[key];
if (!data) {
self.fetchFail(query, 'Error parsing data!');
return;
Expand All @@ -357,12 +359,12 @@ class Store {
return data;
},
error => {
delete this.#_promises[key];
delete this._promises[key];
console.error(error);
self.fetchFail(query, 'Error parsing data!');
}
);
return this.#_promises[key];
return this._promises[key];
}

// Hook to allow full AJAX customization
Expand Down

0 comments on commit 2dca219

Please sign in to comment.