-
-
Save dmfilipenko/afb4559d5be3cf02aa0d05888fe66330 to your computer and use it in GitHub Desktop.
Chrome Extension
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
import { | |
fromEventPattern, | |
Observable, | |
combineLatest, | |
} from 'rxjs'; | |
import { | |
map, | |
concat, | |
withLatestFrom, | |
tap, | |
switchMap, | |
pluck, | |
} from 'rxjs/operators'; | |
import { map as mapIterate, values, pipe as pipeR, flatten } from 'ramda'; | |
type Handler = (message: any, sender: any, sendResponse: (response: any) => void) => void; | |
const message$ = fromEventPattern( | |
(handler: Handler) => chrome.runtime.onMessage.addListener(handler), | |
(handler: Handler) => chrome.runtime.onMessage.removeListener(handler), | |
(message, sender, sendResponse) => ({ message, sender, sendResponse }) | |
).pipe( | |
pluck('message', 'path') | |
) | |
const path$ = Observable.create(function(observer) { | |
chrome.storage.local.get('path', observer.next.bind(observer)) | |
}).pipe( | |
pluck('path'), | |
map(path => path || 0), | |
) | |
const unit$ = Observable.create(function(observer) { | |
chrome.storage.local.get('unit', observer.next.bind(observer)) | |
}).pipe( | |
pluck('unit'), | |
map(unit => unit || 0), | |
) | |
const merged$ = combineLatest( | |
message$, | |
path$, | |
unit$, | |
) | |
merged$.subscribe((a: number[]) => { | |
chrome.storage.local.set({ | |
path: a[0] + (a[2] || 0), | |
unit: a[1] | |
}) | |
}) | |
// merged$.subscribe(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's solution