1- import React from ' https://dev.jspm.io/react' ;
2- import Cache from ' ../src/Browser/CacheClassBrowser.js' ;
3- import { insertTypenames } from ' ../src/Browser/insertTypenames.js' ;
1+ import React from " https://dev.jspm.io/react" ;
2+ import BrowserCache from " ../src/Browser/CacheClassBrowser.js" ;
3+ import { insertTypenames } from " ../src/Browser/insertTypenames.js" ;
44
55const cacheContext = React . createContext ( ) ;
66
77function ObsidianWrapper ( props ) {
8- const [ cache , setCache ] = React . useState ( new Cache ( ) ) ;
8+ const [ cache , setCache ] = React . useState ( new BrowserCache ( ) ) ;
9+
10+ // You have to put your Google Chrome Obsidian developer tool extension id to connect Obsidian Wrapper with dev tool
11+ const chromeExtensionId = "dkbfipkapkljpdbhdihnlnbieffhjdmh" ;
12+ window . localStorage . setItem ( "cache" , JSON . stringify ( cache ) ) ;
913
1014 async function query ( query , options = { } ) {
15+ // dev tool messages
16+ const startTime = Date . now ( ) ;
17+ chrome . runtime . sendMessage ( chromeExtensionId , { query : query } ) ;
18+ chrome . runtime . sendMessage ( chromeExtensionId , {
19+ cache : window . localStorage . getItem ( "cache" ) ,
20+ } ) ;
21+ console . log (
22+ "Here's the message content: " ,
23+ window . localStorage . getItem ( "cache" )
24+ ) ;
1125 // set the options object default properties if not provided
1226 const {
13- endpoint = ' /graphql' ,
27+ endpoint = " /graphql" ,
1428 cacheRead = true ,
1529 cacheWrite = true ,
1630 pollInterval = null ,
@@ -36,9 +50,14 @@ function ObsidianWrapper(props) {
3650 // when the developer decides to only utilize whole query for cache
3751 if ( wholeQuery ) resObj = await cache . readWholeQuery ( query ) ;
3852 else resObj = await cache . read ( query ) ;
53+ console . log ( "query function resObj: " , resObj ) ;
3954 // check if query is stored in cache
4055 if ( resObj ) {
4156 // returning cached response as a promise
57+ const cacheHitResponseTime = Date . now ( ) - startTime ;
58+ chrome . runtime . sendMessage ( chromeExtensionId , {
59+ cacheHitResponseTime : cacheHitResponseTime ,
60+ } ) ;
4261 return new Promise ( ( resolve , reject ) => resolve ( resObj ) ) ;
4362 }
4463 // execute graphql fetch request if cache miss
@@ -55,10 +74,10 @@ function ObsidianWrapper(props) {
5574 try {
5675 // send fetch request with query
5776 const resJSON = await fetch ( endpoint , {
58- method : ' POST' ,
77+ method : " POST" ,
5978 headers : {
60- ' Content-Type' : ' application/json' ,
61- Accept : ' application/json' ,
79+ " Content-Type" : " application/json" ,
80+ Accept : " application/json" ,
6281 } ,
6382 body : JSON . stringify ( { query } ) ,
6483 } ) ;
@@ -69,6 +88,14 @@ function ObsidianWrapper(props) {
6988 if ( wholeQuery ) cache . writeWholeQuery ( query , deepResObj ) ;
7089 else cache . write ( query , deepResObj ) ;
7190 }
91+ const cacheMissResponseTime = Date . now ( ) - startTime ;
92+ chrome . runtime . sendMessage ( chromeExtensionId , {
93+ cacheMissResponseTime : cacheMissResponseTime ,
94+ } ) ;
95+ console . log (
96+ "Here's the response time on the front end: " ,
97+ cacheMissResponseTime
98+ ) ;
7299 return resObj ;
73100 } catch ( e ) {
74101 console . log ( e ) ;
@@ -80,39 +107,86 @@ function ObsidianWrapper(props) {
80107 function clearCache ( ) {
81108 cache . cacheClear ( ) ;
82109 }
83- // mutate method, refer to mutate.js for more info
110+
111+ // breaking out writethrough logic vs. non-writethrough logic
84112 async function mutate ( mutation , options = { } ) {
85- // set the options object default properties if not provided
113+ // dev tool messages
114+ chrome . runtime . sendMessage ( chromeExtensionId , {
115+ mutation : mutation ,
116+ } ) ;
117+ const startTime = Date . now ( ) ;
86118 mutation = insertTypenames ( mutation ) ;
87119 const {
88- endpoint = ' /graphql' ,
120+ endpoint = " /graphql" ,
89121 cacheWrite = true ,
90122 toDelete = false ,
91123 update = null ,
124+ writeThrough = false ,
92125 } = options ;
93- // for any mutation a request to the server is made
94126 try {
95- const responseObj = await fetch ( endpoint , {
96- method : 'POST' ,
97- headers : {
98- 'Content-Type' : 'application/json' ,
99- Accept : 'application/json' ,
100- } ,
101- body : JSON . stringify ( { query : mutation } ) ,
102- } ) . then ( ( resp ) => resp . json ( ) ) ;
103- if ( ! cacheWrite ) return responseObj ;
104- // first behaviour when delete cache is set to true
105- if ( toDelete ) {
106- cache . write ( mutation , responseObj , true ) ;
127+ if ( writeThrough ) {
128+ // if it's a deletion, then delete from cache and return the object
129+ if ( toDelete ) {
130+ const responseObj = await cache . writeThrough (
131+ mutation ,
132+ { } ,
133+ true ,
134+ endpoint
135+ ) ;
136+ const deleteMutationResponseTime = Date . now ( ) - startTime ;
137+ chrome . runtime . sendMessage ( chromeExtensionId , {
138+ deleteMutationResponseTime : deleteMutationResponseTime ,
139+ } ) ;
140+ return responseObj ;
141+ } else {
142+ // for add mutation
143+ const responseObj = await cache . writeThrough (
144+ mutation ,
145+ { } ,
146+ false ,
147+ endpoint
148+ ) ;
149+ // for update mutation
150+ if ( update ) {
151+ // run the update function
152+ update ( cache , responseObj ) ;
153+ }
154+ // always write/over-write to cache (add/update)
155+ // GQL call to make changes and synchronize database
156+ console . log ( "WriteThrough - true " , responseObj ) ;
157+ const addOrUpdateMutationResponseTime = Date . now ( ) - startTime ;
158+ chrome . runtime . sendMessage ( chromeExtensionId , {
159+ addOrUpdateMutationResponseTime : addOrUpdateMutationResponseTime ,
160+ } ) ;
161+ return responseObj ;
162+ }
163+ } else {
164+ // copy-paste mutate logic from 4.
165+
166+ // use cache.write instead of cache.writeThrough
167+ const responseObj = await fetch ( endpoint , {
168+ method : "POST" ,
169+ headers : {
170+ "Content-Type" : "application/json" ,
171+ Accept : "application/json" ,
172+ } ,
173+ body : JSON . stringify ( { query : mutation } ) ,
174+ } ) . then ( ( resp ) => resp . json ( ) ) ;
175+ if ( ! cacheWrite ) return responseObj ;
176+ // first behaviour when delete cache is set to true
177+ if ( toDelete ) {
178+ cache . write ( mutation , responseObj , true ) ;
179+ return responseObj ;
180+ }
181+ // second behaviour if update function provided
182+ if ( update ) {
183+ update ( cache , responseObj ) ;
184+ }
185+ // third behaviour just for normal update (no-delete, no update function)
186+ cache . write ( mutation , responseObj ) ;
187+ console . log ( "WriteThrough - false " , responseObj ) ;
107188 return responseObj ;
108189 }
109- // second behaviour if update function provided
110- if ( update ) {
111- update ( cache , responseObj ) ;
112- }
113- // third behaviour just for normal update (no-delete, no update function)
114- cache . write ( mutation , responseObj ) ;
115- return responseObj ;
116190 } catch ( e ) {
117191 console . log ( e ) ;
118192 }
0 commit comments