-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb-foo.js
179 lines (156 loc) · 6.33 KB
/
web-foo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
(function(){
/*
Things which will find their way here:
- github pages script
- canvas setup and manipulation script
- neural net setup script
- misc utils like: clone, fetch wrapped (probably a bunch of other things done better elsewhere)
Motivation:
- I don't need a large lib or build system setup (for the following)
- I don't want to set up a server (for the following)
- I like to do react / redux / rx with just a few files
- I like to do simple graphic experiments (canvas, d3, )
- I like to do neural networks in the browser
- there are things I do (like clone and fetching) which are lacking in js
How this works (or should work):
- one script tag for lib (this one) at top of html file (maybe another for page js)
- this script will fetch other scripts when told to set up a particular context or use-case
- refer to docs for context / use case
*/
function loadScript(scriptUrl){
return new Promise(function (res, rej) {
let script = document.createElement('script');
script.src = scriptUrl;
script.type = 'text/javascript';
script.onError = rej;
script.async = true;
script.onload = res;
script.addEventListener('error',rej);
script.addEventListener('load',res);
document.head.appendChild(script);
});
}
function consoleOrCallback(callback){
if(callback && typeof callback === 'function'){
return callback;
}
return (error, data) => {
if (error){
console.error(error);
return;
}
if (data){
console.log(data);
}
}
}
const isInitedFactory = function(parent, callback){
const cb = consoleOrCallback(callback);
if(parent.isInited){
cb(`${parent.name}: script already inited!`);
}
var scripts = parent.scripts || [];
Promise.all(scripts.map(s => loadScript(s)))
.then(() => {
parent.isInited = true;
if(parent.scriptsAfter){
parent.scriptsAfter(cb);
return;
}
cb(null, `${parent.name}: script is inited!`);
})
.catch(() => {
const errorMessage = `error initialising ${parent.name}`;
cb(errorMessage);
});
}
const returnProps = function(o){
return Object.keys(o).reduce((all, key) => {
all[key] = o[key];
return all;
}, {})
}
// GITHUBPAGES -------------------------------------------------------------
function githubPages(){ return returnProps(githubPages); }
githubPages.scripts = [
'https://crosshj.com/experiments/ghPageHelper.js'
];
githubPages.init = callback => isInitedFactory(githubPages, callback);
// RXREACT -----------------------------------------------------------------
function rxReact(){ return returnProps(rxReact); }
rxReact.scripts = [
'https://unpkg.com/rxjs@beta/bundles/rxjs.umd.js',
'https://unpkg.com/react@16/umd/react.development.js',
'https://unpkg.com/react-dom@16/umd/react-dom.development.js'
];
rxReact.scriptsAfter = (callback) => {
const { of, fromEvent, from, range } = rxjs;
const { Observable, Subject } = rxjs;
const { map, filter, startWith, scan } = rxjs.operators;
const { render } = ReactDOM;
const { createElement, Component } = React;
const components = {
div: (props, children) => createElement("div", props, children),
textarea: (props, children) => createElement("textarea", props, children),
h4: (props, children) => createElement("h4", props, children),
h1: (props, children) => createElement("h1", props, children),
fragment: children => createElement(Fragment, null, children)
};
const action$ = new Subject();
const dispatcher = function(action){
return action$.next(action);
}
function RXConnector(props) {
this.state = {};
this.render = () => {
return fragment(props.render(this.state));
};
this.observable$ = props.observable$;
this.componentWillMount = function(){
this.observable$.subscribe(o => {
this.setState(o)
});
}
}
RXConnector.prototype = Object.create(Component.prototype);
const Connector = (props, children) => createElement(RXConnector, props, children);
const start = ({reducer, root, attach}) => {
const initialState = {};
const store$ = action$
.pipe(
scan(reducer, initialState)
);
render(
Connector({ observable$: store$, render: root }),
attach
);
};
callback(null, { components, dispatcher, start, React, rxjs });
};
rxReact.init = callback => isInitedFactory(githubPages, callback);
// CANVAS ------------------------------------------------------------------
function canvas(){ return returnProps(canvas); }
canvas.scripts = [
'https://crosshj.com/sandbox/canvas_plus.js'
];
// TODO: include helpers that set everything up except the basic needs
canvas.init = callback => isInitedFactory(canvas, callback);
function neural(){ return returnProps(neural); }
neural.scripts = [
'https://cdnjs.cloudflare.com/ajax/libs/synaptic/1.0.10/synaptic.js'
];
// TODO: include helpers that set everything up except the basic needs
neural.init = callback => isInitedFactory(neural, callback);
function utils(){ return returnProps(utils); }
// TODO: create utils script and host on github
// TODO: include helpers that set everything up except the basic needs
utils.init = callback => isInitedFactory(utils, callback);
// TODO: there should be a way of loading multiple contexts / modules
window.footils = {
githubPages,
rxReact,
canvas,
neural,
utils
};
})();