diff --git a/package.json b/package.json index 1f94688..f1ac6fa 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,6 @@ "url": "https://github.com/ForceDotComLabs/react.force.data/issues" }, "homepage": "https://github.com/ForceDotComLabs/react.force.data#readme", - "peerDependencies": { - "react-native": ">=0.20.0", - "react.force": "git+ssh://git@github.com/ForceDotComLabs/react.force.git" - }, "dependencies": { "lodash.findindex": "^4.4.0", "lodash.keys": "^4.0.7", diff --git a/src/addToBtLogoQueue.js b/src/addToBtLogoQueue.js new file mode 100644 index 0000000..5c7f004 --- /dev/null +++ b/src/addToBtLogoQueue.js @@ -0,0 +1,13 @@ +import btLogoQueue from './btLogoQueue'; + +module.exports = (opts) => { + return new Promise ( + (resolve, reject) => { + if(!opts.cachedBtLogoData || opts.nocache){ + btLogoQueue.add(opts.id); + } else { + console.log('skipping: already cached'); + } + resolve(opts); + }) +} diff --git a/src/addToChatterQueue.js b/src/addToChatterQueue.js new file mode 100644 index 0000000..353f369 --- /dev/null +++ b/src/addToChatterQueue.js @@ -0,0 +1,13 @@ +import chatterQueue from './chatterQueue'; + +module.exports = (opts) => { + return new Promise ( + (resolve, reject) => { + if(!opts.cachedChatterData || opts.nocache){ + chatterQueue.add(opts.id); + } else { + console.log('skipping: already cached'); + } + resolve(opts); + }) +} diff --git a/src/addToQueue.js b/src/addToQueue.js index 610db2c..d531328 100644 --- a/src/addToQueue.js +++ b/src/addToQueue.js @@ -38,4 +38,4 @@ module.exports = (opts) => { resolve(opts); } ); -}; \ No newline at end of file +}; diff --git a/src/btLogoQuery.js b/src/btLogoQuery.js new file mode 100644 index 0000000..f8b8d9a --- /dev/null +++ b/src/btLogoQuery.js @@ -0,0 +1,53 @@ +import {btClient} from 'react.force'; +import union from 'lodash.union'; +import remove from 'lodash.remove'; +import trim from 'lodash.trim'; + +let queryCount = 0; + +const listeners = []; + +const broadcast = (records) => { + const ids = records.map((record)=>{ + return record.id; + }); + listeners.forEach((listener)=>{ + listener(ids, records); + }); +} + +module.exports = (opts) => { + return new Promise( + (resolve, reject) => { + if(!opts.noCache && opts.cachedBtLogoData ){ + opts.btLogoData = opts.cachedBtLogoData; + resolve(opts); + return; + } + + queryCount++; + btClient.btLogoBatchRequest(opts.ids, (response)=>{ + if(!response.hasErrors && response.companyLogoResponseItems.length){ + const records = response.companyLogoResponseItems.map((r, index) => { + let result = r; + result.id = opts.ids[index]; + return result; + }); + broadcast(records); + } + resolve(opts); + }, + (error)=>{ + reject('Error: bluetail query'); + }) + } + ); +}; + +module.exports.addListener = (listener) => { + listeners.push(listener); +}; + +module.exports.getQueryCount = () => { + return queryCount; +}; diff --git a/src/btLogoQueue.js b/src/btLogoQueue.js new file mode 100644 index 0000000..2565af8 --- /dev/null +++ b/src/btLogoQueue.js @@ -0,0 +1,26 @@ +let btLogoQueue = []; +import keys from 'lodash.keys'; +import btLogoQuery from './btLogoQuery'; + +const get = () => { + return btLogoQueue.splice(0, btLogoQueue.length); +} +const add = (id) => { + if(!id){ + return; + } + if(btLogoQueue.indexOf(id) < 0){ + btLogoQueue.push(id); + } + setTimeout(()=>{ + if(btLogoQueue && btLogoQueue.length !== 0){ + const ids = get(); + return btLogoQuery({ids:ids}); + } + },300); +}; + +module.exports = { + add:add, + get:get +}; diff --git a/src/cacheBtLogoData.js b/src/cacheBtLogoData.js new file mode 100644 index 0000000..9333315 --- /dev/null +++ b/src/cacheBtLogoData.js @@ -0,0 +1,24 @@ +import query from './btLogoQuery'; + +const notify = (companyName,blueTailBlobs) => { + blueTailBlobs.forEach((blueTailBlob)=>{ + set(blueTailBlob, companyName); + }); +}; + +query.addListener(notify); + +let cache = {}; + +const get = (companyName)=>{ + return cache[companyName]; +}; + +const set = (blueTailBlob, companyName)=>{ + cache[companyName] = blueTailBlob; +}; + +module.exports = { + get:get, + set:set +}; diff --git a/src/cacheChatterData.js b/src/cacheChatterData.js new file mode 100644 index 0000000..040a715 --- /dev/null +++ b/src/cacheChatterData.js @@ -0,0 +1,24 @@ +import query from './chatterQuery'; + +const notify = (ids,chatterObjs) => { + chatterObjs.forEach((chatterObj)=>{ + set(chatterObj); + }); +}; + +query.addListener(notify); + +let cache = {}; + +const get = (id)=>{ + return cache[id]; +}; + +const set = (chatterObj)=>{ + cache[chatterObj.id] = chatterObj; +}; + +module.exports = { + get:get, + set:set +}; diff --git a/src/cacheReportData.js b/src/cacheReportData.js new file mode 100755 index 0000000..1e1917a --- /dev/null +++ b/src/cacheReportData.js @@ -0,0 +1,24 @@ +// import reportQuery +import query from './reportQuery'; + +const notify = (id,sobj) => { + set(sobj); +}; + +query.addListener(notify); + +let cache = {}; + +const get = (id)=>{ + return cache[id]; +}; + +const set = (sobj)=>{ + let reportId = sobj.attributes.reportId; + cache[reportId] = sobj; +}; + +module.exports = { + get:get, + set:set +}; diff --git a/src/chatterQuery.js b/src/chatterQuery.js new file mode 100644 index 0000000..136b0e9 --- /dev/null +++ b/src/chatterQuery.js @@ -0,0 +1,55 @@ +import {forceClient} from 'react.force'; +import union from 'lodash.union'; +import remove from 'lodash.remove'; +import trim from 'lodash.trim'; + +let queryCount = 0; + +const listeners = []; + +const broadcast = (records) => { + const ids = records.map((record)=>{ + return record.id; + }); + listeners.forEach((listener)=>{ + listener(ids, records); + }); +} + +module.exports = (opts) => { + return new Promise( + (resolve, reject) => { + if(!opts.noCache && opts.cachedChatterData ){ + opts.chatterData = opts.cachedChatterData; + resolve(opts); + return; + } + + queryCount++; + forceClient.bulkChatterUserPics(opts.ids, + (response)=>{ + if(!response.hasErrors && response.results.length){ + const records = response.results.map((r, index) => { + let result = r.result; + result.id = opts.ids[index]; + return result; + }); + broadcast(records); + } + resolve(opts); + }, + (error) => { + reject('Error: chatter query'); + } + ); + } + ); +}; + +module.exports.addListener = (listener) => { + listeners.push(listener); +}; + +module.exports.getQueryCount = () => { + return queryCount; +}; diff --git a/src/chatterQueue.js b/src/chatterQueue.js new file mode 100644 index 0000000..4cfdd9d --- /dev/null +++ b/src/chatterQueue.js @@ -0,0 +1,27 @@ +let chatterQueue = []; +import keys from 'lodash.keys'; +import chatterQuery from './chatterQuery'; + +const get = () => { + return chatterQueue.splice(0, chatterQueue.length); +} +const add = (id) => { + if(!id){ + return; + } + if(chatterQueue.indexOf(id) < 0){ + chatterQueue.push(id); + } + setTimeout(()=>{ + if(chatterQueue && chatterQueue.length !== 0){ + console.log('TRIGGER QUERY !!!'); + const ids = get(); + return chatterQuery({ids:ids}); + } + },300); +}; + +module.exports = { + add:add, + get:get +}; diff --git a/src/dashboard/getDashboardData.js b/src/dashboard/getDashboardData.js new file mode 100644 index 0000000..b12f045 --- /dev/null +++ b/src/dashboard/getDashboardData.js @@ -0,0 +1,34 @@ +import {forceClient} from 'react.force'; + +const listeners = []; + +const broadcast = (records) => { + const ids = records.map((record)=>{ + return record.id; + }); + listeners.forEach((listener)=>{ + listener(ids, records); + }); +} + +module.exports = (opts) => { + return new Promise( + (resolve, reject) => { + forceClient.dashboardData(opts.id, (response)=>{ + if(response){ + response.id = response.attributes.dashboardId; + broadcast([response]); + } + resolve(opts); + }, + (error)=>{ + reject('Error: dashboard response was not received'); + }) + } + ); +}; + +module.exports.addListener = (listener) => { + listeners.push(listener); +}; + diff --git a/src/dashboard/refreshDashboard.js b/src/dashboard/refreshDashboard.js new file mode 100644 index 0000000..d060b96 --- /dev/null +++ b/src/dashboard/refreshDashboard.js @@ -0,0 +1,40 @@ +import {forceClient} from 'react.force'; +import Interval from '../utils/interval'; +import waitForDashboardRefresh from './waitForDashboardRefresh'; + +let _dbRefreshLoop; + +module.exports = (opts) => { + return new Promise( + (resolve, reject)=> { + if(!opts.autoRefresh){ + resolve(opts); + return; + } + _dbRefreshLoop = new Interval(function(time){ + console.log('dbrefreshed@'+time); + forceClient.dashboardRefresh(opts.id, + (response)=>{ + if(response && response.statusUrl){ + resolve(opts); + waitForDashboardRefresh(opts); + return; + } else { + reject('Error: dashboard did not refresh properly'); + } + }, + (error)=>{ + reject('Error: dashboard refresh did not receive data'); + } + ); + } + ,opts.refreshInterval); + + _dbRefreshLoop.start(); + } + ); +} + +module.exports.stop = ()=>{ + _dbRefreshLoop && _dbRefreshLoop.stop(); +} diff --git a/src/dashboard/waitForDashboardRefresh.js b/src/dashboard/waitForDashboardRefresh.js new file mode 100644 index 0000000..3deb62e --- /dev/null +++ b/src/dashboard/waitForDashboardRefresh.js @@ -0,0 +1,31 @@ +import {forceClient} from 'react.force'; +import Interval from '../utils/interval'; +import getDashboardData from './getDashboardData'; + +let _dbStatusLoop; + +module.exports = (opts)=>{ + _dbStatusLoop = new Interval(function(time){ + console.log('dbstatuscheck@'+time); + forceClient.dashboardStatus(opts.id, + (response)=>{ + let isDbRefreshed = response.componentStatus.every((component)=>{ + return component.refreshStatus == 'IDLE'; + }) + if(isDbRefreshed){ + _dbStatusLoop.stop(); + getDashboardData(opts); + } + }, + (error)=>{ + reject('Error: there was an error in the dashboard refresh'); + } + ) + },30000); + + _dbStatusLoop.start(); +} + +module.exports.stop = ()=>{ + _dbStatusLoop && _dbStatusLoop.stop(); +} diff --git a/src/doReportQuery.js b/src/doReportQuery.js new file mode 100755 index 0000000..ca771c8 --- /dev/null +++ b/src/doReportQuery.js @@ -0,0 +1,13 @@ +import reportQuery from './reportQuery'; + +module.exports = (opts) => { + return new Promise ( + (resolve, reject) => { + if(!opts.cachedReportData || opts.nocache){ + reportQuery({id:opts.id}) + } else { + console.log('skipping: already cached'); + } + resolve(opts); + }) +} diff --git a/src/getBtLogoByCompanyName.js b/src/getBtLogoByCompanyName.js new file mode 100644 index 0000000..43bc949 --- /dev/null +++ b/src/getBtLogoByCompanyName.js @@ -0,0 +1,7 @@ +import getCachedBtLogoData from './getCachedBtLogoData'; +import addToBtLogoQueue from './addToBtLogoQueue'; + +module.exports = (id, noCache) => { + return getCachedBtLogoData({id:id, noCache:!!noCache}) + .then(addToBtLogoQueue); +}; diff --git a/src/getByChatterUserId.js b/src/getByChatterUserId.js new file mode 100644 index 0000000..ca8b525 --- /dev/null +++ b/src/getByChatterUserId.js @@ -0,0 +1,7 @@ +import getCachedChatterData from './getCachedChatterData'; +import addToChatterQueue from './addToChatterQueue'; + +module.exports = (id, noCache) => { + return getCachedChatterData({id:id, noCache:!!noCache}) + .then(addToChatterQueue); +}; diff --git a/src/getByReportId.js b/src/getByReportId.js new file mode 100755 index 0000000..b3cb759 --- /dev/null +++ b/src/getByReportId.js @@ -0,0 +1,7 @@ +import getCachedReportData from './getCachedReportData'; +import doReportQuery from './doReportQuery'; + +module.exports = (id, noCache) => { + return getCachedReportData({id:id, noCache:!!noCache}) + .then(doReportQuery); +}; diff --git a/src/getByTypeAndId.js b/src/getByTypeAndId.js index 5be25f4..b9f97b4 100644 --- a/src/getByTypeAndId.js +++ b/src/getByTypeAndId.js @@ -27,20 +27,16 @@ import getCachedSobj from './getCachedSobj'; import getCachedCompactLayout from './getCachedCompactLayout'; import getCompactLayout from './getCompactLayout'; -import doCacheCompactLayout from './doCacheCompactLayout'; -import getCachedDefaultLayout from './getCachedDefaultLayout'; -import getDefaultLayout from './getDefaultLayout'; +// import getCachedDefaultLayout from './getCachedDefaultLayout'; +// import getDefaultLayout from './getDefaultLayout'; import addToQueue from './addToQueue'; -import getMetaByType from './getMetaByType'; - -import queue from './queue'; module.exports = (type, id, noCache) => { return getCachedSobj({type:type,id:id, noCache:!!noCache}) .then(getCachedCompactLayout) .then(getCompactLayout) - .then(getCachedDefaultLayout) - .then(getDefaultLayout) + // .then(getCachedDefaultLayout) + // .then(getDefaultLayout) .then(addToQueue); -}; \ No newline at end of file +}; diff --git a/src/getCachedBtLogoData.js b/src/getCachedBtLogoData.js new file mode 100644 index 0000000..95f5e05 --- /dev/null +++ b/src/getCachedBtLogoData.js @@ -0,0 +1,11 @@ +import cacheBtLogoData from './cacheBtLogoData'; + +module.exports = (opts) => { + return new Promise( + (resolve, reject) => { + const id = opts.id; + const item = cacheBtLogoData.get(id); + opts.cachedBtLogoData = item; + resolve(opts); + }) +} diff --git a/src/getCachedChatterData.js b/src/getCachedChatterData.js new file mode 100644 index 0000000..b64e216 --- /dev/null +++ b/src/getCachedChatterData.js @@ -0,0 +1,11 @@ +import cacheChatterData from './cacheChatterData'; + +module.exports = (opts) => { + return new Promise( + (resolve, reject) => { + const id = opts.id; + const item = cacheChatterData.get(id); + opts.cachedChatterData = item; + resolve(opts); + }) +} diff --git a/src/getCachedCompactLayout.js b/src/getCachedCompactLayout.js index e606345..2c7ed00 100644 --- a/src/getCachedCompactLayout.js +++ b/src/getCachedCompactLayout.js @@ -23,7 +23,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - + import { AsyncStorage } from 'react-native'; import cacheCompact from './cacheCompact'; @@ -38,4 +38,4 @@ module.exports = (opts) => { resolve(opts); } ); -}; \ No newline at end of file +}; diff --git a/src/getCachedDefaultLayout.js b/src/getCachedDefaultLayout.js index f97513a..0f10be5 100644 --- a/src/getCachedDefaultLayout.js +++ b/src/getCachedDefaultLayout.js @@ -36,4 +36,4 @@ module.exports = (opts) => { resolve(opts); } ); -}; \ No newline at end of file +}; diff --git a/src/getCachedReportData.js b/src/getCachedReportData.js new file mode 100755 index 0000000..0b7061d --- /dev/null +++ b/src/getCachedReportData.js @@ -0,0 +1,11 @@ +import cacheReportData from './cacheReportData'; + +module.exports = (opts) => { + return new Promise( + (resolve, reject) => { + const id = opts.id; + const item = cacheReportData.get(id); + opts.cachedReportData = item; + resolve(opts); + }) +} diff --git a/src/getCachedSobj.js b/src/getCachedSobj.js index c1653fa..0b12784 100644 --- a/src/getCachedSobj.js +++ b/src/getCachedSobj.js @@ -23,7 +23,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - + import cache from './cache'; import storage from './storage'; @@ -31,7 +31,6 @@ import storage from './storage'; module.exports = (opts) => { return new Promise( (resolve, reject) => { - if(opts.noCache){ resolve(opts); return; @@ -44,14 +43,17 @@ module.exports = (opts) => { resolve(opts); return; } -/* + + storage.getItem(id, (item)=>{ if(item){ opts.cachedSobj = item; resolve(opts); + } else { + resolve(opts); } + return; }); -*/ } ); -}; \ No newline at end of file +}; diff --git a/src/getCompactLayout.js b/src/getCompactLayout.js index 3d70468..1edf020 100644 --- a/src/getCompactLayout.js +++ b/src/getCompactLayout.js @@ -56,7 +56,7 @@ module.exports = (opts) => { return; } requested.push(opts.type); - forceClient.compactLayout(opts.type, + forceClient.compactLayout(opts.type, (response) => { opts.compactLayout = response; pull(requested,opts.type); @@ -70,4 +70,4 @@ module.exports = (opts) => { module.exports.addListener = (listener) => { listeners.push(listener); -}; \ No newline at end of file +}; diff --git a/src/getDashboardById.js b/src/getDashboardById.js new file mode 100644 index 0000000..ac835ac --- /dev/null +++ b/src/getDashboardById.js @@ -0,0 +1,7 @@ +import getDashboardData from './dashboard/getDashboardData'; +import refreshDashboard from './dashboard/refreshDashboard'; + +module.exports = (id, autoRefresh, refreshInterval, dbRefreshLoop, dbStatusLoop) => { + return getDashboardData({id: id, autoRefresh:autoRefresh, refreshInterval: refreshInterval, dbRefreshLoop:dbRefreshLoop, dbStatusLoop:dbStatusLoop}) + .then(refreshDashboard) +} diff --git a/src/getDefaultLayout.js b/src/getDefaultLayout.js index fdb624d..c389516 100644 --- a/src/getDefaultLayout.js +++ b/src/getDefaultLayout.js @@ -23,7 +23,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - + import {forceClient} from 'react.force'; import pull from 'lodash.pull'; @@ -51,7 +51,7 @@ module.exports = (opts) => { return; } requested.push(opts.type); - forceClient.defaultLayout(opts.type, + forceClient.defaultLayout(opts.type, (response) => { opts.defaultLayout = response; pull(requested,opts.type); @@ -65,4 +65,4 @@ module.exports = (opts) => { module.exports.addListener = (listener) => { listeners.push(listener); -}; \ No newline at end of file +}; diff --git a/src/getMetaByType.js b/src/getMetaByType.js index 6bc6341..661494a 100644 --- a/src/getMetaByType.js +++ b/src/getMetaByType.js @@ -23,18 +23,17 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - -import getDefaultLayout from './getDefaultLayout'; -import getDefaultLayoutFieldNames from './getDefaultLayoutFieldNames'; + +// import getDefaultLayout from './getDefaultLayout'; +// import getDefaultLayoutFieldNames from './getDefaultLayoutFieldNames'; import getCompactLayout from './getCompactLayout'; import getCompactLayoutFieldNames from './getCompactLayoutFieldNames'; -import query from './query'; -import getCompactLayoutTitle from './getCompactLayoutTitle'; -import doCacheSobj from './doCacheSobj'; -import getCachedSobj from './getCachedSobj'; -import getCachedDefaultLayout from './getCachedDefaultLayout'; +// import getCompactLayoutTitle from './getCompactLayoutTitle'; +// import doCacheSobj from './doCacheSobj'; +// import getCachedSobj from './getCachedSobj'; +// import getCachedDefaultLayout from './getCachedDefaultLayout'; import getCachedCompactLayout from './getCachedCompactLayout'; -import doCacheDefaultLayout from './doCacheDefaultLayout'; +// import doCacheDefaultLayout from './doCacheDefaultLayout'; import doCacheCompactLayout from './doCacheCompactLayout'; @@ -44,8 +43,8 @@ module.exports = (opts) => { .then(getCompactLayout) .then(doCacheCompactLayout) .then(getCompactLayoutFieldNames) - .then(getCachedDefaultLayout) - .then(getDefaultLayout) - .then(doCacheDefaultLayout) - .then(getDefaultLayoutFieldNames); -}; \ No newline at end of file + // .then(getCachedDefaultLayout) + // .then(getDefaultLayout) + // .then(doCacheDefaultLayout) + // .then(getDefaultLayoutFieldNames); +}; diff --git a/src/index.js b/src/index.js index c5d0e77..c7ccb0d 100644 --- a/src/index.js +++ b/src/index.js @@ -23,7 +23,6 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - import getDefaultLayout from './getDefaultLayout'; import getCompactLayout from './getCompactLayout'; import getCompactLayoutFieldNames from './getCompactLayoutFieldNames'; @@ -41,6 +40,20 @@ import requestWithTypeAndId from './requestWithTypeAndId'; import clearCache from './clearCache'; +import chatterQuery from './chatterQuery'; +import getByChatterUserId from './getByChatterUserId'; + +import getBtLogoByCompanyName from './getBtLogoByCompanyName'; +import btLogoQuery from './btLogoQuery'; + +import getByReportId from './getByReportId'; +import reportQuery from './reportQuery'; + +import getDashboardById from './getDashboardById'; +import getDashboardData from './dashboard/getDashboardData'; +import waitForDashboardRefresh from './dashboard/waitForDashboardRefresh'; +import refreshDashboard from './dashboard/refreshDashboard'; + module.exports = { getByTypeAndId:getByTypeAndId, getDefaultLayout:getDefaultLayout, @@ -49,5 +62,15 @@ module.exports = { query:query, utils:utils, requestWithTypeAndId:requestWithTypeAndId, - clearCache:clearCache -}; \ No newline at end of file + clearCache:clearCache, + chatterQuery: chatterQuery, + getByChatterUserId: getByChatterUserId, + getBtLogoByCompanyName: getBtLogoByCompanyName, + btLogoQuery: btLogoQuery, + reportQuery: reportQuery, + getByReportId: getByReportId, + getDashboardById: getDashboardById, + getDashboardData: getDashboardData, + waitForDashboardRefresh: waitForDashboardRefresh, + refreshDashboard: refreshDashboard +}; diff --git a/src/query.js b/src/query.js index b482565..7624044 100644 --- a/src/query.js +++ b/src/query.js @@ -23,7 +23,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - + import {forceClient} from 'react.force'; import union from 'lodash.union'; import remove from 'lodash.remove'; @@ -99,4 +99,4 @@ module.exports.addListener = (listener) => { }; module.exports.getQueryCount = () => { return queryCount; -}; \ No newline at end of file +}; diff --git a/src/reportQuery.js b/src/reportQuery.js new file mode 100755 index 0000000..31ae30f --- /dev/null +++ b/src/reportQuery.js @@ -0,0 +1,47 @@ +import {forceClient} from 'react.force'; +import union from 'lodash.union'; +import remove from 'lodash.remove'; +import trim from 'lodash.trim'; + +let queryCount = 0; + +const listeners = []; + +const broadcast = (reportResponse) => { + //only one report to send back + const reportId = reportResponse.attributes.reportId; + listeners.forEach((listener)=>{ + listener(reportId, reportResponse); + }); +} + +module.exports = (opts) => { + return new Promise( + (resolve, reject) => { + if(!opts.noCache && opts.cachedReportData){ + opts.reportData = opts.cachedReportData; + resolve(opts); + return; + } + + queryCount++; + forceClient.reportData(opts.id, + (response)=>{ + broadcast(response); + resolve(opts); + }, + (error)=> { + console.warn(error); + } + ); + } + ); +}; + +module.exports.addListener = (listener) => { + listeners.push(listener); +}; + +module.exports.getQueryCount = () => { + return queryCount; +}; diff --git a/src/storage.js b/src/storage.js index 0006c97..3306d3a 100644 --- a/src/storage.js +++ b/src/storage.js @@ -24,17 +24,12 @@ * POSSIBILITY OF SUCH DAMAGE. */ -import {smartstore} from 'react.force'; +import { AsyncStorage } from 'react-native'; import query from './query'; import cache from './cache'; -const PREFIX = 'sobj_'; - -const SOUP_NAME = PREFIX+'Soup'; - -const externalID = '__shortId'; const notify = (ids,sobjs) => { sobjs.forEach((sobj)=>{ @@ -43,70 +38,52 @@ const notify = (ids,sobjs) => { }; const populateCache = ()=>{ - const querySpec = smartstore.buildAllQuerySpec('__shortId', "ascending", 100); + AsyncStorage.getAllKeys((err, keys) => { + AsyncStorage.multiGet(keys, (err, stores) => { + stores.forEach((result, i, store) => { + const sobj = JSON.parse(store[i][1]); + cache.set(sobj); + }); + }); - smartstore.querySoup( - false, - SOUP_NAME, - querySpec, - (cursor)=>{ - const items = cursor.currentPageOrderedEntries; - if(items && items.length){ - items.forEach((sobj)=>{ - if(sobj.attributes && sobj.attributes.shortId){ - cache.set(sobj); - } - }); - } + }); +}; - }, - (err)=>{ - console.log('ERR: ',err); - }); +const set = (sobj)=>{ + if(sobj && sobj.attributes && sobj.attributes.shortId){ + setItem(sobj.attributes.shortId,sobj); + } }; -init = ()=>{ - smartstore.registerSoup(false, - SOUP_NAME, - [ - {path:externalID, type:"string"} - ], - () => { - populateCache(); - query.addListener(notify); - } - ); +const getShotId = (id)=>{ + if(id && id.length>=15){ + return id.substring(0,15); + } }; -clearAll = ()=>{ - smartstore.clearSoup(false,SOUP_NAME); +const setItem = (id,sobj,callback)=>{ + const shortId = getShotId(id); + return AsyncStorage.setItem(shortId,JSON.stringify(sobj),callback); }; -set = (sobj, callback)=>{ - if(sobj && sobj.attributes && sobj.attributes.shortId){ - sobj[externalID] = sobj.attributes.shortId; - smartstore.upsertSoupEntriesWithExternalId(false, - SOUP_NAME, [ sobj ],externalID, - (sobjs) => { - console.log('DONE UPSERTING!'); - if(callback){ - callback(sobjs); - } - }, - (err) => { - console.log('ERROR!'); - if(callback){ - callback(); - } +const getItem = (id,callback)=>{ + const shortId = getShotId(id); + return AsyncStorage.getItem(shortId,(err, result)=>{ + if(callback){ + if(err || !result){ + return callback(); } - ); - } + const sobj = JSON.parse(result); + return callback(sobj); + } + }); }; - -init(); +populateCache(); +query.addListener(notify); module.exports = { - set:set, - clearAll:clearAll + getItem:getItem, + setItem:setItem, + set:set }; \ No newline at end of file diff --git a/src/storageCompact.js b/src/storageCompact.js index 1dea84f..4bdb65a 100644 --- a/src/storageCompact.js +++ b/src/storageCompact.js @@ -24,15 +24,12 @@ * POSSIBILITY OF SUCH DAMAGE. */ -import {smartstore} from 'react.force'; +import { AsyncStorage } from 'react-native'; import getCompactLayout from './getCompactLayout'; -import cacheCompact from './cacheCompact'; - -const PREFIX = 'compactLayout_'; -const SOUP_NAME = PREFIX+'Soup'; +import cacheCompact from './cacheCompact'; const notify = (type, compactLayout) => { if(type, compactLayout){ @@ -41,46 +38,20 @@ const notify = (type, compactLayout) => { }; const populateCache = ()=>{ - const querySpec = smartstore.buildAllQuerySpec('id', "ascending", 100); - - smartstore.querySoup( - false, - SOUP_NAME, - querySpec, - (cursor)=>{ - const items = cursor.currentPageOrderedEntries; - if(items && items.length){ - items.forEach((compactLayout)=>{ - if(compactLayout.id){ - const type = compactLayout.id.substring(PREFIX.length); - if(type){ - cacheCompact.set(type,compactLayout); - } + AsyncStorage.getAllKeys((err, keys) => { + AsyncStorage.multiGet(keys, (err, stores) => { + stores.forEach((result, i, store) => { + const key = store[i][0]; + if(key && key.indexOf('compactLayout_')===0){ + const compact = JSON.parse(store[i][1]); + if(compact && compact.objectType){ + cacheCompact.set(compact.objectType,compact); } - }); - } - - }, - (err)=>{ - console.log('ERR: ',err); + } + }); }); -}; -init = ()=>{ - smartstore.registerSoup(false, - SOUP_NAME, - [ - {path:"id", type:"string"} - ], - () => { - populateCache(); - getCompactLayout.addListener(notify); - } - ); -}; - -clearAll = ()=>{ - smartstore.clearSoup(false,SOUP_NAME); + }); }; const set = (type,compactLayout)=>{ @@ -90,33 +61,32 @@ const set = (type,compactLayout)=>{ }; const getLayoutKey = (type)=>{ - return PREFIX+type; + return 'compactLayout_'+type; }; const setItem = (type,compactLayout,callback)=>{ const key = getLayoutKey(type); - compactLayout.id = key; - smartstore.upsertSoupEntriesWithExternalId(false, - SOUP_NAME, [ compactLayout ],'id', - (compactLayouts) => { - console.log('DONE UPSERTING!'); - if(callback){ - callback(compactLayouts); - } - }, - (err) => { - console.log('ERROR!'); - if(callback){ - callback(); + return AsyncStorage.setItem(key,JSON.stringify(compactLayout),callback); +}; + +const getItem = (type,callback)=>{ + const key = getLayoutKey(type); + return AsyncStorage.getItem(key,(err, result)=>{ + if(callback){ + if(err || !result){ + return callback(); } + const compactLayout = JSON.parse(result); + return callback(compactLayout); } - ); + }); }; - -init(); +populateCache(); +getCompactLayout.addListener(notify); module.exports = { - set:set, - clearAll:clearAll + getItem:getItem, + setItem:setItem, + set:set }; \ No newline at end of file diff --git a/src/storageDefaultLayout.js b/src/storageDefaultLayout.js index 6470d98..fce6182 100644 --- a/src/storageDefaultLayout.js +++ b/src/storageDefaultLayout.js @@ -24,17 +24,15 @@ * POSSIBILITY OF SUCH DAMAGE. */ -import {smartstore} from 'react.force'; +import { AsyncStorage } from 'react-native'; import getDefaultLayout from './getDefaultLayout'; + import cacheDefault from './cacheDefault'; const PREFIX = 'defaultLayout_'; -const SOUP_NAME = PREFIX+'Soup'; - - const notify = (type, defaultLayout) => { if(type, defaultLayout){ set(type,defaultLayout); @@ -42,46 +40,21 @@ const notify = (type, defaultLayout) => { }; const populateCache = ()=>{ - const querySpec = smartstore.buildAllQuerySpec('id', "ascending", 100); - - smartstore.querySoup( - false, - SOUP_NAME, - querySpec, - (cursor)=>{ - const items = cursor.currentPageOrderedEntries; - if(items && items.length){ - items.forEach((defaultLayout)=>{ - if(defaultLayout.id){ - const type = defaultLayout.id.substring(PREFIX.length); - if(type){ - cacheDefault.set(type,defaultLayout); - } + AsyncStorage.getAllKeys((err, keys) => { + AsyncStorage.multiGet(keys, (err, stores) => { + stores.forEach((result, i, store) => { + const key = store[i][0]; + if(key && key.indexOf(PREFIX)===0){ + const defaultLayout = JSON.parse(store[i][1]); + const type = key.substring(PREFIX.length); + if(defaultLayout && type && type.length){ + cacheDefault.set(type,defaultLayout); } - }); - } - - }, - (err)=>{ - console.log('ERR: ',err); + } + }); }); -}; -clearAll = ()=>{ - smartstore.clearSoup(false,SOUP_NAME); -}; - -init = ()=>{ - smartstore.registerSoup(false, - SOUP_NAME, - [ - {path:"id", type:"string"} - ], - () => { - populateCache(); - getDefaultLayout.addListener(notify); - } - ); + }); }; const set = (type,defaultLayout)=>{ @@ -94,30 +67,29 @@ const getLayoutKey = (type)=>{ return PREFIX+type; }; -const setItem = (type,defaultLayout,callback)=>{ +const setItem = (type,compactLayout,callback)=>{ const key = getLayoutKey(type); - defaultLayout.id = key; - smartstore.upsertSoupEntriesWithExternalId(false, - SOUP_NAME, [ defaultLayout ],'id', - (defaultLayouts) => { - console.log('DONE UPSERTING!'); - if(callback){ - callback(defaultLayouts); - } - }, - (err) => { - console.log('ERROR!'); - if(callback){ - callback(); + return AsyncStorage.setItem(key,JSON.stringify(compactLayout),callback); +}; + +const getItem = (type,callback)=>{ + const key = getLayoutKey(type); + return AsyncStorage.getItem(key,(err, result)=>{ + if(callback){ + if(err || !result){ + return callback(); } + const defaultLayout = JSON.parse(result); + return callback(defaultLayout); } - ); + }); }; - -init(); +populateCache(); +getDefaultLayout.addListener(notify); module.exports = { - set:set, - clearAll:clearAll + getItem:getItem, + setItem:setItem, + set:set }; \ No newline at end of file diff --git a/src/utils/interval.js b/src/utils/interval.js new file mode 100644 index 0000000..393aaf9 --- /dev/null +++ b/src/utils/interval.js @@ -0,0 +1,26 @@ +module.exports = function(render, interval){ + this.start = start; + this.stop = stop; + + let timeoutId, lastTime; + + function start(){ + timeoutId = setTimeout(loop, 0); + lastTime = Date.now(); + return lastTime; + } + + function stop(){ + clearTimeout(timeoutId); + return lastTime; + } + + function loop(){ + let thisTime = Date.now(); + var deltaTime = thisTime - lastTime; + var delay = Math.max(interval - deltaTime, 0); + timeoutId = setTimeout(loop, delay); + lastTime = thisTime + delay; + render(thisTime); + } +}