-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
205 additions
and
37 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 @@ | ||
'use strict' | ||
|
||
import sbp from '@sbp/sbp' | ||
import { NAMESPACE_REGISTRATION } from '../utils/events.js' | ||
|
||
// NOTE: prefix groups with `group/` and users with `user/` ? | ||
sbp('sbp/selectors/register', { | ||
'namespace/lookupCached': (name: string) => { | ||
const cache = sbp('chelonia/rootState').namespaceLookups | ||
// 'cache' may be undefined when starting up or after calling chelonia/reset | ||
return cache?.[name] ?? null | ||
}, | ||
'namespace/lookupReverseCached': (id: string) => { | ||
const cache = sbp('chelonia/rootState').reverseNamespaceLookups | ||
return cache?.[id] ?? null | ||
}, | ||
'namespace/lookup': (name: string, { skipCache }: { skipCache: boolean } = { skipCache: false }) => { | ||
if (!skipCache) { | ||
const cached = sbp('namespace/lookupCached', name) | ||
if (cached) { | ||
// Wrapping in a Promise to return a consistent type across all execution | ||
// paths (next return is a Promise) | ||
// This way we can call .then() on the result | ||
return Promise.resolve(cached) | ||
} | ||
} | ||
return fetch(`${sbp('okTurtles.data/get', 'API_URL')}/name/${encodeURIComponent(name)}`).then((r: Object) => { | ||
if (!r.ok) { | ||
console.warn(`namespace/lookup: ${r.status} for ${name}`) | ||
if (r.status !== 404) { | ||
throw new Error(`${r.status}: ${r.statusText}`) | ||
} | ||
return null | ||
} | ||
return r['text']() | ||
}).then(value => { | ||
if (value !== null) { | ||
const reactiveSet = sbp('chelonia/config').reactiveSet | ||
const rootState = sbp('chelonia/rootState') | ||
if (!rootState.namespaceLookups) reactiveSet(rootState, 'namespaceLookups', Object.create(null)) | ||
if (!rootState.reverseNamespaceLookups) reactiveSet(rootState, 'reverseNamespaceLookups', Object.create(null)) | ||
const cache = rootState.namespaceLookups | ||
const reverseCache = rootState.reverseNamespaceLookups | ||
reactiveSet(cache, name, value) | ||
reactiveSet(reverseCache, value, name) | ||
sbp('okTurtles.events/emit', NAMESPACE_REGISTRATION, { name, value }) | ||
} | ||
return 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
Oops, something went wrong.