Skip to content

Instantly share code, notes, and snippets.

View rony-arnac's full-sized avatar

Rony Fragin rony-arnac

  • Arnac
  • TLV
View GitHub Profile
@rony-arnac
rony-arnac / retry-able-connection.ts
Created January 18, 2023 09:55
retry establishing connection when the background service worker restarts
// Background service worker
function backgroundServiceHandshake() {
chrome.runtime.onMessage.addListener((message: any, _: any, sendResponse: (response: 'ack') => void) => {
if (message === 'syn') {
if (!isInitialized) {
initializeService();
isInitialized = true;
}
sendResponse('ack');
@rony-arnac
rony-arnac / better_storage_manager.ts
Created January 18, 2023 09:54
better storage management that handles concurrent access
const globalKey = 'FORDEFI_data';
const divider = '--';
async function storeComplexData (complexData: Record<string, string[]>) {
// Flatten the complex object to avoid it being overwritten
for (const [itemKey, itemValue] of Object.entries(complexData)) {
const newKey = `${globalKey}${divider}${itemKey}`;
await chrome.storage.local.set({[newKey]: itemValue});
}
}
@rony-arnac
rony-arnac / example_of_concurrent_storage_access.ts
Last active January 18, 2023 09:53
example of how Alice and Bob can overwrite each other's data
/**
* data = {
* balance: 100,
*. addresses: ['0x1', '0x2']
* }
*/
// Alice (1)
const aliceData = await getComplexData();
@rony-arnac
rony-arnac / simple_storage_manager.ts
Last active January 18, 2023 09:53
simple storage management that doesn't handle concurrent access
const key = 'FORDEFI_data';
async function storeComplexData (complexData: Record<string, string[]>) {
await chrome.storage.local.set({[key]: complexData});
}
async function getComplexData () {
return (await chrome.storage.local.get([key]))?.[key];
}