Skip to content

Instantly share code, notes, and snippets.

@TylorS
Last active September 24, 2015 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TylorS/3466e921e234321c4349 to your computer and use it in GitHub Desktop.
Save TylorS/3466e921e234321c4349 to your computer and use it in GitHub Desktop.
const supportsHistory = () => {
const ua = navigator.userAgent;
// We only want Android 2 and 4.0, stock browser, and not Chrome which identifies
// itself as 'Mobile Safari' as well, nor Windows Phone (issue #1471).
if ((ua.indexOf('Android 2.') !== -1 ||
(ua.indexOf('Android 4.0') !== -1)) &&
ua.indexOf('Mobile Safari') !== -1 &&
ua.indexOf('Chrome') === -1 &&
ua.indexOf('Windows Phone') === -1) {
return false;
}
// Return the regular check
return (window.history && 'pushState' in window.history);
}
export {
supportsHistory
};
import {Rx} from '@cycle/core';
import {createHistory, createHashHistory, useQueries} from 'history';
import helpers from './helpers';
const makeHistory = (hash, useQueries, options) => {
hash = hash || helpers.supportsHistory();
if (hash && useQueries) return useQueries(createHashHistory)(options);
if (hash && !useQueries) return createHashHistory(options);
if (!hash && useQueries) return useQueries(createHistory)(options);
if (!hash && !useQueries) return createHistory(options);
}
const createPushState = (history) => {
return const pushState = (path) => {
if ('string' === typeof url) history.pushState({}, url);
// Is an object with state and path;
else if ('object' === typeof url) {
let {state, path} = url;
history.pushState(state, path)
} else {
throw new Error("History Driver input must be a string or object { state: { the: 'state' }, path : '/path' }");
}
}
}
const createHistorySubject = (history) => {
let subject = new Rx.BehaviorSubject();
// Append methods for convenience.
// To be removed if unneeded.
Object.keys(history).forEach(key => {
if (key !== 'listen') subject[key] = history[key];
})
// More descriptive
subject.location = subject.value;
return subject;
}
const makeHistoryDriver = ( { hash = false, useQueries = true, ...options } ) => {
const history = makeHistory(hash, useQueries, options);
const historySubject = createHistorySubject(history);
return const historyDriver = (url$) => {
url$
.subscribe(createPushState(history));
history.listen(location => historySubject.onNext(location));
return historySubject;
}
}
@erykpiast
Copy link

hey, could you provide example of usage of this driver? I see Subject is returned and it worries me a bit :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment