-
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.
- Loading branch information
Showing
10 changed files
with
227 additions
and
29 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
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,51 @@ | ||
import { BehaviorSubject, Observable } from 'rxjs'; | ||
|
||
/** | ||
* | ||
*/ | ||
export class CacheBase<K, V> { | ||
private map: Map<K, V>; | ||
private registrySubject: BehaviorSubject<Map<K, V>>; | ||
|
||
constructor() { | ||
this.map = new Map<K, V>(); | ||
this.registrySubject = new BehaviorSubject<Map<K, V>>(this.map); | ||
} | ||
|
||
get $(): Observable<Map<K, V>> { | ||
return this.registrySubject.asObservable(); | ||
} | ||
|
||
set(key: K, value: V): void { | ||
this.map.set(key, value); | ||
this.notifyChange(); | ||
} | ||
|
||
has(key: K): boolean { | ||
return this.map.has(key); | ||
} | ||
|
||
forEach(callback: (value: V, key: K, map: Map<K, V>) => void): void { | ||
this.map.forEach(callback); | ||
} | ||
|
||
get(key: K): V | undefined { | ||
return this.map.get(key); | ||
} | ||
|
||
getAll() { | ||
return new Map(this.map); | ||
} | ||
|
||
delete(key: K): boolean { | ||
const result = this.map.delete(key); | ||
if (result) { | ||
this.notifyChange(); | ||
} | ||
return result; | ||
} | ||
|
||
private notifyChange(): void { | ||
this.registrySubject.next(new Map(this.map)); | ||
} | ||
} |
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,105 @@ | ||
import { useObservableState } from 'observable-hooks'; | ||
|
||
import Box from '@wcpos/components/src/box'; | ||
import Button from '@wcpos/components/src/button'; | ||
import Table from '@wcpos/components/src/simple-table'; | ||
import Text from '@wcpos/components/src/text'; | ||
import Tree from '@wcpos/components/src/tree'; | ||
|
||
import { useQueryManager } from '../provider'; | ||
|
||
const QueryKeyCell = ({ item: query, column }) => { | ||
return ( | ||
<Button | ||
size="small" | ||
onPress={() => { | ||
console.log(query); | ||
}} | ||
> | ||
{query.id} | ||
</Button> | ||
); | ||
}; | ||
|
||
const QueryParamsCell = ({ item: query, column }) => { | ||
const params = useObservableState(query.params$, query.getParams()); | ||
return <Tree rootName="params" data={params} />; | ||
}; | ||
|
||
const ReplicationStateButton = ({ replication }) => { | ||
const active = useObservableState(replication.active$, false); | ||
return ( | ||
<Box horizontal> | ||
<Button | ||
type={active ? 'success' : 'secondary'} | ||
size="small" | ||
key={replication.endpoint} | ||
onPress={() => { | ||
console.log(replication); | ||
}} | ||
> | ||
{replication.endpoint} | ||
</Button> | ||
</Box> | ||
); | ||
}; | ||
|
||
const ReplicationsCell = ({ item: query, column }) => { | ||
const manager = useQueryManager(); | ||
const replicationsMap = manager.getReplicationStatesByQueryID(query.id); | ||
const replicationsArray = Array.from(replicationsMap.values()); | ||
return ( | ||
<Box space="small"> | ||
{replicationsArray.map((replication) => { | ||
return <ReplicationStateButton replication={replication} />; | ||
})} | ||
</Box> | ||
); | ||
}; | ||
|
||
export const Devtools = () => { | ||
const manager = useQueryManager(); | ||
const queriesMap = useObservableState(manager.queries.$, manager.queries.getAll()); | ||
const queriesArray = Array.from(queriesMap.values()); | ||
|
||
return Table({ | ||
data: queriesArray, | ||
columns: [ | ||
{ | ||
key: 'queryKeys', | ||
label: 'Query Keys', | ||
width: 200, | ||
cellRenderer: QueryKeyCell, | ||
}, | ||
{ | ||
key: 'queryParams', | ||
label: 'Query Params', | ||
width: 200, | ||
cellRenderer: QueryParamsCell, | ||
}, | ||
{ | ||
key: 'replications', | ||
label: 'Replication States', | ||
cellRenderer: ReplicationsCell, | ||
}, | ||
], | ||
}); | ||
|
||
return ( | ||
<Box> | ||
{queriesArray.map((query) => { | ||
const replicationsMap = manager.getReplicationStatesByQueryID(query.id); | ||
console.log(manager); | ||
const replicationsArray = Array.from(replicationsMap.values()); | ||
return ( | ||
<> | ||
<Text key={query.id}>{query.id}</Text> | ||
{replicationsArray.map((replication) => { | ||
return <Text key={replication.endpoint}>{replication.endpoint}</Text>; | ||
})} | ||
</> | ||
); | ||
})} | ||
</Box> | ||
); | ||
}; |
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,8 @@ | ||
import * as devtools from './devtools'; | ||
|
||
export const QueryDevtools: (typeof devtools)['Devtools'] = | ||
process.env.NODE_ENV !== 'development' | ||
? function () { | ||
return null; | ||
} | ||
: devtools.Devtools; |
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,6 @@ | ||
import { CacheBase } from './cache-base'; | ||
|
||
/** | ||
* | ||
*/ | ||
export class QueryCache<K, V> extends CacheBase<K, V> {} |
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,6 @@ | ||
import { CacheBase } from './cache-base'; | ||
|
||
/** | ||
* | ||
*/ | ||
export class ReplicationCache<K, V> extends CacheBase<K, V> {} |
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