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

Beta release 1.5.0 #17

Merged
merged 4 commits into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This is a wrapper for the Chrome Extension Storage API that adds
- [API](#api)
- [interface Bucket](#api-bucket)
- [bucket.get()](#api-bucket-get)
- [bucket.getKeys()](#api-bucket-getKeys)
- [bucket.set()](#api-bucket-set)
- [bucket.remove()](#api-bucket-remove)
- [bucket.clear()](#api-bucket-clear)
Expand Down Expand Up @@ -259,6 +260,19 @@ bucket.get({ a: 123 }) // same, but 123 is default value
bucket.get(({ a }) => a) // resolves to value of "a"
```

## async function `bucket.getKeys` <a name = "api-bucket-getKeys"></a>

Takes an optional getter. Resolves to an array of strings that represents the keys of the values in the storage area bucket.

```typescript
function getKeys() => Promise<string[]>
```

```typescript
bucket.set({ a: 123 })
bucket.getKeys() // Resolves to ['a']
```

## async function `bucket.set` <a name = "api-bucket-set"></a>

Set a value or values in storage. Takes a setter `object` or
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@extend-chrome/storage",
"version": "1.4.5",
"version": "1.5.0-0",
"description": "This is a wrapper for the Chrome Extension Storage API that adds promises and functional set transactions similar to the React `this.setState` API. Functional set transactions make it easy to use the Chrome Storage API to share and manage state between different contexts in a Chrome Extension.",
"repository": "extend-chrome/storage",
"license": "MIT",
Expand Down
15 changes: 10 additions & 5 deletions src/bucket/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import chromep from 'chrome-promise'
import { chromepApi } from 'chrome-promise/chrome-promise'
import { concat, from, fromEventPattern } from 'rxjs'
import { filter, map, mergeMap } from 'rxjs/operators'
import { isNonNull } from '../guards'
import {
AreaName,
Bucket,
Changes,
CoreGetter,
Getter,
} from '../types'
import { concat, from, fromEventPattern } from 'rxjs'
import { filter, map, mergeMap } from 'rxjs/operators'

import chromep from 'chrome-promise'
import { chromepApi } from 'chrome-promise/chrome-promise'
import { invalidSetterReturn } from '../validate'
import { isNonNull } from '../guards'

export { Bucket }

Expand Down Expand Up @@ -290,6 +291,10 @@ export function getBucket<T extends Record<string, any>>(
return set(result)
},

async getKeys() {
return getKeys()
},

get changeStream() {
return changeStream
},
Expand Down
7 changes: 5 additions & 2 deletions src/jest/jest-mock.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Subject } from 'rxjs'
import { getBucket, storage } from './jest-mock'

import { MockBucket } from './jest-mock'
import { Subject } from 'rxjs'

jest.mock('../index.ts')

Expand All @@ -17,6 +18,7 @@ test('storage.local is mocked', async () => {
update: expect.any(MockInstance),
remove: expect.any(MockInstance),
clear: expect.any(MockInstance),
getKeys: expect.any(MockInstance),
changeStream: expect.any(Subject),
valueStream: expect.any(Subject),
})
Expand All @@ -42,12 +44,13 @@ test('getBucket returns MockBucket', async () => {
update: expect.any(MockInstance),
remove: expect.any(MockInstance),
clear: expect.any(MockInstance),
getKeys: expect.any(MockInstance),
changeStream: expect.any(Subject),
valueStream: expect.any(Subject),
})

const defaultValue = { a: 'a', b: 1 }

mockStorageArea.get.mockImplementation(async () => {
return defaultValue
})
Expand Down
4 changes: 3 additions & 1 deletion src/jest/jest-mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Bucket, Changes } from '..'

/* eslint-disable @typescript-eslint/no-unused-vars */
import { Subject } from 'rxjs'
import { Bucket, Changes } from '..'

/**
* This module is a pre-mocked version of storage for use with Jest.
Expand Down Expand Up @@ -37,6 +38,7 @@ export const getBucket = <T extends object>(
update: jest.fn(),
remove: jest.fn(),
clear: jest.fn(),
getKeys: jest.fn(),
changeStream: new Subject<Changes<T>>(),
valueStream: new Subject<T>(),
})
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export interface Bucket<T extends object> {
remove: (query: string | string[]) => Promise<void>
/** Clear the storage area */
clear: () => Promise<void>
/** Get the keys (or property names) of the storage area */
getKeys: () => Promise<string[]>
/** Emits an object with changed storage keys and StorageChange values */
readonly changeStream: Observable<Changes<T>>
/** Emits the current storage values immediately and when changeStream emits */
Expand Down