-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API: add path support (navigation) (#352)
- Loading branch information
Showing
9 changed files
with
281 additions
and
3 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,44 @@ | ||
import { BehaviorSubject } from 'rxjs' | ||
import { first } from 'rxjs/operators' | ||
|
||
function ensureContext (apps, appAddress, context) { | ||
let app = apps.get(appAddress) | ||
if (!app) { | ||
app = new Map() | ||
apps.set(appAddress, app) | ||
} | ||
|
||
let appContext = app.get(context) | ||
if (!appContext) { | ||
appContext = new BehaviorSubject(null) | ||
app.set(context, appContext) | ||
} | ||
|
||
return appContext | ||
} | ||
|
||
export default class AppContextPool { | ||
#apps = new Map() | ||
|
||
hasApp (appAddress) { | ||
return this.#apps.has(appAddress) | ||
} | ||
|
||
async get (appAddress, context) { | ||
const app = this.#apps.get(appAddress) | ||
if (!app || !app.has(context)) { | ||
return null | ||
} | ||
return app.get(context).pipe(first()).toPromise() | ||
} | ||
|
||
observe (appAddress, context) { | ||
const appContext = ensureContext(this.#apps, appAddress, context) | ||
return appContext | ||
} | ||
|
||
set (appAddress, context, value) { | ||
const appContext = ensureContext(this.#apps, appAddress, context) | ||
appContext.next(value) | ||
} | ||
} |
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,62 @@ | ||
import test from 'ava' | ||
import AppContextPool from './index' | ||
|
||
test('AppContextPool starts empty', async (t) => { | ||
// arrange | ||
const appAddress = '0x12' | ||
// act | ||
const pool = new AppContextPool() | ||
// assert | ||
t.false(pool.hasApp(appAddress)) | ||
}) | ||
|
||
test('AppContextPool can create new app contexts', async (t) => { | ||
// arrange | ||
const appAddress = '0x12' | ||
// act | ||
const pool = new AppContextPool() | ||
pool.set(appAddress, 'path', '/vote') | ||
// assert | ||
t.true(pool.hasApp(appAddress)) | ||
}) | ||
|
||
test('AppContextPool can read and write values to app context', async (t) => { | ||
// arrange | ||
const appAddress = '0x12' | ||
// act | ||
const pool = new AppContextPool() | ||
pool.set(appAddress, 'first', 'first value') | ||
pool.set(appAddress, 'second', 'first value') | ||
pool.set(appAddress, 'second', 'second value') | ||
// assert | ||
t.is(await pool.get(appAddress, 'first'), 'first value') | ||
t.is(await pool.get(appAddress, 'second'), 'second value') | ||
}) | ||
|
||
test('AppContextPool can observe values from app context', async (t) => { | ||
// arrange | ||
const appAddress = '0x12' | ||
const contextKey = 'key' | ||
// act | ||
const pool = new AppContextPool() | ||
const observedContext = pool.observe(appAddress, contextKey) | ||
pool.set(appAddress, contextKey, 'first value') // starting value | ||
// assert | ||
let counter = 0 | ||
observedContext.subscribe(val => { | ||
if (counter === 0) { | ||
t.is(val, 'first value') | ||
} else if (counter === 1) { | ||
t.is(val, 'second value') | ||
} else if (counter === 2) { | ||
t.is(val, 'third value') | ||
} else { | ||
t.fail('too many emissions') | ||
} | ||
counter++ | ||
}) | ||
|
||
// Emit after subscribed | ||
pool.set(appAddress, contextKey, 'second value') | ||
pool.set(appAddress, contextKey, 'third value') | ||
}) |
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,14 @@ | ||
export default function (request, proxy, wrapper) { | ||
const [operation] = request.params | ||
|
||
if (operation === 'observe') { | ||
return wrapper.appContextPool.observe(proxy.address, 'path') | ||
} | ||
if (operation === 'modify') { | ||
return wrapper.requestAppPath(proxy.address, request.params[1]) | ||
} | ||
|
||
return Promise.reject( | ||
new Error('Invalid path operation') | ||
) | ||
} |