Skip to content
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

feature: add gelf, a gunified elf for managing pvp hypergraphs from elf world #1389

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make gelf use a gun node explicitly as context
  • Loading branch information
Tyler Childs committed Oct 21, 2024
commit 234ef0253b4c652f3a8183b2d3fe8d3afbd7ccb5
25 changes: 25 additions & 0 deletions examples/gelf/array-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<array-test></array-test>
<script type="module">
import gelf from './gelf.js'

const $ = gelf('array-test', {list: []})

let i = 0
$.draw((target) => {
const { list } = $.learn()

return `
<button>add</button>
${list.join('')}
`
})

$.when('click', 'button', function addArrayItem(event) {
$.teach(++i, (state, payload) => {
return {
...state,
list: [...state.list, payload]
}
})
})
</script>
20 changes: 11 additions & 9 deletions examples/gelf/gelf.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function update(link, target, compositor, lifeCycle={}) {
function draw(link, compositor, lifeCycle={}) {
insight('module:draw', link)
listen(CREATE_EVENT, link, (event) => {
gun.get(this.seed).get(link).on(cache => {
this.get(link).on(cache => {
database[link] = JSON.parse(cache) || {}
update(link, event.target, compositor, lifeCycle)
})
Expand All @@ -61,10 +61,10 @@ export function learn(link) {

export function teach(link, knowledge, nuance = (s, p) => ({...s,...p})) {
insight('module:teach', link)
gun.get(this.seed).get(link).once(cache => {
this.get(link).once(cache => {
const data = cache ? JSON.parse(cache) : {}
const latest = nuance(data, knowledge);
gun.get(this.seed).get(link).put(JSON.stringify(latest))
this.get(link).put(JSON.stringify(latest))
})
}

Expand All @@ -76,15 +76,17 @@ export function when(link1, type, link2, callback) {

export default function module(link, initialState = {}) {
insight('module', link)
teach.call(this, link, initialState)

const seed = this && this.seed ? this.seed : 'root'
const root = gun.get(seed)
teach.call(root, link, initialState)
return {
link,
learn: learn.bind(this, link),
draw: draw.bind(this, link),
style: style.bind(this, link),
when: when.bind(this, link),
teach: teach.bind(this, link),
learn: learn.bind(root, link),
draw: draw.bind(root, link),
style: style.bind(root, link),
when: when.bind(root, link),
teach: teach.bind(root, link),
}
}

Expand Down
14 changes: 5 additions & 9 deletions examples/gelf/note.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<!DOCTYPE html>
<style>html, body, textarea { width: 100%; height: 100%; padding: 0; margin: 0; }</style>
<gelf-note></gelf-note>
<gelf-note>
<textarea id="view" placeholder="write here..."></textarea>
</gelf-note>
<script type="module">
import gelf from './gelf.js'

Expand All @@ -11,14 +13,8 @@
const $ = gelf.call(context, 'gelf-note')

$.draw((target) => {
const { value } = $.learn()

if(!target.innerHTML) {
target.innerHTML = `<textarea id="view" placeholder="write here..."></textarea>`
target.view = target.querySelector('#view')
}

target.view.value = value
: const { value } = $.learn()
target.querySelector('#view').value = value || ''
})

$.when('input', 'textarea', ({target}) => $.teach({
Expand Down