From cc4ba3b063f14943579ffbfe416828590f70ae0a Mon Sep 17 00:00:00 2001 From: "bozo.kopic" Date: Tue, 22 Mar 2022 01:31:27 +0100 Subject: WIP major rewrite --- src_js/hatter/common.js | 66 ------------------ src_js/hatter/main.js | 41 ----------- src_js/hatter/renderer.js | 153 ----------------------------------------- src_js/hatter/util.js | 172 ---------------------------------------------- src_js/hatter/vt.js | 4 -- 5 files changed, 436 deletions(-) delete mode 100644 src_js/hatter/common.js delete mode 100644 src_js/hatter/main.js delete mode 100644 src_js/hatter/renderer.js delete mode 100644 src_js/hatter/util.js delete mode 100644 src_js/hatter/vt.js (limited to 'src_js') diff --git a/src_js/hatter/common.js b/src_js/hatter/common.js deleted file mode 100644 index c6dcfcc..0000000 --- a/src_js/hatter/common.js +++ /dev/null @@ -1,66 +0,0 @@ - -import r from 'hatter/renderer'; -import * as util from 'hatter/util'; - - -export const defaultState = { - conn: null, - log: { - offset: 0, - offsetText: '0', - limit: 0, - limitText: '0', - entries: [] - }, - job: { - active: null, - queue: [] - }, - repositories: [] -}; - - -export function processMsg(msg) { - if (msg.type == 'repositories') { - r.set('repositories', msg.repositories); - } else if (msg.type == 'active_job') { - r.set(['job', 'active'], msg.job); - } else if (msg.type == 'job_queue') { - r.set(['job', 'queue'], msg.jobs); - } else if (msg.type == 'log_entries') { - r.set(['log', 'entires'], msg.entries); - } -} - - -export function addJob(repository) { - r.get('conn').send(JSON.stringify({ - type: 'add_job', - repository: repository, - commit: 'HEAD' - })); -} - - -bean.on(r, 'change', state => { - let newOffset = parseInt(state.log.offsetText); - let newLimit = parseInt(state.log.limitText); - if (util.isInteger(newOffset) && newOffset != state.log.offset) { - r.set(['log', 'offset'], newOffset).then(sendSetLog); - } - if (util.isInteger(newLimit) && newLimit != state.log.limit) { - r.set(['log', 'limit'], newLimit).then(sendSetLog); - } -}); - - -function sendSetLog() { - let conn = r.get('conn'); - if (!conn) - return; - conn.send(JSON.stringify({ - type: 'set_log', - offset: r.get('offset'), - limit: r.get('limit') - })); -} diff --git a/src_js/hatter/main.js b/src_js/hatter/main.js deleted file mode 100644 index 3f64ed3..0000000 --- a/src_js/hatter/main.js +++ /dev/null @@ -1,41 +0,0 @@ -import bean from 'bean'; - -import r from 'hatter/renderer'; -import * as util from 'hatter/util'; -import * as common from 'hatter/common'; -import * as vt from 'hatter/vt'; - -import 'static!static/index.html'; -import 'style/main.scss'; - - -function main() { - let conn = new WebSocket(wsAddress); - - conn.onopen = () => { - let root = document.body.appendChild(document.createElement('div')); - let state = util.set('conn', conn, common.defaultState); - r.init(root, state, vt.main); - }; - - conn.onclose = () => { - alert("Disconnected from server"); - }; - - conn.onerror = () => { - alert("Couldn't connect to server"); - }; - - conn.onmessage = (evt) => { - try { - let msg = JSON.parse(evt.data); - common.processMsg(msg); - } catch(e) { - conn.close(); - throw e; - } - }; -} - - -bean.on(window, 'load', main); diff --git a/src_js/hatter/renderer.js b/src_js/hatter/renderer.js deleted file mode 100644 index aed7c48..0000000 --- a/src_js/hatter/renderer.js +++ /dev/null @@ -1,153 +0,0 @@ -import Delegator from 'dom-delegator'; -import bean from 'bean'; -import vh from 'virtual-dom/h'; -import diff from 'virtual-dom/diff'; -import patch from 'virtual-dom/patch'; -import createElement from 'virtual-dom/create-element'; - -import * as u from 'hatter/util'; - - -const delegator = Delegator(); -const vhTypes = ['VirtualNode', 'Widget']; - - -function vhFromArray(node) { - if (!node) - return []; - if (u.isString(node) || vhTypes.includes(node.type)) - return node; - if (!u.isArray(node)) - throw 'Invalid node structure'; - if (node.length < 1) - return []; - if (typeof node[0] != 'string') - return node.map(vhFromArray); - let hasProps = (node.length > 1 && - u.isObject(node[1]) && - !vhTypes.includes(node[1].type)); - let children = Array.from( - u.flatten(node.slice(hasProps ? 2 : 1).map(vhFromArray))); - let result = hasProps ? vh(node[0], node[1], children) : - vh(node[0], children); - return result; -} - - -class VTreeRenderer { - - constructor(el) { - this._el = el; - this._vtree = null; - } - - render(vtree) { - let vt = vhFromArray(vtree); - if (vt.type == 'VirtualNode') { - if (this._vtree) { - let d = diff(this._vtree, vt); - patch(this._el.firstChild, d); - } else { - while (this._el.firstChild) - this._el.removeChild(this._el.firstChild); - this._el.appendChild(createElement(vt)); - } - this._vtree = vt; - } else { - this._vtree = null; - while (this._el.firstChild) - this._el.removeChild(this._el.firstChild); - } - } - -} - - -export class Renderer { - - constructor(el, initState, vtCb, maxFps) { - this.init(el, initState, vtCb, maxFps); - } - - init(el, initState, vtCb, maxFps) { - this._state = null; - this._changes = []; - this._promise = null; - this._timeout = null; - this._lastRender = null; - this._vtCb = vtCb; - this._maxFps = maxFps; - this._r = new VTreeRenderer(el || document.querySelector('body')); - if (initState) - this.change(_ => initState); - } - - get(...paths) { - return u.get(paths, this._state); - } - - set(path, value) { - if (arguments.length < 2) { - value = path; - path = []; - } - return this.change(path, _ => value); - } - - change(path, cb) { - if (arguments.length < 2) { - cb = path; - path = []; - } - this._changes.push([path, cb]); - if (this._promise) - return this._promise; - this._promise = new Promise((resolve, reject) => { - setTimeout(() => { - try { - this._change(); - } catch(e) { - this._promise = null; - reject(e); - throw e; - } - this._promise = null; - resolve(); - }, 0); - }); - return this._promise; - } - - _change() { - let change = false; - while (this._changes.length > 0) { - let [path, cb] = this._changes.shift(); - let view = u.get(path); - let oldState = this._state; - this._state = u.change(path, cb, this._state); - if (this._state && u.equals(view(oldState), - view(this._state))) - continue; - change = true; - if (!this._vtCb || this._timeout) - continue; - let delay = (!this._lastRender || !this._maxFps ? - 0 : - (1000 / self._maxFps) - - (performance.now() - this._lastRender)); - this._timeout = setTimeout(() => { - this._timeout = null; - this._lastRender = performance.now(); - this._r.render(this._vtCb(this._state)); - bean.fire(this, 'render', this._state); - }, (delay > 0 ? delay : 0)); - } - if (change) - bean.fire(this, 'change', this._state); - } - -} - - -const defaultRenderer = new Renderer(); -export default defaultRenderer; diff --git a/src_js/hatter/util.js b/src_js/hatter/util.js deleted file mode 100644 index e3dbfeb..0000000 --- a/src_js/hatter/util.js +++ /dev/null @@ -1,172 +0,0 @@ - - -export const isArray = Array.isArray; -export const isObject = obj => obj !== null && - typeof(obj) == 'object' && - !isArray(obj); -export const isNumber = n => typeof(n) == 'number'; -export const isInteger = Number.isInteger; -export const isString = str => typeof(str) == 'string'; - - -export function clone(obj) { - if (isArray(obj)) - return Array.from(obj, clone); - if (isObject(obj)) { - let ret = {}; - for (let i in obj) - ret[i] = clone(obj[i]); - return ret; - } - return obj; -} - - -export function equals(x, y) { - if (x === y) - return true; - if (typeof(x) != 'object' || typeof(y) != 'object' || x === null || y === null) - return false; - if (Array.isArray(x) || Array.isArray(y)) { - if (!Array.isArray(x) || !Array.isArray(y) || x.length != y.length) - return false; - } - for (let i in x) - if (!equals(x[i], y[i])) - return false; - for (let i in y) - if (!equals(x[i], y[i])) - return false; - return true; -} - - -export function toPairs(obj) { - return Object.entries(obj); -} - - -export function fromPairs(arr) { - let ret = {}; - for (let [k, v] of arr) - ret[k] = v; - return ret; -} - - -export function* flatten(arr) { - if (isArray(arr)) { - for (let i of arr) - if (isArray(i)) - yield* flatten(i); - else - yield i; - } else { - yield arr; - } -} - - -export function pipe(...fns) { - if (fns.length < 1) - throw 'no functions'; - return function (...args) { - let ret = fns[0].apply(this, args); - for (let fn of fns.slice(1)) - ret = fn(ret); - return ret; - }; -} - - -export function curry(fn) { - let wrapper = function(oldArgs) { - return function(...args) { - args = oldArgs.concat(args); - if (args.length >= fn.length) - return fn.apply(this, args); - return wrapper(args); - }; - }; - return wrapper([]); -} - - -export const get = curry((path, obj) => { - let ret = obj; - for (let i of flatten(path)) { - if (ret === null || typeof(ret) != 'object') - return undefined; - ret = ret[i]; - } - return ret; -}); - - -export const change = curry((path, fn, obj) => { - function _change(path, obj) { - if (isInteger(path[0])) { - obj = (isArray(obj) ? Array.from(obj) : []); - } else if (isString(path[0])) { - obj = (isObject(obj) ? Object.assign({}, obj) : {}); - } else { - throw 'invalid path'; - } - if (path.length > 1) { - obj[path[0]] = _change(path.slice(1), obj[path[0]]); - } else { - obj[path[0]] = fn(obj[path[0]]); - } - return obj; - } - path = Array.from(flatten(path)); - if (path.length < 1) - return fn(obj); - return _change(path, obj); -}); - - -export const set = curry((path, val, obj) => change(path, _ => val, obj)); - - -export const omit = curry((path, obj) => { - function _omit(path, obj) { - if (isInteger(path[0])) { - obj = (isArray(obj) ? Array.from(obj) : []); - } else if (isString(path[0])) { - obj = (isObject(obj) ? Object.assign({}, obj) : {}); - } else { - throw 'invalid path'; - } - if (path.length > 1) { - obj[path[0]] = _omit(path.slice(1), obj[path[0]]); - } else { - delete obj[path[0]]; - } - return obj; - } - path = Array.from(flatten(path)); - if (path.length < 1) - return undefined; - return _omit(path, obj); -}); - - -export const sortBy = curry((fn, arr) => Array.from(arr).sort((x, y) => { - let xVal = fn(x); - let yVal = fn(y); - if (xVal < yVal) - return -1; - if (xVal > yVal) - return 1; - return 0; -})); - - -export const map = curry((fn, arr) => arr.map(fn)); - - -export const filter = curry((fn, arr) => arr.filter(fn)); - - -export const append = curry((val, arr) => arr.concat([val])); diff --git a/src_js/hatter/vt.js b/src_js/hatter/vt.js deleted file mode 100644 index c1fcb9f..0000000 --- a/src_js/hatter/vt.js +++ /dev/null @@ -1,4 +0,0 @@ - -export function main() { - return ['div', 'Hatter']; -} -- cgit v1.2.3-70-g09d2