-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* closes bangle-io/bangle-issues-internal#36 * implement ref * add docs
- Loading branch information
Showing
13 changed files
with
231 additions
and
21 deletions.
There are no files selected for viewing
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
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 +1 @@ | ||
export { createStore, createKey } from './vanilla/index'; | ||
export { createStore, createKey, ref, cleanup } from './vanilla/index'; |
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 |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { Store, createStore } from '../store'; | ||
import waitForExpect from 'wait-for-expect'; | ||
import { testCleanup } from '../helpers/test-cleanup'; | ||
import { createKey } from '../slice/key'; | ||
import { ref } from '../effect/ref'; | ||
import { EffectStore } from '../effect/effect'; | ||
|
||
const sliceAKey = createKey('sliceA', []); | ||
const sliceAField1 = sliceAKey.field('value:sliceAField1'); | ||
const sliceAField2 = sliceAKey.field('value:sliceAField2'); | ||
|
||
const sliceA = sliceAKey.slice({ | ||
fields: { | ||
sliceAField1, | ||
sliceAField2, | ||
}, | ||
}); | ||
|
||
const sliceBKey = createKey('sliceB', []); | ||
const sliceBField1 = sliceBKey.field('value:sliceBField1'); | ||
|
||
const sliceB = sliceBKey.slice({ | ||
fields: { | ||
sliceBField1, | ||
}, | ||
}); | ||
|
||
beforeEach(() => { | ||
testCleanup(); | ||
}); | ||
|
||
test('ref works', async () => { | ||
const myStore = createStore({ | ||
name: 'myStore', | ||
slices: [sliceA, sliceB], | ||
}); | ||
|
||
const getMyRef = ref<{ foo: { counter?: number } }>(() => ({ | ||
foo: {}, | ||
})); | ||
|
||
let derStore: EffectStore | undefined; | ||
|
||
myStore.effect((store) => { | ||
derStore = store; | ||
const val = sliceA.track(store); | ||
const myRef = getMyRef(store); | ||
|
||
myRef.current.foo.counter = 1; | ||
}); | ||
|
||
expect(getMyRef(myStore).current).toEqual({ | ||
foo: {}, | ||
}); | ||
|
||
// effect is deferred so we need to wait for it to run | ||
await waitForExpect(() => { | ||
expect(getMyRef(derStore!).current.foo.counter).toBe(1); | ||
}); | ||
}); | ||
|
||
test('creating another store does not reuse the ref value', async () => { | ||
const myStore = createStore({ | ||
name: 'myStore', | ||
slices: [sliceA, sliceB], | ||
}); | ||
|
||
const myStore2 = createStore({ | ||
name: 'myStore2', | ||
slices: [sliceA, sliceB], | ||
}); | ||
|
||
const getMyRef = ref<{ foo: { counter?: number } }>(() => ({ | ||
foo: {}, | ||
})); | ||
|
||
let derStore: EffectStore | undefined; | ||
let derStore2: EffectStore | undefined; | ||
|
||
myStore.effect((store) => { | ||
derStore = store; | ||
const val = sliceA.track(store); | ||
|
||
const myRef = getMyRef(store); | ||
|
||
myRef.current.foo.counter = 1; | ||
}); | ||
|
||
myStore2.effect((store) => { | ||
derStore2 = store; | ||
const myRef = getMyRef(store); | ||
|
||
myRef.current.foo.counter = 99; | ||
}); | ||
|
||
expect(getMyRef(myStore).current).toEqual({ | ||
foo: {}, | ||
}); | ||
|
||
// effect is deferred so we need to wait for it to run | ||
await waitForExpect(() => { | ||
expect(getMyRef(derStore!).current.foo.counter).toBe(1); | ||
}); | ||
|
||
// effect is deferred so we need to wait for it to run | ||
await waitForExpect(() => { | ||
expect(getMyRef(derStore2!).current.foo.counter).toBe(99); | ||
}); | ||
}); | ||
|
||
test('multiple effects can share the ref value', async () => { | ||
const myStore = createStore({ | ||
name: 'myStore', | ||
slices: [sliceA, sliceB], | ||
}); | ||
|
||
const getMyRef = ref<{ foo: { counter?: number } }>(() => ({ | ||
foo: {}, | ||
})); | ||
|
||
let derStore: EffectStore | undefined; | ||
let derStore2: EffectStore | undefined; | ||
|
||
myStore.effect((store) => { | ||
derStore = store; | ||
const val = sliceA.track(store).sliceAField1; | ||
|
||
const myRef = getMyRef(store); | ||
|
||
myRef.current.foo.counter = 1; | ||
}); | ||
|
||
sliceBKey.effect((store) => { | ||
derStore2 = store; | ||
const val = sliceB.track(store).sliceBField1; | ||
|
||
const myRef = getMyRef(store); | ||
|
||
if (myRef.current.foo.counter === 1) { | ||
myRef.current.foo.counter = 2; | ||
} | ||
}); | ||
|
||
expect(getMyRef(myStore).current).toEqual({ | ||
foo: {}, | ||
}); | ||
|
||
await waitForExpect(() => { | ||
expect(getMyRef(myStore).current.foo.counter).toBe(2); | ||
expect(getMyRef(derStore2!).current.foo.counter).toBe(2); | ||
expect(getMyRef(derStore!).current.foo.counter).toBe(2); | ||
}); | ||
}); |
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 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 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { BaseStore } from '../base-store'; | ||
import { Store } from '../store'; | ||
|
||
export type RefObject<T> = { | ||
current: T; | ||
}; | ||
|
||
export function ref<T>( | ||
init: () => T, | ||
): (store: Store | BaseStore) => RefObject<T> { | ||
const cache = new WeakMap<Store, RefObject<T>>(); | ||
|
||
return (store) => { | ||
const rootStore: Store = store instanceof Store ? store : store._rootStore; | ||
|
||
let existing = cache.get(rootStore); | ||
|
||
if (!existing) { | ||
existing = { | ||
current: init(), | ||
}; | ||
cache.set(rootStore, existing); | ||
} | ||
|
||
return existing; | ||
}; | ||
} |
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,2 +1,4 @@ | ||
export { cleanup } from './effect/cleanup'; | ||
export { createKey } from './slice/key'; | ||
export { createStore } from './store'; | ||
export { ref } from './effect/ref'; |
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