From 2dca219bf3323c5b4f3f9b2165d4ea92ce5586da Mon Sep 17 00:00:00 2001 From: "S. Andrew Sheppard" Date: Wed, 24 Jun 2020 11:19:11 +0900 Subject: [PATCH] use only standard JS --- packages/outbox/src/outbox.js | 37 ++++++------ packages/store/src/store.js | 104 +++++++++++++++++----------------- 2 files changed, 71 insertions(+), 70 deletions(-) diff --git a/packages/outbox/src/outbox.js b/packages/outbox/src/outbox.js index f37ed56b..e54e4c4c 100644 --- a/packages/outbox/src/outbox.js +++ b/packages/outbox/src/outbox.js @@ -64,6 +64,9 @@ class Outbox { this.maxRetries = 10; this.csrftoken = null; this.csrftokenField = 'csrfmiddlewaretoken'; + this._memoryItems = {}; + this._waiting = {}; + this._lastOutbox = []; } init(opts) { @@ -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.' @@ -750,9 +753,6 @@ class Outbox { this.store.dispatch(busy(false)); } - #_waiting = {}; - #_lastOutbox = []; - waitForAll() { return this.waitForItem('ALL'); } @@ -760,10 +760,10 @@ class Outbox { 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; } @@ -772,18 +772,18 @@ 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'); } @@ -791,7 +791,7 @@ class Outbox { 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') { @@ -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; } @@ -811,7 +811,7 @@ class Outbox { } if (waiting) { waiting.forEach(fn => fn(item)); - delete this.#_waiting[id]; + delete this._waiting[id]; } } @@ -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) @@ -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( @@ -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(); diff --git a/packages/store/src/store.js b/packages/store/src/store.js index 8df327ad..9e92a5d2 100644 --- a/packages/store/src/store.js +++ b/packages/store/src/store.js @@ -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), @@ -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); @@ -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); @@ -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); } @@ -146,34 +148,34 @@ 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) { @@ -181,14 +183,14 @@ class Store { } 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) { @@ -329,8 +331,8 @@ class Store { delete data.format; } - if (this.#_promises[key]) { - return this.#_promises[key]; + if (this._promises[key]) { + return this._promises[key]; } if (self.debugNetwork) { @@ -338,9 +340,9 @@ class Store { } 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; @@ -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