-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1069c89
commit e22975d
Showing
2 changed files
with
48 additions
and
14 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 |
---|---|---|
@@ -1,10 +1,28 @@ | ||
import { NO_VALUE } from '..' | ||
import { createFacet } from '../facet' | ||
import { asPromise } from './asPromise' | ||
|
||
it('resolves facets with their values if available', async () => { | ||
it('immediately resolves facets with their values if available', async () => { | ||
const facet = createFacet({ initialValue: 'testing' }) | ||
const value = await asPromise(facet) | ||
const value = await asPromise(facet).promise | ||
expect(value).toEqual('testing') | ||
}) | ||
|
||
it.todo('waits for the facet to have a value, making sure to not hold subscriptions') | ||
it('waits for the facet to have a value, making sure to not hold subscriptions', async () => { | ||
const cleanupSubscription = jest.fn() | ||
const startSubscription = jest.fn().mockImplementation((update) => { | ||
update('testing') | ||
return cleanupSubscription | ||
}) | ||
|
||
const facet = createFacet<string>({ | ||
initialValue: NO_VALUE, | ||
startSubscription, | ||
}) | ||
|
||
const value = await asPromise(facet).promise | ||
expect(value).toEqual('testing') | ||
|
||
expect(startSubscription).toHaveBeenCalledTimes(1) | ||
expect(cleanupSubscription).toHaveBeenCalledTimes(1) | ||
}) |
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,21 +1,37 @@ | ||
import { Facet, NO_VALUE } from '../types' | ||
|
||
/** | ||
* Takes a Facet as returns a Promise that will resolve with its first value | ||
* Takes a Facet and returns a Promise that will resolve with its first value | ||
* | ||
* @param facet | ||
* @returns Promise<T> | ||
*/ | ||
export const asPromise = <T>(facet: Facet<T>) => | ||
new Promise<T>((resolve) => { | ||
const value = facet.get() | ||
if (value !== NO_VALUE) { | ||
resolve(value) | ||
return | ||
export const asPromise = <T>(facet: Facet<T>) => { | ||
let resolve: (value: T | PromiseLike<T>) => void = noop | ||
|
||
const promise = new Promise<T>((_resolve) => { | ||
resolve = _resolve | ||
}) | ||
|
||
const value = facet.get() | ||
if (value !== NO_VALUE) { | ||
resolve(value) | ||
|
||
return { | ||
promise, | ||
cancel: noop, | ||
} | ||
} | ||
|
||
const cleanup = facet.observe((value: T) => { | ||
resolve(value) | ||
const cleanup = facet.observe(resolve) | ||
|
||
return { | ||
promise: promise.then((value) => { | ||
cleanup() | ||
}) | ||
}) | ||
return value | ||
}), | ||
cancel: cleanup, | ||
} | ||
} | ||
|
||
const noop = () => {} |