-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Memory-ish protocol #44
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1c1938a
Add Memory-ish protocol
jquense 917eefd
Merge branch 'master' into memory-protocol
taion d46d7d2
Merge branch 'master' into memory-protocol
taion 6707406
Clean up code and expand tests
taion 97eef28
Use Array.isArray and add docs
taion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import invariant from 'invariant'; | ||
|
||
import createPath from './utils/createPath'; | ||
import ensureLocation from './utils/ensureLocation'; | ||
|
||
const STATE_KEY = '@@farce/state'; | ||
|
||
export default class MemoryProtocol { | ||
constructor(initialLocation, { persistent = false } = {}) { | ||
this._persistent = persistent; | ||
|
||
const initialState = persistent ? this._loadState() : null; | ||
if (initialState) { | ||
this._stack = initialState.stack; | ||
this._index = initialState.index; | ||
} else { | ||
this._stack = [ensureLocation(initialLocation)]; | ||
this._index = 0; | ||
} | ||
|
||
this._keyPrefix = Math.random() | ||
.toString(36) | ||
.slice(2, 8); | ||
this._keyIndex = 0; | ||
|
||
this._listener = null; | ||
} | ||
|
||
_loadState() { | ||
try { | ||
const { stack, index } = JSON.parse( | ||
window.sessionStorage.getItem(STATE_KEY), | ||
); | ||
|
||
// Check that the stack and index at least seem reasonable before using | ||
// them as state. This isn't foolproof, but it might prevent mistakes. | ||
if (Array.isArray(stack) && typeof index === 'number' && stack[index]) { | ||
return { stack, index }; | ||
} | ||
} catch (e) {} // eslint-disable-line no-empty | ||
|
||
return null; | ||
} | ||
|
||
init(delta = 0) { | ||
return { | ||
...this._stack[this._index], | ||
action: 'POP', | ||
index: this._index, | ||
delta, | ||
}; | ||
} | ||
|
||
transition(location) { | ||
// Match BrowserProtocol here in only saving these fields. | ||
const { action, pathname, search, hash, state } = location; | ||
|
||
const push = action === 'PUSH'; | ||
invariant( | ||
push || action === 'REPLACE', | ||
`Unrecognized memory protocol action ${action}.`, | ||
); | ||
|
||
const delta = push ? 1 : 0; | ||
this._index += delta; | ||
|
||
const keyIndex = this._keyIndex++; | ||
const key = `${this._keyPrefix}:${keyIndex.toString(36)}`; | ||
|
||
this._stack[this._index] = { pathname, search, hash, state, key }; | ||
if (push) { | ||
this._stack.length = this._index + 1; | ||
} | ||
|
||
if (this._persistent) { | ||
this._saveState(); | ||
} | ||
|
||
return { ...location, key, index: this._index, delta }; | ||
} | ||
|
||
go(delta) { | ||
const prevIndex = this._index; | ||
|
||
this._index = Math.min( | ||
Math.max(this._index + delta, 0), | ||
this._stack.length - 1, | ||
); | ||
|
||
if (this._index === prevIndex) { | ||
return; | ||
} | ||
|
||
if (this._persistent) { | ||
this._saveState(); | ||
} | ||
|
||
if (this._listener) { | ||
this._listener(this.init(this._index - prevIndex)); | ||
} | ||
} | ||
|
||
_saveState() { | ||
try { | ||
window.sessionStorage.setItem( | ||
STATE_KEY, | ||
JSON.stringify({ | ||
stack: this._stack, | ||
index: this._index, | ||
}), | ||
); | ||
} catch (e) {} // eslint-disable-line no-empty | ||
} | ||
|
||
createHref(location) { | ||
return createPath(location); | ||
} | ||
|
||
subscribe(listener) { | ||
this._listener = listener; | ||
|
||
return () => { | ||
this._listener = null; | ||
}; | ||
} | ||
} |
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,200 @@ | ||
import MemoryProtocol from '../src/MemoryProtocol'; | ||
|
||
describe.only('MemoryProtocol', () => { | ||
it('should parse the initial loocation', () => { | ||
const protocol = new MemoryProtocol('/foo?bar=baz#qux'); | ||
|
||
expect(protocol.init()).to.eql({ | ||
action: 'POP', | ||
pathname: '/foo', | ||
search: '?bar=baz', | ||
hash: '#qux', | ||
index: 0, | ||
delta: 0, | ||
}); | ||
}); | ||
|
||
it('should support basic navigation', () => { | ||
const protocol = new MemoryProtocol('/foo'); | ||
|
||
const listener = sinon.spy(); | ||
protocol.subscribe(listener); | ||
|
||
const barLocation = protocol.transition({ | ||
action: 'PUSH', | ||
pathname: '/bar', | ||
state: { the: 'state' }, | ||
}); | ||
expect(barLocation).to.deep.include({ | ||
action: 'PUSH', | ||
pathname: '/bar', | ||
index: 1, | ||
delta: 1, | ||
state: { the: 'state' }, | ||
}); | ||
expect(barLocation.key).not.to.be.empty(); | ||
|
||
expect( | ||
protocol.transition({ action: 'PUSH', pathname: '/baz' }), | ||
).to.include({ | ||
action: 'PUSH', | ||
pathname: '/baz', | ||
index: 2, | ||
delta: 1, | ||
}); | ||
|
||
expect( | ||
protocol.transition({ action: 'REPLACE', pathname: '/qux' }), | ||
).to.include({ | ||
action: 'REPLACE', | ||
pathname: '/qux', | ||
index: 2, | ||
delta: 0, | ||
}); | ||
|
||
expect(listener).not.to.have.been.called(); | ||
|
||
protocol.go(-1); | ||
|
||
expect(listener).to.have.been.calledOnce(); | ||
expect(listener.firstCall.args[0]).to.deep.include({ | ||
action: 'POP', | ||
pathname: '/bar', | ||
key: barLocation.key, | ||
index: 1, | ||
delta: -1, | ||
state: { the: 'state' }, | ||
}); | ||
}); | ||
|
||
it('should support subscribing and unsubscribing', () => { | ||
const protocol = new MemoryProtocol('/foo'); | ||
protocol.transition({ action: 'PUSH', pathname: '/bar' }); | ||
protocol.transition({ action: 'PUSH', pathname: '/baz' }); | ||
|
||
const listener = sinon.spy(); | ||
const unsubscribe = protocol.subscribe(listener); | ||
|
||
protocol.go(-1); | ||
|
||
expect(listener).to.have.been.calledOnce(); | ||
expect(listener.firstCall.args[0]).to.include({ | ||
action: 'POP', | ||
pathname: '/bar', | ||
}); | ||
listener.reset(); | ||
|
||
unsubscribe(); | ||
|
||
protocol.go(-1); | ||
|
||
expect(listener).not.to.have.been.called(); | ||
}); | ||
|
||
it('should respect stack bounds', () => { | ||
const protocol = new MemoryProtocol('/foo'); | ||
protocol.transition({ action: 'PUSH', pathname: '/bar' }); | ||
protocol.transition({ action: 'PUSH', pathname: '/baz' }); | ||
|
||
const listener = sinon.spy(); | ||
protocol.subscribe(listener); | ||
|
||
protocol.go(-390); | ||
|
||
expect(listener).to.have.been.calledOnce(); | ||
expect(listener.firstCall.args[0]).to.include({ | ||
action: 'POP', | ||
pathname: '/foo', | ||
delta: -2, | ||
}); | ||
listener.reset(); | ||
|
||
protocol.go(-1); | ||
|
||
expect(listener).not.to.have.been.called(); | ||
|
||
protocol.go(+22); | ||
|
||
expect(listener).to.have.been.calledOnce(); | ||
expect(listener.firstCall.args[0]).to.include({ | ||
action: 'POP', | ||
pathname: '/baz', | ||
delta: 2, | ||
}); | ||
listener.reset(); | ||
|
||
protocol.go(+1); | ||
|
||
expect(listener).not.to.have.been.called(); | ||
}); | ||
|
||
it('should reset forward entries on push', () => { | ||
const protocol = new MemoryProtocol('/foo'); | ||
protocol.transition({ action: 'PUSH', pathname: '/bar' }); | ||
protocol.transition({ action: 'PUSH', pathname: '/baz' }); | ||
protocol.go(-2); | ||
protocol.transition({ action: 'REPLACE', pathname: '/qux' }); | ||
|
||
const listener = sinon.spy(); | ||
protocol.subscribe(listener); | ||
|
||
protocol.go(+1); | ||
|
||
expect(listener).to.have.been.calledOnce(); | ||
expect(listener.firstCall.args[0]).to.include({ | ||
action: 'POP', | ||
pathname: '/bar', | ||
delta: 1, | ||
}); | ||
}); | ||
|
||
it('should not reset forward entries on replace', () => { | ||
const protocol = new MemoryProtocol('/foo'); | ||
protocol.transition({ action: 'PUSH', pathname: '/bar' }); | ||
protocol.transition({ action: 'PUSH', pathname: '/baz' }); | ||
protocol.go(-2); | ||
protocol.transition({ action: 'PUSH', pathname: '/qux' }); | ||
|
||
const listener = sinon.spy(); | ||
protocol.subscribe(listener); | ||
|
||
protocol.go(+1); | ||
|
||
expect(listener).not.to.have.been.called(); | ||
}); | ||
|
||
it('should support createHref', () => { | ||
const protocol = new MemoryProtocol('/foo'); | ||
|
||
expect( | ||
protocol.createHref({ | ||
pathname: '/foo', | ||
search: '?bar=baz', | ||
hash: '#qux', | ||
}), | ||
).to.equal('/foo?bar=baz#qux'); | ||
}); | ||
|
||
it('should support persistence', () => { | ||
window.sessionStorage.clear(); | ||
|
||
const protocol1 = new MemoryProtocol('/foo', { persistent: true }); | ||
expect(protocol1.init()).to.include({ | ||
pathname: '/foo', | ||
}); | ||
|
||
protocol1.transition({ action: 'PUSH', pathname: '/bar' }); | ||
protocol1.transition({ action: 'PUSH', pathname: '/baz' }); | ||
protocol1.go(-1); | ||
|
||
const protocol2 = new MemoryProtocol('/foo', { persistent: true }); | ||
expect(protocol2.init()).to.include({ | ||
pathname: '/bar', | ||
}); | ||
|
||
protocol2.go(+1); | ||
expect(protocol2.init()).to.include({ | ||
pathname: '/baz', | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I originally did this sticking index and delta on location, but what's the value in doing so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in principle for consistency with browser protocol... e.g. if you want to animate differently based on whether delta is positive or negative
not sure if index has any purpose... really just from remix-run/history#36 and remix-run/history#334