Skip to content

Commit

Permalink
Merge pull request #450 from coasys/hooks-docs
Browse files Browse the repository at this point in the history
Hooks & docs for langauge and hooks
  • Loading branch information
lucksus authored Feb 20, 2024
2 parents 1035581 + ade3a2e commit c5a2a20
Show file tree
Hide file tree
Showing 36 changed files with 2,616 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,30 @@ jobs:
package: connect/package.json
access: public

- name: Publish ad4m hook helpers
uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.COASYS_NPM_TOKEN }}
package: ad4m-hooks/helpers/package.json
tag: ${{ env.NPM_TAG }}
access: public

- name: Publish ad4m react hooks
uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.COASYS_NPM_TOKEN }}
package: ad4m-hooks/react/package.json
tag: ${{ env.NPM_TAG }}
access: public

- name: Publish ad4m vue hooks
uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.COASYS_NPM_TOKEN }}
package: ad4m-hooks/vue/package.json
tag: ${{ env.NPM_TAG }}
access: public

- name: Publish executor
uses: JS-DevTools/npm-publish@v1
with:
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/publish_staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,30 @@ jobs:
package: connect/package.json
tag: ${{ env.NPM_TAG }}
access: public

- name: Publish ad4m hook helpers
uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.COASYS_NPM_TOKEN }}
package: ad4m-hooks/helpers/package.json
tag: ${{ env.NPM_TAG }}
access: public

- name: Publish ad4m react hooks
uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.COASYS_NPM_TOKEN }}
package: ad4m-hooks/react/package.json
tag: ${{ env.NPM_TAG }}
access: public

- name: Publish ad4m vue hooks
uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.COASYS_NPM_TOKEN }}
package: ad4m-hooks/vue/package.json
tag: ${{ env.NPM_TAG }}
access: public

- name: Publish executor
uses: JS-DevTools/npm-publish@v1
Expand Down
25 changes: 25 additions & 0 deletions ad4m-hooks/.gitignore
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
22 changes: 22 additions & 0 deletions ad4m-hooks/helpers/package.json
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": "*"
}
}
70 changes: 70 additions & 0 deletions ad4m-hooks/helpers/src/cache.ts
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);
}
Loading

0 comments on commit c5a2a20

Please sign in to comment.