Skip to content

Commit

Permalink
fix: deprecation warning on Watcher.enableSingleMode
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Sep 18, 2019
1 parent 7cc3f74 commit 01cee54
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
63 changes: 35 additions & 28 deletions src/social-network-provider/facebook.com/UI/collectPeople.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { LiveSelector, MutationObserverWatcher } from '@holoflows/kit/es'
import { getPersonIdentifierAtFacebook } from '../getPersonIdentifierAtFacebook'
import Services from '../../../extension/service'
import { GroupIdentifier } from '../../../database/type'
import { SocialNetworkUI } from '../../../social-network/ui'

function findPeopleInfo() {
function findPeopleInfo(whoAmI: SocialNetworkUI['currentIdentity']) {
// TODO: support mobile
const bio = new LiveSelector().querySelector<HTMLDivElement>('#profile_timeline_intro_card')
const bio = new LiveSelector().querySelector<HTMLDivElement>('#profile_timeline_intro_card').enableSingleMode()
new MutationObserverWatcher(bio)
.enableSingleMode()
/**
* @var node: bio in the side of user page
*/
Expand All @@ -28,42 +29,48 @@ function findPeopleInfo() {
Services.Crypto.verifyOthersProve(text, id.identifier)
return id
}
tryFindBioKey()
function parseFriendship() {
const thisPerson = tryFindBioKey()

const myID = whoAmI.value
if (!thisPerson || !myID) return
const isFriendNow = isFriend.evaluate()
const myFriends = GroupIdentifier.getDefaultFriendsGroupIdentifier(myID.identifier)
if (isFriendNow === Status.Friend) {
Services.People.addPersonToFriendsGroup(myFriends, [thisPerson.identifier])
console.log('Adding friend', thisPerson.identifier, 'to', myFriends)
} else if (isFriendNow === Status.NonFriend) {
Services.People.removePersonFromFriendsGroup(myFriends, [thisPerson.identifier])
console.log('Removing friend', thisPerson.identifier, 'from', myFriends)
}
}
parseFriendship()
return {
onNodeMutation: tryFindBioKey,
onTargetChanged: tryFindBioKey,
onNodeMutation: parseFriendship,
onTargetChanged: parseFriendship,
}
})
.startWatch()
}
enum Status {
Friend,
Unknown,
NonFriend,
NonFriend = 1,
Friend = 2,
Unknown = 3,
}
/**
* Ack:
* If `#pagelet_timeline_profile_actions > * > *` have 4 children, they are not friend.
* If have 6 children, they are friend.
*/
const isFriend = new LiveSelector().querySelectorAll('#pagelet_timeline_profile_actions > * > *').replace(arr => {
if (arr.length === 6) return [Status.Friend]
else if (arr.length === 4) return [Status.NonFriend]
return [Status.Unknown]
})
function detectIfFriend() {
// TODO: finish this, store it into the database, also do this in twitter
new MutationObserverWatcher(isFriend)
.useForeach(() => {
return {
onTargetChanged() {},
onRemove() {},
}
})
.startWatch()
}
const isFriend = new LiveSelector()
.querySelectorAll('#pagelet_timeline_profile_actions > * > *')
.replace(arr => {
if (arr.length === 6) return [Status.Friend]
else if (arr.length === 4) return [Status.NonFriend]
return [Status.Unknown]
})
.enableSingleMode()

export function collectPeopleFacebook() {
findPeopleInfo()
detectIfFriend()
export function collectPeopleFacebook(this: SocialNetworkUI) {
findPeopleInfo(this.currentIdentity)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ if (isMobileFacebook) {
.map(x => (x.getAttribute('role') === 'dialog' ? x.lastElementChild!.lastElementChild : x))
}
export function injectPostBoxFacebook() {
const watcher = new MutationObserverWatcher(composeBox)
.enableSingleMode()
const watcher = new MutationObserverWatcher(composeBox.clone().enableSingleMode())
.setDomProxyOption({ afterShadowRootInit: { mode: 'closed' } })
.startWatch()
renderInShadowRoot(<AdditionalPostBox />, watcher.firstVirtualNode.afterShadow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { SocialNetworkUI } from '../../../social-network/ui'

export function injectWelcomeBannerFacebook(this: SocialNetworkUI) {
const to = new MutationObserverWatcher(
new LiveSelector().querySelector<HTMLDivElement>(isMobileFacebook ? '#MComposer' : '#pagelet_composer'),
new LiveSelector()
.querySelector<HTMLDivElement>(isMobileFacebook ? '#MComposer' : '#pagelet_composer')
.enableSingleMode(),
)
.enableSingleMode()
.setDomProxyOption({ beforeShadowRootInit: { mode: 'closed' } })
.startWatch()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export function resolveLastRecognizedIdentityFacebook(this: SocialNetworkUI) {
.concat(myUsernameLiveSelectorOnMobile)
.enableSingleMode()
new MutationObserverWatcher(self)
.enableSingleMode()
.setComparer(undefined, (a, b) => a.identifier.equals(b.identifier))
.addListener('onAdd', e => assign(e.value))
.addListener('onChange', e => assign(e.newValue))
Expand Down
6 changes: 4 additions & 2 deletions src/social-network/defaults/injectCommentBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ const defHandler = async (encryptedComment: string, current: PostInfo) => {
export const injectCommentBoxDefaultFactory = (onPasteToCommentBox = defHandler) => {
return (current: PostInfo) => {
if (!current.commentBoxSelector) return nop
const commentBoxWatcher = new MutationObserverWatcher(current.commentBoxSelector, current.rootNode)
const commentBoxWatcher = new MutationObserverWatcher(
current.commentBoxSelector.clone().enableSingleMode(),
current.rootNode,
)
.setDomProxyOption({ afterShadowRootInit: { mode: 'closed' } })
.enableSingleMode()
.startWatch()
const CommentBoxUI = () => {
const payload = useValueRef(current.postPayload)
Expand Down

0 comments on commit 01cee54

Please sign in to comment.