Dealing with bloated JavaScript frameworks is painful, let it Go and handle it by yourself with a tool designed for WebAssembly.
- A Proto Value is an enhanced
js.Value.
Name()Returns the value name.Value()Returns the innerjs.Value.
NewNamedValue(name, js.Value)ReturnsValuebased on name andjs.Value.NewEmptyValue(js.Value)ReturnsValuewith nil name.
- A state is a variable that changes and notifies upon new store.
IdReturns the State id, a string.Store[T]Store T.LoadLoads T.CReturns a receive-only locked channel.
- The Virtual state is a
State[interface{}]by definition. - A logic
virtualstruct exists, and it is what is used to store.
StoreState(id, Virtual)Stores Virtual with id into Storer.NewState(Type, string) VirtualCreates a Virtual state with id, usage of the type is optional and might get used.
LoadState(id) interface{}Loads a state intointerface{}to be cast.
state.Store[T](Storer, Id) State[T]Stores and returns aState[T]with the specified id.state.Load[T](Loader, id) State[T]Returns aState[T]with the specified id, if it doesn't exist it will return nil.
- The template provided by Proto is built on top of
html/templateand supports states.
- This structure is wrapped on
html/templateand includes direct access to it so any change is dealt there except for state implementations.
- This structure is a Virtual state implementing a template loader based on previously set value.
- The Store method behavior changes based on input. If it is a string, it will change the template name or if it is a
proto.MapStoreit will append to itsproto.Store.
template.New(name)Returns a*Template.template.ParseFS(FS, ...patterns)Parses patterns inside FS and returns a*Template.
- Proto includes a client side router responsible for the pages (or routes).
- The page has an exclusive state that evaluates into
textContentin HTML.
- This structure is responsible to hold context, logger, pages and the template.
- The router stores info such as default page and current page.
Add(route, Handler)Adds a new route with specified handler.Remove(route)Removes a route from Router.Template()Returns the inner*Template.Get(pattern)Gets a route with matching pattern.match(pattern)Matches the pattern.Handler()Handles the page build and rendering; This function can be called as much as needed.SetDefault(route)Set default route.
- A page holds context, logger, template name, events slice, post-rendering slice, states, store, router reference, arguments and query info.
- A post-rendering function is called after the template was executed, thus events should start from this step.
LoggerReturns*slog.Logger.SetTemplate(name)Sets the template name.Event(proto.Value)Returns a*Eventbased onproto.Valueand managed by*Page.Context()Returnscontext.Context.Go(route)Changes URL path to route and calls Handler.handle(state.Virtual)Handles Virtual update events.Post(func())Add a function to post slice.
- A page state exclusively sets
textContentdue to safety reasons. - A page state is strict on its created type, any other set value call with mismatched type will return and not cause an update.
router.New(Context, *Logger, *Template)returns a*Router.
- This utility is provided to handle promises of JavaScript.
- Since the structure of JavaScript requires things to just return, it causes a problem with Go because of I/O operations that can lock a thread for as long as it needs. However, in JavaScript it will cause the event loop to get stuck, thus the CPU will reach 100% really fast so we have
AsyncDeadlineglobal variable with a simple timeout. - The previous issue can be solved if the function is fully in the background not requiring immediate return.
proto.AsyncDeadlineThe global deadline variable.proto.PromiseCreates a promise, hasresolveandrejectparameters and immediately returns ajs.Value.proto.AwaitWaits to resolve aPromiseor else the deadline exceeds returning a deadline error.
- Store is a concept of unified storage across Proto.
- A key must be a string.
Set(key, value)Sets key to value.Get(key) valueReturns value of the key.Range(func(key, value) continue)Ranges through store untilcontinueis false or if all elements were sent.Delete(key)Deletes a key and its value.
- This structure implements
Storeon top ofsync.Map.
- This structure converts
interface{}intoAnyStoreand simulates some methods to implementStore. - Range will only run once.
- Delete will set value to nil.
ToStore(interface{})Returns Store based on input.MapStoreFrom(interface{})Creates aMapStorefrom input.StoreToData(interface{})Convertsproto.Storeback tomap[string]interface{}orinterface{}, currently used by*Template.
URLReturns*url.URLfrom current page.CreateCreates an element frominnerHTMLbytes.
ToInterface(proto.Value)Returnsinterface{}fromproto.Value.ToValue(interface{})Returnsjs.Valuefrominterface{}.BuildJSFunc(funcs)Returns a craftedjs.Funcfrom multiple functions.ToType[T](proto.Value)LikeToInterfacebut converts to T or errors.ToRType(Type, proto.Value)Convertsproto.Valueintoreflect.Type, returnsreflect.Valueof said type or error.ToInt(proto.Value)Converts multiple integers representation of JavaScript into Go'sinttype.
ToBytesReturns a[]bytefromproto.Value.ToUint8ArrayReturns aUint8Arrayfrom[]byte.