-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix middleware order * code clean up * new persist option
- Loading branch information
1 parent
1d2cbe9
commit 42c186f
Showing
6 changed files
with
97 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,4 @@ export function useStateMachineContext() { | |
} | ||
|
||
return value; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,57 @@ | ||
import { STORE_DEFAULT_NAME } from '../constants'; | ||
import getStoreData from './getBrowserStoreData'; | ||
import { MiddleWare, GlobalState } from '../types'; | ||
|
||
class StoreFactory { | ||
public storageType: Storage; | ||
public state: GlobalState = {}; | ||
public middleWares: MiddleWare[] = []; | ||
function StoreFactory() { | ||
let storageType: Storage; | ||
let state: GlobalState = {}; | ||
let middleWares: MiddleWare[] = []; | ||
let name = STORE_DEFAULT_NAME; | ||
|
||
constructor(public name = STORE_DEFAULT_NAME) { | ||
try { | ||
this.storageType = | ||
typeof sessionStorage !== 'undefined' | ||
? window.sessionStorage | ||
: ({} as Storage); | ||
} catch { | ||
this.storageType = {} as Storage; | ||
} | ||
try { | ||
storageType = | ||
typeof sessionStorage !== 'undefined' | ||
? window.sessionStorage | ||
: ({} as Storage); | ||
} catch { | ||
storageType = {} as Storage; | ||
} | ||
|
||
updateStore(defaultValues: GlobalState) { | ||
this.state = getStoreData(this.storageType, this.name) || defaultValues; | ||
} | ||
|
||
updateMiddleWares(middleWares: MiddleWare[]) { | ||
return (this.middleWares = middleWares); | ||
} | ||
return { | ||
updateStore(defaultValues: GlobalState) { | ||
try { | ||
state = JSON.parse(storageType.getItem(name) || '') || defaultValues; | ||
} catch { | ||
state = defaultValues; | ||
} | ||
}, | ||
saveStore() { | ||
storageType.setItem(name, JSON.stringify(state)); | ||
}, | ||
get middleWares() { | ||
return middleWares; | ||
}, | ||
set middleWares(wares: MiddleWare[]) { | ||
middleWares = wares; | ||
}, | ||
get state() { | ||
return state; | ||
}, | ||
set state(value) { | ||
state = value; | ||
}, | ||
get name() { | ||
return name; | ||
}, | ||
set name(value) { | ||
name = value; | ||
}, | ||
get storageType() { | ||
return storageType; | ||
}, | ||
set storageType(value) { | ||
storageType = value; | ||
}, | ||
}; | ||
} | ||
|
||
export default new StoreFactory(); | ||
export default StoreFactory(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters