react-encompass-ecs
Version:
Sync data from encompass-ecs to React
3 lines (2 loc) • 2.52 kB
JavaScript
import{useImmer as t}from"use-immer";import{flatten as e,mapValues as r}from"lodash";import n,{useState as o,useEffect as c,createContext as i,useContext as u,useMemo as p}from"react";import{Subject as f}from"rxjs";import{DrawComponent as a,Renders as s,EntityRenderer as l,GeneralRenderer as m,EntityChecker as d}from"encompass-ecs";import{produce as v}from"immer";var y=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(a),_=new f,b=function(t){this.currentUpdaterValue=!0,this.entities={};var e=v(function(t,e,r){t[e]=r}),r=this,n=function(t){function n(){t.apply(this,arguments)}return t&&(n.__proto__=t),(n.prototype=Object.create(t&&t.prototype)).constructor=n,n.prototype.render=function(t){var n=t.get_component(y).entity_id;r.entities=e(r.entities,n,t)},n}(l);n=function(t,e,r,n){var o,c=arguments.length,i=c<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(t,e,r,n);else for(var u=t.length-1;u>=0;u--)(o=t[u])&&(i=(c<3?o(i):c>3?o(e,r,i):o(e,r))||i);return c>3&&i&&Object.defineProperty(e,r,i),i}([s(y)],n),t.add_renderer(n),_.subscribe(function(t){return r.currentUpdaterValue=t}),t.add_renderer(function(t){function e(){t.apply(this,arguments),this.layer=1}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.render=function(){_.next(!r.currentUpdaterValue)},e}(m))},h=i({});function j(n,i,f){void 0===i&&(i=!1),void 0===f&&(f=h);var a=t({}),s=a[0],l=a[1],m=u(f);c(function(){l(function(t){for(var r=0,o=Object.keys(n);r<o.length;r+=1)for(var c=o[r],i=0,u=Object.values(m);i<u.length;i+=1){var p=u[i],f=p.get_component(y).entity_id,a=n[c];void 0===t[c]&&(t[c]={}),t[c][f]!==p&&d.check_entity(p,e(a))&&(t[c][f]=p)}})},[l,m]);var v=p(function(){for(var t=r(s,function(t,e){return Object.values(t).map(function(t){return n[e].map(function(e){return t.get_component(e)})})}),e=0,o=Object.keys(n);e<o.length;e+=1){var c=o[e];t[c]||(t[c]=[])}return t},[s]);return function(t){var e=o(!0)[1];c(function(){if(t){var r=_.subscribe(function(){return e(function(t){return!t})});return function(){return r.unsubscribe()}}},[t])}(i),v}function O(t){return n.createElement((t.context||h).Provider,{value:t.entities},t.children)}export{y as ReactSyncComponent,h as GameEntitiesContext,j as useComponent,O as Provider,_ as updaterContext,b as EntitySyncer};
//# sourceMappingURL=react-encompass-ecs.mjs.map