-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #450 from coasys/hooks-docs
Hooks & docs for langauge and hooks
- Loading branch information
Showing
36 changed files
with
2,616 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
node_modules | ||
dist | ||
package-lock.json | ||
|
||
**/.vitepress/cache | ||
|
||
# Log files | ||
*.log | ||
|
||
# Editor directories and files | ||
.idea | ||
.vscode | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? | ||
*.turbo | ||
dev-dist | ||
.DS_Store | ||
|
||
cachedb | ||
# Local Netlify folder | ||
.netlify | ||
!register.js |
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,22 @@ | ||
{ | ||
"name": "@coasys/hooks-helpers", | ||
"version": "0.8.2-prerelease", | ||
"description": "", | ||
"main": "./src/index.ts", | ||
"module": "./src/index.ts", | ||
"private": false, | ||
"type": "module", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"scripts": {}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@coasys/ad4m": "*", | ||
"@coasys/ad4m-connect": "*", | ||
"uuid": "*" | ||
} | ||
} |
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,70 @@ | ||
import { PerspectiveProxy } from "@coasys/ad4m"; | ||
|
||
const cache: Map<string, any> = new Map(); | ||
const subscribers: Map<string, Function[]> = new Map(); | ||
|
||
export function getCache<T>(key: string) { | ||
const match: T | undefined = cache.get(key); | ||
return match; | ||
} | ||
|
||
export function setCache<T>(key: string, value: T) { | ||
cache.set(key, value); | ||
getSubscribers(key).forEach((cb) => cb()); | ||
} | ||
|
||
export function subscribe(key: string, callback: Function) { | ||
getSubscribers(key).push(callback); | ||
} | ||
|
||
export function unsubscribe(key: string, callback: Function) { | ||
const subs = getSubscribers(key); | ||
const index = subs.indexOf(callback); | ||
if (index >= 0) { | ||
subs.splice(index, 1); | ||
} | ||
} | ||
|
||
export function getSubscribers(key: string) { | ||
if (!subscribers.has(key)) subscribers.set(key, []); | ||
return subscribers.get(key)!; | ||
} | ||
|
||
export function subscribeToPerspective( | ||
perspective: PerspectiveProxy, | ||
added: Function, | ||
removed: Function | ||
) { | ||
const addedKey = `perspective-${perspective.uuid}-added`; | ||
const removedKey = `perspective-${perspective.uuid}-removed`; | ||
|
||
if (!subscribers.has(addedKey)) { | ||
console.log("subscribing!"); | ||
perspective.addListener("link-added", (link) => { | ||
subscribers.get(addedKey).forEach((cb) => cb(link)); | ||
return null; | ||
}); | ||
} | ||
|
||
if (!subscribers.has(removedKey)) { | ||
perspective.addListener("link-removed", (link) => { | ||
subscribers.get(removedKey).forEach((cb) => cb(link)); | ||
return null; | ||
}); | ||
} | ||
|
||
subscribe(addedKey, added); | ||
subscribe(removedKey, removed); | ||
} | ||
|
||
export function unsubscribeFromPerspective( | ||
perspective: PerspectiveProxy, | ||
added: Function, | ||
removed: Function | ||
) { | ||
const addedKey = `perspective-${perspective.uuid}-added`; | ||
const removedKey = `perspective-${perspective.uuid}-removed`; | ||
|
||
unsubscribe(addedKey, added); | ||
unsubscribe(removedKey, removed); | ||
} |
Oops, something went wrong.