-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added KeyValueStore.layerStorage (#2737)
Co-authored-by: Tim <hello@timsmart.co>
- Loading branch information
1 parent
e2740fc
commit 2c2280b
Showing
4 changed files
with
87 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@effect/platform": patch | ||
--- | ||
|
||
added KeyValueStore.layerStorage to wrap instances of the `Storage` type. |
131 changes: 4 additions & 127 deletions
131
packages/platform-browser/src/internal/keyValueStore.ts
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,130 +1,7 @@ | ||
import * as PlatformError from "@effect/platform/Error" | ||
import * as KeyValueStore from "@effect/platform/KeyValueStore" | ||
import * as Effect from "effect/Effect" | ||
import * as Layer from "effect/Layer" | ||
import * as Option from "effect/Option" | ||
|
||
const storageError = (props: Omit<Parameters<typeof PlatformError.SystemError>[0], "reason" | "module">) => | ||
PlatformError.SystemError({ | ||
reason: "PermissionDenied", | ||
module: "KeyValueStore", | ||
...props | ||
}) | ||
/** @internal */ | ||
export const layerSessionStorage = KeyValueStore.layerStorage(sessionStorage) | ||
|
||
/** @internal */ | ||
export const layerLocalStorage = Layer.succeed( | ||
KeyValueStore.KeyValueStore, | ||
KeyValueStore.make({ | ||
get: (key: string) => | ||
Effect.try({ | ||
try: () => Option.fromNullable(localStorage.getItem(key)), | ||
catch: () => | ||
storageError({ | ||
pathOrDescriptor: "layerLocalStorage", | ||
method: "get", | ||
message: `Unable to get item with key ${key}` | ||
}) | ||
}), | ||
|
||
set: (key: string, value: string) => | ||
Effect.try({ | ||
try: () => localStorage.setItem(key, value), | ||
catch: () => | ||
storageError({ | ||
pathOrDescriptor: "layerLocalStorage", | ||
method: "set", | ||
message: `Unable to set item with key ${key}` | ||
}) | ||
}), | ||
|
||
remove: (key: string) => | ||
Effect.try({ | ||
try: () => localStorage.removeItem(key), | ||
catch: () => | ||
storageError({ | ||
pathOrDescriptor: "layerLocalStorage", | ||
method: "remove", | ||
message: `Unable to remove item with key ${key}` | ||
}) | ||
}), | ||
|
||
clear: Effect.try({ | ||
try: () => localStorage.clear(), | ||
catch: () => | ||
storageError({ | ||
pathOrDescriptor: "layerLocalStorage", | ||
method: "clear", | ||
message: `Unable to clear storage` | ||
}) | ||
}), | ||
|
||
size: Effect.try({ | ||
try: () => localStorage.length, | ||
catch: () => | ||
storageError({ | ||
pathOrDescriptor: "layerLocalStorage", | ||
method: "size", | ||
message: `Unable to get size` | ||
}) | ||
}) | ||
}) | ||
) | ||
|
||
/** @internal */ | ||
export const layerSessionStorage = Layer.succeed( | ||
KeyValueStore.KeyValueStore, | ||
KeyValueStore.make({ | ||
get: (key: string) => | ||
Effect.try({ | ||
try: () => Option.fromNullable(sessionStorage.getItem(key)), | ||
catch: () => | ||
storageError({ | ||
pathOrDescriptor: "layerSessionStorage", | ||
method: "get", | ||
message: `Unable to get item with key ${key}` | ||
}) | ||
}), | ||
|
||
set: (key: string, value: string) => | ||
Effect.try({ | ||
try: () => sessionStorage.setItem(key, value), | ||
catch: () => | ||
storageError({ | ||
pathOrDescriptor: "layerSessionStorage", | ||
method: "set", | ||
message: `Unable to set item with key ${key}` | ||
}) | ||
}), | ||
|
||
remove: (key: string) => | ||
Effect.try({ | ||
try: () => sessionStorage.removeItem(key), | ||
catch: () => | ||
storageError({ | ||
pathOrDescriptor: "layerSessionStorage", | ||
method: "remove", | ||
message: `Unable to remove item with key ${key}` | ||
}) | ||
}), | ||
|
||
clear: Effect.try({ | ||
try: () => sessionStorage.clear(), | ||
catch: () => | ||
storageError({ | ||
pathOrDescriptor: "layerSessionStorage", | ||
method: "clear", | ||
message: `Unable to clear storage` | ||
}) | ||
}), | ||
|
||
size: Effect.try({ | ||
try: () => sessionStorage.length, | ||
catch: () => | ||
storageError({ | ||
pathOrDescriptor: "layerSessionStorage", | ||
method: "size", | ||
message: `Unable to get size` | ||
}) | ||
}) | ||
}) | ||
) | ||
/** @internal */ | ||
export const layerLocalStorage = KeyValueStore.layerStorage(localStorage) |
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