Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(local-storage): enhance local storage availability checks FE-1221 #132

Merged
merged 54 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
6176e57
feat(local-storage): enhance local storage availability checks
nelitow Dec 22, 2024
3d71ed5
feat(local-storage): add localStorage availability check
nelitow Dec 22, 2024
e25eda0
fix(local-storage): improve error handling for localStorage operations
nelitow Dec 22, 2024
a9acdca
Update pnpm-lock.yaml
nelitow Dec 22, 2024
b13d1b4
audit
nelitow Dec 22, 2024
c08bf35
fiz audit nanoid
nelitow Dec 22, 2024
d276621
lint
nelitow Dec 22, 2024
5dcee0f
test(react-xstore): enhance test assertions for spy calls in ReactFac…
nelitow Dec 24, 2024
6c6b8c6
lint: prettier
nelitow Dec 24, 2024
1d5cae9
chore: update package.json dependencies to allow newer versions of na…
nelitow Dec 24, 2024
d0688f9
Update pnpm-lock.yaml
nelitow Dec 24, 2024
0eb5fcf
chore: add changeset for minor updates to local-storage and react-xst…
nelitow Dec 24, 2024
672eaec
chore: update baseBranch in changeset config from 'main' to 'origin/m…
nelitow Dec 24, 2024
0c0ba90
Revert "chore: update baseBranch in changeset config from 'main' to '…
nelitow Dec 24, 2024
d3ea02b
Create fuel-labs-ci.md
nelitow Dec 24, 2024
cb3ea5f
chore: standardize formatting in fuel-labs-ci.md for dependency patch…
nelitow Dec 24, 2024
853036c
Update pnpm-lock.yaml
nelitow Dec 24, 2024
a293d6c
Delete fuel-labs-ci.md
nelitow Dec 24, 2024
79f026e
Merge branch 'main' into nj/feat/check-localStorage-availability
nelitow Dec 24, 2024
9e12200
Enhance GitHub Actions workflow with debug information for Git status
nelitow Dec 24, 2024
ade7c4f
Update pr.yaml
nelitow Dec 24, 2024
7cdcbad
Enhance GitHub Actions workflow by adding a step to set up the Git br…
nelitow Dec 24, 2024
efc9537
pr
nelitow Dec 24, 2024
22418b1
Update pr.yaml
nelitow Dec 24, 2024
8cdf1c7
Enhance GitHub Actions workflow by adding a step to fetch the main br…
nelitow Dec 24, 2024
7e6b8eb
Refactor GitHub Actions workflow to include Git configuration and str…
nelitow Dec 24, 2024
7333d70
pr
nelitow Dec 24, 2024
c84cc8a
fix changeset command
nelitow Dec 24, 2024
a372a13
Update pr.yaml
nelitow Dec 24, 2024
88fe0d8
Enhance GitHub Actions workflow by adding a Git setup step that confi…
nelitow Dec 24, 2024
25da362
Update pr.yaml
nelitow Dec 24, 2024
37aa6a5
Update pr.yaml
nelitow Dec 24, 2024
8390dab
Update pr.yaml
nelitow Dec 24, 2024
3e8b0b5
Update pr.yaml
nelitow Dec 24, 2024
de6db7d
Refactor GitHub Actions workflow to replace manual main branch fetch …
nelitow Dec 24, 2024
e8dabcd
Update pr.yaml
nelitow Dec 24, 2024
59372f4
Update pr.yaml
nelitow Dec 24, 2024
d79d313
Update pr.yaml
nelitow Dec 24, 2024
988853a
pr
nelitow Dec 24, 2024
4dcec8b
lock
nelitow Dec 24, 2024
a110a86
up
nelitow Dec 24, 2024
b4052bd
changesets/changelog-github
nelitow Dec 24, 2024
329b5b6
Update pr.yaml
nelitow Dec 24, 2024
c530b79
Update pr.yaml
nelitow Dec 24, 2024
db15a8f
Update pr.yaml
nelitow Dec 24, 2024
74f5631
Update pr.yaml
nelitow Dec 24, 2024
c565923
Update pr.yaml
nelitow Dec 24, 2024
9d683cd
Update pr.yaml
nelitow Dec 24, 2024
519f9f3
Update pr.yaml
nelitow Dec 25, 2024
d0e5015
ci: undo changeset package change
nelitow Dec 25, 2024
a2720f8
ci: undo changeset change
nelitow Dec 25, 2024
d403163
ch: update pnpm lock
nelitow Dec 25, 2024
9a3aeb4
Merge branch 'main' into nj/feat/check-localStorage-availability
nelitow Dec 25, 2024
90588e1
Update curvy-news-work.md
LuizAsFight Dec 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-news-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fuels/react-xstore': minor
---

Update tests to handle the new local-storage checks.
5 changes: 5 additions & 0 deletions .changeset/flat-keys-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fuels/local-storage': minor
---

Check for localStorage availability
1 change: 0 additions & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ jobs:
steps:
- uses: actions/checkout@v3
with:
# need this to get full git-history/clone in order to build changelogs and check changesets
fetch-depth: 0

- name: CI Setup
Expand Down
46 changes: 38 additions & 8 deletions packages/local-storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@ import { EventEmitter } from 'events';
export class LocalStorage {
private prefix!: string;
private emitter!: EventEmitter;
private isAvailable: boolean;

constructor(prefix: string, emitter?: EventEmitter) {
this.prefix = prefix;
this.emitter = emitter ?? new EventEmitter();
this.isAvailable = this.checkLocalStorageAvailability();
}

private checkLocalStorageAvailability(): boolean {
try {
if (typeof window === 'undefined' || !window.localStorage) return false;
const testKey = '__storage_test__';
localStorage.setItem(testKey, testKey);
localStorage.removeItem(testKey);
return true;
} catch {
return false;
}
}

subscribe = (listener: <T extends unknown[]>(...args: T) => void) => {
Expand All @@ -18,11 +32,17 @@ export class LocalStorage {
};

setItem = <T>(key: string, value: T) => {
localStorage.setItem(this.createKey(key), JSON.stringify(value));
this.dispatchChange(key, value);
if (!this.isAvailable) return;
try {
localStorage.setItem(this.createKey(key), JSON.stringify(value));
this.dispatchChange(key, value);
} catch (error) {
// Silently fail if localStorage is not available or quota is exceeded
}
};

getItem = <T>(key: string): T | null => {
if (!this.isAvailable) return null;
try {
const data = localStorage.getItem(this.createKey(key));
return data ? JSON.parse(data) : null;
Expand All @@ -32,15 +52,25 @@ export class LocalStorage {
};

clear = () => {
Object.keys(localStorage)
.filter((key) => key.startsWith(this.prefix))
.forEach((key) => localStorage.removeItem(key));
this.dispatchChange();
if (!this.isAvailable) return;
try {
Object.keys(localStorage)
.filter((key) => key.startsWith(this.prefix))
.forEach((key) => localStorage.removeItem(key));
this.dispatchChange();
} catch (error) {
// Silently fail if localStorage operations fail
}
};

removeItem = (key: string) => {
localStorage.removeItem(this.createKey(key));
this.dispatchChange();
if (!this.isAvailable) return;
try {
localStorage.removeItem(this.createKey(key));
this.dispatchChange();
} catch (error) {
// Silently fail if localStorage operations fail
}
};

// ---------------------------------------------------------------------------
Expand Down
12 changes: 11 additions & 1 deletion packages/react-xstore/src/ReactFactory.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ describe('ReactFactory', () => {
}, opts);

expect(result.current).toEqual([payload]);
expect(spy).toBeCalledTimes(1);
expect(
spy.mock.calls.filter((call) => !call[0].includes('__storage_test__'))
.length,
).toBe(1);
expect(
spy.mock.calls.some(
(call) =>
call[0] === '@xstate/store_todos' &&
JSON.parse(call[1]).context.todos[0].id === payload.id,
),
).toBe(true);

let storageState: ReturnType<typeof store.services.todos.getSnapshot>;
expect(() => {
Expand Down
Loading