subfork 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,7 +5,7 @@ Javascript client library used to connect to [Subfork](https://subfork.com) site
5
5
  Usage:
6
6
 
7
7
  ```html
8
- <script src="https://cdn.jsdelivr.net/gh/subforkdev/subfork.js/subfork.min.js"></script>
8
+ <script src="https://cdn.jsdelivr.net/npm/subfork/dist/subfork.min.js"></script>
9
9
  ```
10
10
 
11
11
  Instantiate [Subfork](https://subfork.com) client:
package/dist/subfork.js CHANGED
@@ -47,12 +47,11 @@ Set on "done" callback when creating task:
47
47
  */
48
48
 
49
49
  // define some constants
50
- const version = "0.1.0";
50
+ const version = "0.1.1";
51
51
  const api_version = "api";
52
52
  const hostname = window.location.hostname;
53
53
  const port = window.location.port;
54
54
  const protocol = window.location.protocol;
55
- const socket_loaded = false;
56
55
  const socket_script = "https://code.subfork.com/socket.io.min.js";
57
56
  const wait_time = 100;
58
57
 
@@ -60,6 +59,7 @@ const wait_time = 100;
60
59
  var message;
61
60
  var server;
62
61
  var socket;
62
+ var socket_loaded = false;
63
63
 
64
64
  // async returns a sha256 string (only works with https)
65
65
  async function sha256(message) {
@@ -1 +1 @@
1
- const version="0.1.0",api_version="api",hostname=window.location.hostname,port=window.location.port,protocol=window.location.protocol,socket_loaded=!1,socket_script="https://code.subfork.com/socket.io.min.js",wait_time=100;var message,server,socket;async function sha256(t){const e=new TextEncoder("utf-8").encode(t),s=await window.crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(s)).map((t=>("00"+t.toString(16)).slice(-2))).join("")}function load_socket_library(t,e){if(socket_loaded)e(t);else{var s=document.createElement("script");s.src=socket_script,document.head.appendChild(s),s.onload=function(){socket_loaded=!0,e(t)}}}function wait_for(t,e){t()?e():window.setTimeout(wait_for.bind(null,t,e),100)}function build_url(t){return"/api/"+t}function is_local(){return!("http:"!==protocol||"localhost"!==hostname&&"0.0.0.0"!==hostname&&"127.0.0.1"!==hostname||"8000"!==port&&"8080"!==port)}function post_request(t,e={},s=null,o=!0){$.ajax({type:"POST",contentType:"application/json; charset=utf-8",url:t,async:o,data:JSON.stringify(e),success:function(t){s?s(t):console.debug("no callback")},dataType:"json"})}class Datatype{constructor(t){this.name=t}create(t,e=null){let s={collection:this.name,data:t,version:"0.1.0"};return post_request(build_url("data/create"),t=s,(function(t){e&&e(t)})),!1}delete(t,e=null){let s={collection:this.name,params:t,version:"0.1.0"};return post_request(build_url("data/delete"),s,(function(t){e&&e(t)})),!1}find(t,e=null,s=!1,o=!0){let a={collection:this.name,expand:s,params:t,version:"0.1.0"};return post_request(build_url("data/get"),a,(function(t){e&&e(t)}),o),!1}update(t,e,s=null){let o={collection:this.name,id:t,data:e,version:"0.1.0"};return post_request(build_url("data/update"),e=o,(function(t){s&&s(t)})),!1}}class SubforkEvent{constructor(t,e){this.name=t,this.type=e.type,this.message=e.message,this.event_data=e}data(){if("data"==this.type)return new Datatype(this.name)}task(){if("task"==this.type){let t=new SubforkTaskQueue(this.event_data.queue);return new SubforkTask(t,this.event_data.task)}}user(){if("user"==this.type)return new SubforkUser(queue,this.event_data.task)}}class SubforkTask{constructor(t,e){this.queue=t,this.data=e}get_error(){return this.data.error}get_results(){try{return JSON.parse(this.data.results)}catch{return this.data.results}}on(t,e){return this.queue.on(t,e)}}class SubforkTaskQueue{constructor(t,e){this.conn=t,this.name=e}create(t){var e=new SubforkTask(this,t);if(this.enqueue(e))return e}enqueue(t){let e={queue:this.name,data:t.data,version:"0.1.0"},s=!1;return post_request(build_url("task/create"),e,(function(t){t.success&&(s=!0)})),s}get(t){let e={queue:this.name,taskid:t,version:"0.1.0"};var s;return post_request(build_url("task/get"),e,(function(t){t.success?s=new SubforkTask(this,t.data):console.error(t.error)}),async=!1),s}on(t,e){let s=this.conn.session.sessionid+":task:"+this.name+":"+t;return socket.on(s,(function(s,o){let a=new SubforkEvent(t,s);e(a)})),!0}}class SubforkUser{constructor(t){this.data=t}}class SubforkCache{constructor(t){this.parent=t,this._cache={}}add(t,e,s){t in this._cache||(this._cache[t]={}),this._cache[t][e]=s}clear(){Object.keys(this._cache).forEach((t=>{delete this._cache[t]}))}del(t,e){t in this._cache&&e in this._cache[t]&&delete this._cache[t][e]}get(t,e){if(t in this._cache&&e in this._cache[t])return this._cache[t][e]}update(t,e){t in this._cache||(this._cache[t]={}),Object.assign(this._cache[t],e)}}class Subfork{constructor(t={}){this.cache=new SubforkCache(this),this.session={},this.set_config(t),this.connect()}set_config(t){this.config=t,this.config.host=this.config.host??window.location.hostname,this.config.port=this.config.port??window.location.port}connect(){this.session=this.get_session_data(),console.debug("sessionid",this.session.sessionid),load_socket_library(this.config.host,(function(t){socket=io("https://events.fork.io"),console.debug("connected to event server")}))}get_session_data(){let t={source:this.config.host,version:"api"},e={};return post_request(build_url("get_session_data"),t,(function(t){t.success&&t.data?e=t.data:console.error(t.error)}),!1),e}data(t){if(!this.cache.get("data",t)){var e=new Datatype(t);this.cache.add("data",t,e)}return this.cache.get("data",t)}is_connected(){return socket_loaded&&socket.connected}ready(t){wait_for((()=>window.socket),(()=>t()))}task(t){if(!this.cache.get("task",t)){var e=new SubforkTaskQueue(this,t);this.cache.add("task",t,e)}return this.cache.get("task",t)}user(t){if(!this.cache.get("user",t)){let s={username:t,version:"0.1.0"};var e;post_request(build_url("user/get"),s,(function(t){t.success&&(e=new SubforkUser(t.data))}),!1),this.cache.add("user",t,e)}return this.cache.get("user",t)}}
1
+ const version="0.1.1",api_version="api",hostname=window.location.hostname,port=window.location.port,protocol=window.location.protocol,socket_script="https://code.subfork.com/socket.io.min.js",wait_time=100;var message,server,socket,socket_loaded=!1;async function sha256(t){const e=new TextEncoder("utf-8").encode(t),s=await window.crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(s)).map((t=>("00"+t.toString(16)).slice(-2))).join("")}function load_socket_library(t,e){if(socket_loaded)e(t);else{var s=document.createElement("script");s.src=socket_script,document.head.appendChild(s),s.onload=function(){socket_loaded=!0,e(t)}}}function wait_for(t,e){t()?e():window.setTimeout(wait_for.bind(null,t,e),100)}function build_url(t){return"/api/"+t}function is_local(){return!("http:"!==protocol||"localhost"!==hostname&&"0.0.0.0"!==hostname&&"127.0.0.1"!==hostname||"8000"!==port&&"8080"!==port)}function post_request(t,e={},s=null,o=!0){$.ajax({type:"POST",contentType:"application/json; charset=utf-8",url:t,async:o,data:JSON.stringify(e),success:function(t){s?s(t):console.debug("no callback")},dataType:"json"})}class Datatype{constructor(t){this.name=t}create(t,e=null){let s={collection:this.name,data:t,version:"0.1.1"};return post_request(build_url("data/create"),t=s,(function(t){e&&e(t)})),!1}delete(t,e=null){let s={collection:this.name,params:t,version:"0.1.1"};return post_request(build_url("data/delete"),s,(function(t){e&&e(t)})),!1}find(t,e=null,s=!1,o=!0){let a={collection:this.name,expand:s,params:t,version:"0.1.1"};return post_request(build_url("data/get"),a,(function(t){e&&e(t)}),o),!1}update(t,e,s=null){let o={collection:this.name,id:t,data:e,version:"0.1.1"};return post_request(build_url("data/update"),e=o,(function(t){s&&s(t)})),!1}}class SubforkEvent{constructor(t,e){this.name=t,this.type=e.type,this.message=e.message,this.event_data=e}data(){if("data"==this.type)return new Datatype(this.name)}task(){if("task"==this.type){let t=new SubforkTaskQueue(this.event_data.queue);return new SubforkTask(t,this.event_data.task)}}user(){if("user"==this.type)return new SubforkUser(queue,this.event_data.task)}}class SubforkTask{constructor(t,e){this.queue=t,this.data=e}get_error(){return this.data.error}get_results(){try{return JSON.parse(this.data.results)}catch{return this.data.results}}on(t,e){return this.queue.on(t,e)}}class SubforkTaskQueue{constructor(t,e){this.conn=t,this.name=e}create(t){var e=new SubforkTask(this,t);if(this.enqueue(e))return e}enqueue(t){let e={queue:this.name,data:t.data,version:"0.1.1"},s=!1;return post_request(build_url("task/create"),e,(function(t){t.success&&(s=!0)})),s}get(t){let e={queue:this.name,taskid:t,version:"0.1.1"};var s;return post_request(build_url("task/get"),e,(function(t){t.success?s=new SubforkTask(this,t.data):console.error(t.error)}),async=!1),s}on(t,e){let s=this.conn.session.sessionid+":task:"+this.name+":"+t;return socket.on(s,(function(s,o){let a=new SubforkEvent(t,s);e(a)})),!0}}class SubforkUser{constructor(t){this.data=t}}class SubforkCache{constructor(t){this.parent=t,this._cache={}}add(t,e,s){t in this._cache||(this._cache[t]={}),this._cache[t][e]=s}clear(){Object.keys(this._cache).forEach((t=>{delete this._cache[t]}))}del(t,e){t in this._cache&&e in this._cache[t]&&delete this._cache[t][e]}get(t,e){if(t in this._cache&&e in this._cache[t])return this._cache[t][e]}update(t,e){t in this._cache||(this._cache[t]={}),Object.assign(this._cache[t],e)}}class Subfork{constructor(t={}){this.cache=new SubforkCache(this),this.session={},this.set_config(t),this.connect()}set_config(t){this.config=t,this.config.host=this.config.host??window.location.hostname,this.config.port=this.config.port??window.location.port}connect(){this.session=this.get_session_data(),console.debug("sessionid",this.session.sessionid),load_socket_library(this.config.host,(function(t){socket=io("https://events.fork.io"),console.debug("connected to event server")}))}get_session_data(){let t={source:this.config.host,version:"api"},e={};return post_request(build_url("get_session_data"),t,(function(t){t.success&&t.data?e=t.data:console.error(t.error)}),!1),e}data(t){if(!this.cache.get("data",t)){var e=new Datatype(t);this.cache.add("data",t,e)}return this.cache.get("data",t)}is_connected(){return socket_loaded&&socket.connected}ready(t){wait_for((()=>window.socket),(()=>t()))}task(t){if(!this.cache.get("task",t)){var e=new SubforkTaskQueue(this,t);this.cache.add("task",t,e)}return this.cache.get("task",t)}user(t){if(!this.cache.get("user",t)){let s={username:t,version:"0.1.1"};var e;post_request(build_url("user/get"),s,(function(t){t.success&&(e=new SubforkUser(t.data))}),!1),this.cache.add("user",t,e)}return this.cache.get("user",t)}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "subfork",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Subfork Javascript client",
5
5
  "main": "dist/subfork.js",
6
6
  "scripts": {
package/subfork.js CHANGED
@@ -47,12 +47,11 @@ Set on "done" callback when creating task:
47
47
  */
48
48
 
49
49
  // define some constants
50
- const version = "0.1.0";
50
+ const version = "0.1.1";
51
51
  const api_version = "api";
52
52
  const hostname = window.location.hostname;
53
53
  const port = window.location.port;
54
54
  const protocol = window.location.protocol;
55
- const socket_loaded = false;
56
55
  const socket_script = "https://code.subfork.com/socket.io.min.js";
57
56
  const wait_time = 100;
58
57
 
@@ -60,6 +59,7 @@ const wait_time = 100;
60
59
  var message;
61
60
  var server;
62
61
  var socket;
62
+ var socket_loaded = false;
63
63
 
64
64
  // async returns a sha256 string (only works with https)
65
65
  async function sha256(message) {