github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/protocol/event.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package protocol
     4  
     5  // https://www.w3.org/TR/DOM-Level-3-Events/#event-flow
     6  // https://developer.mozilla.org/en-US/docs/Web/API/Event
     7  // https://developer.mozilla.org/en-US/docs/Web/Events
     8  type Event interface {
     9  	MainType() EventMainType
    10  	SubType() EventSubType
    11  	Domain() string // (codes) package name
    12  	NodeID() NodeID
    13  	Time() Time
    14  	// Returns true or false depending on how event was initialized. Its return value does not always carry meaning,
    15  	// but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method.
    16  	// It also means subscribers receive events in asynchronous or synchronous manner. true means synchronous manner.
    17  	Cancelable() bool
    18  	// Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise.
    19  	DefaultPrevented() bool
    20  	// Returns true or false depending on how event was initialized.
    21  	// True if event goes through its target's ancestors in reverse tree order, and false otherwise.
    22  	// When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
    23  	// When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
    24  	// Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
    25  	// When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault().
    26  	// This is used to enable performance optimizations described in ยง 2.8 Observing event listeners.
    27  	// When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
    28  	Bubbles() bool
    29  
    30  	// If invoked when the cancelable attribute value is true, and while executing a listener for the event with passive set to false,
    31  	// signals to the operation that caused event to be dispatched that it needs to be canceled.
    32  	PreventDefault()
    33  }
    34  
    35  // EventTarget is a interface implemented to receive events and may have listeners for them.
    36  // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
    37  type EventTarget interface {
    38  	// Appends an event listener for events whose type attribute value is type.
    39  	// The callback argument sets the callback that will be invoked when the event is dispatched.
    40  	// The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
    41  	// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
    42  	AddEventListener(mainType EventMainType, subType EventSubType, callback EventListener, options AddEventListenerOptions)
    43  
    44  	// Removes the event listener in target's event listener list with the same type, callback, and options.
    45  	// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener
    46  	RemoveEventListener(mainType EventMainType, subType EventSubType, callback EventListener, options EventListenerOptions)
    47  
    48  	// DispatchEvent() invokes event handlers synchronously. All applicable event handlers are called and return before DispatchEvent() returns.
    49  	// The terms "notify clients", "send notifications", "trigger notifications", and "fire notifications" are used interchangeably with DispatchEvent.
    50  	// Unlike web APIs, developers can check event.DefaultPrevented() after return, we don't return any data.
    51  	// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent
    52  	// Raise domain events immediately when it is called
    53  	// type DomainEventTarget interface {
    54  	// Raise[T Event](event T)
    55  	// }
    56  	DispatchEvent(event Event)
    57  
    58  	// EventListeners() []EventListener // Due to security problem, can't expose listeners to others
    59  }
    60  
    61  type EventListenerOptions struct {
    62  	// - AddEventListener: A boolean value indicating that events of this type will be dispatched to the registered listener
    63  	// before being dispatched to any EventTarget beneath it in the DOM tree.
    64  	// - RemoveEventListener: A boolean value that specifies whether the EventListener to be removed is registered as a capturing listener or not.
    65  	// If this parameter is absent, a default value of false is assumed.
    66  	Capture bool
    67  }
    68  
    69  type AddEventListenerOptions struct {
    70  	EventListenerOptions
    71  
    72  	// A boolean value indicating that the listener should be invoked at most once after being added.
    73  	// If true, the listener would be automatically removed when invoked. If not specified, defaults to false.
    74  	Once bool
    75  
    76  	// A boolean value that, if true, indicates that the function specified by listener will never call preventDefault().
    77  	// If a passive listener does call preventDefault(), the user agent will do nothing other than generate a warning log.
    78  	// If this parameter is absent, a default value of false is assumed.
    79  	// See Improving scrolling performance with passive listeners to learn more.
    80  	Passive bool
    81  
    82  	// The listener will be removed when receive true on AbortSignal channel.
    83  	// It is not free lunch, so we decide to not support it. Developers can use RemoveEventListener() to remove any listener explicitly.
    84  	// AbortSignal chan bool
    85  }
    86  
    87  // EventListener Usually implement on some kind of services that:
    88  // - Carry log event to desire node and show on screen e.g. in control room of the organization
    89  // - Notify to related person about critical log that must check as soon as possible by pager, sms, email, web notification, user GUI app, ...
    90  // - Local GUI application to notify the developers in AppMode_Dev
    91  type EventListener interface {
    92  	// Non-Blocking, means It must not block the caller in any ways.
    93  	EventHandler(event Event)
    94  }
    95  
    96  // EventMainType indicate main type of events
    97  type EventMainType = MediaTypeID
    98  
    99  const (
   100  	EventMainType_Unset  EventMainType = iota
   101  	EventMainType_Custom               // specific to an application not app engine. like pages or widgets events.
   102  
   103  	EventMainType_Log
   104  	EventMainType_System // Kernel, OS GUI app, Other apps,
   105  	EventMainType_Event  // notify when an event listener register. It can cause some security problems.
   106  	EventMainType_Navigation
   107  
   108  	EventMainType_Storage_Record
   109  	EventMainType_Text
   110  
   111  	// https://developer.mozilla.org/en-US/docs/Web/Events
   112  	EventMainType_Animation
   113  	EventMainType_AudioProcessing
   114  	EventMainType_BeforeUnload
   115  	EventMainType_Blob
   116  	EventMainType_Clipboard
   117  	EventMainType_Close
   118  	EventMainType_Composition
   119  	// EventMainType_Custom
   120  	EventMainType_DeviceMotion
   121  	EventMainType_DeviceOrientation
   122  	EventMainType_DeviceProximity
   123  	EventMainType_Drag
   124  	EventMainType_Error
   125  	EventMainType_Fetch
   126  	EventMainType_Focus
   127  	EventMainType_FormData
   128  	EventMainType_Gamepad
   129  	EventMainType_HashChange
   130  	EventMainType_HIDInputReport
   131  	EventMainType_IDBVersionChange
   132  	EventMainType_Input
   133  	EventMainType_Keyboard
   134  	EventMainType_MediaStream
   135  	EventMainType_Message
   136  	EventMainType_Mouse
   137  	EventMainType_Mutation
   138  	EventMainType_OfflineAudioCompletion
   139  	EventMainType_PageTransition
   140  	EventMainType_PaymentRequestUpdate
   141  	EventMainType_Pointer
   142  	EventMainType_PopState
   143  	EventMainType_Progress
   144  	EventMainType_RTCDataChannel
   145  	EventMainType_RTCPeerConnectionIce
   146  	EventMainType_Storage
   147  	EventMainType_Submit
   148  	EventMainType_SVG
   149  	EventMainType_Time
   150  	EventMainType_Touch
   151  	EventMainType_Track
   152  	EventMainType_Transition
   153  	EventMainType_UI
   154  	EventMainType_UserProximity
   155  	EventMainType_WebGLContext
   156  	EventMainType_Wheel
   157  )
   158  
   159  // EventSubType indicate sub type of events
   160  type EventSubType uint64
   161  
   162  const (
   163  	EventSubType_Unset EventSubType = 0
   164  	// other sub types indicate in each main type file e.g. ./log.go
   165  )