Skip to content

Commit

Permalink
feat: Support SSR. Closes #196
Browse files Browse the repository at this point in the history
  • Loading branch information
Akurganow committed Oct 3, 2020
1 parent 92d0da8 commit 9e17280
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
28 changes: 26 additions & 2 deletions __tests__/create-persisted-state.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { createPersistedState } from '../src'
import storage from '../src/storages/local-storage'
import createStorage from '../src/utils/create-web-storage'
import { renderHook, cleanup, act } from '@testing-library/react-hooks'

const [usePersistedState, clear] = createPersistedState('test', storage)

describe('hook defined correctly', () => {
const [usePersistedState, clear] = createPersistedState('test', storage)

beforeEach(() => {
cleanup()
clear()
Expand Down Expand Up @@ -32,3 +33,26 @@ describe('hook defined correctly', () => {
expect(localStorage.__STORE__['persisted_state_hook:test']).toEqual(expected)
})
})

describe('hook works on SSR', () => {
const testingSSRStorage = createStorage(undefined as unknown as Storage)
const [usePersistedState, clear] = createPersistedState('test', testingSSRStorage)

it('is callable', () => {
const { result } = renderHook(() => usePersistedState('foo', 'bar'))

expect(usePersistedState).toBeDefined()
expect(clear).toBeDefined()
expect(result.current).toBeDefined()
})

it('localstorage called correctly', () => {
const { result } = renderHook(() => usePersistedState('foo', 'bar'))

act(() => {
result.current[1]('baz')
})

expect(result.current[0]).toBe('baz')
})
})
44 changes: 25 additions & 19 deletions src/utils/create-web-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ export default (storage: globalThis.Storage): Storage => ({

if (Array.isArray(keys)) {
keys.forEach(key => {
const item = storage.getItem(key)
const item = typeof storage !== 'undefined' ? storage.getItem(key) : undefined

if (item) result[key] = item
})
} else {
const item = storage.getItem(keys)
const item = typeof storage !== 'undefined' ? storage.getItem(keys) : undefined

if (item) result[keys] = item
}
Expand All @@ -55,44 +55,50 @@ export default (storage: globalThis.Storage): Storage => ({
const changes: { [key: string]: StorageChange } = {}

Object.entries(items).forEach(([key, value]) => {
const oldValue = storage.getItem(key)
const oldValue = typeof storage !== 'undefined' ? storage.getItem(key) : undefined

storage.setItem(key, value)
if (typeof storage !== 'undefined') {
storage.setItem(key, value)

changes[key] = {
oldValue,
newValue: value,
changes[key] = {
oldValue,
newValue: value,
}
}
})

fireStorageEvent(changes)
if (Object.keys(changes).length > 0) fireStorageEvent(changes)
},
remove: keys => {
const changes: { [key: string]: StorageChange } = {}

if (Array.isArray(keys)) {
keys.forEach(key => {
const oldValue = storage.getItem(key)
const oldValue = typeof storage !== 'undefined' ? storage.getItem(key) : undefined

storage.removeItem(key)
if (typeof storage !== 'undefined') {
storage.removeItem(key)

changes[key] = {
oldValue,
newValue: null,
changes[key] = {
oldValue,
newValue: null,
}
}
})
} else {
const oldValue = storage.getItem(keys)
const oldValue = typeof storage !== 'undefined' ? storage.getItem(keys) : undefined

storage.removeItem(keys)
if (typeof storage !== 'undefined') {
storage.removeItem(keys)

changes[keys] = {
oldValue,
newValue: null,
changes[keys] = {
oldValue,
newValue: null,
}
}
}

fireStorageEvent(changes)
if (Object.keys(changes).length > 0) fireStorageEvent(changes)
},
onChanged,
})
Expand Down
2 changes: 1 addition & 1 deletion src/utils/get-persisted-value.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import isFunction from './is-function'

export default function<T>(key:string, initialValue: T | (() => T), persist: string): T {
export default function<T>(key:string, initialValue: T | (() => T), persist?: string): T {
let initialPersist: { [x: string]: unknown }

try {
Expand Down

0 comments on commit 9e17280

Please sign in to comment.