-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamed ihp-datasync-react.js to just react.js
This makes for nicer es6 imports: ```js import { useQuery } from 'ihp-datasync/react'; ```
- Loading branch information
1 parent
a16a6d2
commit 97dd1c1
Showing
2 changed files
with
43 additions
and
40 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,40 +1,3 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { DataSubscription } from './ihp-datasync.js'; | ||
|
||
// Usage: | ||
// | ||
// const messages = useQuery(query('messages').orderBy('createdAt')); | ||
// | ||
export function useQuery(queryBuilder) { | ||
const [records, setRecords] = useState(null); | ||
|
||
useEffect(() => { | ||
const dataSubscription = new DataSubscription(queryBuilder.query); | ||
|
||
dataSubscription.onReady = setRecords; | ||
dataSubscription.onUpdate = (id, changeSet) => { | ||
setRecords(records => { | ||
for (const record of records) { | ||
if (record.id === id) { | ||
Object.assign(record, changeSet); | ||
break; | ||
} | ||
} | ||
|
||
return [...records]; | ||
}); | ||
} | ||
dataSubscription.onCreate = newRecord => { | ||
setRecords(records => [...records, newRecord]); | ||
}; | ||
dataSubscription.onDelete = id => { | ||
setRecords(records => records.filter(record => record.id !== id)); | ||
}; | ||
|
||
return () => { dataSubscription.close() }; | ||
}, [ | ||
JSON.stringify(queryBuilder.query) /* <-- It's terrible - but it works, we should find a better for this */ | ||
]) | ||
|
||
return records; | ||
} | ||
/* This file is only kept to avoid breaking b.c. */ | ||
import { useQuery } from './react.js'; | ||
export { useQuery }; |
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,40 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { DataSubscription } from './ihp-datasync.js'; | ||
|
||
// Usage: | ||
// | ||
// const messages = useQuery(query('messages').orderBy('createdAt')); | ||
// | ||
export function useQuery(queryBuilder) { | ||
const [records, setRecords] = useState(null); | ||
|
||
useEffect(() => { | ||
const dataSubscription = new DataSubscription(queryBuilder.query); | ||
|
||
dataSubscription.onReady = setRecords; | ||
dataSubscription.onUpdate = (id, changeSet) => { | ||
setRecords(records => { | ||
for (const record of records) { | ||
if (record.id === id) { | ||
Object.assign(record, changeSet); | ||
break; | ||
} | ||
} | ||
|
||
return [...records]; | ||
}); | ||
} | ||
dataSubscription.onCreate = newRecord => { | ||
setRecords(records => [...records, newRecord]); | ||
}; | ||
dataSubscription.onDelete = id => { | ||
setRecords(records => records.filter(record => record.id !== id)); | ||
}; | ||
|
||
return () => { dataSubscription.close() }; | ||
}, [ | ||
JSON.stringify(queryBuilder.query) /* <-- It's terrible - but it works, we should find a better for this */ | ||
]) | ||
|
||
return records; | ||
} |