Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(history): rewrite for Cycle Unified
BREAKING CHANGES: New API. You no longer need createHistory() calls that create history objects to be passed to makeHistoryDriver(). You simply call makeHistoryDriver() and those history objects are created under the hood.
- Loading branch information
Showing
16 changed files
with
873 additions
and
581 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
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 {Stream, MemoryStream} from 'xstream'; | ||
import { | ||
createBrowserHistory, | ||
createMemoryHistory, | ||
createHashHistory, | ||
BrowserHistoryBuildOptions, | ||
MemoryHistoryBuildOptions, | ||
HashHistoryBuildOptions, | ||
Location, | ||
} from 'history'; | ||
import {createHistory$} from './createHistory$'; | ||
import { | ||
HistoryInput, | ||
HistoryDriver, | ||
GoBackHistoryInput, | ||
GoForwardHistoryInput, | ||
GoHistoryInput, | ||
PushHistoryInput, | ||
ReplaceHistoryInput, | ||
} from './types'; | ||
|
||
/** | ||
* Create a History Driver to be used in the browser. | ||
*/ | ||
export function makeHistoryDriver(options?: BrowserHistoryBuildOptions): HistoryDriver { | ||
const history = createBrowserHistory(options); | ||
return function historyDriver(sink$: Stream<HistoryInput | string>) { | ||
return createHistory$(history, sink$); | ||
}; | ||
} | ||
|
||
/** | ||
* Create a History Driver to be used in non-browser enviroments | ||
* such as server-side node.js. | ||
*/ | ||
export function makeServerHistoryDriver(options?: MemoryHistoryBuildOptions): HistoryDriver { | ||
const history = createMemoryHistory(options); | ||
return function serverHistoryDriver(sink$: Stream<HistoryInput | string>) { | ||
return createHistory$(history, sink$); | ||
}; | ||
} | ||
|
||
/** | ||
* Create a History Driver for older browsers using hash routing | ||
*/ | ||
export function makeHashHistoryDriver(options?: HashHistoryBuildOptions): HistoryDriver { | ||
const history = createHashHistory(options); | ||
return function hashHistoryDriver(sink$: Stream<HistoryInput | string>) { | ||
return createHistory$(history, sink$); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
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,4 +1,4 @@ | ||
export { Location } from 'history'; | ||
export {Location} from 'history'; | ||
export * from './types'; | ||
export * from './historyDriver'; | ||
export * from './drivers'; | ||
export * from './captureClicks'; |
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,34 @@ | ||
/// <reference path="../../node_modules/@types/mocha/index.d.ts" /> | ||
/// <reference path="../../node_modules/@types/node/index.d.ts" /> | ||
import * as assert from 'assert'; | ||
import xs from 'xstream'; | ||
import {makeHashHistoryDriver, captureClicks, Location} from '../../src'; | ||
|
||
describe('captureClicks', () => { | ||
beforeEach(() => { | ||
window.location.hash = ''; | ||
}); | ||
|
||
it('should allow listening to link clicks and change route', function (done) { | ||
const historyDriver = makeHashHistoryDriver(); | ||
const history$ = captureClicks(historyDriver)(xs.never()); | ||
|
||
const sub = history$.subscribe({ | ||
next: (location: Location) => { | ||
assert.strictEqual(location.pathname, '/test'); | ||
sub.unsubscribe(); | ||
done(); | ||
}, | ||
error: (err) => {}, | ||
complete: () => {}, | ||
}); | ||
|
||
const a = document.createElement('a'); | ||
a.href = '/test'; | ||
document.body.appendChild(a); | ||
|
||
setTimeout(() => { | ||
a.click(); | ||
}); | ||
}); | ||
}); |
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,24 @@ | ||
/// <reference path="../../node_modules/@types/mocha/index.d.ts" /> | ||
/// <reference path="../../node_modules/@types/node/index.d.ts" /> | ||
import * as assert from 'assert'; | ||
import {makeHashHistoryDriver, makeHistoryDriver} from '../../src'; | ||
|
||
describe('makeHistoryDriver', () => { | ||
it('should be a function', () => { | ||
assert.strictEqual(typeof makeHistoryDriver, 'function'); | ||
}); | ||
|
||
it('should return a function' , () => { | ||
assert.strictEqual(typeof makeHistoryDriver(), 'function'); | ||
}); | ||
}); | ||
|
||
describe('makeHashHistoryDriver', () => { | ||
it('should be a function', () => { | ||
assert.strictEqual(typeof makeHashHistoryDriver, 'function'); | ||
}); | ||
|
||
it('should return a function' , () => { | ||
assert.strictEqual(typeof makeHashHistoryDriver(), 'function'); | ||
}); | ||
}); |
Oops, something went wrong.