-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev: Work towards using a webview to display spelling information. (#…
- Loading branch information
Showing
65 changed files
with
2,060 additions
and
451 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
packages/client/src/webview/AppState/Subscribables/StoreValue.test.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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createEmitter, createSubscribable } from './createFunctions'; | ||
import { awaitSubscribable } from './helpers/awaitSubscribable'; | ||
import { createStoreValue } from './StoreValue'; | ||
|
||
describe('StoreValue', () => { | ||
test('createStoreValue', async () => { | ||
const store = createStoreValue(5); | ||
expect(store.value).toBe(5); | ||
store.set(7); | ||
expect(store.value).toBe(7); | ||
const cb = jest.fn(); | ||
store.subscribe(cb); | ||
expect(cb).toHaveBeenLastCalledWith(7); | ||
store.dispose(); | ||
}); | ||
|
||
test('createSubscribableValue', async () => { | ||
const source = createEmitter<number>(); | ||
const sub = createSubscribable((s) => source.subscribe(s)); | ||
const pValue = awaitSubscribable(sub); | ||
source.notify(6); | ||
await expect(pValue).resolves.toBe(6); | ||
sub.dispose(); | ||
}); | ||
}); |
54 changes: 54 additions & 0 deletions
54
packages/client/src/webview/AppState/Subscribables/StoreValue.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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import deepEqual from 'fast-deep-equal'; | ||
|
||
import { toSubscriberFn } from './helpers/toSubscriber'; | ||
import { AbstractSubscribable } from './internal/AbstractSubscribable'; | ||
import type { Subscribable, SubscribableValue, SubscriberLike } from './Subscribables'; | ||
|
||
export interface StoreValue<T> extends SubscribableValue<T> { | ||
value: T; | ||
set(v: T): void; | ||
update(u: (v: T) => T): void; | ||
} | ||
|
||
export function createStoreValue<T>(v: T): StoreValue<T> { | ||
return new StoreValueImpl(v); | ||
} | ||
|
||
class StoreValueImpl<T> extends AbstractSubscribable<T> implements StoreValue<T> { | ||
private _value: T; | ||
constructor(value: T) { | ||
super(); | ||
this._value = value; | ||
} | ||
|
||
get value() { | ||
return this._value; | ||
} | ||
|
||
set value(v: T) { | ||
this.set(v); | ||
} | ||
|
||
set(value: T) { | ||
// Do not update if the value has not changed. | ||
if (this._value === value || deepEqual(this._value, value)) return; | ||
this._value = value; | ||
this.notify(value); | ||
return; | ||
} | ||
|
||
subscribe(s: SubscriberLike<T>) { | ||
const dispose = super.subscribe(s); | ||
const sFn = toSubscriberFn(s); | ||
sFn(this._value); | ||
return dispose; | ||
} | ||
|
||
update(u: (v: T) => T) { | ||
return this.set(u(this._value)); | ||
} | ||
} | ||
|
||
export type MakeSubscribable<T, RO extends keyof T | never = never> = { | ||
[K in keyof T]: K extends RO ? Subscribable<T[K]> : StoreValue<T[K]>; | ||
}; |
Oops, something went wrong.