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